gradient_edit.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. picker->set_pick_color(points[grabbed].color);
  70. Size2 minsize = popup->get_combined_minimum_size();
  71. bool show_above = false;
  72. if (get_global_position().y + get_size().y + minsize.y > get_viewport_rect().size.y) {
  73. show_above = true;
  74. }
  75. if (show_above) {
  76. popup->set_position(get_global_position() - Vector2(0, minsize.y));
  77. } else {
  78. popup->set_position(get_global_position() + Vector2(0, get_size().y));
  79. }
  80. popup->popup();
  81. }
  82. GradientEdit::~GradientEdit() {
  83. }
  84. void GradientEdit::_gui_input(const Ref<InputEvent> &p_event) {
  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. if (pos == -1) {
  160. prev.color = Color(0, 0, 0);
  161. prev.offset = 0;
  162. if (points.size()) {
  163. next = points[0];
  164. } else {
  165. next.color = Color(1, 1, 1);
  166. next.offset = 1.0;
  167. }
  168. } else {
  169. if (pos == points.size() - 1) {
  170. next.color = Color(1, 1, 1);
  171. next.offset = 1.0;
  172. } else {
  173. next = points[pos + 1];
  174. }
  175. prev = points[pos];
  176. }
  177. newPoint.color = prev.color.linear_interpolate(next.color, (newPoint.offset - prev.offset) / (next.offset - prev.offset));
  178. points.push_back(newPoint);
  179. points.sort();
  180. for (int i = 0; i < points.size(); i++) {
  181. if (points[i].offset == newPoint.offset) {
  182. grabbed = i;
  183. break;
  184. }
  185. }
  186. emit_signal("ramp_changed");
  187. }
  188. if (mb.is_valid() && mb->get_button_index() == 1 && !mb->is_pressed()) {
  189. if (grabbing) {
  190. grabbing = false;
  191. emit_signal("ramp_changed");
  192. }
  193. update();
  194. }
  195. Ref<InputEventMouseMotion> mm = p_event;
  196. if (mm.is_valid() && grabbing) {
  197. int total_w = get_size().width - get_size().height - SPACING;
  198. int x = mm->get_position().x;
  199. float newofs = CLAMP(x / float(total_w), 0, 1);
  200. // Snap to "round" coordinates if holding Ctrl.
  201. // Be more precise if holding Shift as well
  202. if (mm->get_control()) {
  203. newofs = Math::stepify(newofs, mm->get_shift() ? 0.025 : 0.1);
  204. } else if (mm->get_shift()) {
  205. // Snap to nearest point if holding just Shift
  206. const float snap_threshold = 0.03;
  207. float smallest_ofs = snap_threshold;
  208. bool found = false;
  209. int nearest_point = 0;
  210. for (int i = 0; i < points.size(); ++i) {
  211. if (i != grabbed) {
  212. float temp_ofs = ABS(points[i].offset - newofs);
  213. if (temp_ofs < smallest_ofs) {
  214. smallest_ofs = temp_ofs;
  215. nearest_point = i;
  216. if (found)
  217. break;
  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. newofs = CLAMP(newofs, 0, 1);
  228. }
  229. }
  230. bool valid = true;
  231. for (int i = 0; i < points.size(); i++) {
  232. if (points[i].offset == newofs && i != grabbed) {
  233. valid = false;
  234. break;
  235. }
  236. }
  237. if (!valid || grabbed == -1) {
  238. return;
  239. }
  240. points.write[grabbed].offset = newofs;
  241. points.sort();
  242. for (int i = 0; i < points.size(); i++) {
  243. if (points[i].offset == newofs) {
  244. grabbed = i;
  245. break;
  246. }
  247. }
  248. emit_signal("ramp_changed");
  249. update();
  250. }
  251. }
  252. void GradientEdit::_notification(int p_what) {
  253. if (p_what == NOTIFICATION_ENTER_TREE) {
  254. if (!picker->is_connected("color_changed", callable_mp(this, &GradientEdit::_color_changed))) {
  255. picker->connect("color_changed", callable_mp(this, &GradientEdit::_color_changed));
  256. }
  257. }
  258. if (p_what == NOTIFICATION_DRAW) {
  259. int w = get_size().x;
  260. int h = get_size().y;
  261. if (w == 0 || h == 0)
  262. return; //Safety check. We have division by 'h'. And in any case there is nothing to draw with such size
  263. int total_w = get_size().width - get_size().height - SPACING;
  264. //Draw checker pattern for ramp
  265. _draw_checker(0, 0, total_w, h);
  266. //Draw color ramp
  267. Gradient::Point prev;
  268. prev.offset = 0;
  269. if (points.size() == 0)
  270. prev.color = Color(0, 0, 0); //Draw black rectangle if we have no points
  271. else
  272. prev.color = points[0].color; //Extend color of first point to the beginning.
  273. for (int i = -1; i < points.size(); i++) {
  274. Gradient::Point next;
  275. //If there is no next point
  276. if (i + 1 == points.size()) {
  277. if (points.size() == 0)
  278. next.color = Color(0, 0, 0); //Draw black rectangle if we have no points
  279. else
  280. next.color = points[i].color; //Extend color of last point to the end.
  281. next.offset = 1;
  282. } else {
  283. next = points[i + 1];
  284. }
  285. if (prev.offset == next.offset) {
  286. prev = next;
  287. continue;
  288. }
  289. Vector<Vector2> points;
  290. Vector<Color> colors;
  291. points.push_back(Vector2(prev.offset * total_w, h));
  292. points.push_back(Vector2(prev.offset * total_w, 0));
  293. points.push_back(Vector2(next.offset * total_w, 0));
  294. points.push_back(Vector2(next.offset * total_w, h));
  295. colors.push_back(prev.color);
  296. colors.push_back(prev.color);
  297. colors.push_back(next.color);
  298. colors.push_back(next.color);
  299. draw_primitive(points, colors, Vector<Point2>());
  300. prev = next;
  301. }
  302. //Draw point markers
  303. for (int i = 0; i < points.size(); i++) {
  304. Color col = points[i].color.contrasted();
  305. col.a = 0.9;
  306. draw_line(Vector2(points[i].offset * total_w, 0), Vector2(points[i].offset * total_w, h / 2), col);
  307. Rect2 rect = Rect2(points[i].offset * total_w - POINT_WIDTH / 2, h / 2, POINT_WIDTH, h / 2);
  308. draw_rect(rect, points[i].color, true);
  309. draw_rect(rect, col, false);
  310. if (grabbed == i) {
  311. rect = rect.grow(-1);
  312. if (has_focus()) {
  313. draw_rect(rect, Color(1, 0, 0, 0.9), false);
  314. } else {
  315. draw_rect(rect, Color(0.6, 0, 0, 0.9), false);
  316. }
  317. rect = rect.grow(-1);
  318. draw_rect(rect, col, false);
  319. }
  320. }
  321. //Draw "button" for color selector
  322. _draw_checker(total_w + SPACING, 0, h, h);
  323. if (grabbed != -1) {
  324. //Draw with selection color
  325. draw_rect(Rect2(total_w + SPACING, 0, h, h), points[grabbed].color);
  326. } else {
  327. //if no color selected draw grey color with 'X' on top.
  328. draw_rect(Rect2(total_w + SPACING, 0, h, h), Color(0.5, 0.5, 0.5, 1));
  329. draw_line(Vector2(total_w + SPACING, 0), Vector2(total_w + SPACING + h, h), Color(1, 1, 1, 0.6));
  330. draw_line(Vector2(total_w + SPACING, h), Vector2(total_w + SPACING + h, 0), Color(1, 1, 1, 0.6));
  331. }
  332. //Draw borders around color ramp if in focus
  333. if (has_focus()) {
  334. draw_line(Vector2(-1, -1), Vector2(total_w + 1, -1), Color(1, 1, 1, 0.6));
  335. draw_line(Vector2(total_w + 1, -1), Vector2(total_w + 1, h + 1), Color(1, 1, 1, 0.6));
  336. draw_line(Vector2(total_w + 1, h + 1), Vector2(-1, h + 1), Color(1, 1, 1, 0.6));
  337. draw_line(Vector2(-1, -1), Vector2(-1, h + 1), Color(1, 1, 1, 0.6));
  338. }
  339. }
  340. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  341. if (!is_visible()) {
  342. grabbing = false;
  343. }
  344. }
  345. }
  346. void GradientEdit::_draw_checker(int x, int y, int w, int h) {
  347. //Draw it with polygon to insert UVs for scale
  348. Vector<Vector2> backPoints;
  349. backPoints.push_back(Vector2(x, y));
  350. backPoints.push_back(Vector2(x, y + h));
  351. backPoints.push_back(Vector2(x + w, y + h));
  352. backPoints.push_back(Vector2(x + w, y));
  353. Vector<Color> colorPoints;
  354. colorPoints.push_back(Color(1, 1, 1, 1));
  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. Vector<Vector2> uvPoints;
  359. //Draw checker pattern pixel-perfect and scale it by 2.
  360. uvPoints.push_back(Vector2(x, y));
  361. uvPoints.push_back(Vector2(x, y + h * .5f / checker->get_height()));
  362. uvPoints.push_back(Vector2(x + w * .5f / checker->get_width(), y + h * .5f / checker->get_height()));
  363. uvPoints.push_back(Vector2(x + w * .5f / checker->get_width(), y));
  364. draw_polygon(backPoints, colorPoints, uvPoints, checker);
  365. }
  366. Size2 GradientEdit::get_minimum_size() const {
  367. return Vector2(0, 16);
  368. }
  369. void GradientEdit::_color_changed(const Color &p_color) {
  370. if (grabbed == -1)
  371. return;
  372. points.write[grabbed].color = p_color;
  373. update();
  374. emit_signal("ramp_changed");
  375. }
  376. void GradientEdit::set_ramp(const Vector<float> &p_offsets, const Vector<Color> &p_colors) {
  377. ERR_FAIL_COND(p_offsets.size() != p_colors.size());
  378. points.clear();
  379. for (int i = 0; i < p_offsets.size(); i++) {
  380. Gradient::Point p;
  381. p.offset = p_offsets[i];
  382. p.color = p_colors[i];
  383. points.push_back(p);
  384. }
  385. points.sort();
  386. update();
  387. }
  388. Vector<float> GradientEdit::get_offsets() const {
  389. Vector<float> ret;
  390. for (int i = 0; i < points.size(); i++)
  391. ret.push_back(points[i].offset);
  392. return ret;
  393. }
  394. Vector<Color> GradientEdit::get_colors() const {
  395. Vector<Color> ret;
  396. for (int i = 0; i < points.size(); i++)
  397. ret.push_back(points[i].color);
  398. return ret;
  399. }
  400. void GradientEdit::set_points(Vector<Gradient::Point> &p_points) {
  401. if (points.size() != p_points.size())
  402. grabbed = -1;
  403. points.clear();
  404. points = p_points;
  405. }
  406. Vector<Gradient::Point> &GradientEdit::get_points() {
  407. return points;
  408. }
  409. void GradientEdit::_bind_methods() {
  410. ClassDB::bind_method(D_METHOD("_gui_input"), &GradientEdit::_gui_input);
  411. ADD_SIGNAL(MethodInfo("ramp_changed"));
  412. }