sprite_2d_editor_plugin.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*************************************************************************/
  2. /* sprite_2d_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "sprite_2d_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "core/math/geometry_2d.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_scale.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/scene_tree_dock.h"
  37. #include "scene/2d/collision_polygon_2d.h"
  38. #include "scene/2d/light_occluder_2d.h"
  39. #include "scene/2d/mesh_instance_2d.h"
  40. #include "scene/2d/polygon_2d.h"
  41. #include "scene/gui/box_container.h"
  42. #include "thirdparty/misc/clipper.hpp"
  43. void Sprite2DEditor::_node_removed(Node *p_node) {
  44. if (p_node == node) {
  45. node = nullptr;
  46. options->hide();
  47. }
  48. }
  49. void Sprite2DEditor::edit(Sprite2D *p_sprite) {
  50. node = p_sprite;
  51. }
  52. #define PRECISION 10.0
  53. Vector<Vector2> expand(const Vector<Vector2> &points, const Rect2i &rect, float epsilon = 2.0) {
  54. int size = points.size();
  55. ERR_FAIL_COND_V(size < 2, Vector<Vector2>());
  56. ClipperLib::Path subj;
  57. ClipperLib::PolyTree solution;
  58. ClipperLib::PolyTree out;
  59. for (int i = 0; i < points.size(); i++) {
  60. subj << ClipperLib::IntPoint(points[i].x * PRECISION, points[i].y * PRECISION);
  61. }
  62. ClipperLib::ClipperOffset co;
  63. co.AddPath(subj, ClipperLib::jtMiter, ClipperLib::etClosedPolygon);
  64. co.Execute(solution, epsilon * PRECISION);
  65. ClipperLib::PolyNode *p = solution.GetFirst();
  66. ERR_FAIL_COND_V(!p, points);
  67. while (p->IsHole()) {
  68. p = p->GetNext();
  69. }
  70. //turn the result into simply polygon (AKA, fix overlap)
  71. //clamp into the specified rect
  72. ClipperLib::Clipper cl;
  73. cl.StrictlySimple(true);
  74. cl.AddPath(p->Contour, ClipperLib::ptSubject, true);
  75. //create the clipping rect
  76. ClipperLib::Path clamp;
  77. clamp.push_back(ClipperLib::IntPoint(0, 0));
  78. clamp.push_back(ClipperLib::IntPoint(rect.size.width * PRECISION, 0));
  79. clamp.push_back(ClipperLib::IntPoint(rect.size.width * PRECISION, rect.size.height * PRECISION));
  80. clamp.push_back(ClipperLib::IntPoint(0, rect.size.height * PRECISION));
  81. cl.AddPath(clamp, ClipperLib::ptClip, true);
  82. cl.Execute(ClipperLib::ctIntersection, out);
  83. Vector<Vector2> outPoints;
  84. ClipperLib::PolyNode *p2 = out.GetFirst();
  85. ERR_FAIL_COND_V(!p2, points);
  86. while (p2->IsHole()) {
  87. p2 = p2->GetNext();
  88. }
  89. int lasti = p2->Contour.size() - 1;
  90. Vector2 prev = Vector2(p2->Contour[lasti].X / PRECISION, p2->Contour[lasti].Y / PRECISION);
  91. for (uint64_t i = 0; i < p2->Contour.size(); i++) {
  92. Vector2 cur = Vector2(p2->Contour[i].X / PRECISION, p2->Contour[i].Y / PRECISION);
  93. if (cur.distance_to(prev) > 0.5) {
  94. outPoints.push_back(cur);
  95. prev = cur;
  96. }
  97. }
  98. return outPoints;
  99. }
  100. void Sprite2DEditor::_menu_option(int p_option) {
  101. if (!node) {
  102. return;
  103. }
  104. selected_menu_item = (Menu)p_option;
  105. switch (p_option) {
  106. case MENU_OPTION_CONVERT_TO_MESH_2D: {
  107. debug_uv_dialog->set_ok_button_text(TTR("Create MeshInstance2D"));
  108. debug_uv_dialog->set_title(TTR("MeshInstance2D Preview"));
  109. _update_mesh_data();
  110. debug_uv_dialog->popup_centered();
  111. debug_uv->queue_redraw();
  112. } break;
  113. case MENU_OPTION_CONVERT_TO_POLYGON_2D: {
  114. debug_uv_dialog->set_ok_button_text(TTR("Create Polygon2D"));
  115. debug_uv_dialog->set_title(TTR("Polygon2D Preview"));
  116. _update_mesh_data();
  117. debug_uv_dialog->popup_centered();
  118. debug_uv->queue_redraw();
  119. } break;
  120. case MENU_OPTION_CREATE_COLLISION_POLY_2D: {
  121. debug_uv_dialog->set_ok_button_text(TTR("Create CollisionPolygon2D"));
  122. debug_uv_dialog->set_title(TTR("CollisionPolygon2D Preview"));
  123. _update_mesh_data();
  124. debug_uv_dialog->popup_centered();
  125. debug_uv->queue_redraw();
  126. } break;
  127. case MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D: {
  128. debug_uv_dialog->set_ok_button_text(TTR("Create LightOccluder2D"));
  129. debug_uv_dialog->set_title(TTR("LightOccluder2D Preview"));
  130. _update_mesh_data();
  131. debug_uv_dialog->popup_centered();
  132. debug_uv->queue_redraw();
  133. } break;
  134. }
  135. }
  136. void Sprite2DEditor::_update_mesh_data() {
  137. if (node->get_owner() != get_tree()->get_edited_scene_root()) {
  138. err_dialog->set_text(TTR("Can't convert a Sprite2D from a foreign scene."));
  139. err_dialog->popup_centered();
  140. }
  141. Ref<Texture2D> texture = node->get_texture();
  142. if (texture.is_null()) {
  143. err_dialog->set_text(TTR("Sprite2D is empty!"));
  144. err_dialog->popup_centered();
  145. return;
  146. }
  147. if (node->get_hframes() > 1 || node->get_vframes() > 1) {
  148. err_dialog->set_text(TTR("Can't convert a sprite using animation frames to mesh."));
  149. err_dialog->popup_centered();
  150. return;
  151. }
  152. Ref<Image> image = texture->get_image();
  153. ERR_FAIL_COND(image.is_null());
  154. if (image->is_compressed()) {
  155. image->decompress();
  156. }
  157. Rect2 rect;
  158. if (node->is_region_enabled()) {
  159. rect = node->get_region_rect();
  160. } else {
  161. rect.size = image->get_size();
  162. }
  163. Ref<BitMap> bm;
  164. bm.instantiate();
  165. bm->create_from_image_alpha(image);
  166. int shrink = shrink_pixels->get_value();
  167. if (shrink > 0) {
  168. bm->shrink_mask(shrink, rect);
  169. }
  170. int grow = grow_pixels->get_value();
  171. if (grow > 0) {
  172. bm->grow_mask(grow, rect);
  173. }
  174. float epsilon = simplification->get_value();
  175. Vector<Vector<Vector2>> lines = bm->clip_opaque_to_polygons(rect, epsilon);
  176. uv_lines.clear();
  177. computed_vertices.clear();
  178. computed_uv.clear();
  179. computed_indices.clear();
  180. Size2 img_size = image->get_size();
  181. for (int i = 0; i < lines.size(); i++) {
  182. lines.write[i] = expand(lines[i], rect, epsilon);
  183. }
  184. if (selected_menu_item == MENU_OPTION_CONVERT_TO_MESH_2D) {
  185. for (int j = 0; j < lines.size(); j++) {
  186. int index_ofs = computed_vertices.size();
  187. for (int i = 0; i < lines[j].size(); i++) {
  188. Vector2 vtx = lines[j][i];
  189. computed_uv.push_back(vtx / img_size);
  190. vtx -= rect.position; //offset by rect position
  191. //flip if flipped
  192. if (node->is_flipped_h()) {
  193. vtx.x = rect.size.x - vtx.x - 1.0;
  194. }
  195. if (node->is_flipped_v()) {
  196. vtx.y = rect.size.y - vtx.y - 1.0;
  197. }
  198. if (node->is_centered()) {
  199. vtx -= rect.size / 2.0;
  200. }
  201. computed_vertices.push_back(vtx);
  202. }
  203. Vector<int> poly = Geometry2D::triangulate_polygon(lines[j]);
  204. for (int i = 0; i < poly.size(); i += 3) {
  205. for (int k = 0; k < 3; k++) {
  206. int idx = i + k;
  207. int idxn = i + (k + 1) % 3;
  208. uv_lines.push_back(lines[j][poly[idx]]);
  209. uv_lines.push_back(lines[j][poly[idxn]]);
  210. computed_indices.push_back(poly[idx] + index_ofs);
  211. }
  212. }
  213. }
  214. }
  215. outline_lines.clear();
  216. computed_outline_lines.clear();
  217. if (selected_menu_item == MENU_OPTION_CONVERT_TO_POLYGON_2D || selected_menu_item == MENU_OPTION_CREATE_COLLISION_POLY_2D || selected_menu_item == MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D) {
  218. outline_lines.resize(lines.size());
  219. computed_outline_lines.resize(lines.size());
  220. for (int pi = 0; pi < lines.size(); pi++) {
  221. Vector<Vector2> ol;
  222. Vector<Vector2> col;
  223. ol.resize(lines[pi].size());
  224. col.resize(lines[pi].size());
  225. for (int i = 0; i < lines[pi].size(); i++) {
  226. Vector2 vtx = lines[pi][i];
  227. ol.write[i] = vtx;
  228. vtx -= rect.position; //offset by rect position
  229. //flip if flipped
  230. if (node->is_flipped_h()) {
  231. vtx.x = rect.size.x - vtx.x - 1.0;
  232. }
  233. if (node->is_flipped_v()) {
  234. vtx.y = rect.size.y - vtx.y - 1.0;
  235. }
  236. if (node->is_centered()) {
  237. vtx -= rect.size / 2.0;
  238. }
  239. col.write[i] = vtx;
  240. }
  241. outline_lines.write[pi] = ol;
  242. computed_outline_lines.write[pi] = col;
  243. }
  244. }
  245. debug_uv->queue_redraw();
  246. }
  247. void Sprite2DEditor::_create_node() {
  248. switch (selected_menu_item) {
  249. case MENU_OPTION_CONVERT_TO_MESH_2D: {
  250. _convert_to_mesh_2d_node();
  251. } break;
  252. case MENU_OPTION_CONVERT_TO_POLYGON_2D: {
  253. _convert_to_polygon_2d_node();
  254. } break;
  255. case MENU_OPTION_CREATE_COLLISION_POLY_2D: {
  256. _create_collision_polygon_2d_node();
  257. } break;
  258. case MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D: {
  259. _create_light_occluder_2d_node();
  260. } break;
  261. }
  262. }
  263. void Sprite2DEditor::_convert_to_mesh_2d_node() {
  264. if (computed_vertices.size() < 3) {
  265. err_dialog->set_text(TTR("Invalid geometry, can't replace by mesh."));
  266. err_dialog->popup_centered();
  267. return;
  268. }
  269. Ref<ArrayMesh> mesh;
  270. mesh.instantiate();
  271. Array a;
  272. a.resize(Mesh::ARRAY_MAX);
  273. a[Mesh::ARRAY_VERTEX] = computed_vertices;
  274. a[Mesh::ARRAY_TEX_UV] = computed_uv;
  275. a[Mesh::ARRAY_INDEX] = computed_indices;
  276. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  277. MeshInstance2D *mesh_instance = memnew(MeshInstance2D);
  278. mesh_instance->set_mesh(mesh);
  279. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  280. ur->create_action(TTR("Convert to MeshInstance2D"));
  281. ur->add_do_method(SceneTreeDock::get_singleton(), "replace_node", node, mesh_instance, true, false);
  282. ur->add_do_reference(mesh_instance);
  283. ur->add_undo_method(SceneTreeDock::get_singleton(), "replace_node", mesh_instance, node, false, false);
  284. ur->add_undo_reference(node);
  285. ur->commit_action();
  286. }
  287. void Sprite2DEditor::_convert_to_polygon_2d_node() {
  288. if (computed_outline_lines.is_empty()) {
  289. err_dialog->set_text(TTR("Invalid geometry, can't create polygon."));
  290. err_dialog->popup_centered();
  291. return;
  292. }
  293. Polygon2D *polygon_2d_instance = memnew(Polygon2D);
  294. int total_point_count = 0;
  295. for (int i = 0; i < computed_outline_lines.size(); i++) {
  296. total_point_count += computed_outline_lines[i].size();
  297. }
  298. PackedVector2Array polygon;
  299. polygon.resize(total_point_count);
  300. Vector2 *polygon_write = polygon.ptrw();
  301. PackedVector2Array uvs;
  302. uvs.resize(total_point_count);
  303. Vector2 *uvs_write = uvs.ptrw();
  304. int current_point_index = 0;
  305. Array polys;
  306. polys.resize(computed_outline_lines.size());
  307. for (int i = 0; i < computed_outline_lines.size(); i++) {
  308. Vector<Vector2> outline = computed_outline_lines[i];
  309. Vector<Vector2> uv_outline = outline_lines[i];
  310. PackedInt32Array pia;
  311. pia.resize(outline.size());
  312. int *pia_write = pia.ptrw();
  313. for (int pi = 0; pi < outline.size(); pi++) {
  314. polygon_write[current_point_index] = outline[pi];
  315. uvs_write[current_point_index] = uv_outline[pi];
  316. pia_write[pi] = current_point_index;
  317. current_point_index++;
  318. }
  319. polys[i] = pia;
  320. }
  321. polygon_2d_instance->set_uv(uvs);
  322. polygon_2d_instance->set_polygon(polygon);
  323. polygon_2d_instance->set_polygons(polys);
  324. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  325. ur->create_action(TTR("Convert to Polygon2D"));
  326. ur->add_do_method(SceneTreeDock::get_singleton(), "replace_node", node, polygon_2d_instance, true, false);
  327. ur->add_do_reference(polygon_2d_instance);
  328. ur->add_undo_method(SceneTreeDock::get_singleton(), "replace_node", polygon_2d_instance, node, false, false);
  329. ur->add_undo_reference(node);
  330. ur->commit_action();
  331. }
  332. void Sprite2DEditor::_create_collision_polygon_2d_node() {
  333. if (computed_outline_lines.is_empty()) {
  334. err_dialog->set_text(TTR("Invalid geometry, can't create collision polygon."));
  335. err_dialog->popup_centered();
  336. return;
  337. }
  338. for (int i = 0; i < computed_outline_lines.size(); i++) {
  339. Vector<Vector2> outline = computed_outline_lines[i];
  340. CollisionPolygon2D *collision_polygon_2d_instance = memnew(CollisionPolygon2D);
  341. collision_polygon_2d_instance->set_polygon(outline);
  342. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  343. ur->create_action(TTR("Create CollisionPolygon2D Sibling"));
  344. ur->add_do_method(this, "_add_as_sibling_or_child", node, collision_polygon_2d_instance);
  345. ur->add_do_reference(collision_polygon_2d_instance);
  346. ur->add_undo_method(node != this->get_tree()->get_edited_scene_root() ? node->get_parent() : this->get_tree()->get_edited_scene_root(), "remove_child", collision_polygon_2d_instance);
  347. ur->commit_action();
  348. }
  349. }
  350. void Sprite2DEditor::_create_light_occluder_2d_node() {
  351. if (computed_outline_lines.is_empty()) {
  352. err_dialog->set_text(TTR("Invalid geometry, can't create light occluder."));
  353. err_dialog->popup_centered();
  354. return;
  355. }
  356. for (int i = 0; i < computed_outline_lines.size(); i++) {
  357. Vector<Vector2> outline = computed_outline_lines[i];
  358. Ref<OccluderPolygon2D> polygon;
  359. polygon.instantiate();
  360. PackedVector2Array a;
  361. a.resize(outline.size());
  362. Vector2 *aw = a.ptrw();
  363. for (int io = 0; io < outline.size(); io++) {
  364. aw[io] = outline[io];
  365. }
  366. polygon->set_polygon(a);
  367. LightOccluder2D *light_occluder_2d_instance = memnew(LightOccluder2D);
  368. light_occluder_2d_instance->set_occluder_polygon(polygon);
  369. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  370. ur->create_action(TTR("Create LightOccluder2D Sibling"));
  371. ur->add_do_method(this, "_add_as_sibling_or_child", node, light_occluder_2d_instance);
  372. ur->add_do_reference(light_occluder_2d_instance);
  373. ur->add_undo_method(node != this->get_tree()->get_edited_scene_root() ? node->get_parent() : this->get_tree()->get_edited_scene_root(), "remove_child", light_occluder_2d_instance);
  374. ur->commit_action();
  375. }
  376. }
  377. void Sprite2DEditor::_add_as_sibling_or_child(Node *p_own_node, Node *p_new_node) {
  378. // Can't make sibling if own node is scene root
  379. if (p_own_node != this->get_tree()->get_edited_scene_root()) {
  380. p_own_node->get_parent()->add_child(p_new_node, true);
  381. Object::cast_to<Node2D>(p_new_node)->set_transform(Object::cast_to<Node2D>(p_own_node)->get_transform());
  382. } else {
  383. p_own_node->add_child(p_new_node, true);
  384. }
  385. p_new_node->set_owner(this->get_tree()->get_edited_scene_root());
  386. }
  387. void Sprite2DEditor::_debug_uv_draw() {
  388. Ref<Texture2D> tex = node->get_texture();
  389. ERR_FAIL_COND(!tex.is_valid());
  390. Point2 draw_pos_offset = Point2(1.0, 1.0);
  391. Size2 draw_size_offset = Size2(2.0, 2.0);
  392. debug_uv->set_clip_contents(true);
  393. debug_uv->draw_texture(tex, draw_pos_offset);
  394. debug_uv->set_custom_minimum_size(tex->get_size() + draw_size_offset);
  395. debug_uv->draw_set_transform(draw_pos_offset, 0, Size2(1.0, 1.0));
  396. Color color = Color(1.0, 0.8, 0.7);
  397. if (selected_menu_item == MENU_OPTION_CONVERT_TO_MESH_2D && uv_lines.size() > 0) {
  398. debug_uv->draw_multiline(uv_lines, color);
  399. } else if ((selected_menu_item == MENU_OPTION_CONVERT_TO_POLYGON_2D || selected_menu_item == MENU_OPTION_CREATE_COLLISION_POLY_2D || selected_menu_item == MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D) && outline_lines.size() > 0) {
  400. for (int i = 0; i < outline_lines.size(); i++) {
  401. Vector<Vector2> outline = outline_lines[i];
  402. debug_uv->draw_polyline(outline, color);
  403. debug_uv->draw_line(outline[0], outline[outline.size() - 1], color);
  404. }
  405. }
  406. }
  407. void Sprite2DEditor::_notification(int p_what) {
  408. switch (p_what) {
  409. case NOTIFICATION_ENTER_TREE:
  410. case NOTIFICATION_THEME_CHANGED: {
  411. options->set_icon(get_theme_icon(SNAME("Sprite2D"), SNAME("EditorIcons")));
  412. options->get_popup()->set_item_icon(MENU_OPTION_CONVERT_TO_MESH_2D, get_theme_icon(SNAME("MeshInstance2D"), SNAME("EditorIcons")));
  413. options->get_popup()->set_item_icon(MENU_OPTION_CONVERT_TO_POLYGON_2D, get_theme_icon(SNAME("Polygon2D"), SNAME("EditorIcons")));
  414. options->get_popup()->set_item_icon(MENU_OPTION_CREATE_COLLISION_POLY_2D, get_theme_icon(SNAME("CollisionPolygon2D"), SNAME("EditorIcons")));
  415. options->get_popup()->set_item_icon(MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D, get_theme_icon(SNAME("LightOccluder2D"), SNAME("EditorIcons")));
  416. } break;
  417. }
  418. }
  419. void Sprite2DEditor::_bind_methods() {
  420. ClassDB::bind_method("_add_as_sibling_or_child", &Sprite2DEditor::_add_as_sibling_or_child);
  421. }
  422. Sprite2DEditor::Sprite2DEditor() {
  423. options = memnew(MenuButton);
  424. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);
  425. options->set_text(TTR("Sprite2D"));
  426. options->get_popup()->add_item(TTR("Convert to MeshInstance2D"), MENU_OPTION_CONVERT_TO_MESH_2D);
  427. options->get_popup()->add_item(TTR("Convert to Polygon2D"), MENU_OPTION_CONVERT_TO_POLYGON_2D);
  428. options->get_popup()->add_item(TTR("Create CollisionPolygon2D Sibling"), MENU_OPTION_CREATE_COLLISION_POLY_2D);
  429. options->get_popup()->add_item(TTR("Create LightOccluder2D Sibling"), MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D);
  430. options->set_switch_on_hover(true);
  431. options->get_popup()->connect("id_pressed", callable_mp(this, &Sprite2DEditor::_menu_option));
  432. err_dialog = memnew(AcceptDialog);
  433. add_child(err_dialog);
  434. debug_uv_dialog = memnew(ConfirmationDialog);
  435. VBoxContainer *vb = memnew(VBoxContainer);
  436. debug_uv_dialog->add_child(vb);
  437. ScrollContainer *scroll = memnew(ScrollContainer);
  438. scroll->set_custom_minimum_size(Size2(800, 500) * EDSCALE);
  439. vb->add_margin_child(TTR("Preview:"), scroll, true);
  440. debug_uv = memnew(Control);
  441. debug_uv->connect("draw", callable_mp(this, &Sprite2DEditor::_debug_uv_draw));
  442. scroll->add_child(debug_uv);
  443. debug_uv_dialog->connect("confirmed", callable_mp(this, &Sprite2DEditor::_create_node));
  444. HBoxContainer *hb = memnew(HBoxContainer);
  445. hb->add_child(memnew(Label(TTR("Simplification:"))));
  446. simplification = memnew(SpinBox);
  447. simplification->set_min(0.01);
  448. simplification->set_max(10.00);
  449. simplification->set_step(0.01);
  450. simplification->set_value(2);
  451. hb->add_child(simplification);
  452. hb->add_spacer();
  453. hb->add_child(memnew(Label(TTR("Shrink (Pixels):"))));
  454. shrink_pixels = memnew(SpinBox);
  455. shrink_pixels->set_min(0);
  456. shrink_pixels->set_max(10);
  457. shrink_pixels->set_step(1);
  458. shrink_pixels->set_value(0);
  459. hb->add_child(shrink_pixels);
  460. hb->add_spacer();
  461. hb->add_child(memnew(Label(TTR("Grow (Pixels):"))));
  462. grow_pixels = memnew(SpinBox);
  463. grow_pixels->set_min(0);
  464. grow_pixels->set_max(10);
  465. grow_pixels->set_step(1);
  466. grow_pixels->set_value(2);
  467. hb->add_child(grow_pixels);
  468. hb->add_spacer();
  469. update_preview = memnew(Button);
  470. update_preview->set_text(TTR("Update Preview"));
  471. update_preview->connect("pressed", callable_mp(this, &Sprite2DEditor::_update_mesh_data));
  472. hb->add_child(update_preview);
  473. vb->add_margin_child(TTR("Settings:"), hb);
  474. add_child(debug_uv_dialog);
  475. }
  476. void Sprite2DEditorPlugin::edit(Object *p_object) {
  477. sprite_editor->edit(Object::cast_to<Sprite2D>(p_object));
  478. }
  479. bool Sprite2DEditorPlugin::handles(Object *p_object) const {
  480. return p_object->is_class("Sprite2D");
  481. }
  482. void Sprite2DEditorPlugin::make_visible(bool p_visible) {
  483. if (p_visible) {
  484. sprite_editor->options->show();
  485. } else {
  486. sprite_editor->options->hide();
  487. sprite_editor->edit(nullptr);
  488. }
  489. }
  490. Sprite2DEditorPlugin::Sprite2DEditorPlugin() {
  491. sprite_editor = memnew(Sprite2DEditor);
  492. EditorNode::get_singleton()->get_main_control()->add_child(sprite_editor);
  493. make_visible(false);
  494. //sprite_editor->options->hide();
  495. }
  496. Sprite2DEditorPlugin::~Sprite2DEditorPlugin() {
  497. }