gradient_edit.cpp 13 KB

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