polygon_2d_editor_plugin.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*************************************************************************/
  2. /* polygon_2d_editor_plugin.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 "polygon_2d_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "editor/editor_settings.h"
  33. #include "os/file_access.h"
  34. #include "os/input.h"
  35. #include "os/keyboard.h"
  36. Node2D *Polygon2DEditor::_get_node() const {
  37. return node;
  38. }
  39. void Polygon2DEditor::_set_node(Node *p_polygon) {
  40. node = Object::cast_to<Polygon2D>(p_polygon);
  41. }
  42. Vector2 Polygon2DEditor::_get_offset(int p_idx) const {
  43. return node->get_offset();
  44. }
  45. void Polygon2DEditor::_notification(int p_what) {
  46. switch (p_what) {
  47. case NOTIFICATION_READY: {
  48. button_uv->set_icon(get_icon("Uv", "EditorIcons"));
  49. uv_button[UV_MODE_EDIT_POINT]->set_icon(get_icon("ToolSelect", "EditorIcons"));
  50. uv_button[UV_MODE_MOVE]->set_icon(get_icon("ToolMove", "EditorIcons"));
  51. uv_button[UV_MODE_ROTATE]->set_icon(get_icon("ToolRotate", "EditorIcons"));
  52. uv_button[UV_MODE_SCALE]->set_icon(get_icon("ToolScale", "EditorIcons"));
  53. b_snap_grid->set_icon(get_icon("Grid", "EditorIcons"));
  54. b_snap_enable->set_icon(get_icon("SnapGrid", "EditorIcons"));
  55. uv_icon_zoom->set_texture(get_icon("Zoom", "EditorIcons"));
  56. } break;
  57. case NOTIFICATION_PHYSICS_PROCESS: {
  58. } break;
  59. }
  60. }
  61. void Polygon2DEditor::_menu_option(int p_option) {
  62. switch (p_option) {
  63. case MODE_EDIT_UV: {
  64. if (node->get_texture().is_null()) {
  65. error->set_text("No texture in this polygon.\nSet a texture to be able to edit UV.");
  66. error->popup_centered_minsize();
  67. return;
  68. }
  69. PoolVector<Vector2> points = node->get_polygon();
  70. PoolVector<Vector2> uvs = node->get_uv();
  71. if (uvs.size() != points.size()) {
  72. undo_redo->create_action(TTR("Create UV Map"));
  73. undo_redo->add_do_method(node, "set_uv", points);
  74. undo_redo->add_undo_method(node, "set_uv", uvs);
  75. undo_redo->add_do_method(uv_edit_draw, "update");
  76. undo_redo->add_undo_method(uv_edit_draw, "update");
  77. undo_redo->commit_action();
  78. }
  79. uv_edit->popup_centered_ratio(0.85);
  80. } break;
  81. case UVEDIT_POLYGON_TO_UV: {
  82. PoolVector<Vector2> points = node->get_polygon();
  83. if (points.size() == 0)
  84. break;
  85. PoolVector<Vector2> uvs = node->get_uv();
  86. undo_redo->create_action(TTR("Create UV Map"));
  87. undo_redo->add_do_method(node, "set_uv", points);
  88. undo_redo->add_undo_method(node, "set_uv", uvs);
  89. undo_redo->add_do_method(uv_edit_draw, "update");
  90. undo_redo->add_undo_method(uv_edit_draw, "update");
  91. undo_redo->commit_action();
  92. } break;
  93. case UVEDIT_UV_TO_POLYGON: {
  94. PoolVector<Vector2> points = node->get_polygon();
  95. PoolVector<Vector2> uvs = node->get_uv();
  96. if (uvs.size() == 0)
  97. break;
  98. undo_redo->create_action(TTR("Create UV Map"));
  99. undo_redo->add_do_method(node, "set_polygon", uvs);
  100. undo_redo->add_undo_method(node, "set_polygon", points);
  101. undo_redo->add_do_method(uv_edit_draw, "update");
  102. undo_redo->add_undo_method(uv_edit_draw, "update");
  103. undo_redo->commit_action();
  104. } break;
  105. case UVEDIT_UV_CLEAR: {
  106. PoolVector<Vector2> uvs = node->get_uv();
  107. if (uvs.size() == 0)
  108. break;
  109. undo_redo->create_action(TTR("Create UV Map"));
  110. undo_redo->add_do_method(node, "set_uv", PoolVector<Vector2>());
  111. undo_redo->add_undo_method(node, "set_uv", uvs);
  112. undo_redo->add_do_method(uv_edit_draw, "update");
  113. undo_redo->add_undo_method(uv_edit_draw, "update");
  114. undo_redo->commit_action();
  115. } break;
  116. default: {
  117. AbstractPolygon2DEditor::_menu_option(p_option);
  118. } break;
  119. }
  120. }
  121. void Polygon2DEditor::_set_use_snap(bool p_use) {
  122. use_snap = p_use;
  123. }
  124. void Polygon2DEditor::_set_show_grid(bool p_show) {
  125. snap_show_grid = p_show;
  126. uv_edit_draw->update();
  127. }
  128. void Polygon2DEditor::_set_snap_off_x(float p_val) {
  129. snap_offset.x = p_val;
  130. uv_edit_draw->update();
  131. }
  132. void Polygon2DEditor::_set_snap_off_y(float p_val) {
  133. snap_offset.y = p_val;
  134. uv_edit_draw->update();
  135. }
  136. void Polygon2DEditor::_set_snap_step_x(float p_val) {
  137. snap_step.x = p_val;
  138. uv_edit_draw->update();
  139. }
  140. void Polygon2DEditor::_set_snap_step_y(float p_val) {
  141. snap_step.y = p_val;
  142. uv_edit_draw->update();
  143. }
  144. void Polygon2DEditor::_uv_mode(int p_mode) {
  145. uv_mode = UVMode(p_mode);
  146. for (int i = 0; i < UV_MODE_MAX; i++) {
  147. uv_button[i]->set_pressed(p_mode == i);
  148. }
  149. }
  150. void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) {
  151. Transform2D mtx;
  152. mtx.elements[2] = -uv_draw_ofs;
  153. mtx.scale_basis(Vector2(uv_draw_zoom, uv_draw_zoom));
  154. Ref<InputEventMouseButton> mb = p_input;
  155. if (mb.is_valid()) {
  156. if (mb->get_button_index() == BUTTON_LEFT) {
  157. if (mb->is_pressed()) {
  158. uv_drag_from = Vector2(mb->get_position().x, mb->get_position().y);
  159. uv_drag = true;
  160. uv_prev = node->get_uv();
  161. uv_move_current = uv_mode;
  162. if (uv_move_current == UV_MODE_EDIT_POINT) {
  163. if (mb->get_shift() && mb->get_command())
  164. uv_move_current = UV_MODE_SCALE;
  165. else if (mb->get_shift())
  166. uv_move_current = UV_MODE_MOVE;
  167. else if (mb->get_command())
  168. uv_move_current = UV_MODE_ROTATE;
  169. }
  170. if (uv_move_current == UV_MODE_EDIT_POINT) {
  171. uv_drag_index = -1;
  172. for (int i = 0; i < uv_prev.size(); i++) {
  173. Vector2 tuv = mtx.xform(uv_prev[i]);
  174. if (tuv.distance_to(Vector2(mb->get_position().x, mb->get_position().y)) < 8) {
  175. uv_drag_from = tuv;
  176. uv_drag_index = i;
  177. }
  178. }
  179. if (uv_drag_index == -1) {
  180. uv_drag = false;
  181. }
  182. }
  183. } else if (uv_drag) {
  184. undo_redo->create_action(TTR("Transform UV Map"));
  185. undo_redo->add_do_method(node, "set_uv", node->get_uv());
  186. undo_redo->add_undo_method(node, "set_uv", uv_prev);
  187. undo_redo->add_do_method(uv_edit_draw, "update");
  188. undo_redo->add_undo_method(uv_edit_draw, "update");
  189. undo_redo->commit_action();
  190. uv_drag = false;
  191. }
  192. } else if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) {
  193. if (uv_drag) {
  194. uv_drag = false;
  195. node->set_uv(uv_prev);
  196. uv_edit_draw->update();
  197. }
  198. } else if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) {
  199. uv_zoom->set_value(uv_zoom->get_value() / (1 - (0.1 * mb->get_factor())));
  200. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) {
  201. uv_zoom->set_value(uv_zoom->get_value() * (1 - (0.1 * mb->get_factor())));
  202. }
  203. }
  204. Ref<InputEventMouseMotion> mm = p_input;
  205. if (mm.is_valid()) {
  206. if ((mm->get_button_mask() & BUTTON_MASK_MIDDLE) || Input::get_singleton()->is_key_pressed(KEY_SPACE)) {
  207. Vector2 drag(mm->get_relative().x, mm->get_relative().y);
  208. uv_hscroll->set_value(uv_hscroll->get_value() - drag.x);
  209. uv_vscroll->set_value(uv_vscroll->get_value() - drag.y);
  210. } else if (uv_drag) {
  211. Vector2 uv_drag_to = mm->get_position();
  212. Vector2 drag = mtx.affine_inverse().xform(uv_drag_to) - mtx.affine_inverse().xform(uv_drag_from);
  213. switch (uv_move_current) {
  214. case UV_MODE_EDIT_POINT: {
  215. PoolVector<Vector2> uv_new = uv_prev;
  216. uv_new.set(uv_drag_index, uv_new[uv_drag_index] + drag);
  217. node->set_uv(uv_new);
  218. } break;
  219. case UV_MODE_MOVE: {
  220. PoolVector<Vector2> uv_new = uv_prev;
  221. for (int i = 0; i < uv_new.size(); i++)
  222. uv_new.set(i, uv_new[i] + drag);
  223. node->set_uv(uv_new);
  224. } break;
  225. case UV_MODE_ROTATE: {
  226. Vector2 center;
  227. PoolVector<Vector2> uv_new = uv_prev;
  228. for (int i = 0; i < uv_new.size(); i++)
  229. center += uv_prev[i];
  230. center /= uv_new.size();
  231. float angle = (uv_drag_from - mtx.xform(center)).normalized().angle_to((uv_drag_to - mtx.xform(center)).normalized());
  232. for (int i = 0; i < uv_new.size(); i++) {
  233. Vector2 rel = uv_prev[i] - center;
  234. rel = rel.rotated(angle);
  235. uv_new.set(i, center + rel);
  236. }
  237. node->set_uv(uv_new);
  238. } break;
  239. case UV_MODE_SCALE: {
  240. Vector2 center;
  241. PoolVector<Vector2> uv_new = uv_prev;
  242. for (int i = 0; i < uv_new.size(); i++)
  243. center += uv_prev[i];
  244. center /= uv_new.size();
  245. float from_dist = uv_drag_from.distance_to(mtx.xform(center));
  246. float to_dist = uv_drag_to.distance_to(mtx.xform(center));
  247. if (from_dist < 2)
  248. break;
  249. float scale = to_dist / from_dist;
  250. for (int i = 0; i < uv_new.size(); i++) {
  251. Vector2 rel = uv_prev[i] - center;
  252. rel = rel * scale;
  253. uv_new.set(i, center + rel);
  254. }
  255. node->set_uv(uv_new);
  256. } break;
  257. }
  258. uv_edit_draw->update();
  259. }
  260. }
  261. }
  262. void Polygon2DEditor::_uv_scroll_changed(float) {
  263. if (updating_uv_scroll)
  264. return;
  265. uv_draw_ofs.x = uv_hscroll->get_value();
  266. uv_draw_ofs.y = uv_vscroll->get_value();
  267. uv_draw_zoom = uv_zoom->get_value();
  268. uv_edit_draw->update();
  269. }
  270. void Polygon2DEditor::_uv_draw() {
  271. Ref<Texture> base_tex = node->get_texture();
  272. if (base_tex.is_null())
  273. return;
  274. Transform2D mtx;
  275. mtx.elements[2] = -uv_draw_ofs;
  276. mtx.scale_basis(Vector2(uv_draw_zoom, uv_draw_zoom));
  277. VS::get_singleton()->canvas_item_add_set_transform(uv_edit_draw->get_canvas_item(), mtx);
  278. uv_edit_draw->draw_texture(base_tex, Point2());
  279. VS::get_singleton()->canvas_item_add_set_transform(uv_edit_draw->get_canvas_item(), Transform2D());
  280. if (snap_show_grid) {
  281. Size2 s = uv_edit_draw->get_size();
  282. int last_cell = 0;
  283. if (snap_step.x != 0) {
  284. for (int i = 0; i < s.width; i++) {
  285. int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / snap_step.x));
  286. if (i == 0)
  287. last_cell = cell;
  288. if (last_cell != cell)
  289. uv_edit_draw->draw_line(Point2(i, 0), Point2(i, s.height), Color(0.3, 0.7, 1, 0.3));
  290. last_cell = cell;
  291. }
  292. }
  293. if (snap_step.y != 0) {
  294. for (int i = 0; i < s.height; i++) {
  295. int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / snap_step.y));
  296. if (i == 0)
  297. last_cell = cell;
  298. if (last_cell != cell)
  299. uv_edit_draw->draw_line(Point2(0, i), Point2(s.width, i), Color(0.3, 0.7, 1, 0.3));
  300. last_cell = cell;
  301. }
  302. }
  303. }
  304. PoolVector<Vector2> uvs = node->get_uv();
  305. Ref<Texture> handle = get_icon("EditorHandle", "EditorIcons");
  306. Rect2 rect(Point2(), mtx.basis_xform(base_tex->get_size()));
  307. rect.expand_to(mtx.basis_xform(uv_edit_draw->get_size()));
  308. for (int i = 0; i < uvs.size(); i++) {
  309. int next = (i + 1) % uvs.size();
  310. uv_edit_draw->draw_line(mtx.xform(uvs[i]), mtx.xform(uvs[next]), Color(0.9, 0.5, 0.5), 2);
  311. uv_edit_draw->draw_texture(handle, mtx.xform(uvs[i]) - handle->get_size() * 0.5);
  312. rect.expand_to(mtx.basis_xform(uvs[i]));
  313. }
  314. rect = rect.grow(200);
  315. updating_uv_scroll = true;
  316. uv_hscroll->set_min(rect.position.x);
  317. uv_hscroll->set_max(rect.position.x + rect.size.x);
  318. uv_hscroll->set_page(uv_edit_draw->get_size().x);
  319. uv_hscroll->set_value(uv_draw_ofs.x);
  320. uv_hscroll->set_step(0.001);
  321. uv_vscroll->set_min(rect.position.y);
  322. uv_vscroll->set_max(rect.position.y + rect.size.y);
  323. uv_vscroll->set_page(uv_edit_draw->get_size().y);
  324. uv_vscroll->set_value(uv_draw_ofs.y);
  325. uv_vscroll->set_step(0.001);
  326. updating_uv_scroll = false;
  327. }
  328. void Polygon2DEditor::_bind_methods() {
  329. ClassDB::bind_method(D_METHOD("_uv_mode"), &Polygon2DEditor::_uv_mode);
  330. ClassDB::bind_method(D_METHOD("_uv_draw"), &Polygon2DEditor::_uv_draw);
  331. ClassDB::bind_method(D_METHOD("_uv_input"), &Polygon2DEditor::_uv_input);
  332. ClassDB::bind_method(D_METHOD("_uv_scroll_changed"), &Polygon2DEditor::_uv_scroll_changed);
  333. ClassDB::bind_method(D_METHOD("_set_use_snap"), &Polygon2DEditor::_set_use_snap);
  334. ClassDB::bind_method(D_METHOD("_set_show_grid"), &Polygon2DEditor::_set_show_grid);
  335. ClassDB::bind_method(D_METHOD("_set_snap_off_x"), &Polygon2DEditor::_set_snap_off_x);
  336. ClassDB::bind_method(D_METHOD("_set_snap_off_y"), &Polygon2DEditor::_set_snap_off_y);
  337. ClassDB::bind_method(D_METHOD("_set_snap_step_x"), &Polygon2DEditor::_set_snap_step_x);
  338. ClassDB::bind_method(D_METHOD("_set_snap_step_y"), &Polygon2DEditor::_set_snap_step_y);
  339. }
  340. Vector2 Polygon2DEditor::snap_point(Vector2 p_target) const {
  341. if (use_snap) {
  342. p_target.x = Math::snap_scalar(snap_offset.x * uv_draw_zoom - uv_draw_ofs.x, snap_step.x * uv_draw_zoom, p_target.x);
  343. p_target.y = Math::snap_scalar(snap_offset.y * uv_draw_zoom - uv_draw_ofs.y, snap_step.y * uv_draw_zoom, p_target.y);
  344. }
  345. return p_target;
  346. }
  347. Polygon2DEditor::Polygon2DEditor(EditorNode *p_editor)
  348. : AbstractPolygon2DEditor(p_editor) {
  349. snap_step = Vector2(10, 10);
  350. use_snap = false;
  351. snap_show_grid = false;
  352. button_uv = memnew(ToolButton);
  353. add_child(button_uv);
  354. button_uv->connect("pressed", this, "_menu_option", varray(MODE_EDIT_UV));
  355. uv_mode = UV_MODE_EDIT_POINT;
  356. uv_edit = memnew(AcceptDialog);
  357. add_child(uv_edit);
  358. uv_edit->set_title(TTR("Polygon 2D UV Editor"));
  359. uv_edit->set_self_modulate(Color(1, 1, 1, 0.9));
  360. VBoxContainer *uv_main_vb = memnew(VBoxContainer);
  361. uv_edit->add_child(uv_main_vb);
  362. //uv_edit->set_child_rect(uv_main_vb);
  363. HBoxContainer *uv_mode_hb = memnew(HBoxContainer);
  364. uv_main_vb->add_child(uv_mode_hb);
  365. for (int i = 0; i < UV_MODE_MAX; i++) {
  366. uv_button[i] = memnew(ToolButton);
  367. uv_button[i]->set_toggle_mode(true);
  368. uv_mode_hb->add_child(uv_button[i]);
  369. uv_button[i]->connect("pressed", this, "_uv_mode", varray(i));
  370. uv_button[i]->set_focus_mode(FOCUS_NONE);
  371. }
  372. uv_button[0]->set_tooltip(TTR("Move Point") + "\n" + TTR("Ctrl: Rotate") + "\n" + TTR("Shift: Move All") + "\n" + TTR("Shift+Ctrl: Scale"));
  373. uv_button[1]->set_tooltip(TTR("Move Polygon"));
  374. uv_button[2]->set_tooltip(TTR("Rotate Polygon"));
  375. uv_button[3]->set_tooltip(TTR("Scale Polygon"));
  376. uv_button[0]->set_pressed(true);
  377. HBoxContainer *uv_main_hb = memnew(HBoxContainer);
  378. uv_main_vb->add_child(uv_main_hb);
  379. uv_edit_draw = memnew(Control);
  380. uv_main_hb->add_child(uv_edit_draw);
  381. uv_main_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  382. uv_edit_draw->set_h_size_flags(SIZE_EXPAND_FILL);
  383. uv_menu = memnew(MenuButton);
  384. uv_mode_hb->add_child(uv_menu);
  385. uv_menu->set_text(TTR("Edit"));
  386. uv_menu->get_popup()->add_item(TTR("Polygon->UV"), UVEDIT_POLYGON_TO_UV);
  387. uv_menu->get_popup()->add_item(TTR("UV->Polygon"), UVEDIT_UV_TO_POLYGON);
  388. uv_menu->get_popup()->add_separator();
  389. uv_menu->get_popup()->add_item(TTR("Clear UV"), UVEDIT_UV_CLEAR);
  390. uv_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  391. uv_mode_hb->add_child(memnew(VSeparator));
  392. b_snap_enable = memnew(ToolButton);
  393. uv_mode_hb->add_child(b_snap_enable);
  394. b_snap_enable->set_text(TTR("Snap"));
  395. b_snap_enable->set_focus_mode(FOCUS_NONE);
  396. b_snap_enable->set_toggle_mode(true);
  397. b_snap_enable->set_pressed(use_snap);
  398. b_snap_enable->set_tooltip(TTR("Enable Snap"));
  399. b_snap_enable->connect("toggled", this, "_set_use_snap");
  400. b_snap_grid = memnew(ToolButton);
  401. uv_mode_hb->add_child(b_snap_grid);
  402. b_snap_grid->set_text(TTR("Grid"));
  403. b_snap_grid->set_focus_mode(FOCUS_NONE);
  404. b_snap_grid->set_toggle_mode(true);
  405. b_snap_grid->set_pressed(snap_show_grid);
  406. b_snap_grid->set_tooltip(TTR("Show Grid"));
  407. b_snap_grid->connect("toggled", this, "_set_show_grid");
  408. uv_mode_hb->add_child(memnew(VSeparator));
  409. uv_mode_hb->add_child(memnew(Label(TTR("Grid Offset:"))));
  410. SpinBox *sb_off_x = memnew(SpinBox);
  411. sb_off_x->set_min(-256);
  412. sb_off_x->set_max(256);
  413. sb_off_x->set_step(1);
  414. sb_off_x->set_value(snap_offset.x);
  415. sb_off_x->set_suffix("px");
  416. sb_off_x->connect("value_changed", this, "_set_snap_off_x");
  417. uv_mode_hb->add_child(sb_off_x);
  418. SpinBox *sb_off_y = memnew(SpinBox);
  419. sb_off_y->set_min(-256);
  420. sb_off_y->set_max(256);
  421. sb_off_y->set_step(1);
  422. sb_off_y->set_value(snap_offset.y);
  423. sb_off_y->set_suffix("px");
  424. sb_off_y->connect("value_changed", this, "_set_snap_off_y");
  425. uv_mode_hb->add_child(sb_off_y);
  426. uv_mode_hb->add_child(memnew(VSeparator));
  427. uv_mode_hb->add_child(memnew(Label(TTR("Grid Step:"))));
  428. SpinBox *sb_step_x = memnew(SpinBox);
  429. sb_step_x->set_min(-256);
  430. sb_step_x->set_max(256);
  431. sb_step_x->set_step(1);
  432. sb_step_x->set_value(snap_step.x);
  433. sb_step_x->set_suffix("px");
  434. sb_step_x->connect("value_changed", this, "_set_snap_step_x");
  435. uv_mode_hb->add_child(sb_step_x);
  436. SpinBox *sb_step_y = memnew(SpinBox);
  437. sb_step_y->set_min(-256);
  438. sb_step_y->set_max(256);
  439. sb_step_y->set_step(1);
  440. sb_step_y->set_value(snap_step.y);
  441. sb_step_y->set_suffix("px");
  442. sb_step_y->connect("value_changed", this, "_set_snap_step_y");
  443. uv_mode_hb->add_child(sb_step_y);
  444. uv_mode_hb->add_child(memnew(VSeparator));
  445. uv_icon_zoom = memnew(TextureRect);
  446. uv_mode_hb->add_child(uv_icon_zoom);
  447. uv_zoom = memnew(HSlider);
  448. uv_zoom->set_min(0.01);
  449. uv_zoom->set_max(4);
  450. uv_zoom->set_value(1);
  451. uv_zoom->set_step(0.01);
  452. uv_mode_hb->add_child(uv_zoom);
  453. uv_zoom->set_custom_minimum_size(Size2(200, 0));
  454. uv_zoom_value = memnew(SpinBox);
  455. uv_zoom->share(uv_zoom_value);
  456. uv_zoom_value->set_custom_minimum_size(Size2(50, 0));
  457. uv_mode_hb->add_child(uv_zoom_value);
  458. uv_zoom->connect("value_changed", this, "_uv_scroll_changed");
  459. uv_vscroll = memnew(VScrollBar);
  460. uv_main_hb->add_child(uv_vscroll);
  461. uv_vscroll->connect("value_changed", this, "_uv_scroll_changed");
  462. uv_hscroll = memnew(HScrollBar);
  463. uv_main_vb->add_child(uv_hscroll);
  464. uv_hscroll->connect("value_changed", this, "_uv_scroll_changed");
  465. uv_edit_draw->connect("draw", this, "_uv_draw");
  466. uv_edit_draw->connect("gui_input", this, "_uv_input");
  467. uv_draw_zoom = 1.0;
  468. uv_drag_index = -1;
  469. uv_drag = false;
  470. updating_uv_scroll = false;
  471. error = memnew(AcceptDialog);
  472. add_child(error);
  473. uv_edit_draw->set_clip_contents(true);
  474. }
  475. Polygon2DEditorPlugin::Polygon2DEditorPlugin(EditorNode *p_node)
  476. : AbstractPolygon2DEditorPlugin(p_node, memnew(Polygon2DEditor(p_node)), "Polygon2D") {
  477. }