gradient_edit.cpp 13 KB

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