gradient_edit.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*************************************************************************/
  2. /* gradient_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_DELETE && grabbed != -1) {
  80. points.remove(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() == 1 && 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() == 2 && mb->is_pressed()) {
  96. grabbed = _get_point_from_pos(mb->get_position().x);
  97. if (grabbed != -1) {
  98. points.remove(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() == 1 && 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() == 1 && 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() == 1 && !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. if (p_what == NOTIFICATION_ENTER_TREE) {
  250. if (!picker->is_connected("color_changed", callable_mp(this, &GradientEdit::_color_changed))) {
  251. picker->connect("color_changed", callable_mp(this, &GradientEdit::_color_changed));
  252. }
  253. }
  254. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  255. draw_spacing = BASE_SPACING * get_theme_default_base_scale();
  256. draw_point_width = BASE_POINT_WIDTH * get_theme_default_base_scale();
  257. }
  258. if (p_what == NOTIFICATION_DRAW) {
  259. int w = get_size().x;
  260. int h = get_size().y;
  261. if (w == 0 || h == 0) {
  262. return; // Safety check. We have division by 'h'. And in any case there is nothing to draw with such size.
  263. }
  264. int total_w = get_size().width - get_size().height - draw_spacing;
  265. // Draw checker pattern for ramp.
  266. draw_texture_rect(get_theme_icon(SNAME("GuiMiniCheckerboard"), SNAME("EditorIcons")), Rect2(0, 0, total_w, h), true);
  267. // Draw color ramp.
  268. gradient_cache->set_points(points);
  269. gradient_cache->set_interpolation_mode(interpolation_mode);
  270. preview_texture->set_gradient(gradient_cache);
  271. draw_texture_rect(preview_texture, Rect2(0, 0, total_w, h));
  272. // Draw point markers.
  273. for (int i = 0; i < points.size(); i++) {
  274. Color col = points[i].color.inverted();
  275. col.a = 0.9;
  276. draw_line(Vector2(points[i].offset * total_w, 0), Vector2(points[i].offset * total_w, h / 2), col);
  277. Rect2 rect = Rect2(points[i].offset * total_w - draw_point_width / 2, h / 2, draw_point_width, h / 2);
  278. draw_rect(rect, points[i].color, true);
  279. draw_rect(rect, col, false);
  280. if (grabbed == i) {
  281. rect = rect.grow(-1);
  282. if (has_focus()) {
  283. draw_rect(rect, Color(1, 0, 0, 0.9), false);
  284. } else {
  285. draw_rect(rect, Color(0.6, 0, 0, 0.9), false);
  286. }
  287. rect = rect.grow(-1);
  288. draw_rect(rect, col, false);
  289. }
  290. }
  291. //Draw "button" for color selector
  292. draw_texture_rect(get_theme_icon(SNAME("GuiMiniCheckerboard"), SNAME("EditorIcons")), Rect2(total_w + draw_spacing, 0, h, h), true);
  293. if (grabbed != -1) {
  294. //Draw with selection color
  295. draw_rect(Rect2(total_w + draw_spacing, 0, h, h), points[grabbed].color);
  296. } else {
  297. //if no color selected draw grey color with 'X' on top.
  298. draw_rect(Rect2(total_w + draw_spacing, 0, h, h), Color(0.5, 0.5, 0.5, 1));
  299. draw_line(Vector2(total_w + draw_spacing, 0), Vector2(total_w + draw_spacing + h, h), Color(1, 1, 1, 0.6));
  300. draw_line(Vector2(total_w + draw_spacing, h), Vector2(total_w + draw_spacing + h, 0), Color(1, 1, 1, 0.6));
  301. }
  302. // Draw borders around color ramp if in focus.
  303. if (has_focus()) {
  304. draw_line(Vector2(-1, -1), Vector2(total_w + 1, -1), Color(1, 1, 1, 0.6));
  305. draw_line(Vector2(total_w + 1, -1), Vector2(total_w + 1, h + 1), Color(1, 1, 1, 0.6));
  306. draw_line(Vector2(total_w + 1, h + 1), Vector2(-1, h + 1), Color(1, 1, 1, 0.6));
  307. draw_line(Vector2(-1, -1), Vector2(-1, h + 1), Color(1, 1, 1, 0.6));
  308. }
  309. }
  310. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  311. if (!is_visible()) {
  312. grabbing = false;
  313. }
  314. }
  315. }
  316. Size2 GradientEdit::get_minimum_size() const {
  317. return Vector2(0, 16);
  318. }
  319. void GradientEdit::_color_changed(const Color &p_color) {
  320. if (grabbed == -1) {
  321. return;
  322. }
  323. points.write[grabbed].color = p_color;
  324. update();
  325. emit_signal(SNAME("ramp_changed"));
  326. }
  327. void GradientEdit::set_ramp(const Vector<real_t> &p_offsets, const Vector<Color> &p_colors) {
  328. ERR_FAIL_COND(p_offsets.size() != p_colors.size());
  329. points.clear();
  330. for (int i = 0; i < p_offsets.size(); i++) {
  331. Gradient::Point p;
  332. p.offset = p_offsets[i];
  333. p.color = p_colors[i];
  334. points.push_back(p);
  335. }
  336. points.sort();
  337. update();
  338. }
  339. Vector<real_t> GradientEdit::get_offsets() const {
  340. Vector<real_t> ret;
  341. for (int i = 0; i < points.size(); i++) {
  342. ret.push_back(points[i].offset);
  343. }
  344. return ret;
  345. }
  346. Vector<Color> GradientEdit::get_colors() const {
  347. Vector<Color> ret;
  348. for (int i = 0; i < points.size(); i++) {
  349. ret.push_back(points[i].color);
  350. }
  351. return ret;
  352. }
  353. void GradientEdit::set_points(Vector<Gradient::Point> &p_points) {
  354. if (points.size() != p_points.size()) {
  355. grabbed = -1;
  356. }
  357. points.clear();
  358. points = p_points;
  359. points.sort();
  360. }
  361. Vector<Gradient::Point> &GradientEdit::get_points() {
  362. return points;
  363. }
  364. void GradientEdit::set_interpolation_mode(Gradient::InterpolationMode p_interp_mode) {
  365. interpolation_mode = p_interp_mode;
  366. }
  367. Gradient::InterpolationMode GradientEdit::get_interpolation_mode() {
  368. return interpolation_mode;
  369. }
  370. void GradientEdit::_bind_methods() {
  371. ADD_SIGNAL(MethodInfo("ramp_changed"));
  372. }