sprite_editor_plugin.cpp 19 KB

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