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