gradient_editor.cpp 14 KB

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