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