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