gradient_edit.cpp 13 KB

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