gradient_edit.cpp 13 KB

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