sprite_editor_plugin.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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<Texture> 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. if (image->is_compressed()) {
  147. image->decompress();
  148. }
  149. Rect2 rect;
  150. if (node->is_region())
  151. rect = node->get_region_rect();
  152. else
  153. rect.size = Size2(image->get_width(), image->get_height());
  154. Ref<BitMap> bm;
  155. bm.instance();
  156. bm->create_from_image_alpha(image);
  157. int shrink = shrink_pixels->get_value();
  158. if (shrink > 0) {
  159. bm->shrink_mask(shrink, rect);
  160. }
  161. int grow = grow_pixels->get_value();
  162. if (grow > 0) {
  163. bm->grow_mask(grow, rect);
  164. }
  165. float epsilon = simplification->get_value();
  166. Vector<Vector<Vector2> > lines = bm->clip_opaque_to_polygons(rect, epsilon);
  167. uv_lines.clear();
  168. computed_vertices.clear();
  169. computed_uv.clear();
  170. computed_indices.clear();
  171. Size2 img_size = Vector2(image->get_width(), image->get_height());
  172. for (int i = 0; i < lines.size(); i++) {
  173. lines.write[i] = expand(lines[i], rect, epsilon);
  174. }
  175. if (selected_menu_item == MENU_OPTION_CONVERT_TO_MESH_2D) {
  176. for (int j = 0; j < lines.size(); j++) {
  177. int index_ofs = computed_vertices.size();
  178. for (int i = 0; i < lines[j].size(); i++) {
  179. Vector2 vtx = lines[j][i];
  180. computed_uv.push_back(vtx / img_size);
  181. vtx -= rect.position; //offset by rect position
  182. //flip if flipped
  183. if (node->is_flipped_h())
  184. vtx.x = rect.size.x - vtx.x - 1.0;
  185. if (node->is_flipped_v())
  186. vtx.y = rect.size.y - vtx.y - 1.0;
  187. if (node->is_centered())
  188. vtx -= rect.size / 2.0;
  189. computed_vertices.push_back(vtx);
  190. }
  191. Vector<int> poly = Geometry::triangulate_polygon(lines[j]);
  192. for (int i = 0; i < poly.size(); i += 3) {
  193. for (int k = 0; k < 3; k++) {
  194. int idx = i + k;
  195. int idxn = i + (k + 1) % 3;
  196. uv_lines.push_back(lines[j][poly[idx]]);
  197. uv_lines.push_back(lines[j][poly[idxn]]);
  198. computed_indices.push_back(poly[idx] + index_ofs);
  199. }
  200. }
  201. }
  202. }
  203. outline_lines.clear();
  204. computed_outline_lines.clear();
  205. 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) {
  206. outline_lines.resize(lines.size());
  207. computed_outline_lines.resize(lines.size());
  208. for (int pi = 0; pi < lines.size(); pi++) {
  209. Vector<Vector2> ol;
  210. Vector<Vector2> col;
  211. ol.resize(lines[pi].size());
  212. col.resize(lines[pi].size());
  213. for (int i = 0; i < lines[pi].size(); i++) {
  214. Vector2 vtx = lines[pi][i];
  215. ol.write[i] = vtx;
  216. vtx -= rect.position; //offset by rect position
  217. //flip if flipped
  218. if (node->is_flipped_h())
  219. vtx.x = rect.size.x - vtx.x - 1.0;
  220. if (node->is_flipped_v())
  221. vtx.y = rect.size.y - vtx.y - 1.0;
  222. if (node->is_centered())
  223. vtx -= rect.size / 2.0;
  224. col.write[i] = vtx;
  225. }
  226. outline_lines.write[pi] = ol;
  227. computed_outline_lines.write[pi] = col;
  228. }
  229. }
  230. debug_uv->update();
  231. }
  232. void SpriteEditor::_create_node() {
  233. switch (selected_menu_item) {
  234. case MENU_OPTION_CONVERT_TO_MESH_2D: {
  235. _convert_to_mesh_2d_node();
  236. } break;
  237. case MENU_OPTION_CONVERT_TO_POLYGON_2D: {
  238. _convert_to_polygon_2d_node();
  239. } break;
  240. case MENU_OPTION_CREATE_COLLISION_POLY_2D: {
  241. _create_collision_polygon_2d_node();
  242. } break;
  243. case MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D: {
  244. _create_light_occluder_2d_node();
  245. } break;
  246. }
  247. }
  248. void SpriteEditor::_convert_to_mesh_2d_node() {
  249. if (computed_vertices.size() < 3) {
  250. err_dialog->set_text(TTR("Invalid geometry, can't replace by mesh."));
  251. err_dialog->popup_centered_minsize();
  252. return;
  253. }
  254. Ref<ArrayMesh> mesh;
  255. mesh.instance();
  256. Array a;
  257. a.resize(Mesh::ARRAY_MAX);
  258. a[Mesh::ARRAY_VERTEX] = computed_vertices;
  259. a[Mesh::ARRAY_TEX_UV] = computed_uv;
  260. a[Mesh::ARRAY_INDEX] = computed_indices;
  261. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  262. MeshInstance2D *mesh_instance = memnew(MeshInstance2D);
  263. mesh_instance->set_mesh(mesh);
  264. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  265. ur->create_action(TTR("Convert to Mesh2D"));
  266. ur->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock(), "replace_node", node, mesh_instance, true, false);
  267. ur->add_do_reference(mesh_instance);
  268. ur->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock(), "replace_node", mesh_instance, node, false, false);
  269. ur->add_undo_reference(node);
  270. ur->commit_action();
  271. }
  272. void SpriteEditor::_convert_to_polygon_2d_node() {
  273. if (computed_outline_lines.empty()) {
  274. err_dialog->set_text(TTR("Invalid geometry, can't create polygon."));
  275. err_dialog->popup_centered_minsize();
  276. return;
  277. }
  278. Polygon2D *polygon_2d_instance = memnew(Polygon2D);
  279. int total_point_count = 0;
  280. for (int i = 0; i < computed_outline_lines.size(); i++)
  281. total_point_count += computed_outline_lines[i].size();
  282. PoolVector2Array polygon;
  283. polygon.resize(total_point_count);
  284. PoolVector2Array::Write polygon_write = polygon.write();
  285. PoolVector2Array uvs;
  286. uvs.resize(total_point_count);
  287. PoolVector2Array::Write uvs_write = uvs.write();
  288. int current_point_index = 0;
  289. Array polys;
  290. polys.resize(computed_outline_lines.size());
  291. for (int i = 0; i < computed_outline_lines.size(); i++) {
  292. Vector<Vector2> outline = computed_outline_lines[i];
  293. Vector<Vector2> uv_outline = outline_lines[i];
  294. PoolIntArray pia;
  295. pia.resize(outline.size());
  296. PoolIntArray::Write pia_write = pia.write();
  297. for (int pi = 0; pi < outline.size(); pi++) {
  298. polygon_write[current_point_index] = outline[pi];
  299. uvs_write[current_point_index] = uv_outline[pi];
  300. pia_write[pi] = current_point_index;
  301. current_point_index++;
  302. }
  303. polys[i] = pia;
  304. }
  305. polygon_2d_instance->set_uv(uvs);
  306. polygon_2d_instance->set_polygon(polygon);
  307. polygon_2d_instance->set_polygons(polys);
  308. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  309. ur->create_action(TTR("Convert to Polygon2D"));
  310. ur->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock(), "replace_node", node, polygon_2d_instance, true, false);
  311. ur->add_do_reference(polygon_2d_instance);
  312. ur->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock(), "replace_node", polygon_2d_instance, node, false, false);
  313. ur->add_undo_reference(node);
  314. ur->commit_action();
  315. }
  316. void SpriteEditor::_create_collision_polygon_2d_node() {
  317. if (computed_outline_lines.empty()) {
  318. err_dialog->set_text(TTR("Invalid geometry, can't create collision polygon."));
  319. err_dialog->popup_centered_minsize();
  320. return;
  321. }
  322. for (int i = 0; i < computed_outline_lines.size(); i++) {
  323. Vector<Vector2> outline = computed_outline_lines[i];
  324. CollisionPolygon2D *collision_polygon_2d_instance = memnew(CollisionPolygon2D);
  325. collision_polygon_2d_instance->set_polygon(outline);
  326. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  327. ur->create_action(TTR("Create CollisionPolygon2D Sibling"));
  328. ur->add_do_method(this, "_add_as_sibling_or_child", node, collision_polygon_2d_instance);
  329. ur->add_do_reference(collision_polygon_2d_instance);
  330. 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);
  331. ur->commit_action();
  332. }
  333. }
  334. void SpriteEditor::_create_light_occluder_2d_node() {
  335. if (computed_outline_lines.empty()) {
  336. err_dialog->set_text(TTR("Invalid geometry, can't create light occluder."));
  337. err_dialog->popup_centered_minsize();
  338. return;
  339. }
  340. for (int i = 0; i < computed_outline_lines.size(); i++) {
  341. Vector<Vector2> outline = computed_outline_lines[i];
  342. Ref<OccluderPolygon2D> polygon;
  343. polygon.instance();
  344. PoolVector2Array a;
  345. a.resize(outline.size());
  346. PoolVector2Array::Write aw = a.write();
  347. for (int io = 0; io < outline.size(); io++) {
  348. aw[io] = outline[io];
  349. }
  350. polygon->set_polygon(a);
  351. LightOccluder2D *light_occluder_2d_instance = memnew(LightOccluder2D);
  352. light_occluder_2d_instance->set_occluder_polygon(polygon);
  353. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  354. ur->create_action(TTR("Create LightOccluder2D Sibling"));
  355. ur->add_do_method(this, "_add_as_sibling_or_child", node, light_occluder_2d_instance);
  356. ur->add_do_reference(light_occluder_2d_instance);
  357. 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);
  358. ur->commit_action();
  359. }
  360. }
  361. void SpriteEditor::_add_as_sibling_or_child(Node *p_own_node, Node *p_new_node) {
  362. // Can't make sibling if own node is scene root
  363. if (p_own_node != this->get_tree()->get_edited_scene_root()) {
  364. p_own_node->get_parent()->add_child(p_new_node, true);
  365. Object::cast_to<Node2D>(p_new_node)->set_transform(Object::cast_to<Node2D>(p_own_node)->get_transform());
  366. } else {
  367. p_own_node->add_child(p_new_node, true);
  368. }
  369. p_new_node->set_owner(this->get_tree()->get_edited_scene_root());
  370. }
  371. void SpriteEditor::_debug_uv_draw() {
  372. Ref<Texture> tex = node->get_texture();
  373. ERR_FAIL_COND(!tex.is_valid());
  374. Point2 draw_pos_offset = Point2(1.0, 1.0);
  375. Size2 draw_size_offset = Size2(2.0, 2.0);
  376. debug_uv->set_clip_contents(true);
  377. debug_uv->draw_texture(tex, draw_pos_offset);
  378. debug_uv->set_custom_minimum_size(tex->get_size() + draw_size_offset);
  379. debug_uv->draw_set_transform(draw_pos_offset, 0, Size2(1.0, 1.0));
  380. Color color = Color(1.0, 0.8, 0.7);
  381. if (selected_menu_item == MENU_OPTION_CONVERT_TO_MESH_2D && uv_lines.size() > 0) {
  382. debug_uv->draw_multiline(uv_lines, color);
  383. } 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) {
  384. for (int i = 0; i < outline_lines.size(); i++) {
  385. Vector<Vector2> outline = outline_lines[i];
  386. debug_uv->draw_polyline(outline, color);
  387. debug_uv->draw_line(outline[0], outline[outline.size() - 1], color);
  388. }
  389. }
  390. }
  391. void SpriteEditor::_bind_methods() {
  392. ClassDB::bind_method("_menu_option", &SpriteEditor::_menu_option);
  393. ClassDB::bind_method("_debug_uv_draw", &SpriteEditor::_debug_uv_draw);
  394. ClassDB::bind_method("_update_mesh_data", &SpriteEditor::_update_mesh_data);
  395. ClassDB::bind_method("_create_node", &SpriteEditor::_create_node);
  396. ClassDB::bind_method("_add_as_sibling_or_child", &SpriteEditor::_add_as_sibling_or_child);
  397. }
  398. SpriteEditor::SpriteEditor() {
  399. options = memnew(MenuButton);
  400. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);
  401. options->set_text(TTR("Sprite"));
  402. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Sprite", "EditorIcons"));
  403. options->get_popup()->add_item(TTR("Convert to Mesh2D"), MENU_OPTION_CONVERT_TO_MESH_2D);
  404. options->get_popup()->add_item(TTR("Convert to Polygon2D"), MENU_OPTION_CONVERT_TO_POLYGON_2D);
  405. options->get_popup()->add_item(TTR("Create CollisionPolygon2D Sibling"), MENU_OPTION_CREATE_COLLISION_POLY_2D);
  406. options->get_popup()->add_item(TTR("Create LightOccluder2D Sibling"), MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D);
  407. options->set_switch_on_hover(true);
  408. options->get_popup()->connect("id_pressed", this, "_menu_option");
  409. err_dialog = memnew(AcceptDialog);
  410. add_child(err_dialog);
  411. debug_uv_dialog = memnew(ConfirmationDialog);
  412. debug_uv_dialog->get_ok()->set_text(TTR("Create Mesh2D"));
  413. debug_uv_dialog->set_title("Mesh 2D Preview");
  414. VBoxContainer *vb = memnew(VBoxContainer);
  415. debug_uv_dialog->add_child(vb);
  416. ScrollContainer *scroll = memnew(ScrollContainer);
  417. scroll->set_custom_minimum_size(Size2(800, 500) * EDSCALE);
  418. scroll->set_enable_h_scroll(true);
  419. scroll->set_enable_v_scroll(true);
  420. vb->add_margin_child(TTR("Preview:"), scroll, true);
  421. debug_uv = memnew(Control);
  422. debug_uv->connect("draw", this, "_debug_uv_draw");
  423. scroll->add_child(debug_uv);
  424. debug_uv_dialog->connect("confirmed", this, "_create_node");
  425. HBoxContainer *hb = memnew(HBoxContainer);
  426. hb->add_child(memnew(Label(TTR("Simplification: "))));
  427. simplification = memnew(SpinBox);
  428. simplification->set_min(0.01);
  429. simplification->set_max(10.00);
  430. simplification->set_step(0.01);
  431. simplification->set_value(2);
  432. hb->add_child(simplification);
  433. hb->add_spacer();
  434. hb->add_child(memnew(Label(TTR("Shrink (Pixels): "))));
  435. shrink_pixels = memnew(SpinBox);
  436. shrink_pixels->set_min(0);
  437. shrink_pixels->set_max(10);
  438. shrink_pixels->set_step(1);
  439. shrink_pixels->set_value(0);
  440. hb->add_child(shrink_pixels);
  441. hb->add_spacer();
  442. hb->add_child(memnew(Label(TTR("Grow (Pixels): "))));
  443. grow_pixels = memnew(SpinBox);
  444. grow_pixels->set_min(0);
  445. grow_pixels->set_max(10);
  446. grow_pixels->set_step(1);
  447. grow_pixels->set_value(2);
  448. hb->add_child(grow_pixels);
  449. hb->add_spacer();
  450. update_preview = memnew(Button);
  451. update_preview->set_text(TTR("Update Preview"));
  452. update_preview->connect("pressed", this, "_update_mesh_data");
  453. hb->add_child(update_preview);
  454. vb->add_margin_child(TTR("Settings:"), hb);
  455. add_child(debug_uv_dialog);
  456. }
  457. void SpriteEditorPlugin::edit(Object *p_object) {
  458. sprite_editor->edit(Object::cast_to<Sprite>(p_object));
  459. }
  460. bool SpriteEditorPlugin::handles(Object *p_object) const {
  461. return p_object->is_class("Sprite");
  462. }
  463. void SpriteEditorPlugin::make_visible(bool p_visible) {
  464. if (p_visible) {
  465. sprite_editor->options->show();
  466. } else {
  467. sprite_editor->options->hide();
  468. sprite_editor->edit(NULL);
  469. }
  470. }
  471. SpriteEditorPlugin::SpriteEditorPlugin(EditorNode *p_node) {
  472. editor = p_node;
  473. sprite_editor = memnew(SpriteEditor);
  474. editor->get_viewport()->add_child(sprite_editor);
  475. make_visible(false);
  476. //sprite_editor->options->hide();
  477. }
  478. SpriteEditorPlugin::~SpriteEditorPlugin() {
  479. }