sprite_editor_plugin.cpp 18 KB

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