gradient_edit.cpp 14 KB

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