gradient_editor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /**************************************************************************/
  2. /* gradient_editor.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.h"
  31. #include "core/os/keyboard.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "scene/resources/gradient_texture.h"
  37. void GradientEditor::set_gradient(const Ref<Gradient> &p_gradient) {
  38. gradient = p_gradient;
  39. connect("ramp_changed", callable_mp(this, &GradientEditor::_ramp_changed));
  40. gradient->connect_changed(callable_mp(this, &GradientEditor::_gradient_changed));
  41. set_points(gradient->get_points());
  42. set_interpolation_mode(gradient->get_interpolation_mode());
  43. set_interpolation_color_space(gradient->get_interpolation_color_space());
  44. }
  45. void GradientEditor::reverse_gradient() {
  46. gradient->reverse();
  47. set_points(gradient->get_points());
  48. emit_signal(SNAME("ramp_changed"));
  49. queue_redraw();
  50. }
  51. int GradientEditor::_get_point_from_pos(int x) {
  52. int result = -1;
  53. int total_w = get_size().width - get_size().height - draw_spacing - handle_width;
  54. float min_distance = 1e20;
  55. for (int i = 0; i < points.size(); i++) {
  56. // Check if we clicked at point.
  57. float distance = ABS(x - points[i].offset * total_w);
  58. float min = handle_width * 0.85; // Allow the mouse to be more than half a handle width away for ease of grabbing.
  59. if (distance <= min && distance < min_distance) {
  60. result = i;
  61. min_distance = distance;
  62. }
  63. }
  64. return result;
  65. }
  66. void GradientEditor::_show_color_picker() {
  67. if (grabbed == -1) {
  68. return;
  69. }
  70. picker->set_pick_color(points[grabbed].color);
  71. Size2 minsize = popup->get_contents_minimum_size();
  72. bool show_above = false;
  73. if (get_global_position().y + get_size().y + minsize.y > get_viewport_rect().size.y) {
  74. show_above = true;
  75. }
  76. if (show_above) {
  77. popup->set_position(get_screen_position() - Vector2(0, minsize.y));
  78. } else {
  79. popup->set_position(get_screen_position() + Vector2(0, get_size().y));
  80. }
  81. popup->popup();
  82. }
  83. void GradientEditor::_gradient_changed() {
  84. if (editing) {
  85. return;
  86. }
  87. editing = true;
  88. Vector<Gradient::Point> grad_points = gradient->get_points();
  89. set_points(grad_points);
  90. set_interpolation_mode(gradient->get_interpolation_mode());
  91. set_interpolation_color_space(gradient->get_interpolation_color_space());
  92. queue_redraw();
  93. editing = false;
  94. }
  95. void GradientEditor::_ramp_changed() {
  96. editing = true;
  97. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  98. undo_redo->create_action(TTR("Gradient Edited"), UndoRedo::MERGE_ENDS);
  99. undo_redo->add_do_method(gradient.ptr(), "set_offsets", get_offsets());
  100. undo_redo->add_do_method(gradient.ptr(), "set_colors", get_colors());
  101. undo_redo->add_do_method(gradient.ptr(), "set_interpolation_mode", get_interpolation_mode());
  102. undo_redo->add_do_method(gradient.ptr(), "set_interpolation_color_space", get_interpolation_color_space());
  103. undo_redo->add_undo_method(gradient.ptr(), "set_offsets", gradient->get_offsets());
  104. undo_redo->add_undo_method(gradient.ptr(), "set_colors", gradient->get_colors());
  105. undo_redo->add_undo_method(gradient.ptr(), "set_interpolation_mode", gradient->get_interpolation_mode());
  106. undo_redo->add_undo_method(gradient.ptr(), "set_interpolation_color_space", gradient->get_interpolation_color_space());
  107. undo_redo->commit_action();
  108. editing = false;
  109. }
  110. void GradientEditor::_color_changed(const Color &p_color) {
  111. if (grabbed == -1) {
  112. return;
  113. }
  114. points.write[grabbed].color = p_color;
  115. queue_redraw();
  116. emit_signal(SNAME("ramp_changed"));
  117. }
  118. void GradientEditor::set_ramp(const Vector<float> &p_offsets, const Vector<Color> &p_colors) {
  119. ERR_FAIL_COND(p_offsets.size() != p_colors.size());
  120. points.clear();
  121. for (int i = 0; i < p_offsets.size(); i++) {
  122. Gradient::Point p;
  123. p.offset = p_offsets[i];
  124. p.color = p_colors[i];
  125. points.push_back(p);
  126. }
  127. points.sort();
  128. queue_redraw();
  129. }
  130. Vector<float> GradientEditor::get_offsets() const {
  131. Vector<float> ret;
  132. for (int i = 0; i < points.size(); i++) {
  133. ret.push_back(points[i].offset);
  134. }
  135. return ret;
  136. }
  137. Vector<Color> GradientEditor::get_colors() const {
  138. Vector<Color> ret;
  139. for (int i = 0; i < points.size(); i++) {
  140. ret.push_back(points[i].color);
  141. }
  142. return ret;
  143. }
  144. void GradientEditor::set_points(Vector<Gradient::Point> &p_points) {
  145. if (points.size() != p_points.size()) {
  146. grabbed = -1;
  147. }
  148. points.clear();
  149. points = p_points;
  150. points.sort();
  151. }
  152. Vector<Gradient::Point> &GradientEditor::get_points() {
  153. return points;
  154. }
  155. void GradientEditor::set_interpolation_mode(Gradient::InterpolationMode p_interp_mode) {
  156. interpolation_mode = p_interp_mode;
  157. }
  158. Gradient::InterpolationMode GradientEditor::get_interpolation_mode() {
  159. return interpolation_mode;
  160. }
  161. void GradientEditor::set_interpolation_color_space(Gradient::ColorSpace p_color_space) {
  162. interpolation_color_space = p_color_space;
  163. }
  164. Gradient::ColorSpace GradientEditor::get_interpolation_color_space() {
  165. return interpolation_color_space;
  166. }
  167. ColorPicker *GradientEditor::get_picker() {
  168. return picker;
  169. }
  170. PopupPanel *GradientEditor::get_popup() {
  171. return popup;
  172. }
  173. Size2 GradientEditor::get_minimum_size() const {
  174. return Size2(0, 60) * EDSCALE;
  175. }
  176. void GradientEditor::gui_input(const Ref<InputEvent> &p_event) {
  177. ERR_FAIL_COND(p_event.is_null());
  178. Ref<InputEventKey> k = p_event;
  179. if (k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && grabbed != -1) {
  180. points.remove_at(grabbed);
  181. grabbed = -1;
  182. grabbing = false;
  183. queue_redraw();
  184. emit_signal(SNAME("ramp_changed"));
  185. accept_event();
  186. }
  187. Ref<InputEventMouseButton> mb = p_event;
  188. if (mb.is_valid() && mb->is_pressed()) {
  189. float adjusted_mb_x = mb->get_position().x - handle_width / 2;
  190. // Delete point on right click.
  191. if (mb->get_button_index() == MouseButton::RIGHT) {
  192. grabbed = _get_point_from_pos(adjusted_mb_x);
  193. if (grabbed != -1) {
  194. points.remove_at(grabbed);
  195. grabbed = -1;
  196. grabbing = false;
  197. queue_redraw();
  198. emit_signal(SNAME("ramp_changed"));
  199. accept_event();
  200. }
  201. }
  202. // Hold Alt key to duplicate selected color.
  203. if (mb->get_button_index() == MouseButton::LEFT && mb->is_alt_pressed()) {
  204. grabbed = _get_point_from_pos(adjusted_mb_x);
  205. if (grabbed != -1) {
  206. int total_w = get_size().width - get_size().height - draw_spacing - handle_width;
  207. Gradient::Point new_point = points[grabbed];
  208. new_point.offset = CLAMP(adjusted_mb_x / float(total_w), 0, 1);
  209. points.push_back(new_point);
  210. points.sort();
  211. for (int i = 0; i < points.size(); ++i) {
  212. if (points[i].offset == new_point.offset) {
  213. grabbed = i;
  214. break;
  215. }
  216. }
  217. emit_signal(SNAME("ramp_changed"));
  218. queue_redraw();
  219. }
  220. }
  221. // Select.
  222. if (mb->get_button_index() == MouseButton::LEFT) {
  223. queue_redraw();
  224. int total_w = get_size().width - get_size().height - draw_spacing - handle_width;
  225. // Check if color selector was clicked or ramp was double-clicked.
  226. if (adjusted_mb_x > total_w + draw_spacing) {
  227. if (!mb->is_double_click()) {
  228. _show_color_picker();
  229. }
  230. return;
  231. } else if (mb->is_double_click()) {
  232. grabbed = _get_point_from_pos(adjusted_mb_x);
  233. _show_color_picker();
  234. accept_event();
  235. return;
  236. }
  237. grabbing = true;
  238. grabbed = _get_point_from_pos(adjusted_mb_x);
  239. // Grab or select.
  240. if (grabbed != -1) {
  241. return;
  242. }
  243. // Insert point.
  244. Gradient::Point new_point;
  245. new_point.offset = CLAMP(adjusted_mb_x / float(total_w), 0, 1);
  246. new_point.color = gradient->get_color_at_offset(new_point.offset);
  247. points.push_back(new_point);
  248. points.sort();
  249. for (int i = 0; i < points.size(); i++) {
  250. if (points[i].offset == new_point.offset) {
  251. grabbed = i;
  252. break;
  253. }
  254. }
  255. emit_signal(SNAME("ramp_changed"));
  256. }
  257. }
  258. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  259. if (grabbing) {
  260. grabbing = false;
  261. emit_signal(SNAME("ramp_changed"));
  262. }
  263. queue_redraw();
  264. }
  265. Ref<InputEventMouseMotion> mm = p_event;
  266. if (mm.is_valid() && grabbing) {
  267. float adjusted_mm_x = mm->get_position().x - handle_width / 2;
  268. int total_w = get_size().width - get_size().height - draw_spacing - handle_width;
  269. float newofs = CLAMP(adjusted_mm_x / float(total_w), 0, 1);
  270. // Snap to "round" coordinates if holding Ctrl.
  271. // Be more precise if holding Shift as well.
  272. if (mm->is_ctrl_pressed()) {
  273. newofs = Math::snapped(newofs, mm->is_shift_pressed() ? 0.025 : 0.1);
  274. } else if (mm->is_shift_pressed()) {
  275. // Snap to nearest point if holding just Shift.
  276. const float snap_threshold = 0.03;
  277. float smallest_ofs = snap_threshold;
  278. bool found = false;
  279. int nearest_point = 0;
  280. for (int i = 0; i < points.size(); ++i) {
  281. if (i != grabbed) {
  282. float temp_ofs = ABS(points[i].offset - newofs);
  283. if (temp_ofs < smallest_ofs) {
  284. smallest_ofs = temp_ofs;
  285. nearest_point = i;
  286. if (found) {
  287. break;
  288. }
  289. found = true;
  290. }
  291. }
  292. }
  293. if (found) {
  294. if (points[nearest_point].offset < newofs) {
  295. newofs = points[nearest_point].offset + 0.00001;
  296. } else {
  297. newofs = points[nearest_point].offset - 0.00001;
  298. }
  299. newofs = CLAMP(newofs, 0, 1);
  300. }
  301. }
  302. bool valid = true;
  303. for (int i = 0; i < points.size(); i++) {
  304. if (points[i].offset == newofs && i != grabbed) {
  305. valid = false;
  306. break;
  307. }
  308. }
  309. if (!valid || grabbed == -1) {
  310. return;
  311. }
  312. points.write[grabbed].offset = newofs;
  313. points.sort();
  314. for (int i = 0; i < points.size(); i++) {
  315. if (points[i].offset == newofs) {
  316. grabbed = i;
  317. break;
  318. }
  319. }
  320. emit_signal(SNAME("ramp_changed"));
  321. queue_redraw();
  322. }
  323. }
  324. void GradientEditor::_notification(int p_what) {
  325. switch (p_what) {
  326. case NOTIFICATION_ENTER_TREE: {
  327. if (!picker->is_connected("color_changed", callable_mp(this, &GradientEditor::_color_changed))) {
  328. picker->connect("color_changed", callable_mp(this, &GradientEditor::_color_changed));
  329. }
  330. [[fallthrough]];
  331. }
  332. case NOTIFICATION_THEME_CHANGED: {
  333. draw_spacing = BASE_SPACING * get_theme_default_base_scale();
  334. handle_width = BASE_HANDLE_WIDTH * get_theme_default_base_scale();
  335. } break;
  336. case NOTIFICATION_DRAW: {
  337. int w = get_size().x;
  338. int h = get_size().y;
  339. if (w == 0 || h == 0) {
  340. return; // Safety check. We have division by 'h'. And in any case there is nothing to draw with such size.
  341. }
  342. int total_w = get_size().width - get_size().height - draw_spacing - handle_width;
  343. // Draw checker pattern for ramp.
  344. draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), Rect2(handle_width / 2, 0, total_w, h), true);
  345. // Draw color ramp.
  346. gradient_cache->set_points(points);
  347. gradient_cache->set_interpolation_mode(interpolation_mode);
  348. gradient_cache->set_interpolation_color_space(interpolation_color_space);
  349. preview_texture->set_gradient(gradient_cache);
  350. draw_texture_rect(preview_texture, Rect2(handle_width / 2, 0, total_w, h));
  351. // Draw borders around color ramp if in focus.
  352. if (has_focus()) {
  353. draw_rect(Rect2(handle_width / 2, 0, total_w, h), Color(1, 1, 1, 0.9), false, 1);
  354. }
  355. // Draw point markers.
  356. for (int i = 0; i < points.size(); i++) {
  357. Color col = points[i].color.get_v() > 0.5 ? Color(0, 0, 0) : Color(1, 1, 1);
  358. col.a = 0.9;
  359. draw_line(Vector2(points[i].offset * total_w + handle_width / 2, 0), Vector2(points[i].offset * total_w + handle_width / 2, h / 2), col);
  360. Rect2 rect = Rect2(points[i].offset * total_w, h / 2, handle_width, h / 2);
  361. draw_rect(rect, points[i].color, true);
  362. draw_rect(rect, col, false, 1);
  363. if (grabbed == i) {
  364. const Color focus_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  365. rect = rect.grow(-1);
  366. if (has_focus()) {
  367. draw_rect(rect, focus_color, false, 1);
  368. } else {
  369. draw_rect(rect, focus_color.darkened(0.4), false, 1);
  370. }
  371. rect = rect.grow(-1);
  372. draw_rect(rect, col, false, 1);
  373. }
  374. }
  375. // Draw "button" for color selector.
  376. int button_offset = total_w + handle_width + draw_spacing;
  377. draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), Rect2(button_offset, 0, h, h), true);
  378. if (grabbed != -1) {
  379. // Draw with selection color.
  380. draw_rect(Rect2(button_offset, 0, h, h), points[grabbed].color);
  381. } else {
  382. // If no color selected draw gray color with 'X' on top.
  383. draw_rect(Rect2(button_offset, 0, h, h), Color(0.5, 0.5, 0.5, 1));
  384. draw_line(Vector2(button_offset, 0), Vector2(button_offset + h, h), Color(1, 1, 1, 0.6));
  385. draw_line(Vector2(button_offset, h), Vector2(button_offset + h, 0), Color(1, 1, 1, 0.6));
  386. }
  387. } break;
  388. case NOTIFICATION_VISIBILITY_CHANGED: {
  389. if (!is_visible()) {
  390. grabbing = false;
  391. }
  392. } break;
  393. }
  394. }
  395. void GradientEditor::_bind_methods() {
  396. ADD_SIGNAL(MethodInfo("ramp_changed"));
  397. }
  398. GradientEditor::GradientEditor() {
  399. set_focus_mode(FOCUS_ALL);
  400. popup = memnew(PopupPanel);
  401. picker = memnew(ColorPicker);
  402. popup->add_child(picker);
  403. popup->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(GradientEditor::get_picker()));
  404. gradient_cache.instantiate();
  405. preview_texture.instantiate();
  406. preview_texture->set_width(1024);
  407. add_child(popup, false, INTERNAL_MODE_FRONT);
  408. }
  409. GradientEditor::~GradientEditor() {
  410. }