gradient_editor.cpp 15 KB

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