polygon_2d_editor_plugin.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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) 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 "polygon_2d_editor_plugin.h"
  31. #include "core/input/input_event.h"
  32. #include "core/math/geometry_2d.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_scale.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_undo_redo_manager.h"
  37. #include "editor/plugins/canvas_item_editor_plugin.h"
  38. #include "scene/2d/skeleton_2d.h"
  39. #include "scene/gui/check_box.h"
  40. #include "scene/gui/dialogs.h"
  41. #include "scene/gui/label.h"
  42. #include "scene/gui/menu_button.h"
  43. #include "scene/gui/panel.h"
  44. #include "scene/gui/scroll_container.h"
  45. #include "scene/gui/separator.h"
  46. #include "scene/gui/slider.h"
  47. #include "scene/gui/spin_box.h"
  48. #include "scene/gui/split_container.h"
  49. #include "scene/gui/texture_rect.h"
  50. #include "scene/gui/view_panner.h"
  51. Node2D *Polygon2DEditor::_get_node() const {
  52. return node;
  53. }
  54. void Polygon2DEditor::_set_node(Node *p_polygon) {
  55. node = Object::cast_to<Polygon2D>(p_polygon);
  56. _update_polygon_editing_state();
  57. }
  58. Vector2 Polygon2DEditor::_get_offset(int p_idx) const {
  59. return node->get_offset();
  60. }
  61. int Polygon2DEditor::_get_polygon_count() const {
  62. if (node->get_internal_vertex_count() > 0) {
  63. return 0; //do not edit if internal vertices exist
  64. } else {
  65. return 1;
  66. }
  67. }
  68. void Polygon2DEditor::_notification(int p_what) {
  69. switch (p_what) {
  70. case NOTIFICATION_ENTER_TREE:
  71. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  72. uv_panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
  73. } break;
  74. case NOTIFICATION_READY: {
  75. button_uv->set_icon(get_editor_theme_icon(SNAME("Uv")));
  76. uv_button[UV_MODE_CREATE]->set_icon(get_editor_theme_icon(SNAME("Edit")));
  77. uv_button[UV_MODE_CREATE_INTERNAL]->set_icon(get_editor_theme_icon(SNAME("EditInternal")));
  78. uv_button[UV_MODE_REMOVE_INTERNAL]->set_icon(get_editor_theme_icon(SNAME("RemoveInternal")));
  79. uv_button[UV_MODE_EDIT_POINT]->set_icon(get_editor_theme_icon(SNAME("ToolSelect")));
  80. uv_button[UV_MODE_MOVE]->set_icon(get_editor_theme_icon(SNAME("ToolMove")));
  81. uv_button[UV_MODE_ROTATE]->set_icon(get_editor_theme_icon(SNAME("ToolRotate")));
  82. uv_button[UV_MODE_SCALE]->set_icon(get_editor_theme_icon(SNAME("ToolScale")));
  83. uv_button[UV_MODE_ADD_POLYGON]->set_icon(get_editor_theme_icon(SNAME("Edit")));
  84. uv_button[UV_MODE_REMOVE_POLYGON]->set_icon(get_editor_theme_icon(SNAME("Close")));
  85. uv_button[UV_MODE_PAINT_WEIGHT]->set_icon(get_editor_theme_icon(SNAME("Bucket")));
  86. uv_button[UV_MODE_CLEAR_WEIGHT]->set_icon(get_editor_theme_icon(SNAME("Clear")));
  87. b_snap_grid->set_icon(get_editor_theme_icon(SNAME("Grid")));
  88. b_snap_enable->set_icon(get_editor_theme_icon(SNAME("SnapGrid")));
  89. uv_icon_zoom->set_texture(get_editor_theme_icon(SNAME("Zoom")));
  90. uv_vscroll->set_anchors_and_offsets_preset(PRESET_RIGHT_WIDE);
  91. uv_hscroll->set_anchors_and_offsets_preset(PRESET_BOTTOM_WIDE);
  92. [[fallthrough]];
  93. }
  94. case NOTIFICATION_THEME_CHANGED: {
  95. uv_edit_draw->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  96. bone_scroll->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  97. } break;
  98. case NOTIFICATION_VISIBILITY_CHANGED: {
  99. if (!is_visible()) {
  100. uv_edit->hide();
  101. }
  102. } break;
  103. }
  104. }
  105. void Polygon2DEditor::_sync_bones() {
  106. Skeleton2D *skeleton = nullptr;
  107. if (!node->has_node(node->get_skeleton())) {
  108. error->set_text(TTR("The skeleton property of the Polygon2D does not point to a Skeleton2D node"));
  109. error->popup_centered();
  110. } else {
  111. Node *sn = node->get_node(node->get_skeleton());
  112. skeleton = Object::cast_to<Skeleton2D>(sn);
  113. }
  114. Array prev_bones = node->call("_get_bones");
  115. node->clear_bones();
  116. if (!skeleton) {
  117. error->set_text(TTR("The skeleton property of the Polygon2D does not point to a Skeleton2D node"));
  118. error->popup_centered();
  119. } else {
  120. for (int i = 0; i < skeleton->get_bone_count(); i++) {
  121. NodePath path = skeleton->get_path_to(skeleton->get_bone(i));
  122. Vector<float> weights;
  123. int wc = node->get_polygon().size();
  124. for (int j = 0; j < prev_bones.size(); j += 2) {
  125. NodePath pvp = prev_bones[j];
  126. Vector<float> pv = prev_bones[j + 1];
  127. if (pvp == path && pv.size() == wc) {
  128. weights = pv;
  129. }
  130. }
  131. if (weights.size() == 0) { //create them
  132. weights.resize(node->get_polygon().size());
  133. float *w = weights.ptrw();
  134. for (int j = 0; j < wc; j++) {
  135. w[j] = 0.0;
  136. }
  137. }
  138. node->add_bone(path, weights);
  139. }
  140. }
  141. Array new_bones = node->call("_get_bones");
  142. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  143. undo_redo->create_action(TTR("Sync Bones"));
  144. undo_redo->add_do_method(node, "_set_bones", new_bones);
  145. undo_redo->add_undo_method(node, "_set_bones", prev_bones);
  146. undo_redo->add_do_method(this, "_update_bone_list");
  147. undo_redo->add_undo_method(this, "_update_bone_list");
  148. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  149. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  150. undo_redo->commit_action();
  151. }
  152. void Polygon2DEditor::_update_bone_list() {
  153. NodePath selected;
  154. while (bone_scroll_vb->get_child_count()) {
  155. CheckBox *cb = Object::cast_to<CheckBox>(bone_scroll_vb->get_child(0));
  156. if (cb && cb->is_pressed()) {
  157. selected = cb->get_meta("bone_path");
  158. }
  159. memdelete(bone_scroll_vb->get_child(0));
  160. }
  161. Ref<ButtonGroup> bg;
  162. bg.instantiate();
  163. for (int i = 0; i < node->get_bone_count(); i++) {
  164. CheckBox *cb = memnew(CheckBox);
  165. NodePath np = node->get_bone_path(i);
  166. String name;
  167. if (np.get_name_count()) {
  168. name = np.get_name(np.get_name_count() - 1);
  169. }
  170. if (name.is_empty()) {
  171. name = "Bone " + itos(i);
  172. }
  173. cb->set_text(name);
  174. cb->set_button_group(bg);
  175. cb->set_meta("bone_path", np);
  176. cb->set_focus_mode(FOCUS_NONE);
  177. bone_scroll_vb->add_child(cb);
  178. if (np == selected || bone_scroll_vb->get_child_count() < 2) {
  179. cb->set_pressed(true);
  180. }
  181. cb->connect("pressed", callable_mp(this, &Polygon2DEditor::_bone_paint_selected).bind(i));
  182. }
  183. uv_edit_draw->queue_redraw();
  184. }
  185. void Polygon2DEditor::_bone_paint_selected(int p_index) {
  186. uv_edit_draw->queue_redraw();
  187. }
  188. void Polygon2DEditor::_uv_edit_mode_select(int p_mode) {
  189. if (p_mode == 0) { //uv
  190. uv_button[UV_MODE_CREATE]->hide();
  191. uv_button[UV_MODE_CREATE_INTERNAL]->hide();
  192. uv_button[UV_MODE_REMOVE_INTERNAL]->hide();
  193. for (int i = UV_MODE_EDIT_POINT; i <= UV_MODE_SCALE; i++) {
  194. uv_button[i]->show();
  195. }
  196. uv_button[UV_MODE_ADD_POLYGON]->hide();
  197. uv_button[UV_MODE_REMOVE_POLYGON]->hide();
  198. uv_button[UV_MODE_PAINT_WEIGHT]->hide();
  199. uv_button[UV_MODE_CLEAR_WEIGHT]->hide();
  200. _uv_mode(UV_MODE_EDIT_POINT);
  201. bone_scroll_main_vb->hide();
  202. bone_paint_strength->hide();
  203. bone_paint_radius->hide();
  204. bone_paint_radius_label->hide();
  205. } else if (p_mode == 1) { //poly
  206. for (int i = 0; i <= UV_MODE_SCALE; i++) {
  207. uv_button[i]->show();
  208. }
  209. uv_button[UV_MODE_ADD_POLYGON]->hide();
  210. uv_button[UV_MODE_REMOVE_POLYGON]->hide();
  211. uv_button[UV_MODE_PAINT_WEIGHT]->hide();
  212. uv_button[UV_MODE_CLEAR_WEIGHT]->hide();
  213. _uv_mode(UV_MODE_EDIT_POINT);
  214. bone_scroll_main_vb->hide();
  215. bone_paint_strength->hide();
  216. bone_paint_radius->hide();
  217. bone_paint_radius_label->hide();
  218. } else if (p_mode == 2) { //splits
  219. for (int i = 0; i <= UV_MODE_SCALE; i++) {
  220. uv_button[i]->hide();
  221. }
  222. uv_button[UV_MODE_ADD_POLYGON]->show();
  223. uv_button[UV_MODE_REMOVE_POLYGON]->show();
  224. uv_button[UV_MODE_PAINT_WEIGHT]->hide();
  225. uv_button[UV_MODE_CLEAR_WEIGHT]->hide();
  226. _uv_mode(UV_MODE_ADD_POLYGON);
  227. bone_scroll_main_vb->hide();
  228. bone_paint_strength->hide();
  229. bone_paint_radius->hide();
  230. bone_paint_radius_label->hide();
  231. } else if (p_mode == 3) { //bones´
  232. for (int i = 0; i <= UV_MODE_REMOVE_POLYGON; i++) {
  233. uv_button[i]->hide();
  234. }
  235. uv_button[UV_MODE_PAINT_WEIGHT]->show();
  236. uv_button[UV_MODE_CLEAR_WEIGHT]->show();
  237. _uv_mode(UV_MODE_PAINT_WEIGHT);
  238. bone_scroll_main_vb->show();
  239. bone_paint_strength->show();
  240. bone_paint_radius->show();
  241. bone_paint_radius_label->show();
  242. _update_bone_list();
  243. bone_paint_pos = Vector2(-100000, -100000); //send brush away when switching
  244. }
  245. uv_edit->set_size(uv_edit->get_size()); // Necessary readjustment of the popup window.
  246. uv_edit_draw->queue_redraw();
  247. }
  248. void Polygon2DEditor::_uv_edit_popup_hide() {
  249. EditorSettings::get_singleton()->set("interface/dialogs/uv_editor_bounds", Rect2(uv_edit->get_position(), uv_edit->get_size()));
  250. _cancel_editing();
  251. }
  252. void Polygon2DEditor::_menu_option(int p_option) {
  253. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  254. switch (p_option) {
  255. case MODE_EDIT_UV: {
  256. if (node->get_texture().is_null()) {
  257. error->set_text(TTR("No texture in this polygon.\nSet a texture to be able to edit UV."));
  258. error->popup_centered();
  259. return;
  260. }
  261. uv_edit_draw->set_texture_filter(node->get_texture_filter_in_tree());
  262. Vector<Vector2> points = node->get_polygon();
  263. Vector<Vector2> uvs = node->get_uv();
  264. if (uvs.size() != points.size()) {
  265. undo_redo->create_action(TTR("Create UV Map"));
  266. undo_redo->add_do_method(node, "set_uv", points);
  267. undo_redo->add_undo_method(node, "set_uv", uvs);
  268. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  269. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  270. undo_redo->commit_action();
  271. }
  272. if (EditorSettings::get_singleton()->has_setting("interface/dialogs/uv_editor_bounds")) {
  273. uv_edit->popup(EDITOR_GET("interface/dialogs/uv_editor_bounds"));
  274. } else {
  275. uv_edit->popup_centered_ratio(0.85);
  276. }
  277. _update_bone_list();
  278. } break;
  279. case UVEDIT_POLYGON_TO_UV: {
  280. Vector<Vector2> points = node->get_polygon();
  281. if (points.size() == 0) {
  282. break;
  283. }
  284. Vector<Vector2> uvs = node->get_uv();
  285. undo_redo->create_action(TTR("Create UV Map"));
  286. undo_redo->add_do_method(node, "set_uv", points);
  287. undo_redo->add_undo_method(node, "set_uv", uvs);
  288. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  289. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  290. undo_redo->commit_action();
  291. } break;
  292. case UVEDIT_UV_TO_POLYGON: {
  293. Vector<Vector2> points = node->get_polygon();
  294. Vector<Vector2> uvs = node->get_uv();
  295. if (uvs.size() == 0) {
  296. break;
  297. }
  298. undo_redo->create_action(TTR("Create Polygon"));
  299. undo_redo->add_do_method(node, "set_polygon", uvs);
  300. undo_redo->add_undo_method(node, "set_polygon", points);
  301. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  302. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  303. undo_redo->commit_action();
  304. } break;
  305. case UVEDIT_UV_CLEAR: {
  306. Vector<Vector2> uvs = node->get_uv();
  307. if (uvs.size() == 0) {
  308. break;
  309. }
  310. undo_redo->create_action(TTR("Create UV Map"));
  311. undo_redo->add_do_method(node, "set_uv", Vector<Vector2>());
  312. undo_redo->add_undo_method(node, "set_uv", uvs);
  313. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  314. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  315. undo_redo->commit_action();
  316. } break;
  317. case UVEDIT_GRID_SETTINGS: {
  318. grid_settings->popup_centered();
  319. } break;
  320. default: {
  321. AbstractPolygon2DEditor::_menu_option(p_option);
  322. } break;
  323. }
  324. }
  325. void Polygon2DEditor::_cancel_editing() {
  326. if (uv_create) {
  327. uv_drag = false;
  328. uv_create = false;
  329. node->set_uv(uv_create_uv_prev);
  330. node->set_polygon(uv_create_poly_prev);
  331. node->set_internal_vertex_count(uv_create_prev_internal_vertices);
  332. node->set_vertex_colors(uv_create_colors_prev);
  333. node->call("_set_bones", uv_create_bones_prev);
  334. node->set_polygons(polygons_prev);
  335. _update_polygon_editing_state();
  336. } else if (uv_drag) {
  337. uv_drag = false;
  338. if (uv_edit_mode[0]->is_pressed()) { // Edit UV.
  339. node->set_uv(points_prev);
  340. } else if (uv_edit_mode[1]->is_pressed()) { // Edit polygon.
  341. node->set_polygon(points_prev);
  342. }
  343. }
  344. polygon_create.clear();
  345. }
  346. void Polygon2DEditor::_update_polygon_editing_state() {
  347. if (!_get_node()) {
  348. return;
  349. }
  350. if (node->get_internal_vertex_count() > 0) {
  351. disable_polygon_editing(true, TTR("Polygon 2D has internal vertices, so it can no longer be edited in the viewport."));
  352. } else {
  353. disable_polygon_editing(false, String());
  354. }
  355. }
  356. void Polygon2DEditor::_commit_action() {
  357. // Makes that undo/redoing actions made outside of the UV editor still affect its polygon.
  358. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  359. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  360. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  361. undo_redo->add_do_method(CanvasItemEditor::get_singleton(), "update_viewport");
  362. undo_redo->add_undo_method(CanvasItemEditor::get_singleton(), "update_viewport");
  363. undo_redo->commit_action();
  364. }
  365. void Polygon2DEditor::_set_use_snap(bool p_use) {
  366. use_snap = p_use;
  367. EditorSettings::get_singleton()->set_project_metadata("polygon_2d_uv_editor", "snap_enabled", p_use);
  368. }
  369. void Polygon2DEditor::_set_show_grid(bool p_show) {
  370. snap_show_grid = p_show;
  371. EditorSettings::get_singleton()->set_project_metadata("polygon_2d_uv_editor", "show_grid", p_show);
  372. uv_edit_draw->queue_redraw();
  373. }
  374. void Polygon2DEditor::_set_snap_off_x(real_t p_val) {
  375. snap_offset.x = p_val;
  376. EditorSettings::get_singleton()->set_project_metadata("polygon_2d_uv_editor", "snap_offset", snap_offset);
  377. uv_edit_draw->queue_redraw();
  378. }
  379. void Polygon2DEditor::_set_snap_off_y(real_t p_val) {
  380. snap_offset.y = p_val;
  381. EditorSettings::get_singleton()->set_project_metadata("polygon_2d_uv_editor", "snap_offset", snap_offset);
  382. uv_edit_draw->queue_redraw();
  383. }
  384. void Polygon2DEditor::_set_snap_step_x(real_t p_val) {
  385. snap_step.x = p_val;
  386. EditorSettings::get_singleton()->set_project_metadata("polygon_2d_uv_editor", "snap_step", snap_step);
  387. uv_edit_draw->queue_redraw();
  388. }
  389. void Polygon2DEditor::_set_snap_step_y(real_t p_val) {
  390. snap_step.y = p_val;
  391. EditorSettings::get_singleton()->set_project_metadata("polygon_2d_uv_editor", "snap_step", snap_step);
  392. uv_edit_draw->queue_redraw();
  393. }
  394. void Polygon2DEditor::_uv_mode(int p_mode) {
  395. polygon_create.clear();
  396. uv_drag = false;
  397. uv_create = false;
  398. uv_mode = UVMode(p_mode);
  399. for (int i = 0; i < UV_MODE_MAX; i++) {
  400. uv_button[i]->set_pressed(p_mode == i);
  401. }
  402. }
  403. void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) {
  404. if (!_get_node()) {
  405. return;
  406. }
  407. if (uv_panner->gui_input(p_input)) {
  408. accept_event();
  409. return;
  410. }
  411. Transform2D mtx;
  412. mtx.columns[2] = -uv_draw_ofs;
  413. mtx.scale_basis(Vector2(uv_draw_zoom, uv_draw_zoom));
  414. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  415. Ref<InputEventMouseButton> mb = p_input;
  416. if (mb.is_valid()) {
  417. if (mb->get_button_index() == MouseButton::LEFT) {
  418. if (mb->is_pressed()) {
  419. uv_drag_from = snap_point(mb->get_position());
  420. uv_drag = true;
  421. points_prev = node->get_uv();
  422. if (uv_edit_mode[0]->is_pressed()) { //edit uv
  423. points_prev = node->get_uv();
  424. } else { //edit polygon
  425. points_prev = node->get_polygon();
  426. }
  427. uv_move_current = uv_mode;
  428. if (uv_move_current == UV_MODE_CREATE) {
  429. if (!uv_create) {
  430. points_prev.clear();
  431. Vector2 tuv = mtx.affine_inverse().xform(snap_point(mb->get_position()));
  432. points_prev.push_back(tuv);
  433. uv_create_to = tuv;
  434. point_drag_index = 0;
  435. uv_drag_from = tuv;
  436. uv_drag = true;
  437. uv_create = true;
  438. uv_create_uv_prev = node->get_uv();
  439. uv_create_poly_prev = node->get_polygon();
  440. uv_create_prev_internal_vertices = node->get_internal_vertex_count();
  441. uv_create_colors_prev = node->get_vertex_colors();
  442. uv_create_bones_prev = node->call("_get_bones");
  443. polygons_prev = node->get_polygons();
  444. disable_polygon_editing(false, String());
  445. node->set_polygon(points_prev);
  446. node->set_uv(points_prev);
  447. node->set_internal_vertex_count(0);
  448. uv_edit_draw->queue_redraw();
  449. } else {
  450. Vector2 tuv = mtx.affine_inverse().xform(snap_point(mb->get_position()));
  451. // Close the polygon if selected point is near start. Threshold for closing scaled by zoom level
  452. if (points_prev.size() > 2 && tuv.distance_to(points_prev[0]) < (8 / uv_draw_zoom)) {
  453. undo_redo->create_action(TTR("Create Polygon & UV"));
  454. undo_redo->add_do_method(node, "set_uv", node->get_uv());
  455. undo_redo->add_undo_method(node, "set_uv", uv_create_uv_prev);
  456. undo_redo->add_do_method(node, "set_polygon", node->get_polygon());
  457. undo_redo->add_undo_method(node, "set_polygon", uv_create_poly_prev);
  458. undo_redo->add_do_method(node, "set_internal_vertex_count", 0);
  459. undo_redo->add_undo_method(node, "set_internal_vertex_count", uv_create_prev_internal_vertices);
  460. undo_redo->add_do_method(node, "set_vertex_colors", Vector<Color>());
  461. undo_redo->add_undo_method(node, "set_vertex_colors", uv_create_colors_prev);
  462. undo_redo->add_do_method(node, "clear_bones");
  463. undo_redo->add_undo_method(node, "_set_bones", uv_create_bones_prev);
  464. undo_redo->add_do_method(this, "_update_polygon_editing_state");
  465. undo_redo->add_undo_method(this, "_update_polygon_editing_state");
  466. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  467. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  468. undo_redo->commit_action();
  469. uv_drag = false;
  470. uv_create = false;
  471. _uv_mode(UV_MODE_EDIT_POINT);
  472. _menu_option(MODE_EDIT);
  473. } else {
  474. points_prev.push_back(tuv);
  475. point_drag_index = points_prev.size() - 1;
  476. uv_drag_from = tuv;
  477. }
  478. node->set_polygon(points_prev);
  479. node->set_uv(points_prev);
  480. }
  481. CanvasItemEditor::get_singleton()->update_viewport();
  482. }
  483. if (uv_move_current == UV_MODE_CREATE_INTERNAL) {
  484. uv_create_uv_prev = node->get_uv();
  485. uv_create_poly_prev = node->get_polygon();
  486. uv_create_colors_prev = node->get_vertex_colors();
  487. uv_create_bones_prev = node->call("_get_bones");
  488. int internal_vertices = node->get_internal_vertex_count();
  489. Vector2 pos = mtx.affine_inverse().xform(snap_point(mb->get_position()));
  490. uv_create_poly_prev.push_back(pos);
  491. uv_create_uv_prev.push_back(pos);
  492. if (uv_create_colors_prev.size()) {
  493. uv_create_colors_prev.push_back(Color(1, 1, 1));
  494. }
  495. undo_redo->create_action(TTR("Create Internal Vertex"));
  496. undo_redo->add_do_method(node, "set_uv", uv_create_uv_prev);
  497. undo_redo->add_undo_method(node, "set_uv", node->get_uv());
  498. undo_redo->add_do_method(node, "set_polygon", uv_create_poly_prev);
  499. undo_redo->add_undo_method(node, "set_polygon", node->get_polygon());
  500. undo_redo->add_do_method(node, "set_vertex_colors", uv_create_colors_prev);
  501. undo_redo->add_undo_method(node, "set_vertex_colors", node->get_vertex_colors());
  502. for (int i = 0; i < node->get_bone_count(); i++) {
  503. Vector<float> bonew = node->get_bone_weights(i);
  504. bonew.push_back(0);
  505. undo_redo->add_do_method(node, "set_bone_weights", i, bonew);
  506. undo_redo->add_undo_method(node, "set_bone_weights", i, node->get_bone_weights(i));
  507. }
  508. undo_redo->add_do_method(node, "set_internal_vertex_count", internal_vertices + 1);
  509. undo_redo->add_undo_method(node, "set_internal_vertex_count", internal_vertices);
  510. undo_redo->add_do_method(this, "_update_polygon_editing_state");
  511. undo_redo->add_undo_method(this, "_update_polygon_editing_state");
  512. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  513. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  514. undo_redo->commit_action();
  515. }
  516. if (uv_move_current == UV_MODE_REMOVE_INTERNAL) {
  517. uv_create_uv_prev = node->get_uv();
  518. uv_create_poly_prev = node->get_polygon();
  519. uv_create_colors_prev = node->get_vertex_colors();
  520. uv_create_bones_prev = node->call("_get_bones");
  521. int internal_vertices = node->get_internal_vertex_count();
  522. if (internal_vertices <= 0) {
  523. return;
  524. }
  525. int closest = -1;
  526. real_t closest_dist = 1e20;
  527. for (int i = points_prev.size() - internal_vertices; i < points_prev.size(); i++) {
  528. Vector2 tuv = mtx.xform(uv_create_poly_prev[i]);
  529. real_t dist = tuv.distance_to(mb->get_position());
  530. if (dist < 8 && dist < closest_dist) {
  531. closest = i;
  532. closest_dist = dist;
  533. }
  534. }
  535. if (closest == -1) {
  536. return;
  537. }
  538. uv_create_poly_prev.remove_at(closest);
  539. uv_create_uv_prev.remove_at(closest);
  540. if (uv_create_colors_prev.size()) {
  541. uv_create_colors_prev.remove_at(closest);
  542. }
  543. undo_redo->create_action(TTR("Remove Internal Vertex"));
  544. undo_redo->add_do_method(node, "set_uv", uv_create_uv_prev);
  545. undo_redo->add_undo_method(node, "set_uv", node->get_uv());
  546. undo_redo->add_do_method(node, "set_polygon", uv_create_poly_prev);
  547. undo_redo->add_undo_method(node, "set_polygon", node->get_polygon());
  548. undo_redo->add_do_method(node, "set_vertex_colors", uv_create_colors_prev);
  549. undo_redo->add_undo_method(node, "set_vertex_colors", node->get_vertex_colors());
  550. for (int i = 0; i < node->get_bone_count(); i++) {
  551. Vector<float> bonew = node->get_bone_weights(i);
  552. bonew.remove_at(closest);
  553. undo_redo->add_do_method(node, "set_bone_weights", i, bonew);
  554. undo_redo->add_undo_method(node, "set_bone_weights", i, node->get_bone_weights(i));
  555. }
  556. undo_redo->add_do_method(node, "set_internal_vertex_count", internal_vertices - 1);
  557. undo_redo->add_undo_method(node, "set_internal_vertex_count", internal_vertices);
  558. undo_redo->add_do_method(this, "_update_polygon_editing_state");
  559. undo_redo->add_undo_method(this, "_update_polygon_editing_state");
  560. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  561. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  562. undo_redo->commit_action();
  563. }
  564. if (uv_move_current == UV_MODE_EDIT_POINT) {
  565. if (mb->is_shift_pressed() && mb->is_command_or_control_pressed()) {
  566. uv_move_current = UV_MODE_SCALE;
  567. } else if (mb->is_shift_pressed()) {
  568. uv_move_current = UV_MODE_MOVE;
  569. } else if (mb->is_command_or_control_pressed()) {
  570. uv_move_current = UV_MODE_ROTATE;
  571. }
  572. }
  573. if (uv_move_current == UV_MODE_EDIT_POINT) {
  574. point_drag_index = -1;
  575. for (int i = 0; i < points_prev.size(); i++) {
  576. Vector2 tuv = mtx.xform(points_prev[i]);
  577. if (tuv.distance_to(mb->get_position()) < 8) {
  578. uv_drag_from = tuv;
  579. point_drag_index = i;
  580. }
  581. }
  582. if (point_drag_index == -1) {
  583. uv_drag = false;
  584. }
  585. }
  586. if (uv_move_current == UV_MODE_ADD_POLYGON) {
  587. int closest = -1;
  588. real_t closest_dist = 1e20;
  589. for (int i = 0; i < points_prev.size(); i++) {
  590. Vector2 tuv = mtx.xform(points_prev[i]);
  591. real_t dist = tuv.distance_to(mb->get_position());
  592. if (dist < 8 && dist < closest_dist) {
  593. closest = i;
  594. closest_dist = dist;
  595. }
  596. }
  597. if (closest != -1) {
  598. if (polygon_create.size() && closest == polygon_create[0]) {
  599. //close
  600. if (polygon_create.size() < 3) {
  601. error->set_text(TTR("Invalid Polygon (need 3 different vertices)"));
  602. error->popup_centered();
  603. } else {
  604. Array polygons = node->get_polygons();
  605. polygons = polygons.duplicate(); //copy because its a reference
  606. //todo, could check whether it already exists?
  607. polygons.push_back(polygon_create);
  608. undo_redo->create_action(TTR("Add Custom Polygon"));
  609. undo_redo->add_do_method(node, "set_polygons", polygons);
  610. undo_redo->add_undo_method(node, "set_polygons", node->get_polygons());
  611. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  612. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  613. undo_redo->commit_action();
  614. }
  615. polygon_create.clear();
  616. } else if (!polygon_create.has(closest)) {
  617. //add temporarily if not exists
  618. polygon_create.push_back(closest);
  619. }
  620. }
  621. }
  622. if (uv_move_current == UV_MODE_REMOVE_POLYGON) {
  623. Array polygons = node->get_polygons();
  624. polygons = polygons.duplicate(); //copy because its a reference
  625. int erase_index = -1;
  626. for (int i = polygons.size() - 1; i >= 0; i--) {
  627. Vector<int> points = polygons[i];
  628. Vector<Vector2> polys;
  629. polys.resize(points.size());
  630. for (int j = 0; j < polys.size(); j++) {
  631. int idx = points[j];
  632. if (idx < 0 || idx >= points_prev.size()) {
  633. continue;
  634. }
  635. polys.write[j] = mtx.xform(points_prev[idx]);
  636. }
  637. if (Geometry2D::is_point_in_polygon(mb->get_position(), polys)) {
  638. erase_index = i;
  639. break;
  640. }
  641. }
  642. if (erase_index != -1) {
  643. polygons.remove_at(erase_index);
  644. undo_redo->create_action(TTR("Remove Custom Polygon"));
  645. undo_redo->add_do_method(node, "set_polygons", polygons);
  646. undo_redo->add_undo_method(node, "set_polygons", node->get_polygons());
  647. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  648. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  649. undo_redo->commit_action();
  650. }
  651. }
  652. if (uv_move_current == UV_MODE_PAINT_WEIGHT || uv_move_current == UV_MODE_CLEAR_WEIGHT) {
  653. int bone_selected = -1;
  654. for (int i = 0; i < bone_scroll_vb->get_child_count(); i++) {
  655. CheckBox *c = Object::cast_to<CheckBox>(bone_scroll_vb->get_child(i));
  656. if (c && c->is_pressed()) {
  657. bone_selected = i;
  658. break;
  659. }
  660. }
  661. if (bone_selected != -1 && node->get_bone_weights(bone_selected).size() == points_prev.size()) {
  662. prev_weights = node->get_bone_weights(bone_selected);
  663. bone_painting = true;
  664. bone_painting_bone = bone_selected;
  665. }
  666. }
  667. } else {
  668. if (uv_drag && !uv_create) {
  669. if (uv_edit_mode[0]->is_pressed()) { // Edit UV.
  670. undo_redo->create_action(TTR("Transform UV Map"));
  671. undo_redo->add_do_method(node, "set_uv", node->get_uv());
  672. undo_redo->add_undo_method(node, "set_uv", points_prev);
  673. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  674. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  675. undo_redo->commit_action();
  676. } else if (uv_edit_mode[1]->is_pressed() && uv_move_current == UV_MODE_EDIT_POINT) { // Edit polygon.
  677. undo_redo->create_action(TTR("Transform Polygon"));
  678. undo_redo->add_do_method(node, "set_polygon", node->get_polygon());
  679. undo_redo->add_undo_method(node, "set_polygon", points_prev);
  680. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  681. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  682. undo_redo->commit_action();
  683. }
  684. uv_drag = false;
  685. }
  686. if (bone_painting) {
  687. undo_redo->create_action(TTR("Paint Bone Weights"));
  688. undo_redo->add_do_method(node, "set_bone_weights", bone_painting_bone, node->get_bone_weights(bone_painting_bone));
  689. undo_redo->add_undo_method(node, "set_bone_weights", bone_painting_bone, prev_weights);
  690. undo_redo->add_do_method(uv_edit_draw, "queue_redraw");
  691. undo_redo->add_undo_method(uv_edit_draw, "queue_redraw");
  692. undo_redo->commit_action();
  693. bone_painting = false;
  694. }
  695. }
  696. } else if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  697. _cancel_editing();
  698. if (bone_painting) {
  699. node->set_bone_weights(bone_painting_bone, prev_weights);
  700. }
  701. uv_edit_draw->queue_redraw();
  702. }
  703. }
  704. Ref<InputEventMouseMotion> mm = p_input;
  705. if (mm.is_valid()) {
  706. if (uv_drag) {
  707. Vector2 uv_drag_to = mm->get_position();
  708. uv_drag_to = snap_point(uv_drag_to); // FIXME: Only works correctly with 'UV_MODE_EDIT_POINT', it's imprecise with the rest.
  709. Vector2 drag = mtx.affine_inverse().xform(uv_drag_to) - mtx.affine_inverse().xform(uv_drag_from);
  710. switch (uv_move_current) {
  711. case UV_MODE_CREATE: {
  712. if (uv_create) {
  713. uv_create_to = mtx.affine_inverse().xform(snap_point(mm->get_position()));
  714. }
  715. } break;
  716. case UV_MODE_EDIT_POINT: {
  717. Vector<Vector2> uv_new = points_prev;
  718. uv_new.set(point_drag_index, uv_new[point_drag_index] + drag);
  719. if (uv_edit_mode[0]->is_pressed()) { //edit uv
  720. node->set_uv(uv_new);
  721. } else if (uv_edit_mode[1]->is_pressed()) { //edit polygon
  722. node->set_polygon(uv_new);
  723. }
  724. } break;
  725. case UV_MODE_MOVE: {
  726. Vector<Vector2> uv_new = points_prev;
  727. for (int i = 0; i < uv_new.size(); i++) {
  728. uv_new.set(i, uv_new[i] + drag);
  729. }
  730. if (uv_edit_mode[0]->is_pressed()) { //edit uv
  731. node->set_uv(uv_new);
  732. } else if (uv_edit_mode[1]->is_pressed()) { //edit polygon
  733. node->set_polygon(uv_new);
  734. }
  735. } break;
  736. case UV_MODE_ROTATE: {
  737. Vector2 center;
  738. Vector<Vector2> uv_new = points_prev;
  739. for (int i = 0; i < uv_new.size(); i++) {
  740. center += points_prev[i];
  741. }
  742. center /= uv_new.size();
  743. real_t angle = (uv_drag_from - mtx.xform(center)).normalized().angle_to((uv_drag_to - mtx.xform(center)).normalized());
  744. for (int i = 0; i < uv_new.size(); i++) {
  745. Vector2 rel = points_prev[i] - center;
  746. rel = rel.rotated(angle);
  747. uv_new.set(i, center + rel);
  748. }
  749. if (uv_edit_mode[0]->is_pressed()) { //edit uv
  750. node->set_uv(uv_new);
  751. } else if (uv_edit_mode[1]->is_pressed()) { //edit polygon
  752. node->set_polygon(uv_new);
  753. }
  754. } break;
  755. case UV_MODE_SCALE: {
  756. Vector2 center;
  757. Vector<Vector2> uv_new = points_prev;
  758. for (int i = 0; i < uv_new.size(); i++) {
  759. center += points_prev[i];
  760. }
  761. center /= uv_new.size();
  762. real_t from_dist = uv_drag_from.distance_to(mtx.xform(center));
  763. real_t to_dist = uv_drag_to.distance_to(mtx.xform(center));
  764. if (from_dist < 2) {
  765. break;
  766. }
  767. real_t scale = to_dist / from_dist;
  768. for (int i = 0; i < uv_new.size(); i++) {
  769. Vector2 rel = points_prev[i] - center;
  770. rel = rel * scale;
  771. uv_new.set(i, center + rel);
  772. }
  773. if (uv_edit_mode[0]->is_pressed()) { //edit uv
  774. node->set_uv(uv_new);
  775. } else if (uv_edit_mode[1]->is_pressed()) { //edit polygon
  776. node->set_polygon(uv_new);
  777. }
  778. } break;
  779. case UV_MODE_PAINT_WEIGHT:
  780. case UV_MODE_CLEAR_WEIGHT: {
  781. bone_paint_pos = mm->get_position();
  782. } break;
  783. default: {
  784. }
  785. }
  786. if (bone_painting) {
  787. Vector<float> painted_weights = node->get_bone_weights(bone_painting_bone);
  788. {
  789. int pc = painted_weights.size();
  790. real_t amount = bone_paint_strength->get_value();
  791. real_t radius = bone_paint_radius->get_value() * EDSCALE;
  792. if (uv_mode == UV_MODE_CLEAR_WEIGHT) {
  793. amount = -amount;
  794. }
  795. float *w = painted_weights.ptrw();
  796. const float *r = prev_weights.ptr();
  797. const Vector2 *rv = points_prev.ptr();
  798. for (int i = 0; i < pc; i++) {
  799. if (mtx.xform(rv[i]).distance_to(bone_paint_pos) < radius) {
  800. w[i] = CLAMP(r[i] + amount, 0, 1);
  801. }
  802. }
  803. }
  804. node->set_bone_weights(bone_painting_bone, painted_weights);
  805. }
  806. uv_edit_draw->queue_redraw();
  807. CanvasItemEditor::get_singleton()->update_viewport();
  808. } else if (polygon_create.size()) {
  809. uv_create_to = mtx.affine_inverse().xform(mm->get_position());
  810. uv_edit_draw->queue_redraw();
  811. } else if (uv_mode == UV_MODE_PAINT_WEIGHT || uv_mode == UV_MODE_CLEAR_WEIGHT) {
  812. bone_paint_pos = mm->get_position();
  813. uv_edit_draw->queue_redraw();
  814. }
  815. }
  816. Ref<InputEventMagnifyGesture> magnify_gesture = p_input;
  817. if (magnify_gesture.is_valid()) {
  818. uv_zoom->set_value(uv_zoom->get_value() * magnify_gesture->get_factor());
  819. }
  820. Ref<InputEventPanGesture> pan_gesture = p_input;
  821. if (pan_gesture.is_valid()) {
  822. uv_hscroll->set_value(uv_hscroll->get_value() + uv_hscroll->get_page() * pan_gesture->get_delta().x / 8);
  823. uv_vscroll->set_value(uv_vscroll->get_value() + uv_vscroll->get_page() * pan_gesture->get_delta().y / 8);
  824. }
  825. }
  826. void Polygon2DEditor::_uv_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  827. uv_hscroll->set_value(uv_hscroll->get_value() - p_scroll_vec.x);
  828. uv_vscroll->set_value(uv_vscroll->get_value() - p_scroll_vec.y);
  829. }
  830. void Polygon2DEditor::_uv_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  831. uv_zoom->set_value(uv_zoom->get_value() * p_zoom_factor);
  832. }
  833. void Polygon2DEditor::_uv_scroll_changed(real_t) {
  834. if (updating_uv_scroll) {
  835. return;
  836. }
  837. uv_draw_ofs.x = uv_hscroll->get_value();
  838. uv_draw_ofs.y = uv_vscroll->get_value();
  839. uv_draw_zoom = uv_zoom->get_value();
  840. uv_edit_draw->queue_redraw();
  841. }
  842. void Polygon2DEditor::_uv_draw() {
  843. if (!uv_edit->is_visible() || !_get_node()) {
  844. return;
  845. }
  846. Ref<Texture2D> base_tex = node->get_texture();
  847. if (base_tex.is_null()) {
  848. return;
  849. }
  850. String warning;
  851. Transform2D mtx;
  852. mtx.columns[2] = -uv_draw_ofs;
  853. mtx.scale_basis(Vector2(uv_draw_zoom, uv_draw_zoom));
  854. RS::get_singleton()->canvas_item_add_set_transform(uv_edit_draw->get_canvas_item(), mtx);
  855. uv_edit_draw->draw_texture(base_tex, Point2());
  856. RS::get_singleton()->canvas_item_add_set_transform(uv_edit_draw->get_canvas_item(), Transform2D());
  857. if (snap_show_grid) {
  858. Color grid_color = Color(1.0, 1.0, 1.0, 0.15);
  859. Size2 s = uv_edit_draw->get_size();
  860. int last_cell = 0;
  861. if (snap_step.x != 0) {
  862. for (int i = 0; i < s.width; i++) {
  863. int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / snap_step.x));
  864. if (i == 0) {
  865. last_cell = cell;
  866. }
  867. if (last_cell != cell) {
  868. uv_edit_draw->draw_line(Point2(i, 0), Point2(i, s.height), grid_color, Math::round(EDSCALE));
  869. }
  870. last_cell = cell;
  871. }
  872. }
  873. if (snap_step.y != 0) {
  874. for (int i = 0; i < s.height; i++) {
  875. int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / snap_step.y));
  876. if (i == 0) {
  877. last_cell = cell;
  878. }
  879. if (last_cell != cell) {
  880. uv_edit_draw->draw_line(Point2(0, i), Point2(s.width, i), grid_color, Math::round(EDSCALE));
  881. }
  882. last_cell = cell;
  883. }
  884. }
  885. }
  886. Array polygons = node->get_polygons();
  887. Vector<Vector2> uvs;
  888. if (uv_edit_mode[0]->is_pressed()) { //edit uv
  889. uvs = node->get_uv();
  890. } else { //edit polygon
  891. uvs = node->get_polygon();
  892. }
  893. const float *weight_r = nullptr;
  894. if (uv_edit_mode[3]->is_pressed()) {
  895. int bone_selected = -1;
  896. for (int i = 0; i < bone_scroll_vb->get_child_count(); i++) {
  897. CheckBox *c = Object::cast_to<CheckBox>(bone_scroll_vb->get_child(i));
  898. if (c && c->is_pressed()) {
  899. bone_selected = i;
  900. break;
  901. }
  902. }
  903. if (bone_selected != -1 && node->get_bone_weights(bone_selected).size() == uvs.size()) {
  904. weight_r = node->get_bone_weights(bone_selected).ptr();
  905. }
  906. }
  907. // All UV points are sharp, so use the sharp handle icon
  908. Ref<Texture2D> handle = get_editor_theme_icon(SNAME("EditorPathSharpHandle"));
  909. Color poly_line_color = Color(0.9, 0.5, 0.5);
  910. if (polygons.size() || polygon_create.size()) {
  911. poly_line_color.a *= 0.25;
  912. }
  913. Color polygon_line_color = Color(0.5, 0.5, 0.9);
  914. Vector<Color> polygon_fill_color;
  915. {
  916. Color pf = polygon_line_color;
  917. pf.a *= 0.5;
  918. polygon_fill_color.push_back(pf);
  919. }
  920. Color prev_color = Color(0.5, 0.5, 0.5);
  921. Rect2 rect;
  922. int uv_draw_max = uvs.size();
  923. uv_draw_max -= node->get_internal_vertex_count();
  924. if (uv_draw_max < 0) {
  925. uv_draw_max = 0;
  926. }
  927. for (int i = 0; i < uvs.size(); i++) {
  928. int next = uv_draw_max > 0 ? (i + 1) % uv_draw_max : 0;
  929. if (i < uv_draw_max && uv_drag && uv_move_current == UV_MODE_EDIT_POINT && EDITOR_GET("editors/polygon_editor/show_previous_outline")) {
  930. uv_edit_draw->draw_line(mtx.xform(points_prev[i]), mtx.xform(points_prev[next]), prev_color, Math::round(EDSCALE));
  931. }
  932. Vector2 next_point = uvs[next];
  933. if (uv_create && i == uvs.size() - 1) {
  934. next_point = uv_create_to;
  935. }
  936. if (i < uv_draw_max) { // If using or creating polygons, do not show outline (will show polygons instead).
  937. uv_edit_draw->draw_line(mtx.xform(uvs[i]), mtx.xform(next_point), poly_line_color, Math::round(EDSCALE));
  938. }
  939. }
  940. for (int i = 0; i < polygons.size(); i++) {
  941. Vector<int> points = polygons[i];
  942. Vector<Vector2> polypoints;
  943. for (int j = 0; j < points.size(); j++) {
  944. int next = (j + 1) % points.size();
  945. int idx = points[j];
  946. int idx_next = points[next];
  947. if (idx < 0 || idx >= uvs.size()) {
  948. continue;
  949. }
  950. polypoints.push_back(mtx.xform(uvs[idx]));
  951. if (idx_next < 0 || idx_next >= uvs.size()) {
  952. continue;
  953. }
  954. uv_edit_draw->draw_line(mtx.xform(uvs[idx]), mtx.xform(uvs[idx_next]), polygon_line_color, Math::round(EDSCALE));
  955. }
  956. if (points.size() >= 3) {
  957. uv_edit_draw->draw_polygon(polypoints, polygon_fill_color);
  958. }
  959. }
  960. for (int i = 0; i < uvs.size(); i++) {
  961. if (weight_r) {
  962. Vector2 draw_pos = mtx.xform(uvs[i]);
  963. float weight = weight_r[i];
  964. uv_edit_draw->draw_rect(Rect2(draw_pos - Vector2(2, 2) * EDSCALE, Vector2(5, 5) * EDSCALE), Color(weight, weight, weight, 1.0), Math::round(EDSCALE));
  965. } else {
  966. if (i < uv_draw_max) {
  967. uv_edit_draw->draw_texture(handle, mtx.xform(uvs[i]) - handle->get_size() * 0.5);
  968. } else {
  969. // Internal vertex
  970. uv_edit_draw->draw_texture(handle, mtx.xform(uvs[i]) - handle->get_size() * 0.5, Color(0.6, 0.8, 1));
  971. }
  972. }
  973. }
  974. if (polygon_create.size()) {
  975. for (int i = 0; i < polygon_create.size(); i++) {
  976. Vector2 from = uvs[polygon_create[i]];
  977. Vector2 to = (i + 1) < polygon_create.size() ? uvs[polygon_create[i + 1]] : uv_create_to;
  978. uv_edit_draw->draw_line(mtx.xform(from), mtx.xform(to), polygon_line_color, Math::round(EDSCALE));
  979. }
  980. }
  981. if (uv_mode == UV_MODE_PAINT_WEIGHT || uv_mode == UV_MODE_CLEAR_WEIGHT) {
  982. NodePath bone_path;
  983. for (int i = 0; i < bone_scroll_vb->get_child_count(); i++) {
  984. CheckBox *c = Object::cast_to<CheckBox>(bone_scroll_vb->get_child(i));
  985. if (c && c->is_pressed()) {
  986. bone_path = node->get_bone_path(i);
  987. break;
  988. }
  989. }
  990. //draw skeleton
  991. NodePath skeleton_path = node->get_skeleton();
  992. if (node->has_node(skeleton_path)) {
  993. Skeleton2D *skeleton = Object::cast_to<Skeleton2D>(node->get_node(skeleton_path));
  994. if (skeleton) {
  995. for (int i = 0; i < skeleton->get_bone_count(); i++) {
  996. Bone2D *bone = skeleton->get_bone(i);
  997. if (bone->get_rest() == Transform2D(0, 0, 0, 0, 0, 0)) {
  998. continue; //not set
  999. }
  1000. bool current = bone_path == skeleton->get_path_to(bone);
  1001. bool found_child = false;
  1002. for (int j = 0; j < bone->get_child_count(); j++) {
  1003. Bone2D *n = Object::cast_to<Bone2D>(bone->get_child(j));
  1004. if (!n) {
  1005. continue;
  1006. }
  1007. found_child = true;
  1008. Transform2D bone_xform = node->get_global_transform().affine_inverse() * (skeleton->get_global_transform() * bone->get_skeleton_rest());
  1009. Transform2D endpoint_xform = bone_xform * n->get_transform();
  1010. Color color = current ? Color(1, 1, 1) : Color(0.5, 0.5, 0.5);
  1011. uv_edit_draw->draw_line(mtx.xform(bone_xform.get_origin()), mtx.xform(endpoint_xform.get_origin()), Color(0, 0, 0), Math::round((current ? 5 : 4) * EDSCALE));
  1012. uv_edit_draw->draw_line(mtx.xform(bone_xform.get_origin()), mtx.xform(endpoint_xform.get_origin()), color, Math::round((current ? 3 : 2) * EDSCALE));
  1013. }
  1014. if (!found_child) {
  1015. //draw normally
  1016. Transform2D bone_xform = node->get_global_transform().affine_inverse() * (skeleton->get_global_transform() * bone->get_skeleton_rest());
  1017. Transform2D endpoint_xform = bone_xform * Transform2D(0, Vector2(bone->get_length(), 0));
  1018. Color color = current ? Color(1, 1, 1) : Color(0.5, 0.5, 0.5);
  1019. uv_edit_draw->draw_line(mtx.xform(bone_xform.get_origin()), mtx.xform(endpoint_xform.get_origin()), Color(0, 0, 0), Math::round((current ? 5 : 4) * EDSCALE));
  1020. uv_edit_draw->draw_line(mtx.xform(bone_xform.get_origin()), mtx.xform(endpoint_xform.get_origin()), color, Math::round((current ? 3 : 2) * EDSCALE));
  1021. }
  1022. }
  1023. }
  1024. }
  1025. //draw paint circle
  1026. uv_edit_draw->draw_circle(bone_paint_pos, bone_paint_radius->get_value() * EDSCALE, Color(1, 1, 1, 0.1));
  1027. }
  1028. rect.position = -uv_edit_draw->get_size();
  1029. rect.size = uv_edit_draw->get_size() * 2.0 + base_tex->get_size() * uv_draw_zoom;
  1030. updating_uv_scroll = true;
  1031. uv_hscroll->set_min(rect.position.x);
  1032. uv_hscroll->set_max(rect.position.x + rect.size.x);
  1033. if (ABS(rect.position.x - (rect.position.x + rect.size.x)) <= uv_edit_draw->get_size().x) {
  1034. uv_hscroll->hide();
  1035. } else {
  1036. uv_hscroll->show();
  1037. uv_hscroll->set_page(uv_edit_draw->get_size().x);
  1038. uv_hscroll->set_value(uv_draw_ofs.x);
  1039. }
  1040. uv_vscroll->set_min(rect.position.y);
  1041. uv_vscroll->set_max(rect.position.y + rect.size.y);
  1042. if (ABS(rect.position.y - (rect.position.y + rect.size.y)) <= uv_edit_draw->get_size().y) {
  1043. uv_vscroll->hide();
  1044. } else {
  1045. uv_vscroll->show();
  1046. uv_vscroll->set_page(uv_edit_draw->get_size().y);
  1047. uv_vscroll->set_value(uv_draw_ofs.y);
  1048. }
  1049. Size2 hmin = uv_hscroll->get_combined_minimum_size();
  1050. Size2 vmin = uv_vscroll->get_combined_minimum_size();
  1051. // Avoid scrollbar overlapping.
  1052. uv_hscroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, uv_vscroll->is_visible() ? -vmin.width : 0);
  1053. uv_vscroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, uv_hscroll->is_visible() ? -hmin.height : 0);
  1054. updating_uv_scroll = false;
  1055. }
  1056. void Polygon2DEditor::_bind_methods() {
  1057. ClassDB::bind_method(D_METHOD("_update_bone_list"), &Polygon2DEditor::_update_bone_list);
  1058. ClassDB::bind_method(D_METHOD("_update_polygon_editing_state"), &Polygon2DEditor::_update_polygon_editing_state);
  1059. }
  1060. Vector2 Polygon2DEditor::snap_point(Vector2 p_target) const {
  1061. if (use_snap) {
  1062. 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);
  1063. 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);
  1064. }
  1065. return p_target;
  1066. }
  1067. Polygon2DEditor::Polygon2DEditor() {
  1068. snap_offset = EditorSettings::get_singleton()->get_project_metadata("polygon_2d_uv_editor", "snap_offset", Vector2());
  1069. // A power-of-two value works better as a default grid size.
  1070. snap_step = EditorSettings::get_singleton()->get_project_metadata("polygon_2d_uv_editor", "snap_step", Vector2(8, 8));
  1071. use_snap = EditorSettings::get_singleton()->get_project_metadata("polygon_2d_uv_editor", "snap_enabled", false);
  1072. snap_show_grid = EditorSettings::get_singleton()->get_project_metadata("polygon_2d_uv_editor", "show_grid", false);
  1073. button_uv = memnew(Button);
  1074. button_uv->set_theme_type_variation("FlatButton");
  1075. add_child(button_uv);
  1076. button_uv->set_tooltip_text(TTR("Open Polygon 2D UV editor."));
  1077. button_uv->connect("pressed", callable_mp(this, &Polygon2DEditor::_menu_option).bind(MODE_EDIT_UV));
  1078. uv_mode = UV_MODE_EDIT_POINT;
  1079. uv_edit = memnew(AcceptDialog);
  1080. add_child(uv_edit);
  1081. uv_edit->set_title(TTR("Polygon 2D UV Editor"));
  1082. uv_edit->connect("canceled", callable_mp(this, &Polygon2DEditor::_uv_edit_popup_hide));
  1083. VBoxContainer *uv_main_vb = memnew(VBoxContainer);
  1084. uv_edit->add_child(uv_main_vb);
  1085. HBoxContainer *uv_mode_hb = memnew(HBoxContainer);
  1086. uv_edit_group.instantiate();
  1087. uv_edit_mode[0] = memnew(Button);
  1088. uv_mode_hb->add_child(uv_edit_mode[0]);
  1089. uv_edit_mode[0]->set_toggle_mode(true);
  1090. uv_edit_mode[1] = memnew(Button);
  1091. uv_mode_hb->add_child(uv_edit_mode[1]);
  1092. uv_edit_mode[1]->set_toggle_mode(true);
  1093. uv_edit_mode[2] = memnew(Button);
  1094. uv_mode_hb->add_child(uv_edit_mode[2]);
  1095. uv_edit_mode[2]->set_toggle_mode(true);
  1096. uv_edit_mode[3] = memnew(Button);
  1097. uv_mode_hb->add_child(uv_edit_mode[3]);
  1098. uv_edit_mode[3]->set_toggle_mode(true);
  1099. uv_edit_mode[0]->set_text(TTR("UV"));
  1100. uv_edit_mode[0]->set_pressed(true);
  1101. uv_edit_mode[1]->set_text(TTR("Points"));
  1102. uv_edit_mode[2]->set_text(TTR("Polygons"));
  1103. uv_edit_mode[3]->set_text(TTR("Bones"));
  1104. uv_edit_mode[0]->set_button_group(uv_edit_group);
  1105. uv_edit_mode[1]->set_button_group(uv_edit_group);
  1106. uv_edit_mode[2]->set_button_group(uv_edit_group);
  1107. uv_edit_mode[3]->set_button_group(uv_edit_group);
  1108. uv_edit_mode[0]->connect("pressed", callable_mp(this, &Polygon2DEditor::_uv_edit_mode_select).bind(0));
  1109. uv_edit_mode[1]->connect("pressed", callable_mp(this, &Polygon2DEditor::_uv_edit_mode_select).bind(1));
  1110. uv_edit_mode[2]->connect("pressed", callable_mp(this, &Polygon2DEditor::_uv_edit_mode_select).bind(2));
  1111. uv_edit_mode[3]->connect("pressed", callable_mp(this, &Polygon2DEditor::_uv_edit_mode_select).bind(3));
  1112. uv_mode_hb->add_child(memnew(VSeparator));
  1113. uv_main_vb->add_child(uv_mode_hb);
  1114. for (int i = 0; i < UV_MODE_MAX; i++) {
  1115. uv_button[i] = memnew(Button);
  1116. uv_button[i]->set_theme_type_variation("FlatButton");
  1117. uv_button[i]->set_toggle_mode(true);
  1118. uv_mode_hb->add_child(uv_button[i]);
  1119. uv_button[i]->connect("pressed", callable_mp(this, &Polygon2DEditor::_uv_mode).bind(i));
  1120. uv_button[i]->set_focus_mode(FOCUS_NONE);
  1121. }
  1122. uv_button[UV_MODE_CREATE]->set_tooltip_text(TTR("Create Polygon"));
  1123. uv_button[UV_MODE_CREATE_INTERNAL]->set_tooltip_text(TTR("Create Internal Vertex"));
  1124. uv_button[UV_MODE_REMOVE_INTERNAL]->set_tooltip_text(TTR("Remove Internal Vertex"));
  1125. Key key = (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) ? Key::META : Key::CTRL;
  1126. uv_button[UV_MODE_EDIT_POINT]->set_tooltip_text(TTR("Move Points") + "\n" + find_keycode_name(key) + TTR(": Rotate") + "\n" + TTR("Shift: Move All") + "\n" + keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Shift: Scale"));
  1127. uv_button[UV_MODE_MOVE]->set_tooltip_text(TTR("Move Polygon"));
  1128. uv_button[UV_MODE_ROTATE]->set_tooltip_text(TTR("Rotate Polygon"));
  1129. uv_button[UV_MODE_SCALE]->set_tooltip_text(TTR("Scale Polygon"));
  1130. uv_button[UV_MODE_ADD_POLYGON]->set_tooltip_text(TTR("Create a custom polygon. Enables custom polygon rendering."));
  1131. uv_button[UV_MODE_REMOVE_POLYGON]->set_tooltip_text(TTR("Remove a custom polygon. If none remain, custom polygon rendering is disabled."));
  1132. uv_button[UV_MODE_PAINT_WEIGHT]->set_tooltip_text(TTR("Paint weights with specified intensity."));
  1133. uv_button[UV_MODE_CLEAR_WEIGHT]->set_tooltip_text(TTR("Unpaint weights with specified intensity."));
  1134. uv_button[UV_MODE_CREATE]->hide();
  1135. uv_button[UV_MODE_CREATE_INTERNAL]->hide();
  1136. uv_button[UV_MODE_REMOVE_INTERNAL]->hide();
  1137. uv_button[UV_MODE_ADD_POLYGON]->hide();
  1138. uv_button[UV_MODE_REMOVE_POLYGON]->hide();
  1139. uv_button[UV_MODE_PAINT_WEIGHT]->hide();
  1140. uv_button[UV_MODE_CLEAR_WEIGHT]->hide();
  1141. uv_button[UV_MODE_EDIT_POINT]->set_pressed(true);
  1142. bone_paint_strength = memnew(HSlider);
  1143. uv_mode_hb->add_child(bone_paint_strength);
  1144. bone_paint_strength->set_custom_minimum_size(Size2(75 * EDSCALE, 0));
  1145. bone_paint_strength->set_v_size_flags(SIZE_SHRINK_CENTER);
  1146. bone_paint_strength->set_min(0);
  1147. bone_paint_strength->set_max(1);
  1148. bone_paint_strength->set_step(0.01);
  1149. bone_paint_strength->set_value(0.5);
  1150. bone_paint_radius_label = memnew(Label(TTR("Radius:")));
  1151. uv_mode_hb->add_child(bone_paint_radius_label);
  1152. bone_paint_radius = memnew(SpinBox);
  1153. uv_mode_hb->add_child(bone_paint_radius);
  1154. bone_paint_strength->hide();
  1155. bone_paint_radius->hide();
  1156. bone_paint_radius_label->hide();
  1157. bone_paint_radius->set_min(1);
  1158. bone_paint_radius->set_max(100);
  1159. bone_paint_radius->set_step(1);
  1160. bone_paint_radius->set_value(32);
  1161. HSplitContainer *uv_main_hsc = memnew(HSplitContainer);
  1162. uv_main_vb->add_child(uv_main_hsc);
  1163. uv_main_hsc->set_v_size_flags(SIZE_EXPAND_FILL);
  1164. uv_edit_draw = memnew(Panel);
  1165. uv_main_hsc->add_child(uv_edit_draw);
  1166. uv_edit_draw->set_h_size_flags(SIZE_EXPAND_FILL);
  1167. uv_edit_draw->set_custom_minimum_size(Size2(200, 200) * EDSCALE);
  1168. Control *space = memnew(Control);
  1169. uv_mode_hb->add_child(space);
  1170. space->set_h_size_flags(SIZE_EXPAND_FILL);
  1171. uv_menu = memnew(MenuButton);
  1172. uv_mode_hb->add_child(uv_menu);
  1173. uv_menu->set_flat(false);
  1174. uv_menu->set_theme_type_variation("FlatMenuButton");
  1175. uv_menu->set_text(TTR("Edit"));
  1176. uv_menu->get_popup()->add_item(TTR("Copy Polygon to UV"), UVEDIT_POLYGON_TO_UV);
  1177. uv_menu->get_popup()->add_item(TTR("Copy UV to Polygon"), UVEDIT_UV_TO_POLYGON);
  1178. uv_menu->get_popup()->add_separator();
  1179. uv_menu->get_popup()->add_item(TTR("Clear UV"), UVEDIT_UV_CLEAR);
  1180. uv_menu->get_popup()->add_separator();
  1181. uv_menu->get_popup()->add_item(TTR("Grid Settings"), UVEDIT_GRID_SETTINGS);
  1182. uv_menu->get_popup()->connect("id_pressed", callable_mp(this, &Polygon2DEditor::_menu_option));
  1183. uv_mode_hb->add_child(memnew(VSeparator));
  1184. b_snap_enable = memnew(Button);
  1185. b_snap_enable->set_theme_type_variation("FlatButton");
  1186. uv_mode_hb->add_child(b_snap_enable);
  1187. b_snap_enable->set_text(TTR("Snap"));
  1188. b_snap_enable->set_focus_mode(FOCUS_NONE);
  1189. b_snap_enable->set_toggle_mode(true);
  1190. b_snap_enable->set_pressed(use_snap);
  1191. b_snap_enable->set_tooltip_text(TTR("Enable Snap"));
  1192. b_snap_enable->connect("toggled", callable_mp(this, &Polygon2DEditor::_set_use_snap));
  1193. b_snap_grid = memnew(Button);
  1194. b_snap_grid->set_theme_type_variation("FlatButton");
  1195. uv_mode_hb->add_child(b_snap_grid);
  1196. b_snap_grid->set_text(TTR("Grid"));
  1197. b_snap_grid->set_focus_mode(FOCUS_NONE);
  1198. b_snap_grid->set_toggle_mode(true);
  1199. b_snap_grid->set_pressed(snap_show_grid);
  1200. b_snap_grid->set_tooltip_text(TTR("Show Grid"));
  1201. b_snap_grid->connect("toggled", callable_mp(this, &Polygon2DEditor::_set_show_grid));
  1202. grid_settings = memnew(AcceptDialog);
  1203. grid_settings->set_title(TTR("Configure Grid:"));
  1204. add_child(grid_settings);
  1205. VBoxContainer *grid_settings_vb = memnew(VBoxContainer);
  1206. grid_settings->add_child(grid_settings_vb);
  1207. SpinBox *sb_off_x = memnew(SpinBox);
  1208. sb_off_x->set_min(-256);
  1209. sb_off_x->set_max(256);
  1210. sb_off_x->set_step(1);
  1211. sb_off_x->set_value(snap_offset.x);
  1212. sb_off_x->set_suffix("px");
  1213. sb_off_x->connect("value_changed", callable_mp(this, &Polygon2DEditor::_set_snap_off_x));
  1214. grid_settings_vb->add_margin_child(TTR("Grid Offset X:"), sb_off_x);
  1215. SpinBox *sb_off_y = memnew(SpinBox);
  1216. sb_off_y->set_min(-256);
  1217. sb_off_y->set_max(256);
  1218. sb_off_y->set_step(1);
  1219. sb_off_y->set_value(snap_offset.y);
  1220. sb_off_y->set_suffix("px");
  1221. sb_off_y->connect("value_changed", callable_mp(this, &Polygon2DEditor::_set_snap_off_y));
  1222. grid_settings_vb->add_margin_child(TTR("Grid Offset Y:"), sb_off_y);
  1223. SpinBox *sb_step_x = memnew(SpinBox);
  1224. sb_step_x->set_min(-256);
  1225. sb_step_x->set_max(256);
  1226. sb_step_x->set_step(1);
  1227. sb_step_x->set_value(snap_step.x);
  1228. sb_step_x->set_suffix("px");
  1229. sb_step_x->connect("value_changed", callable_mp(this, &Polygon2DEditor::_set_snap_step_x));
  1230. grid_settings_vb->add_margin_child(TTR("Grid Step X:"), sb_step_x);
  1231. SpinBox *sb_step_y = memnew(SpinBox);
  1232. sb_step_y->set_min(-256);
  1233. sb_step_y->set_max(256);
  1234. sb_step_y->set_step(1);
  1235. sb_step_y->set_value(snap_step.y);
  1236. sb_step_y->set_suffix("px");
  1237. sb_step_y->connect("value_changed", callable_mp(this, &Polygon2DEditor::_set_snap_step_y));
  1238. grid_settings_vb->add_margin_child(TTR("Grid Step Y:"), sb_step_y);
  1239. uv_mode_hb->add_child(memnew(VSeparator));
  1240. uv_icon_zoom = memnew(TextureRect);
  1241. uv_icon_zoom->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  1242. uv_mode_hb->add_child(uv_icon_zoom);
  1243. uv_zoom = memnew(HSlider);
  1244. uv_zoom->set_min(0.01);
  1245. uv_zoom->set_max(16);
  1246. uv_zoom->set_value(1);
  1247. uv_zoom->set_step(0.01);
  1248. uv_zoom->set_v_size_flags(SIZE_SHRINK_CENTER);
  1249. uv_mode_hb->add_child(uv_zoom);
  1250. uv_zoom->set_custom_minimum_size(Size2(80 * EDSCALE, 0));
  1251. uv_zoom_value = memnew(SpinBox);
  1252. uv_zoom->share(uv_zoom_value);
  1253. uv_zoom_value->set_custom_minimum_size(Size2(50, 0));
  1254. uv_mode_hb->add_child(uv_zoom_value);
  1255. uv_zoom->connect("value_changed", callable_mp(this, &Polygon2DEditor::_uv_scroll_changed));
  1256. uv_vscroll = memnew(VScrollBar);
  1257. uv_vscroll->set_step(0.001);
  1258. uv_edit_draw->add_child(uv_vscroll);
  1259. uv_vscroll->connect("value_changed", callable_mp(this, &Polygon2DEditor::_uv_scroll_changed));
  1260. uv_hscroll = memnew(HScrollBar);
  1261. uv_hscroll->set_step(0.001);
  1262. uv_edit_draw->add_child(uv_hscroll);
  1263. uv_hscroll->connect("value_changed", callable_mp(this, &Polygon2DEditor::_uv_scroll_changed));
  1264. bone_scroll_main_vb = memnew(VBoxContainer);
  1265. bone_scroll_main_vb->hide();
  1266. bone_scroll_main_vb->set_custom_minimum_size(Size2(150 * EDSCALE, 0));
  1267. sync_bones = memnew(Button(TTR("Sync Bones to Polygon")));
  1268. bone_scroll_main_vb->add_child(sync_bones);
  1269. sync_bones->set_h_size_flags(0);
  1270. sync_bones->connect("pressed", callable_mp(this, &Polygon2DEditor::_sync_bones));
  1271. uv_main_hsc->add_child(bone_scroll_main_vb);
  1272. bone_scroll = memnew(ScrollContainer);
  1273. bone_scroll->set_v_scroll(true);
  1274. bone_scroll->set_h_scroll(false);
  1275. bone_scroll_main_vb->add_child(bone_scroll);
  1276. bone_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  1277. bone_scroll_vb = memnew(VBoxContainer);
  1278. bone_scroll->add_child(bone_scroll_vb);
  1279. uv_panner.instantiate();
  1280. uv_panner->set_callbacks(callable_mp(this, &Polygon2DEditor::_uv_pan_callback), callable_mp(this, &Polygon2DEditor::_uv_zoom_callback));
  1281. uv_edit_draw->connect("draw", callable_mp(this, &Polygon2DEditor::_uv_draw));
  1282. uv_edit_draw->connect("gui_input", callable_mp(this, &Polygon2DEditor::_uv_input));
  1283. uv_edit_draw->connect("focus_exited", callable_mp(uv_panner.ptr(), &ViewPanner::release_pan_key));
  1284. uv_edit_draw->set_focus_mode(FOCUS_CLICK);
  1285. uv_draw_zoom = 1.0;
  1286. point_drag_index = -1;
  1287. uv_drag = false;
  1288. uv_create = false;
  1289. updating_uv_scroll = false;
  1290. bone_painting = false;
  1291. error = memnew(AcceptDialog);
  1292. add_child(error);
  1293. uv_edit_draw->set_clip_contents(true);
  1294. }
  1295. Polygon2DEditorPlugin::Polygon2DEditorPlugin() :
  1296. AbstractPolygon2DEditorPlugin(memnew(Polygon2DEditor), "Polygon2D") {
  1297. }