gradient_editor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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;
  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 / 2 * 1.7); // Make it easier to grab.
  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. // Show color picker on double click.
  177. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_double_click() && mb->is_pressed()) {
  178. grabbed = _get_point_from_pos(mb->get_position().x);
  179. _show_color_picker();
  180. accept_event();
  181. return;
  182. }
  183. // Delete point on right click.
  184. if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  185. grabbed = _get_point_from_pos(mb->get_position().x);
  186. if (grabbed != -1) {
  187. points.remove_at(grabbed);
  188. grabbed = -1;
  189. grabbing = false;
  190. queue_redraw();
  191. emit_signal(SNAME("ramp_changed"));
  192. accept_event();
  193. }
  194. }
  195. // Hold alt key to duplicate selected color.
  196. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed() && mb->is_alt_pressed()) {
  197. int x = mb->get_position().x;
  198. grabbed = _get_point_from_pos(x);
  199. if (grabbed != -1) {
  200. int total_w = get_size().width - get_size().height - draw_spacing;
  201. Gradient::Point new_point = points[grabbed];
  202. new_point.offset = CLAMP(x / float(total_w), 0, 1);
  203. points.push_back(new_point);
  204. points.sort();
  205. for (int i = 0; i < points.size(); ++i) {
  206. if (points[i].offset == new_point.offset) {
  207. grabbed = i;
  208. break;
  209. }
  210. }
  211. emit_signal(SNAME("ramp_changed"));
  212. queue_redraw();
  213. }
  214. }
  215. // Select.
  216. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  217. queue_redraw();
  218. int x = mb->get_position().x;
  219. int total_w = get_size().width - get_size().height - draw_spacing;
  220. //Check if color selector was clicked.
  221. if (x > total_w + draw_spacing) {
  222. _show_color_picker();
  223. return;
  224. }
  225. grabbing = true;
  226. grabbed = _get_point_from_pos(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(x / float(total_w), 0, 1);
  234. Gradient::Point prev;
  235. Gradient::Point next;
  236. int pos = -1;
  237. for (int i = 0; i < points.size(); i++) {
  238. if (points[i].offset < new_point.offset) {
  239. pos = i;
  240. }
  241. }
  242. if (pos == -1) {
  243. prev.color = Color(0, 0, 0);
  244. prev.offset = 0;
  245. if (points.size()) {
  246. next = points[0];
  247. } else {
  248. next.color = Color(1, 1, 1);
  249. next.offset = 1.0;
  250. }
  251. } else {
  252. if (pos == points.size() - 1) {
  253. next.color = Color(1, 1, 1);
  254. next.offset = 1.0;
  255. } else {
  256. next = points[pos + 1];
  257. }
  258. prev = points[pos];
  259. }
  260. new_point.color = prev.color.lerp(next.color, (new_point.offset - prev.offset) / (next.offset - prev.offset));
  261. points.push_back(new_point);
  262. points.sort();
  263. for (int i = 0; i < points.size(); i++) {
  264. if (points[i].offset == new_point.offset) {
  265. grabbed = i;
  266. break;
  267. }
  268. }
  269. emit_signal(SNAME("ramp_changed"));
  270. }
  271. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  272. if (grabbing) {
  273. grabbing = false;
  274. emit_signal(SNAME("ramp_changed"));
  275. }
  276. queue_redraw();
  277. }
  278. Ref<InputEventMouseMotion> mm = p_event;
  279. if (mm.is_valid() && grabbing) {
  280. int total_w = get_size().width - get_size().height - draw_spacing;
  281. int x = mm->get_position().x;
  282. float newofs = CLAMP(x / float(total_w), 0, 1);
  283. // Snap to "round" coordinates if holding Ctrl.
  284. // Be more precise if holding Shift as well.
  285. if (mm->is_ctrl_pressed()) {
  286. newofs = Math::snapped(newofs, mm->is_shift_pressed() ? 0.025 : 0.1);
  287. } else if (mm->is_shift_pressed()) {
  288. // Snap to nearest point if holding just Shift
  289. const float snap_threshold = 0.03;
  290. float smallest_ofs = snap_threshold;
  291. bool found = false;
  292. int nearest_point = 0;
  293. for (int i = 0; i < points.size(); ++i) {
  294. if (i != grabbed) {
  295. float temp_ofs = ABS(points[i].offset - newofs);
  296. if (temp_ofs < smallest_ofs) {
  297. smallest_ofs = temp_ofs;
  298. nearest_point = i;
  299. if (found) {
  300. break;
  301. }
  302. found = true;
  303. }
  304. }
  305. }
  306. if (found) {
  307. if (points[nearest_point].offset < newofs) {
  308. newofs = points[nearest_point].offset + 0.00001;
  309. } else {
  310. newofs = points[nearest_point].offset - 0.00001;
  311. }
  312. newofs = CLAMP(newofs, 0, 1);
  313. }
  314. }
  315. bool valid = true;
  316. for (int i = 0; i < points.size(); i++) {
  317. if (points[i].offset == newofs && i != grabbed) {
  318. valid = false;
  319. break;
  320. }
  321. }
  322. if (!valid || grabbed == -1) {
  323. return;
  324. }
  325. points.write[grabbed].offset = newofs;
  326. points.sort();
  327. for (int i = 0; i < points.size(); i++) {
  328. if (points[i].offset == newofs) {
  329. grabbed = i;
  330. break;
  331. }
  332. }
  333. emit_signal(SNAME("ramp_changed"));
  334. queue_redraw();
  335. }
  336. }
  337. void GradientEditor::_notification(int p_what) {
  338. switch (p_what) {
  339. case NOTIFICATION_ENTER_TREE: {
  340. if (!picker->is_connected("color_changed", callable_mp(this, &GradientEditor::_color_changed))) {
  341. picker->connect("color_changed", callable_mp(this, &GradientEditor::_color_changed));
  342. }
  343. [[fallthrough]];
  344. }
  345. case NOTIFICATION_THEME_CHANGED: {
  346. draw_spacing = BASE_SPACING * get_theme_default_base_scale();
  347. handle_width = BASE_HANDLE_WIDTH * get_theme_default_base_scale();
  348. } break;
  349. case NOTIFICATION_DRAW: {
  350. int w = get_size().x;
  351. int h = get_size().y;
  352. if (w == 0 || h == 0) {
  353. return; // Safety check. We have division by 'h'. And in any case there is nothing to draw with such size.
  354. }
  355. int total_w = get_size().width - get_size().height - draw_spacing - handle_width;
  356. // Draw checker pattern for ramp.
  357. draw_texture_rect(get_theme_icon(SNAME("GuiMiniCheckerboard"), SNAME("EditorIcons")), Rect2(handle_width / 2, 0, total_w, h), true);
  358. // Draw color ramp.
  359. gradient_cache->set_points(points);
  360. gradient_cache->set_interpolation_mode(interpolation_mode);
  361. preview_texture->set_gradient(gradient_cache);
  362. draw_texture_rect(preview_texture, Rect2(handle_width / 2, 0, total_w, h));
  363. // Draw borders around color ramp if in focus.
  364. if (has_focus()) {
  365. draw_rect(Rect2(handle_width / 2, 0, total_w, h), Color(1, 1, 1, 0.9), false);
  366. }
  367. // Draw point markers.
  368. for (int i = 0; i < points.size(); i++) {
  369. Color col = points[i].color.get_v() > 0.5 ? Color(0, 0, 0) : Color(1, 1, 1);
  370. col.a = 0.9;
  371. draw_line(Vector2(points[i].offset * total_w + handle_width / 2, 0), Vector2(points[i].offset * total_w + handle_width / 2, h / 2), col);
  372. Rect2 rect = Rect2(points[i].offset * total_w, h / 2, handle_width, h / 2);
  373. draw_rect(rect, points[i].color, true);
  374. draw_rect(rect, col, false);
  375. if (grabbed == i) {
  376. const Color focus_color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  377. rect = rect.grow(-1);
  378. if (has_focus()) {
  379. draw_rect(rect, focus_color, false);
  380. } else {
  381. draw_rect(rect, focus_color.darkened(0.4), false);
  382. }
  383. rect = rect.grow(-1);
  384. draw_rect(rect, col, false);
  385. }
  386. }
  387. // Draw "button" for color selector.
  388. int button_offset = total_w + handle_width + draw_spacing;
  389. draw_texture_rect(get_theme_icon(SNAME("GuiMiniCheckerboard"), SNAME("EditorIcons")), Rect2(button_offset, 0, h, h), true);
  390. if (grabbed != -1) {
  391. // Draw with selection color.
  392. draw_rect(Rect2(button_offset, 0, h, h), points[grabbed].color);
  393. } else {
  394. // If no color selected draw grey color with 'X' on top.
  395. draw_rect(Rect2(button_offset, 0, h, h), Color(0.5, 0.5, 0.5, 1));
  396. draw_line(Vector2(button_offset, 0), Vector2(button_offset + h, h), Color(1, 1, 1, 0.6));
  397. draw_line(Vector2(button_offset, h), Vector2(button_offset + h, 0), Color(1, 1, 1, 0.6));
  398. }
  399. } break;
  400. case NOTIFICATION_VISIBILITY_CHANGED: {
  401. if (!is_visible()) {
  402. grabbing = false;
  403. }
  404. } break;
  405. }
  406. }
  407. void GradientEditor::_bind_methods() {
  408. ADD_SIGNAL(MethodInfo("ramp_changed"));
  409. }
  410. GradientEditor::GradientEditor() {
  411. set_focus_mode(FOCUS_ALL);
  412. popup = memnew(PopupPanel);
  413. picker = memnew(ColorPicker);
  414. popup->add_child(picker);
  415. popup->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(GradientEditor::get_picker()));
  416. gradient_cache.instantiate();
  417. preview_texture.instantiate();
  418. preview_texture->set_width(1024);
  419. add_child(popup, false, INTERNAL_MODE_FRONT);
  420. }
  421. GradientEditor::~GradientEditor() {
  422. }