gradient_editor.cpp 15 KB

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