| 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 "core/math/geometry_2d.h" |
| 34 | #include "editor/docks/scene_tree_dock.h" |
| 35 | #include "editor/editor_node.h" |
| 36 | #include "editor/editor_undo_redo_manager.h" |
| 37 | #include "editor/gui/editor_zoom_widget.h" |
| 38 | #include "editor/scene/canvas_item_editor_plugin.h" |
| 39 | #include "editor/settings/editor_settings.h" |
| 40 | #include "editor/themes/editor_scale.h" |
| 41 | #include "scene/2d/light_occluder_2d.h" |
| 42 | #include "scene/2d/mesh_instance_2d.h" |
| 43 | #include "scene/2d/physics/collision_polygon_2d.h" |
| 44 | #include "scene/2d/polygon_2d.h" |
| 45 | #include "scene/gui/box_container.h" |
| 46 | #include "scene/gui/menu_button.h" |
| 47 | #include "scene/gui/panel.h" |
| 48 | #include "scene/gui/view_panner.h" |
| 49 | #include "scene/resources/mesh.h" |
| 50 | #include "thirdparty/clipper2/include/clipper2/clipper.h" |
| 51 | |
| 52 | #define PRECISION 1 |
| 53 | |
| 54 | void Sprite2DEditor::_node_removed(Node *p_node) { |
| 55 | if (p_node == node) { |
| 56 | node = nullptr; |
| 57 | options->hide(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | Vector<Vector2> expand(const Vector<Vector2> &points, const Rect2i &rect, float epsilon = 2.0) { |
| 62 | int size = points.size(); |
| 63 | ERR_FAIL_COND_V(size < 2, Vector<Vector2>()); |
| 64 | |
| 65 | Clipper2Lib::PathD subj(points.size()); |
| 66 | for (int i = 0; i < points.size(); i++) { |
| 67 | subj[i] = Clipper2Lib::PointD(points[i].x, points[i].y); |
| 68 | } |
| 69 | |
| 70 | Clipper2Lib::PathsD solution = Clipper2Lib::InflatePaths({ subj }, epsilon, Clipper2Lib::JoinType::Miter, Clipper2Lib::EndType::Polygon, 2.0, PRECISION, 0.0); |
| 71 | // Here the miter_limit = 2.0 and arc_tolerance = 0.0 are Clipper2 defaults, |
| 72 | // and PRECISION is used to scale points up internally, to attain the desired precision. |
| 73 | |
| 74 | ERR_FAIL_COND_V(solution.size() == 0, points); |
| 75 | |
| 76 | // Clamp into the specified rect. |
| 77 | Clipper2Lib::RectD clamp(rect.position.x, |
| 78 | rect.position.y, |
| 79 | rect.position.x + rect.size.width, |
| 80 | rect.position.y + rect.size.height); |
| 81 | Clipper2Lib::PathsD out = Clipper2Lib::RectClip(clamp, solution[0], PRECISION); |
| 82 | // Here PRECISION is used to scale points up internally, to attain the desired precision. |
| 83 | |
| 84 | ERR_FAIL_COND_V(out.size() == 0, points); |
| 85 | |
| 86 | const Clipper2Lib::PathD &p2 = out[0]; |
| 87 | |
| 88 | Vector<Vector2> outPoints; |
| 89 | |
| 90 | int lasti = p2.size() - 1; |
| 91 | Vector2 prev = Vector2(p2[lasti].x, p2[lasti].y); |
| 92 | for (uint64_t i = 0; i < p2.size(); i++) { |
| 93 | Vector2 cur = Vector2(p2[i].x, p2[i].y); |
| 94 | if (cur.distance_to(prev) > 0.5) { |
| 95 | outPoints.push_back(cur); |
| 96 | prev = cur; |
| 97 | } |
| 98 | } |
| 99 | return outPoints; |
| 100 | } |
| 101 | |
| 102 | void Sprite2DEditor::_menu_option(int p_option) { |
| 103 | if (!node) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | selected_menu_item = (Menu)p_option; |
| 108 | |
| 109 | switch (p_option) { |
| 110 | case MENU_OPTION_CONVERT_TO_MESH_2D: { |
| 111 | debug_uv_dialog->set_ok_button_text(TTR("Create MeshInstance2D")); |
| 112 | debug_uv_dialog->set_title(TTR("MeshInstance2D Preview")); |
| 113 | |
| 114 | _popup_debug_uv_dialog(); |
| 115 | } break; |
| 116 | case MENU_OPTION_CONVERT_TO_POLYGON_2D: { |
| 117 | debug_uv_dialog->set_ok_button_text(TTR("Create Polygon2D")); |
| 118 | debug_uv_dialog->set_title(TTR("Polygon2D Preview")); |
| 119 | |
| 120 | _popup_debug_uv_dialog(); |
| 121 | } break; |
| 122 | case MENU_OPTION_CREATE_COLLISION_POLY_2D: { |
| 123 | debug_uv_dialog->set_ok_button_text(TTR("Create CollisionPolygon2D")); |
| 124 | debug_uv_dialog->set_title(TTR("CollisionPolygon2D Preview")); |
| 125 | |
| 126 | _popup_debug_uv_dialog(); |
| 127 | } break; |
| 128 | case MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D: { |
| 129 | debug_uv_dialog->set_ok_button_text(TTR("Create LightOccluder2D")); |
| 130 | debug_uv_dialog->set_title(TTR("LightOccluder2D Preview")); |
| 131 | |
| 132 | _popup_debug_uv_dialog(); |
| 133 | } break; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void Sprite2DEditor::_popup_debug_uv_dialog() { |
| 138 | String error_message; |
| 139 | if (node->get_owner() != get_tree()->get_edited_scene_root() && node != get_tree()->get_edited_scene_root()) { |
| 140 | error_message = TTR("Can't convert a sprite from a foreign scene."); |
| 141 | } |
| 142 | Ref<Texture2D> texture = node->get_texture(); |
| 143 | if (texture.is_null()) { |
| 144 | error_message = TTR("Can't convert an empty sprite to mesh."); |
| 145 | } |
| 146 | |
| 147 | if (!error_message.is_empty()) { |
| 148 | err_dialog->set_text(error_message); |
| 149 | err_dialog->popup_centered(); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | _update_mesh_data(); |
| 154 | debug_uv_dialog->popup_centered(); |
| 155 | get_tree()->connect("process_frame", callable_mp(this, &Sprite2DEditor::_center_view), CONNECT_ONE_SHOT); |
| 156 | debug_uv->set_texture_filter(node->get_texture_filter_in_tree()); |
| 157 | debug_uv->queue_redraw(); |
| 158 | } |
| 159 | |
| 160 | void Sprite2DEditor::_update_mesh_data() { |
| 161 | ERR_FAIL_NULL(node); |
| 162 | Ref<Texture2D> texture = node->get_texture(); |
| 163 | ERR_FAIL_COND(texture.is_null()); |
| 164 | Ref<Image> image = texture->get_image(); |
| 165 | ERR_FAIL_COND(image.is_null()); |
| 166 | |
| 167 | if (image->is_compressed()) { |
| 168 | image->decompress(); |
| 169 | } |
| 170 | |
| 171 | Rect2 rect = node->is_region_enabled() ? node->get_region_rect() : Rect2(Point2(), image->get_size()); |
| 172 | rect.size /= Vector2(node->get_hframes(), node->get_vframes()); |
| 173 | rect.position += node->get_frame_coords() * rect.size; |
| 174 | |
| 175 | Ref<BitMap> bm; |
| 176 | bm.instantiate(); |
| 177 | bm->create_from_image_alpha(image); |
| 178 | |
| 179 | int shrink = shrink_pixels->get_value(); |
| 180 | if (shrink > 0) { |
| 181 | bm->shrink_mask(shrink, rect); |
| 182 | } |
| 183 | |
| 184 | int grow = grow_pixels->get_value(); |
| 185 | if (grow > 0) { |
| 186 | bm->grow_mask(grow, rect); |
| 187 | } |
| 188 | |
| 189 | float epsilon = simplification->get_value(); |
| 190 | |
| 191 | Vector<Vector<Vector2>> lines = bm->clip_opaque_to_polygons(rect, epsilon); |
| 192 | |
| 193 | uv_lines.clear(); |
| 194 | |
| 195 | computed_vertices.clear(); |
| 196 | computed_uv.clear(); |
| 197 | computed_indices.clear(); |
| 198 | |
| 199 | Size2 img_size = image->get_size(); |
| 200 | for (int i = 0; i < lines.size(); i++) { |
| 201 | lines.write[i] = expand(lines[i], rect, epsilon); |
| 202 | } |
| 203 | |
| 204 | if (selected_menu_item == MENU_OPTION_CONVERT_TO_MESH_2D) { |
| 205 | for (int j = 0; j < lines.size(); j++) { |
| 206 | int index_ofs = computed_vertices.size(); |
| 207 | |
| 208 | for (int i = 0; i < lines[j].size(); i++) { |
| 209 | Vector2 vtx = lines[j][i]; |
| 210 | computed_uv.push_back((vtx + rect.position) / img_size); |
| 211 | |
| 212 | if (node->is_flipped_h()) { |
| 213 | vtx.x = rect.size.x - vtx.x; |
| 214 | } |
| 215 | if (node->is_flipped_v()) { |
| 216 | vtx.y = rect.size.y - vtx.y; |
| 217 | } |
| 218 | vtx += node->get_offset(); |
| 219 | if (node->is_centered()) { |
| 220 | vtx -= rect.size / 2.0; |
| 221 | } |
| 222 | |
| 223 | computed_vertices.push_back(vtx); |
| 224 | } |
| 225 | |
| 226 | Vector<int> poly = Geometry2D::triangulate_polygon(lines[j]); |
| 227 | |
| 228 | for (int i = 0; i < poly.size(); i += 3) { |
| 229 | for (int k = 0; k < 3; k++) { |
| 230 | int idx = i + k; |
| 231 | int idxn = i + (k + 1) % 3; |
| 232 | uv_lines.push_back(lines[j][poly[idx]] + rect.position); |
| 233 | uv_lines.push_back(lines[j][poly[idxn]] + rect.position); |
| 234 | |
| 235 | computed_indices.push_back(poly[idx] + index_ofs); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | outline_lines.clear(); |
| 242 | computed_outline_lines.clear(); |
| 243 | |
| 244 | 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) { |
| 245 | outline_lines.resize(lines.size()); |
| 246 | computed_outline_lines.resize(lines.size()); |
| 247 | for (int pi = 0; pi < lines.size(); pi++) { |
| 248 | Vector<Vector2> ol; |
| 249 | Vector<Vector2> col; |
| 250 | |
| 251 | ol.resize(lines[pi].size()); |
| 252 | col.resize(lines[pi].size()); |
| 253 | |
| 254 | for (int i = 0; i < lines[pi].size(); i++) { |
| 255 | Vector2 vtx = lines[pi][i]; |
| 256 | ol.write[i] = vtx + rect.position; |
| 257 | |
| 258 | if (node->is_flipped_h()) { |
| 259 | vtx.x = rect.size.x - vtx.x; |
| 260 | } |
| 261 | if (node->is_flipped_v()) { |
| 262 | vtx.y = rect.size.y - vtx.y; |
| 263 | } |
| 264 | // Don't bake offset to Polygon2D which has offset property. |
| 265 | if (selected_menu_item != MENU_OPTION_CONVERT_TO_POLYGON_2D) { |
| 266 | vtx += node->get_offset(); |
| 267 | } |
| 268 | if (node->is_centered()) { |
| 269 | vtx -= rect.size / 2.0; |
| 270 | } |
| 271 | |
| 272 | col.write[i] = vtx; |
| 273 | } |
| 274 | |
| 275 | outline_lines.write[pi] = ol; |
| 276 | computed_outline_lines.write[pi] = col; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | debug_uv->queue_redraw(); |
| 281 | } |
| 282 | |
| 283 | void Sprite2DEditor::_create_node() { |
| 284 | switch (selected_menu_item) { |
| 285 | case MENU_OPTION_CONVERT_TO_MESH_2D: { |
| 286 | _convert_to_mesh_2d_node(); |
| 287 | } break; |
| 288 | case MENU_OPTION_CONVERT_TO_POLYGON_2D: { |
| 289 | _convert_to_polygon_2d_node(); |
| 290 | } break; |
| 291 | case MENU_OPTION_CREATE_COLLISION_POLY_2D: { |
| 292 | _create_collision_polygon_2d_node(); |
| 293 | } break; |
| 294 | case MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D: { |
| 295 | _create_light_occluder_2d_node(); |
| 296 | } break; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void Sprite2DEditor::_convert_to_mesh_2d_node() { |
| 301 | if (computed_vertices.size() < 3) { |
| 302 | err_dialog->set_text(TTR("Invalid geometry, can't replace by mesh.")); |
| 303 | err_dialog->popup_centered(); |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | Ref<ArrayMesh> mesh; |
| 308 | mesh.instantiate(); |
| 309 | |
| 310 | Array a; |
| 311 | a.resize(Mesh::ARRAY_MAX); |
| 312 | a[Mesh::ARRAY_VERTEX] = computed_vertices; |
| 313 | a[Mesh::ARRAY_TEX_UV] = computed_uv; |
| 314 | a[Mesh::ARRAY_INDEX] = computed_indices; |
| 315 | |
| 316 | mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES); |
| 317 | |
| 318 | MeshInstance2D *mesh_instance = memnew(MeshInstance2D); |
| 319 | mesh_instance->set_mesh(mesh); |
| 320 | |
| 321 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 322 | ur->create_action(TTR("Convert to MeshInstance2D"), UndoRedo::MERGE_DISABLE, node); |
| 323 | SceneTreeDock::get_singleton()->replace_node(node, mesh_instance); |
| 324 | ur->commit_action(false); |
| 325 | } |
| 326 | |
| 327 | void Sprite2DEditor::_convert_to_polygon_2d_node() { |
| 328 | if (computed_outline_lines.is_empty()) { |
| 329 | err_dialog->set_text(TTR("Invalid geometry, can't create polygon.")); |
| 330 | err_dialog->popup_centered(); |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | Polygon2D *polygon_2d_instance = memnew(Polygon2D); |
| 335 | |
| 336 | int total_point_count = 0; |
| 337 | for (int i = 0; i < computed_outline_lines.size(); i++) { |
| 338 | total_point_count += computed_outline_lines[i].size(); |
| 339 | } |
| 340 | |
| 341 | PackedVector2Array polygon; |
| 342 | polygon.resize(total_point_count); |
| 343 | Vector2 *polygon_write = polygon.ptrw(); |
| 344 | |
| 345 | PackedVector2Array uvs; |
| 346 | uvs.resize(total_point_count); |
| 347 | Vector2 *uvs_write = uvs.ptrw(); |
| 348 | |
| 349 | int current_point_index = 0; |
| 350 | |
| 351 | Array polys; |
| 352 | polys.resize(computed_outline_lines.size()); |
| 353 | |
| 354 | for (int i = 0; i < computed_outline_lines.size(); i++) { |
| 355 | Vector<Vector2> outline = computed_outline_lines[i]; |
| 356 | Vector<Vector2> uv_outline = outline_lines[i]; |
| 357 | |
| 358 | PackedInt32Array pia; |
| 359 | pia.resize(outline.size()); |
| 360 | int *pia_write = pia.ptrw(); |
| 361 | |
| 362 | for (int pi = 0; pi < outline.size(); pi++) { |
| 363 | polygon_write[current_point_index] = outline[pi]; |
| 364 | uvs_write[current_point_index] = uv_outline[pi]; |
| 365 | pia_write[pi] = current_point_index; |
| 366 | current_point_index++; |
| 367 | } |
| 368 | |
| 369 | polys[i] = pia; |
| 370 | } |
| 371 | |
| 372 | polygon_2d_instance->set_uv(uvs); |
| 373 | polygon_2d_instance->set_polygon(polygon); |
| 374 | polygon_2d_instance->set_polygons(polys); |
| 375 | |
| 376 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 377 | ur->create_action(TTR("Convert to Polygon2D"), UndoRedo::MERGE_DISABLE, node); |
| 378 | SceneTreeDock::get_singleton()->replace_node(node, polygon_2d_instance); |
| 379 | ur->commit_action(false); |
| 380 | } |
| 381 | |
| 382 | void Sprite2DEditor::_create_collision_polygon_2d_node() { |
| 383 | if (computed_outline_lines.is_empty()) { |
| 384 | err_dialog->set_text(TTR("Invalid geometry, can't create collision polygon.")); |
| 385 | err_dialog->popup_centered(); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | for (int i = 0; i < computed_outline_lines.size(); i++) { |
| 390 | Vector<Vector2> outline = computed_outline_lines[i]; |
| 391 | |
| 392 | CollisionPolygon2D *collision_polygon_2d_instance = memnew(CollisionPolygon2D); |
| 393 | collision_polygon_2d_instance->set_polygon(outline); |
| 394 | |
| 395 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 396 | ur->create_action(TTR("Create CollisionPolygon2D Sibling"), UndoRedo::MERGE_DISABLE, node); |
| 397 | ur->add_do_method(this, "_add_as_sibling_or_child", node, collision_polygon_2d_instance); |
| 398 | ur->add_do_reference(collision_polygon_2d_instance); |
| 399 | ur->add_undo_method(node != get_tree()->get_edited_scene_root() ? node->get_parent() : get_tree()->get_edited_scene_root(), "remove_child", collision_polygon_2d_instance); |
| 400 | ur->commit_action(); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | void Sprite2DEditor::_create_light_occluder_2d_node() { |
| 405 | if (computed_outline_lines.is_empty()) { |
| 406 | err_dialog->set_text(TTR("Invalid geometry, can't create light occluder.")); |
| 407 | err_dialog->popup_centered(); |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | for (int i = 0; i < computed_outline_lines.size(); i++) { |
| 412 | Vector<Vector2> outline = computed_outline_lines[i]; |
| 413 | |
| 414 | Ref<OccluderPolygon2D> polygon; |
| 415 | polygon.instantiate(); |
| 416 | |
| 417 | PackedVector2Array a; |
| 418 | a.resize(outline.size()); |
| 419 | Vector2 *aw = a.ptrw(); |
| 420 | for (int io = 0; io < outline.size(); io++) { |
| 421 | aw[io] = outline[io]; |
| 422 | } |
| 423 | polygon->set_polygon(a); |
| 424 | |
| 425 | LightOccluder2D *light_occluder_2d_instance = memnew(LightOccluder2D); |
| 426 | light_occluder_2d_instance->set_occluder_polygon(polygon); |
| 427 | |
| 428 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 429 | ur->create_action(TTR("Create LightOccluder2D Sibling"), UndoRedo::MERGE_DISABLE, node); |
| 430 | ur->add_do_method(this, "_add_as_sibling_or_child", node, light_occluder_2d_instance); |
| 431 | ur->add_do_reference(light_occluder_2d_instance); |
| 432 | ur->add_undo_method(node != get_tree()->get_edited_scene_root() ? node->get_parent() : get_tree()->get_edited_scene_root(), "remove_child", light_occluder_2d_instance); |
| 433 | ur->commit_action(); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | void Sprite2DEditor::_add_as_sibling_or_child(Node *p_own_node, Node *p_new_node) { |
| 438 | // Can't make sibling if own node is scene root |
| 439 | if (p_own_node != get_tree()->get_edited_scene_root()) { |
| 440 | p_own_node->get_parent()->add_child(p_new_node, true); |
| 441 | Object::cast_to<Node2D>(p_new_node)->set_transform(Object::cast_to<Node2D>(p_own_node)->get_transform()); |
| 442 | } else { |
| 443 | p_own_node->add_child(p_new_node, true); |
| 444 | } |
| 445 | |
| 446 | p_new_node->set_owner(get_tree()->get_edited_scene_root()); |
| 447 | } |
| 448 | |
| 449 | void Sprite2DEditor::_sync_sprite_resize_mode() { |
| 450 | if (node != nullptr) { |
| 451 | node->_editor_set_dragging_to_resize_rect(resize_region_rect->is_pressed()); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | void Sprite2DEditor::_update_sprite_resize_mode_button() { |
| 456 | if (node == nullptr) { |
| 457 | return; |
| 458 | } |
| 459 | resize_region_rect->set_disabled(!node->is_region_enabled()); |
| 460 | resize_region_rect->set_pressed(node->_editor_is_dragging_to_resiz_rect()); |
| 461 | resize_region_rect->set_tooltip_text(node->is_region_enabled() ? "" : TTRC("Sprite's region needs to be enabled in the inspector.")); |
| 462 | } |
| 463 | |
| 464 | void Sprite2DEditor::_debug_uv_input(const Ref<InputEvent> &p_input) { |
| 465 | if (panner->gui_input(p_input, debug_uv->get_global_rect())) { |
| 466 | accept_event(); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | void Sprite2DEditor::_debug_uv_draw() { |
| 471 | debug_uv->draw_set_transform(-draw_offset * draw_zoom, 0, Vector2(draw_zoom, draw_zoom)); |
| 472 | |
| 473 | Ref<Texture2D> tex = node->get_texture(); |
| 474 | ERR_FAIL_COND(tex.is_null()); |
| 475 | |
| 476 | debug_uv->draw_texture(tex, Point2()); |
| 477 | |
| 478 | Color color = Color(1.0, 0.8, 0.7); |
| 479 | |
| 480 | if (selected_menu_item == MENU_OPTION_CONVERT_TO_MESH_2D && uv_lines.size() > 0) { |
| 481 | debug_uv->draw_multiline(uv_lines, color); |
| 482 | |
| 483 | } 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) { |
| 484 | for (int i = 0; i < outline_lines.size(); i++) { |
| 485 | Vector<Vector2> outline = outline_lines[i]; |
| 486 | |
| 487 | debug_uv->draw_polyline(outline, color); |
| 488 | debug_uv->draw_line(outline[0], outline[outline.size() - 1], color); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | void Sprite2DEditor::_center_view() { |
| 494 | Ref<Texture2D> tex = node->get_texture(); |
| 495 | ERR_FAIL_COND(tex.is_null()); |
| 496 | Vector2 zoom_factor = (debug_uv->get_size() - Vector2(1, 1) * 50 * EDSCALE) / tex->get_size(); |
| 497 | zoom_widget->set_zoom(MIN(zoom_factor.x, zoom_factor.y)); |
| 498 | // Recalculate scroll limits. |
| 499 | _update_zoom_and_pan(false); |
| 500 | |
| 501 | Vector2 offset = (tex->get_size() - debug_uv->get_size() / zoom_widget->get_zoom()) / 2; |
| 502 | h_scroll->set_value_no_signal(offset.x); |
| 503 | v_scroll->set_value_no_signal(offset.y); |
| 504 | _update_zoom_and_pan(false); |
| 505 | } |
| 506 | |
| 507 | void Sprite2DEditor::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) { |
| 508 | h_scroll->set_value_no_signal(h_scroll->get_value() - p_scroll_vec.x / draw_zoom); |
| 509 | v_scroll->set_value_no_signal(v_scroll->get_value() - p_scroll_vec.y / draw_zoom); |
| 510 | _update_zoom_and_pan(false); |
| 511 | } |
| 512 | |
| 513 | void Sprite2DEditor::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) { |
| 514 | const real_t prev_zoom = draw_zoom; |
| 515 | zoom_widget->set_zoom(draw_zoom * p_zoom_factor); |
| 516 | draw_offset += p_origin / prev_zoom - p_origin / zoom_widget->get_zoom(); |
| 517 | h_scroll->set_value_no_signal(draw_offset.x); |
| 518 | v_scroll->set_value_no_signal(draw_offset.y); |
| 519 | _update_zoom_and_pan(false); |
| 520 | } |
| 521 | |
| 522 | void Sprite2DEditor::_update_zoom_and_pan(bool p_zoom_at_center) { |
| 523 | real_t previous_zoom = draw_zoom; |
| 524 | draw_zoom = zoom_widget->get_zoom(); |
| 525 | draw_offset = Vector2(h_scroll->get_value(), v_scroll->get_value()); |
| 526 | if (p_zoom_at_center) { |
| 527 | Vector2 center = debug_uv->get_size() / 2; |
| 528 | draw_offset += center / previous_zoom - center / draw_zoom; |
| 529 | } |
| 530 | |
| 531 | Ref<Texture2D> tex = node->get_texture(); |
| 532 | ERR_FAIL_COND(tex.is_null()); |
| 533 | |
| 534 | Point2 min_corner; |
| 535 | Point2 max_corner = tex->get_size(); |
| 536 | Size2 page_size = debug_uv->get_size() / draw_zoom; |
| 537 | Vector2 margin = Vector2(50, 50) * EDSCALE / draw_zoom; |
| 538 | min_corner -= page_size - margin; |
| 539 | max_corner += page_size - margin; |
| 540 | |
| 541 | h_scroll->set_block_signals(true); |
| 542 | h_scroll->set_min(min_corner.x); |
| 543 | h_scroll->set_max(max_corner.x); |
| 544 | h_scroll->set_page(page_size.x); |
| 545 | h_scroll->set_value(draw_offset.x); |
| 546 | h_scroll->set_block_signals(false); |
| 547 | |
| 548 | v_scroll->set_block_signals(true); |
| 549 | v_scroll->set_min(min_corner.y); |
| 550 | v_scroll->set_max(max_corner.y); |
| 551 | v_scroll->set_page(page_size.y); |
| 552 | v_scroll->set_value(draw_offset.y); |
| 553 | v_scroll->set_block_signals(false); |
| 554 | |
| 555 | debug_uv->queue_redraw(); |
| 556 | } |
| 557 | |
| 558 | void Sprite2DEditor::_notification(int p_what) { |
| 559 | switch (p_what) { |
| 560 | case NOTIFICATION_READY: { |
| 561 | v_scroll->set_anchors_and_offsets_preset(Control::PRESET_RIGHT_WIDE); |
| 562 | h_scroll->set_anchors_and_offsets_preset(Control::PRESET_BOTTOM_WIDE); |
| 563 | // Avoid scrollbar overlapping. |
| 564 | Size2 hmin = h_scroll->get_combined_minimum_size(); |
| 565 | Size2 vmin = v_scroll->get_combined_minimum_size(); |
| 566 | h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -vmin.width); |
| 567 | v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -hmin.height); |
| 568 | [[fallthrough]]; |
| 569 | } |
| 570 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 571 | if (!EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) { |
| 572 | break; |
| 573 | } |
| 574 | [[fallthrough]]; |
| 575 | } |
| 576 | case NOTIFICATION_ENTER_TREE: { |
| 577 | panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning"))); |
| 578 | panner->setup_warped_panning(debug_uv_dialog, EDITOR_GET("editors/panning/warped_mouse_panning")); |
| 579 | } break; |
| 580 | case NOTIFICATION_THEME_CHANGED: { |
| 581 | options->set_button_icon(get_editor_theme_icon(SNAME("Sprite2D"))); |
| 582 | |
| 583 | options->get_popup()->set_item_icon(MENU_OPTION_CONVERT_TO_MESH_2D, get_editor_theme_icon(SNAME("MeshInstance2D"))); |
| 584 | options->get_popup()->set_item_icon(MENU_OPTION_CONVERT_TO_POLYGON_2D, get_editor_theme_icon(SNAME("Polygon2D"))); |
| 585 | options->get_popup()->set_item_icon(MENU_OPTION_CREATE_COLLISION_POLY_2D, get_editor_theme_icon(SNAME("CollisionPolygon2D"))); |
| 586 | options->get_popup()->set_item_icon(MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D, get_editor_theme_icon(SNAME("LightOccluder2D"))); |
| 587 | |
| 588 | resize_region_rect->set_button_icon(get_editor_theme_icon(SNAME("KeepAspect"))); |
| 589 | } break; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | void Sprite2DEditor::_bind_methods() { |
| 594 | ClassDB::bind_method("_add_as_sibling_or_child", &Sprite2DEditor::_add_as_sibling_or_child); |
| 595 | } |
| 596 | |
| 597 | void Sprite2DEditor::edit(Sprite2D *p_sprite) { |
| 598 | Callable callback_update_button = callable_mp(this, &Sprite2DEditor::_update_sprite_resize_mode_button); |
| 599 | StringName signal_name = SNAME("_editor_region_rect_enabled"); |
| 600 | |
| 601 | if (node != nullptr && node->is_connected(signal_name, callback_update_button)) { |
| 602 | node->disconnect(signal_name, callback_update_button); |
| 603 | } |
| 604 | |
| 605 | node = p_sprite; |
| 606 | |
| 607 | if (node != nullptr && !node->is_connected(signal_name, callback_update_button)) { |
| 608 | node->connect(signal_name, callback_update_button); |
| 609 | } |
| 610 | |
| 611 | _update_sprite_resize_mode_button(); |
| 612 | } |
| 613 | |
| 614 | Sprite2DEditor::Sprite2DEditor() { |
| 615 | // Top HBoxContainer definition |
| 616 | top_hb = memnew(HBoxContainer); |
| 617 | |
| 618 | CanvasItemEditor::get_singleton()->add_control_to_menu_panel(top_hb); |
| 619 | |
| 620 | // Options definition |
| 621 | options = memnew(MenuButton); |
| 622 | |
| 623 | top_hb->add_child(options); |
| 624 | |
| 625 | options->set_text(TTR("Sprite2D")); |
| 626 | options->set_flat(false); |
| 627 | options->set_theme_type_variation("FlatMenuButtonNoIconTint"); |
| 628 | |
| 629 | options->get_popup()->add_item(TTR("Convert to MeshInstance2D"), MENU_OPTION_CONVERT_TO_MESH_2D); |
| 630 | options->get_popup()->add_item(TTR("Convert to Polygon2D"), MENU_OPTION_CONVERT_TO_POLYGON_2D); |
| 631 | options->get_popup()->add_item(TTR("Create CollisionPolygon2D Sibling"), MENU_OPTION_CREATE_COLLISION_POLY_2D); |
| 632 | options->get_popup()->add_item(TTR("Create LightOccluder2D Sibling"), MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D); |
| 633 | options->set_switch_on_hover(true); |
| 634 | |
| 635 | options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Sprite2DEditor::_menu_option)); |
| 636 | |
| 637 | // Resize region rect definition |
| 638 | resize_region_rect = memnew(Button); |
| 639 | |
| 640 | resize_region_rect->set_theme_type_variation("FlatMenuButton"); |
| 641 | resize_region_rect->set_toggle_mode(true); |
| 642 | resize_region_rect->set_shortcut(ED_SHORTCUT("canvas_item_editor/resize_region_rect", TTRC("Drag to Resize Region Rect"), KeyModifierMask::CMD_OR_CTRL | Key::R)); |
| 643 | |
| 644 | resize_region_rect->connect(SceneStringName(pressed), callable_mp(this, &Sprite2DEditor::_sync_sprite_resize_mode)); |
| 645 | |
| 646 | top_hb->add_child(resize_region_rect); |
| 647 | |
| 648 | // Other elements definition |
| 649 | err_dialog = memnew(AcceptDialog); |
| 650 | add_child(err_dialog); |
| 651 | |
| 652 | debug_uv_dialog = memnew(ConfirmationDialog); |
| 653 | debug_uv_dialog->set_size(Size2(960, 540) * EDSCALE); |
| 654 | VBoxContainer *vb = memnew(VBoxContainer); |
| 655 | debug_uv_dialog->add_child(vb); |
| 656 | debug_uv = memnew(Panel); |
| 657 | debug_uv->connect(SceneStringName(gui_input), callable_mp(this, &Sprite2DEditor::_debug_uv_input)); |
| 658 | debug_uv->connect(SceneStringName(draw), callable_mp(this, &Sprite2DEditor::_debug_uv_draw)); |
| 659 | debug_uv->set_clip_contents(true); |
| 660 | vb->add_margin_child(TTR("Preview:"), debug_uv, true); |
| 661 | |
| 662 | panner.instantiate(); |
| 663 | panner->set_callbacks(callable_mp(this, &Sprite2DEditor::_pan_callback), callable_mp(this, &Sprite2DEditor::_zoom_callback)); |
| 664 | |
| 665 | zoom_widget = memnew(EditorZoomWidget); |
| 666 | debug_uv->add_child(zoom_widget); |
| 667 | zoom_widget->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT, Control::PRESET_MODE_MINSIZE, 2 * EDSCALE); |
| 668 | zoom_widget->connect("zoom_changed", callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(true)); |
| 669 | zoom_widget->set_shortcut_context(nullptr); |
| 670 | |
| 671 | v_scroll = memnew(VScrollBar); |
| 672 | debug_uv->add_child(v_scroll); |
| 673 | v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(false)); |
| 674 | h_scroll = memnew(HScrollBar); |
| 675 | debug_uv->add_child(h_scroll); |
| 676 | h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(false)); |
| 677 | |
| 678 | debug_uv_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Sprite2DEditor::_create_node)); |
| 679 | |
| 680 | HBoxContainer *hb = memnew(HBoxContainer); |
| 681 | hb->add_child(memnew(Label(TTR("Simplification:")))); |
| 682 | simplification = memnew(SpinBox); |
| 683 | simplification->set_min(0.01); |
| 684 | simplification->set_max(10.00); |
| 685 | simplification->set_step(0.01); |
| 686 | simplification->set_value(2); |
| 687 | simplification->set_accessibility_name(TTRC("Simplification:")); |
| 688 | hb->add_child(simplification); |
| 689 | hb->add_spacer(); |
| 690 | hb->add_child(memnew(Label(TTR("Shrink (Pixels):")))); |
| 691 | shrink_pixels = memnew(SpinBox); |
| 692 | shrink_pixels->set_min(0); |
| 693 | shrink_pixels->set_max(10); |
| 694 | shrink_pixels->set_step(1); |
| 695 | shrink_pixels->set_value(0); |
| 696 | shrink_pixels->set_accessibility_name(TTRC("Shrink (Pixels):")); |
| 697 | hb->add_child(shrink_pixels); |
| 698 | hb->add_spacer(); |
| 699 | hb->add_child(memnew(Label(TTR("Grow (Pixels):")))); |
| 700 | grow_pixels = memnew(SpinBox); |
| 701 | grow_pixels->set_min(0); |
| 702 | grow_pixels->set_max(10); |
| 703 | grow_pixels->set_step(1); |
| 704 | grow_pixels->set_value(2); |
| 705 | grow_pixels->set_accessibility_name(TTRC("Grow (Pixels):")); |
| 706 | hb->add_child(grow_pixels); |
| 707 | hb->add_spacer(); |
| 708 | update_preview = memnew(Button); |
| 709 | update_preview->set_text(TTR("Update Preview")); |
| 710 | update_preview->connect(SceneStringName(pressed), callable_mp(this, &Sprite2DEditor::_update_mesh_data)); |
| 711 | hb->add_child(update_preview); |
| 712 | vb->add_margin_child(TTR("Settings:"), hb); |
| 713 | |
| 714 | add_child(debug_uv_dialog); |
| 715 | } |
| 716 | |
| 717 | void Sprite2DEditorPlugin::edit(Object *p_object) { |
| 718 | sprite_editor->edit(Object::cast_to<Sprite2D>(p_object)); |
| 719 | } |
| 720 | |
| 721 | bool Sprite2DEditorPlugin::handles(Object *p_object) const { |
| 722 | return p_object->is_class("Sprite2D"); |
| 723 | } |
| 724 | |
| 725 | void Sprite2DEditorPlugin::make_visible(bool p_visible) { |
| 726 | if (p_visible) { |
| 727 | sprite_editor->top_hb->show(); |
| 728 | } else { |
| 729 | sprite_editor->top_hb->hide(); |
| 730 | sprite_editor->edit(nullptr); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | Sprite2DEditorPlugin::Sprite2DEditorPlugin() { |
| 735 | sprite_editor = memnew(Sprite2DEditor); |
| 736 | EditorNode::get_singleton()->get_gui_base()->add_child(sprite_editor); |
| 737 | |
| 738 | make_visible(false); |
| 739 | //sprite_editor->options->hide(); |
| 740 | } |