gradient_edit.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. 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. checker->create_from_image(img, ImageTexture::FLAG_REPEAT);
  51. }
  52. int GradientEdit::_get_point_from_pos(int x) {
  53. int result = -1;
  54. int total_w = get_size().width - get_size().height - SPACING;
  55. float min_distance = 1e20;
  56. for (int i = 0; i < points.size(); i++) {
  57. //Check if we clicked at point
  58. float distance = ABS(x - points[i].offset * total_w);
  59. float min = (POINT_WIDTH / 2 * 1.7); //make it easier to grab
  60. if (distance <= min && distance < min_distance) {
  61. result = i;
  62. min_distance = distance;
  63. }
  64. }
  65. return result;
  66. }
  67. void GradientEdit::_show_color_picker() {
  68. if (grabbed == -1)
  69. return;
  70. picker->set_pick_color(points[grabbed].color);
  71. Size2 minsize = popup->get_combined_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_global_position() - Vector2(0, minsize.y));
  78. } else {
  79. popup->set_position(get_global_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_scancode() == 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. 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.linear_interpolate(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::stepify(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. found = true;
  220. }
  221. }
  222. }
  223. if (found) {
  224. if (points[nearest_point].offset < newofs)
  225. newofs = points[nearest_point].offset + 0.00001;
  226. else
  227. newofs = points[nearest_point].offset - 0.00001;
  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("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", this, "_color_changed")) {
  256. picker->connect("color_changed", this, "_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. int total_w = get_size().width - get_size().height - SPACING;
  265. //Draw checker pattern for ramp
  266. _draw_checker(0, 0, total_w, h);
  267. //Draw color ramp
  268. Gradient::Point prev;
  269. prev.offset = 0;
  270. if (points.size() == 0)
  271. prev.color = Color(0, 0, 0); //Draw black rectangle if we have no points
  272. else
  273. prev.color = points[0].color; //Extend color of first point to the beginning.
  274. for (int i = -1; i < points.size(); i++) {
  275. Gradient::Point next;
  276. //If there is no next point
  277. if (i + 1 == points.size()) {
  278. if (points.size() == 0)
  279. next.color = Color(0, 0, 0); //Draw black rectangle if we have no points
  280. else
  281. next.color = points[i].color; //Extend color of last point to the end.
  282. next.offset = 1;
  283. } else {
  284. next = points[i + 1];
  285. }
  286. if (prev.offset == next.offset) {
  287. prev = next;
  288. continue;
  289. }
  290. Vector<Vector2> points;
  291. Vector<Color> colors;
  292. points.push_back(Vector2(prev.offset * total_w, h));
  293. points.push_back(Vector2(prev.offset * total_w, 0));
  294. points.push_back(Vector2(next.offset * total_w, 0));
  295. points.push_back(Vector2(next.offset * total_w, h));
  296. colors.push_back(prev.color);
  297. colors.push_back(prev.color);
  298. colors.push_back(next.color);
  299. colors.push_back(next.color);
  300. draw_primitive(points, colors, Vector<Point2>());
  301. prev = next;
  302. }
  303. //Draw point markers
  304. for (int i = 0; i < points.size(); i++) {
  305. Color col = points[i].color.contrasted();
  306. col.a = 0.9;
  307. draw_line(Vector2(points[i].offset * total_w, 0), Vector2(points[i].offset * total_w, h / 2), col);
  308. Rect2 rect = Rect2(points[i].offset * total_w - POINT_WIDTH / 2, h / 2, POINT_WIDTH, h / 2);
  309. draw_rect(rect, points[i].color, true);
  310. draw_rect(rect, col, false);
  311. if (grabbed == i) {
  312. rect = rect.grow(-1);
  313. if (has_focus()) {
  314. draw_rect(rect, Color(1, 0, 0, 0.9), false);
  315. } else {
  316. draw_rect(rect, Color(0.6, 0, 0, 0.9), false);
  317. }
  318. rect = rect.grow(-1);
  319. draw_rect(rect, col, false);
  320. }
  321. }
  322. //Draw "button" for color selector
  323. _draw_checker(total_w + SPACING, 0, h, h);
  324. if (grabbed != -1) {
  325. //Draw with selection color
  326. draw_rect(Rect2(total_w + SPACING, 0, h, h), points[grabbed].color);
  327. } else {
  328. //if no color selected draw grey color with 'X' on top.
  329. draw_rect(Rect2(total_w + SPACING, 0, h, h), Color(0.5, 0.5, 0.5, 1));
  330. draw_line(Vector2(total_w + SPACING, 0), Vector2(total_w + SPACING + h, h), Color(1, 1, 1, 0.6));
  331. draw_line(Vector2(total_w + SPACING, h), Vector2(total_w + SPACING + h, 0), Color(1, 1, 1, 0.6));
  332. }
  333. //Draw borders around color ramp if in focus
  334. if (has_focus()) {
  335. draw_line(Vector2(-1, -1), Vector2(total_w + 1, -1), Color(1, 1, 1, 0.6));
  336. draw_line(Vector2(total_w + 1, -1), Vector2(total_w + 1, h + 1), Color(1, 1, 1, 0.6));
  337. draw_line(Vector2(total_w + 1, h + 1), Vector2(-1, h + 1), Color(1, 1, 1, 0.6));
  338. draw_line(Vector2(-1, -1), Vector2(-1, h + 1), Color(1, 1, 1, 0.6));
  339. }
  340. }
  341. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  342. if (!is_visible()) {
  343. grabbing = false;
  344. }
  345. }
  346. }
  347. void GradientEdit::_draw_checker(int x, int y, int w, int h) {
  348. //Draw it with polygon to insert UVs for scale
  349. Vector<Vector2> backPoints;
  350. backPoints.push_back(Vector2(x, y));
  351. backPoints.push_back(Vector2(x, y + h));
  352. backPoints.push_back(Vector2(x + w, y + h));
  353. backPoints.push_back(Vector2(x + w, y));
  354. Vector<Color> colorPoints;
  355. colorPoints.push_back(Color(1, 1, 1, 1));
  356. colorPoints.push_back(Color(1, 1, 1, 1));
  357. colorPoints.push_back(Color(1, 1, 1, 1));
  358. colorPoints.push_back(Color(1, 1, 1, 1));
  359. Vector<Vector2> uvPoints;
  360. //Draw checker pattern pixel-perfect and scale it by 2.
  361. uvPoints.push_back(Vector2(x, y));
  362. uvPoints.push_back(Vector2(x, y + h * .5f / checker->get_height()));
  363. uvPoints.push_back(Vector2(x + w * .5f / checker->get_width(), y + h * .5f / checker->get_height()));
  364. uvPoints.push_back(Vector2(x + w * .5f / checker->get_width(), y));
  365. draw_polygon(backPoints, colorPoints, uvPoints, checker);
  366. }
  367. Size2 GradientEdit::get_minimum_size() const {
  368. return Vector2(0, 16);
  369. }
  370. void GradientEdit::_color_changed(const Color &p_color) {
  371. if (grabbed == -1)
  372. return;
  373. points.write[grabbed].color = p_color;
  374. update();
  375. emit_signal("ramp_changed");
  376. }
  377. void GradientEdit::set_ramp(const Vector<float> &p_offsets, const Vector<Color> &p_colors) {
  378. ERR_FAIL_COND(p_offsets.size() != p_colors.size());
  379. points.clear();
  380. for (int i = 0; i < p_offsets.size(); i++) {
  381. Gradient::Point p;
  382. p.offset = p_offsets[i];
  383. p.color = p_colors[i];
  384. points.push_back(p);
  385. }
  386. points.sort();
  387. update();
  388. }
  389. Vector<float> GradientEdit::get_offsets() const {
  390. Vector<float> ret;
  391. for (int i = 0; i < points.size(); i++)
  392. ret.push_back(points[i].offset);
  393. return ret;
  394. }
  395. Vector<Color> GradientEdit::get_colors() const {
  396. Vector<Color> ret;
  397. for (int i = 0; i < points.size(); i++)
  398. ret.push_back(points[i].color);
  399. return ret;
  400. }
  401. void GradientEdit::set_points(Vector<Gradient::Point> &p_points) {
  402. if (points.size() != p_points.size())
  403. grabbed = -1;
  404. points.clear();
  405. points = p_points;
  406. }
  407. Vector<Gradient::Point> &GradientEdit::get_points() {
  408. return points;
  409. }
  410. void GradientEdit::_bind_methods() {
  411. ClassDB::bind_method(D_METHOD("_gui_input"), &GradientEdit::_gui_input);
  412. ClassDB::bind_method(D_METHOD("_color_changed"), &GradientEdit::_color_changed);
  413. ADD_SIGNAL(MethodInfo("ramp_changed"));
  414. }