gradient_editor_plugin.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /**************************************************************************/
  2. /* gradient_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "gradient_editor_plugin.h"
  31. #include "core/os/keyboard.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/gui/editor_spin_slider.h"
  37. #include "editor/plugins/canvas_item_editor_plugin.h"
  38. #include "editor/plugins/node_3d_editor_plugin.h"
  39. #include "editor/themes/editor_scale.h"
  40. #include "scene/gui/color_picker.h"
  41. #include "scene/gui/flow_container.h"
  42. #include "scene/gui/popup.h"
  43. #include "scene/gui/separator.h"
  44. #include "scene/resources/gradient_texture.h"
  45. int GradientEdit::_get_point_at(int p_xpos) const {
  46. int result = -1;
  47. int total_w = _get_gradient_rect_width();
  48. float min_distance = handle_width * 0.8; // Allow the cursor to be more than half a handle width away for ease of use.
  49. for (int i = 0; i < gradient->get_point_count(); i++) {
  50. // Ignore points outside of [0, 1].
  51. if (gradient->get_offset(i) < 0) {
  52. continue;
  53. } else if (gradient->get_offset(i) > 1) {
  54. break;
  55. }
  56. // Check if we clicked at point.
  57. float distance = ABS(p_xpos - gradient->get_offset(i) * total_w);
  58. if (distance < min_distance) {
  59. result = i;
  60. min_distance = distance;
  61. }
  62. }
  63. return result;
  64. }
  65. int GradientEdit::_predict_insertion_index(float p_offset) {
  66. int result = 0;
  67. while (result < gradient->get_point_count() && gradient->get_offset(result) < p_offset) {
  68. result++;
  69. }
  70. return result;
  71. }
  72. int GradientEdit::_get_gradient_rect_width() const {
  73. return get_size().width - get_size().height - draw_spacing - handle_width;
  74. }
  75. void GradientEdit::_show_color_picker() {
  76. if (selected_index == -1) {
  77. return;
  78. }
  79. picker->set_pick_color(gradient->get_color(selected_index));
  80. Size2 minsize = popup->get_contents_minimum_size();
  81. float viewport_height = get_viewport_rect().size.y;
  82. // Determine in which direction to show the popup. By default popup below.
  83. // But if the popup doesn't fit below and the Gradient Editor is in the bottom half of the viewport, show above.
  84. bool show_above = get_global_position().y + get_size().y + minsize.y > viewport_height && get_global_position().y * 2 + get_size().y > viewport_height;
  85. float v_offset = show_above ? -minsize.y : get_size().y;
  86. popup->set_position(get_screen_position() + Vector2(0, v_offset));
  87. popup->popup();
  88. }
  89. void GradientEdit::_color_changed(const Color &p_color) {
  90. set_color(selected_index, p_color);
  91. }
  92. void GradientEdit::set_gradient(const Ref<Gradient> &p_gradient) {
  93. gradient = p_gradient;
  94. gradient->connect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  95. }
  96. const Ref<Gradient> &GradientEdit::get_gradient() const {
  97. return gradient;
  98. }
  99. void GradientEdit::add_point(float p_offset, const Color &p_color) {
  100. int new_idx = _predict_insertion_index(p_offset);
  101. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  102. undo_redo->create_action(TTR("Add Gradient Point"));
  103. undo_redo->add_do_method(*gradient, "add_point", p_offset, p_color);
  104. undo_redo->add_do_method(this, "set_selected_index", new_idx);
  105. undo_redo->add_undo_method(*gradient, "remove_point", new_idx);
  106. undo_redo->add_undo_method(this, "set_selected_index", -1);
  107. undo_redo->commit_action();
  108. }
  109. void GradientEdit::remove_point(int p_index) {
  110. ERR_FAIL_INDEX_MSG(p_index, gradient->get_point_count(), "Gradient point is out of bounds.");
  111. if (gradient->get_point_count() <= 1) {
  112. return;
  113. }
  114. // If the point is removed while it's being moved, remember its old offset.
  115. float old_offset = (grabbing == GRAB_MOVE) ? pre_grab_offset : gradient->get_offset(p_index);
  116. Color old_color = gradient->get_color(p_index);
  117. int new_selected_index = selected_index;
  118. // Reselect the old selected point if it's not the deleted one.
  119. if (new_selected_index > p_index) {
  120. new_selected_index -= 1;
  121. } else if (new_selected_index == p_index) {
  122. new_selected_index = -1;
  123. }
  124. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  125. undo_redo->create_action(TTR("Remove Gradient Point"));
  126. undo_redo->add_do_method(*gradient, "remove_point", p_index);
  127. undo_redo->add_do_method(this, "set_selected_index", new_selected_index);
  128. undo_redo->add_undo_method(*gradient, "add_point", old_offset, old_color);
  129. undo_redo->add_undo_method(this, "set_selected_index", selected_index);
  130. undo_redo->commit_action();
  131. }
  132. void GradientEdit::set_offset(int p_index, float p_offset) {
  133. ERR_FAIL_INDEX_MSG(p_index, gradient->get_point_count(), "Gradient point is out of bounds.");
  134. // Use pre_grab_offset to determine things for the undo/redo.
  135. if (Math::is_equal_approx(pre_grab_offset, p_offset)) {
  136. return;
  137. }
  138. int new_idx = _predict_insertion_index(p_offset);
  139. gradient->set_offset(p_index, pre_grab_offset); // Pretend the point started from its old place.
  140. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  141. undo_redo->create_action(TTR("Move Gradient Point"));
  142. undo_redo->add_do_method(*gradient, "set_offset", pre_grab_index, p_offset);
  143. undo_redo->add_do_method(this, "set_selected_index", new_idx);
  144. undo_redo->add_undo_method(*gradient, "set_offset", new_idx, pre_grab_offset);
  145. undo_redo->add_undo_method(this, "set_selected_index", pre_grab_index);
  146. undo_redo->commit_action();
  147. queue_redraw();
  148. }
  149. void GradientEdit::set_color(int p_index, const Color &p_color) {
  150. ERR_FAIL_INDEX_MSG(p_index, gradient->get_point_count(), "Gradient point is out of bounds.");
  151. Color old_color = gradient->get_color(p_index);
  152. if (old_color == p_color) {
  153. return;
  154. }
  155. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  156. undo_redo->create_action(TTR("Recolor Gradient Point"), UndoRedo::MERGE_ENDS);
  157. undo_redo->add_do_method(*gradient, "set_color", p_index, p_color);
  158. undo_redo->add_undo_method(*gradient, "set_color", p_index, old_color);
  159. undo_redo->commit_action();
  160. queue_redraw();
  161. }
  162. void GradientEdit::reverse_gradient() {
  163. int new_selected_idx = (selected_index == -1) ? -1 : (gradient->get_point_count() - selected_index - 1);
  164. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  165. undo_redo->create_action(TTR("Reverse Gradient"), UndoRedo::MERGE_DISABLE, *gradient);
  166. undo_redo->add_do_method(*gradient, "reverse");
  167. undo_redo->add_do_method(this, "set_selected_index", new_selected_idx);
  168. undo_redo->add_undo_method(*gradient, "reverse");
  169. undo_redo->add_undo_method(this, "set_selected_index", selected_index);
  170. undo_redo->commit_action();
  171. }
  172. void GradientEdit::set_selected_index(int p_index) {
  173. selected_index = p_index;
  174. queue_redraw();
  175. }
  176. void GradientEdit::set_snap_enabled(bool p_enabled) {
  177. snap_enabled = p_enabled;
  178. queue_redraw();
  179. if (gradient.is_valid()) {
  180. if (snap_enabled) {
  181. gradient->set_meta(SNAME("_snap_enabled"), true);
  182. } else {
  183. gradient->remove_meta(SNAME("_snap_enabled"));
  184. }
  185. }
  186. }
  187. void GradientEdit::set_snap_count(int p_count) {
  188. snap_count = p_count;
  189. queue_redraw();
  190. if (gradient.is_valid()) {
  191. if (snap_count != GradientEditor::DEFAULT_SNAP) {
  192. gradient->set_meta(SNAME("_snap_count"), snap_count);
  193. } else {
  194. gradient->remove_meta(SNAME("_snap_count"));
  195. }
  196. }
  197. }
  198. ColorPicker *GradientEdit::get_picker() const {
  199. return picker;
  200. }
  201. PopupPanel *GradientEdit::get_popup() const {
  202. return popup;
  203. }
  204. void GradientEdit::gui_input(const Ref<InputEvent> &p_event) {
  205. ERR_FAIL_COND(p_event.is_null());
  206. Ref<InputEventKey> k = p_event;
  207. if (k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && selected_index != -1) {
  208. if (grabbing == GRAB_ADD) {
  209. gradient->remove_point(selected_index); // Point is temporary, so remove directly from gradient.
  210. set_selected_index(-1);
  211. } else {
  212. remove_point(selected_index);
  213. }
  214. grabbing = GRAB_NONE;
  215. hovered_index = -1;
  216. accept_event();
  217. }
  218. Ref<InputEventMouseButton> mb = p_event;
  219. if (mb.is_valid() && mb->is_pressed()) {
  220. float adjusted_mb_x = mb->get_position().x - handle_width / 2;
  221. bool should_snap = snap_enabled || mb->is_ctrl_pressed();
  222. // Delete point or move it to old position on middle or right click.
  223. if (mb->get_button_index() == MouseButton::RIGHT || mb->get_button_index() == MouseButton::MIDDLE) {
  224. if (grabbing == GRAB_MOVE && mb->get_button_index() == MouseButton::RIGHT) {
  225. gradient->set_offset(selected_index, pre_grab_offset);
  226. set_selected_index(pre_grab_index);
  227. } else {
  228. int point_to_remove = _get_point_at(adjusted_mb_x);
  229. if (point_to_remove == -1) {
  230. set_selected_index(-1); // Nothing on the place of the click, just deselect any handle.
  231. } else {
  232. if (grabbing == GRAB_ADD) {
  233. gradient->remove_point(point_to_remove); // Point is temporary, so remove directly from gradient.
  234. set_selected_index(-1);
  235. } else {
  236. remove_point(point_to_remove);
  237. }
  238. hovered_index = -1;
  239. }
  240. }
  241. grabbing = GRAB_NONE;
  242. accept_event();
  243. }
  244. // Select point.
  245. if (mb->get_button_index() == MouseButton::LEFT) {
  246. int total_w = _get_gradient_rect_width();
  247. // Check if color picker was clicked or gradient was double-clicked.
  248. if (adjusted_mb_x > total_w + draw_spacing) {
  249. if (!mb->is_double_click()) {
  250. _show_color_picker();
  251. }
  252. accept_event();
  253. return;
  254. } else if (mb->is_double_click()) {
  255. set_selected_index(_get_point_at(adjusted_mb_x));
  256. _show_color_picker();
  257. accept_event();
  258. return;
  259. }
  260. if (grabbing == GRAB_NONE) {
  261. set_selected_index(_get_point_at(adjusted_mb_x));
  262. }
  263. if (selected_index != -1 && !mb->is_alt_pressed()) {
  264. // An existing point was grabbed.
  265. grabbing = GRAB_MOVE;
  266. pre_grab_offset = gradient->get_offset(selected_index);
  267. pre_grab_index = selected_index;
  268. } else if (grabbing == GRAB_NONE) {
  269. // Adding a new point. Insert a temporary point for the user to adjust, so it's not in the undo/redo.
  270. float new_offset = CLAMP(adjusted_mb_x / float(total_w), 0, 1);
  271. if (should_snap) {
  272. new_offset = Math::snapped(new_offset, 1.0 / snap_count);
  273. }
  274. for (int i = 0; i < gradient->get_point_count(); i++) {
  275. if (gradient->get_offset(i) == new_offset) {
  276. // If another point with the same offset is found, then
  277. // tweak it if Alt was pressed, otherwise something has gone wrong, so stop the operation.
  278. if (mb->is_alt_pressed()) {
  279. new_offset = MIN(gradient->get_offset(i) + 0.00001, 1);
  280. } else {
  281. return;
  282. }
  283. }
  284. }
  285. Color new_color = gradient->get_color_at_offset(new_offset);
  286. if (mb->is_alt_pressed()) {
  287. // Alt + Click on a point duplicates it. So copy its color.
  288. int point_to_copy = _get_point_at(adjusted_mb_x);
  289. if (point_to_copy != -1) {
  290. new_color = gradient->get_color(point_to_copy);
  291. }
  292. }
  293. // Add a temporary point for the user to adjust before adding it permanently.
  294. gradient->add_point(new_offset, new_color);
  295. set_selected_index(_predict_insertion_index(new_offset));
  296. grabbing = GRAB_ADD;
  297. }
  298. }
  299. }
  300. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  301. if (grabbing == GRAB_MOVE) {
  302. // Finish moving a point.
  303. set_offset(selected_index, gradient->get_offset(selected_index));
  304. grabbing = GRAB_NONE;
  305. } else if (grabbing == GRAB_ADD) {
  306. // Finish inserting a new point. Remove the temporary point and insert the permanent one in its place.
  307. float new_offset = gradient->get_offset(selected_index);
  308. Color new_color = gradient->get_color(selected_index);
  309. gradient->remove_point(selected_index);
  310. add_point(new_offset, new_color);
  311. grabbing = GRAB_NONE;
  312. }
  313. }
  314. Ref<InputEventMouseMotion> mm = p_event;
  315. if (mm.is_valid()) {
  316. int total_w = _get_gradient_rect_width();
  317. float adjusted_mm_x = mm->get_position().x - handle_width / 2;
  318. bool should_snap = snap_enabled || mm->is_ctrl_pressed();
  319. // Hovering logic.
  320. if (grabbing == GRAB_NONE) {
  321. int nearest_point = _get_point_at(adjusted_mm_x);
  322. if (hovered_index != nearest_point) {
  323. hovered_index = nearest_point;
  324. queue_redraw();
  325. }
  326. return;
  327. } else {
  328. hovered_index = -1;
  329. }
  330. // Grabbing logic.
  331. float new_offset = CLAMP(adjusted_mm_x / float(total_w), 0, 1);
  332. // Give the ability to snap right next to a point when using Shift.
  333. if (mm->is_shift_pressed()) {
  334. float smallest_offset = should_snap ? (0.5 / snap_count) : 0.01;
  335. int nearest_idx = -1;
  336. // Only check the two adjacent points to find which one is the nearest.
  337. if (selected_index > 0) {
  338. float temp_offset = ABS(gradient->get_offset(selected_index - 1) - new_offset);
  339. if (temp_offset < smallest_offset) {
  340. smallest_offset = temp_offset;
  341. nearest_idx = selected_index - 1;
  342. }
  343. }
  344. if (selected_index < gradient->get_point_count() - 1) {
  345. float temp_offset = ABS(gradient->get_offset(selected_index + 1) - new_offset);
  346. if (temp_offset < smallest_offset) {
  347. smallest_offset = temp_offset;
  348. nearest_idx = selected_index + 1;
  349. }
  350. }
  351. if (nearest_idx != -1) {
  352. // Snap to the point with a slight adjustment to the left or right.
  353. float adjustment = gradient->get_offset(nearest_idx) < new_offset ? 0.00001 : -0.00001;
  354. new_offset = CLAMP(gradient->get_offset(nearest_idx) + adjustment, 0, 1);
  355. } else if (should_snap) {
  356. new_offset = Math::snapped(new_offset, 1.0 / snap_count);
  357. }
  358. } else if (should_snap) {
  359. // Shift is not pressed, so snap fully without adjustments.
  360. new_offset = Math::snapped(new_offset, 1.0 / snap_count);
  361. }
  362. // Don't move the point if its new offset would be the same as another point's.
  363. for (int i = 0; i < gradient->get_point_count(); i++) {
  364. if (gradient->get_offset(i) == new_offset && i != selected_index) {
  365. return;
  366. }
  367. }
  368. if (selected_index == -1) {
  369. return;
  370. }
  371. // We want to only save this action for undo/redo when released, so don't use set_offset() yet.
  372. gradient->set_offset(selected_index, new_offset);
  373. // Update selected_index after the gradient updates its indices, so you keep holding the same color.
  374. for (int i = 0; i < gradient->get_point_count(); i++) {
  375. if (gradient->get_offset(i) == new_offset) {
  376. set_selected_index(i);
  377. break;
  378. }
  379. }
  380. }
  381. }
  382. void GradientEdit::_redraw() {
  383. int w = get_size().x;
  384. int h = get_size().y - draw_spacing; // A bit of spacing below the gradient too.
  385. if (w == 0 || h == 0) {
  386. return; // Safety check as there is nothing to draw with such size.
  387. }
  388. int total_w = _get_gradient_rect_width();
  389. int half_handle_width = handle_width * 0.5;
  390. // Draw gradient.
  391. draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), Rect2(half_handle_width, 0, total_w, h), true);
  392. preview_texture->set_gradient(gradient);
  393. draw_texture_rect(preview_texture, Rect2(half_handle_width, 0, total_w, h));
  394. // Draw vertical snap lines.
  395. if (snap_enabled || (Input::get_singleton()->is_key_pressed(Key::CTRL) && grabbing != GRAB_NONE)) {
  396. const Color line_color = Color(0.5, 0.5, 0.5, 0.5);
  397. for (int idx = 1; idx < snap_count; idx++) {
  398. float offset_x = idx * total_w / (float)snap_count + half_handle_width;
  399. draw_line(Point2(offset_x, 0), Point2(offset_x, h), line_color);
  400. }
  401. }
  402. // Draw handles.
  403. for (int i = 0; i < gradient->get_point_count(); i++) {
  404. // Only draw handles for points in [0, 1]. If there are points before or after, draw a little indicator.
  405. if (gradient->get_offset(i) < 0.0) {
  406. continue;
  407. } else if (gradient->get_offset(i) > 1.0) {
  408. break;
  409. }
  410. // White or black handle color, to contrast with the selected color's brightness.
  411. // Also consider the fact that the color may be translucent.
  412. // The checkerboard pattern in the background has an average luminance of 0.75.
  413. Color inside_col = gradient->get_color(i);
  414. Color border_col = Math::lerp(0.75f, inside_col.get_luminance(), inside_col.a) > 0.455 ? Color(0, 0, 0) : Color(1, 1, 1);
  415. int handle_thickness = MAX(1, Math::round(EDSCALE));
  416. float handle_x_pos = gradient->get_offset(i) * total_w + half_handle_width;
  417. float handle_start_x = handle_x_pos - half_handle_width;
  418. Rect2 rect = Rect2(handle_start_x, h / 2, handle_width, h / 2);
  419. if (inside_col.a < 1) {
  420. // If the color is translucent, draw a little opaque rectangle at the bottom to more easily see it.
  421. draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), rect, true);
  422. draw_rect(rect, inside_col, true);
  423. Color inside_col_opaque = inside_col;
  424. inside_col_opaque.a = 1.0;
  425. draw_rect(Rect2(handle_start_x + handle_thickness / 2.0, h * 0.9 - handle_thickness / 2.0, handle_width - handle_thickness, h * 0.1), inside_col_opaque, true);
  426. } else {
  427. draw_rect(rect, inside_col, true);
  428. }
  429. if (selected_index == i) {
  430. // Handle is selected.
  431. draw_rect(rect, border_col, false, handle_thickness);
  432. draw_line(Vector2(handle_x_pos, 0), Vector2(handle_x_pos, h / 2 - handle_thickness), border_col, handle_thickness);
  433. if (inside_col.a < 1) {
  434. draw_line(Vector2(handle_start_x + handle_thickness / 2.0, h * 0.9 - handle_thickness), Vector2(handle_start_x + handle_width - handle_thickness / 2.0, h * 0.9 - handle_thickness), border_col, handle_thickness);
  435. }
  436. rect = rect.grow(-handle_thickness);
  437. const Color focus_col = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  438. draw_rect(rect, has_focus() ? focus_col : focus_col.darkened(0.4), false, handle_thickness);
  439. rect = rect.grow(-handle_thickness);
  440. draw_rect(rect, border_col, false, handle_thickness);
  441. } else {
  442. // Handle isn't selected.
  443. border_col.a = 0.9;
  444. draw_rect(rect, border_col, false, handle_thickness);
  445. draw_line(Vector2(handle_x_pos, 0), Vector2(handle_x_pos, h / 2 - handle_thickness), border_col, handle_thickness);
  446. if (inside_col.a < 1) {
  447. draw_line(Vector2(handle_start_x + handle_thickness / 2.0, h * 0.9 - handle_thickness), Vector2(handle_start_x + handle_width - handle_thickness / 2.0, h * 0.9 - handle_thickness), border_col, handle_thickness);
  448. }
  449. if (hovered_index == i) {
  450. // Draw a subtle translucent rect inside the handle if it's being hovered.
  451. rect = rect.grow(-handle_thickness);
  452. border_col.a = 0.54;
  453. draw_rect(rect, border_col, false, handle_thickness);
  454. }
  455. }
  456. }
  457. // Draw "button" for color selector.
  458. int button_offset = total_w + handle_width + draw_spacing;
  459. if (selected_index != -1) {
  460. Color grabbed_col = gradient->get_color(selected_index);
  461. if (grabbed_col.a < 1) {
  462. draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), Rect2(button_offset, 0, h, h), true);
  463. }
  464. draw_rect(Rect2(button_offset, 0, h, h), grabbed_col);
  465. if (grabbed_col.r > 1 || grabbed_col.g > 1 || grabbed_col.b > 1) {
  466. // Draw an indicator to denote that the currently selected color is "overbright".
  467. draw_texture(get_theme_icon(SNAME("overbright_indicator"), SNAME("ColorPicker")), Point2(button_offset, 0));
  468. }
  469. } else {
  470. // If no color is selected, draw gray color with 'X' on top.
  471. draw_rect(Rect2(button_offset, 0, h, h), Color(0.5, 0.5, 0.5, 1));
  472. draw_line(Vector2(button_offset, 0), Vector2(button_offset + h, h), Color(0.8, 0.8, 0.8));
  473. draw_line(Vector2(button_offset, h), Vector2(button_offset + h, 0), Color(0.8, 0.8, 0.8));
  474. }
  475. }
  476. void GradientEdit::_notification(int p_what) {
  477. switch (p_what) {
  478. case NOTIFICATION_THEME_CHANGED: {
  479. draw_spacing = BASE_SPACING * get_theme_default_base_scale();
  480. handle_width = BASE_HANDLE_WIDTH * get_theme_default_base_scale();
  481. } break;
  482. case NOTIFICATION_DRAW: {
  483. _redraw();
  484. } break;
  485. case NOTIFICATION_MOUSE_EXIT: {
  486. if (hovered_index != -1) {
  487. hovered_index = -1;
  488. queue_redraw();
  489. }
  490. } break;
  491. case NOTIFICATION_VISIBILITY_CHANGED: {
  492. if (!is_visible()) {
  493. grabbing = GRAB_NONE;
  494. }
  495. } break;
  496. }
  497. }
  498. void GradientEdit::_bind_methods() {
  499. ClassDB::bind_method(D_METHOD("set_selected_index", "index"), &GradientEdit::set_selected_index);
  500. }
  501. GradientEdit::GradientEdit() {
  502. set_focus_mode(FOCUS_ALL);
  503. set_custom_minimum_size(Size2(0, 60) * EDSCALE);
  504. picker = memnew(ColorPicker);
  505. int picker_shape = EDITOR_GET("interface/inspector/default_color_picker_shape");
  506. picker->set_picker_shape((ColorPicker::PickerShapeType)picker_shape);
  507. picker->connect("color_changed", callable_mp(this, &GradientEdit::_color_changed));
  508. popup = memnew(PopupPanel);
  509. popup->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(picker));
  510. add_child(popup, false, INTERNAL_MODE_FRONT);
  511. popup->add_child(picker);
  512. preview_texture.instantiate();
  513. preview_texture->set_width(1024);
  514. }
  515. ///////////////////////
  516. const int GradientEditor::DEFAULT_SNAP = 10;
  517. void GradientEditor::_set_snap_enabled(bool p_enabled) {
  518. gradient_editor_rect->set_snap_enabled(p_enabled);
  519. snap_count_edit->set_visible(p_enabled);
  520. }
  521. void GradientEditor::_set_snap_count(int p_count) {
  522. gradient_editor_rect->set_snap_count(CLAMP(p_count, 2, 100));
  523. }
  524. void GradientEditor::set_gradient(const Ref<Gradient> &p_gradient) {
  525. gradient_editor_rect->set_gradient(p_gradient);
  526. }
  527. void GradientEditor::_notification(int p_what) {
  528. switch (p_what) {
  529. case NOTIFICATION_THEME_CHANGED: {
  530. reverse_button->set_button_icon(get_editor_theme_icon(SNAME("ReverseGradient")));
  531. snap_button->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));
  532. } break;
  533. case NOTIFICATION_READY: {
  534. Ref<Gradient> gradient = gradient_editor_rect->get_gradient();
  535. if (gradient.is_valid()) {
  536. // Set snapping settings based on the gradient's meta.
  537. snap_button->set_pressed(gradient->get_meta("_snap_enabled", false));
  538. snap_count_edit->set_value(gradient->get_meta("_snap_count", DEFAULT_SNAP));
  539. }
  540. } break;
  541. }
  542. }
  543. GradientEditor::GradientEditor() {
  544. HFlowContainer *toolbar = memnew(HFlowContainer);
  545. add_child(toolbar);
  546. reverse_button = memnew(Button);
  547. reverse_button->set_tooltip_text(TTR("Reverse/Mirror Gradient"));
  548. toolbar->add_child(reverse_button);
  549. toolbar->add_child(memnew(VSeparator));
  550. snap_button = memnew(Button);
  551. snap_button->set_tooltip_text(TTR("Toggle Grid Snap"));
  552. snap_button->set_toggle_mode(true);
  553. toolbar->add_child(snap_button);
  554. snap_button->connect(SceneStringName(toggled), callable_mp(this, &GradientEditor::_set_snap_enabled));
  555. snap_count_edit = memnew(EditorSpinSlider);
  556. snap_count_edit->set_min(2);
  557. snap_count_edit->set_max(100);
  558. snap_count_edit->set_value(DEFAULT_SNAP);
  559. snap_count_edit->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  560. toolbar->add_child(snap_count_edit);
  561. snap_count_edit->connect(SceneStringName(value_changed), callable_mp(this, &GradientEditor::_set_snap_count));
  562. gradient_editor_rect = memnew(GradientEdit);
  563. add_child(gradient_editor_rect);
  564. reverse_button->connect(SceneStringName(pressed), callable_mp(gradient_editor_rect, &GradientEdit::reverse_gradient));
  565. set_mouse_filter(MOUSE_FILTER_STOP);
  566. _set_snap_enabled(snap_button->is_pressed());
  567. _set_snap_count(snap_count_edit->get_value());
  568. }
  569. ///////////////////////
  570. bool EditorInspectorPluginGradient::can_handle(Object *p_object) {
  571. return Object::cast_to<Gradient>(p_object) != nullptr;
  572. }
  573. void EditorInspectorPluginGradient::parse_begin(Object *p_object) {
  574. Gradient *gradient = Object::cast_to<Gradient>(p_object);
  575. ERR_FAIL_NULL(gradient);
  576. Ref<Gradient> g(gradient);
  577. GradientEditor *editor = memnew(GradientEditor);
  578. editor->set_gradient(g);
  579. add_custom_control(editor);
  580. }
  581. ///////////////////////
  582. GradientEditorPlugin::GradientEditorPlugin() {
  583. Ref<EditorInspectorPluginGradient> plugin;
  584. plugin.instantiate();
  585. add_inspector_plugin(plugin);
  586. }