gradient_edit.cpp 14 KB

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