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