gradient_edit.cpp 13 KB

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