Path: blob/master/editor/scene/2d/sprite_2d_editor_plugin.cpp
20850 views
/**************************************************************************/1/* sprite_2d_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "sprite_2d_editor_plugin.h"3132#include "core/math/geometry_2d.h"33#include "editor/docks/scene_tree_dock.h"34#include "editor/editor_node.h"35#include "editor/editor_undo_redo_manager.h"36#include "editor/gui/editor_zoom_widget.h"37#include "editor/scene/canvas_item_editor_plugin.h"38#include "editor/settings/editor_settings.h"39#include "editor/themes/editor_scale.h"40#include "scene/2d/light_occluder_2d.h"41#include "scene/2d/mesh_instance_2d.h"42#include "scene/2d/physics/collision_polygon_2d.h"43#include "scene/2d/polygon_2d.h"44#include "scene/gui/box_container.h"45#include "scene/gui/menu_button.h"46#include "scene/gui/panel.h"47#include "scene/gui/view_panner.h"48#include "scene/resources/mesh.h"49#include "thirdparty/clipper2/include/clipper2/clipper.h"5051#define PRECISION 15253void Sprite2DEditor::_node_removed(Node *p_node) {54if (p_node == node) {55node = nullptr;56options->hide();57}58}5960Vector<Vector2> expand(const Vector<Vector2> &points, const Rect2i &rect, float epsilon = 2.0) {61int size = points.size();62ERR_FAIL_COND_V(size < 2, Vector<Vector2>());6364Clipper2Lib::PathD subj(points.size());65for (int i = 0; i < points.size(); i++) {66subj[i] = Clipper2Lib::PointD(points[i].x, points[i].y);67}6869Clipper2Lib::PathsD solution = Clipper2Lib::InflatePaths({ subj }, epsilon, Clipper2Lib::JoinType::Miter, Clipper2Lib::EndType::Polygon, 2.0, PRECISION, 0.0);70// Here the miter_limit = 2.0 and arc_tolerance = 0.0 are Clipper2 defaults,71// and PRECISION is used to scale points up internally, to attain the desired precision.7273ERR_FAIL_COND_V(solution.size() == 0, points);7475// Clamp into the specified rect.76Clipper2Lib::RectD clamp(rect.position.x,77rect.position.y,78rect.position.x + rect.size.width,79rect.position.y + rect.size.height);80Clipper2Lib::PathsD out = Clipper2Lib::RectClip(clamp, solution[0], PRECISION);81// Here PRECISION is used to scale points up internally, to attain the desired precision.8283ERR_FAIL_COND_V(out.size() == 0, points);8485const Clipper2Lib::PathD &p2 = out[0];8687Vector<Vector2> outPoints;8889int lasti = p2.size() - 1;90Vector2 prev = Vector2(p2[lasti].x, p2[lasti].y);91for (uint64_t i = 0; i < p2.size(); i++) {92Vector2 cur = Vector2(p2[i].x, p2[i].y);93if (cur.distance_to(prev) > 0.5) {94outPoints.push_back(cur);95prev = cur;96}97}98return outPoints;99}100101void Sprite2DEditor::_menu_option(int p_option) {102if (!node) {103return;104}105106selected_menu_item = (Menu)p_option;107108switch (p_option) {109case MENU_OPTION_CONVERT_TO_MESH_2D: {110debug_uv_dialog->set_ok_button_text(TTR("Create MeshInstance2D"));111debug_uv_dialog->set_title(TTR("MeshInstance2D Preview"));112113_popup_debug_uv_dialog();114} break;115case MENU_OPTION_CONVERT_TO_POLYGON_2D: {116debug_uv_dialog->set_ok_button_text(TTR("Create Polygon2D"));117debug_uv_dialog->set_title(TTR("Polygon2D Preview"));118119_popup_debug_uv_dialog();120} break;121case MENU_OPTION_CREATE_COLLISION_POLY_2D: {122debug_uv_dialog->set_ok_button_text(TTR("Create CollisionPolygon2D"));123debug_uv_dialog->set_title(TTR("CollisionPolygon2D Preview"));124125_popup_debug_uv_dialog();126} break;127case MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D: {128debug_uv_dialog->set_ok_button_text(TTR("Create LightOccluder2D"));129debug_uv_dialog->set_title(TTR("LightOccluder2D Preview"));130131_popup_debug_uv_dialog();132} break;133}134}135136void Sprite2DEditor::_popup_debug_uv_dialog() {137String error_message;138if (node->get_owner() != get_tree()->get_edited_scene_root() && node != get_tree()->get_edited_scene_root()) {139error_message = TTR("Can't convert a sprite from a foreign scene.");140}141Ref<Texture2D> texture = node->get_texture();142if (texture.is_null()) {143error_message = TTR("Can't convert an empty sprite to mesh.");144}145146if (!error_message.is_empty()) {147err_dialog->set_text(error_message);148err_dialog->popup_centered();149return;150}151152_update_mesh_data();153debug_uv_dialog->popup_centered();154get_tree()->connect("process_frame", callable_mp(this, &Sprite2DEditor::_center_view), CONNECT_ONE_SHOT);155debug_uv->set_texture_filter(node->get_texture_filter_in_tree());156debug_uv->queue_redraw();157}158159void Sprite2DEditor::_update_mesh_data() {160ERR_FAIL_NULL(node);161Ref<Texture2D> texture = node->get_texture();162ERR_FAIL_COND(texture.is_null());163Ref<Image> image = texture->get_image();164ERR_FAIL_COND(image.is_null());165166if (image->is_compressed()) {167image->decompress();168}169170Rect2 rect = node->is_region_enabled() ? node->get_region_rect() : Rect2(Point2(), image->get_size());171rect.size /= Vector2(node->get_hframes(), node->get_vframes());172rect.position += node->get_frame_coords() * rect.size;173174Ref<BitMap> bm;175bm.instantiate();176bm->create_from_image_alpha(image);177178int shrink = shrink_pixels->get_value();179if (shrink > 0) {180bm->shrink_mask(shrink, rect);181}182183int grow = grow_pixels->get_value();184if (grow > 0) {185bm->grow_mask(grow, rect);186}187188float epsilon = simplification->get_value();189190Vector<Vector<Vector2>> lines = bm->clip_opaque_to_polygons(rect, epsilon);191192uv_lines.clear();193194computed_vertices.clear();195computed_uv.clear();196computed_indices.clear();197198Size2 img_size = image->get_size();199for (int i = 0; i < lines.size(); i++) {200lines.write[i] = expand(lines[i], rect, epsilon);201}202203if (selected_menu_item == MENU_OPTION_CONVERT_TO_MESH_2D) {204for (int j = 0; j < lines.size(); j++) {205int index_ofs = computed_vertices.size();206207for (int i = 0; i < lines[j].size(); i++) {208Vector2 vtx = lines[j][i];209computed_uv.push_back((vtx + rect.position) / img_size);210211if (node->is_flipped_h()) {212vtx.x = rect.size.x - vtx.x;213}214if (node->is_flipped_v()) {215vtx.y = rect.size.y - vtx.y;216}217vtx += node->get_offset();218if (node->is_centered()) {219vtx -= rect.size / 2.0;220}221222computed_vertices.push_back(vtx);223}224225Vector<int> poly = Geometry2D::triangulate_polygon(lines[j]);226227for (int i = 0; i < poly.size(); i += 3) {228for (int k = 0; k < 3; k++) {229int idx = i + k;230int idxn = i + (k + 1) % 3;231uv_lines.push_back(lines[j][poly[idx]] + rect.position);232uv_lines.push_back(lines[j][poly[idxn]] + rect.position);233234computed_indices.push_back(poly[idx] + index_ofs);235}236}237}238}239240outline_lines.clear();241computed_outline_lines.clear();242243if (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) {244outline_lines.resize(lines.size());245computed_outline_lines.resize(lines.size());246for (int pi = 0; pi < lines.size(); pi++) {247Vector<Vector2> ol;248Vector<Vector2> col;249250ol.resize(lines[pi].size());251col.resize(lines[pi].size());252253for (int i = 0; i < lines[pi].size(); i++) {254Vector2 vtx = lines[pi][i];255ol.write[i] = vtx + rect.position;256257if (node->is_flipped_h()) {258vtx.x = rect.size.x - vtx.x;259}260if (node->is_flipped_v()) {261vtx.y = rect.size.y - vtx.y;262}263// Don't bake offset to Polygon2D which has offset property.264if (selected_menu_item != MENU_OPTION_CONVERT_TO_POLYGON_2D) {265vtx += node->get_offset();266}267if (node->is_centered()) {268vtx -= rect.size / 2.0;269}270271col.write[i] = vtx;272}273274outline_lines.write[pi] = ol;275computed_outline_lines.write[pi] = col;276}277}278279debug_uv->queue_redraw();280}281282void Sprite2DEditor::_create_node() {283switch (selected_menu_item) {284case MENU_OPTION_CONVERT_TO_MESH_2D: {285_convert_to_mesh_2d_node();286} break;287case MENU_OPTION_CONVERT_TO_POLYGON_2D: {288_convert_to_polygon_2d_node();289} break;290case MENU_OPTION_CREATE_COLLISION_POLY_2D: {291_create_collision_polygon_2d_node();292} break;293case MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D: {294_create_light_occluder_2d_node();295} break;296}297}298299void Sprite2DEditor::_convert_to_mesh_2d_node() {300if (computed_vertices.size() < 3) {301err_dialog->set_text(TTR("Invalid geometry, can't replace by mesh."));302err_dialog->popup_centered();303return;304}305306Ref<ArrayMesh> mesh;307mesh.instantiate();308309Array a;310a.resize(Mesh::ARRAY_MAX);311a[Mesh::ARRAY_VERTEX] = computed_vertices;312a[Mesh::ARRAY_TEX_UV] = computed_uv;313a[Mesh::ARRAY_INDEX] = computed_indices;314315mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);316317MeshInstance2D *mesh_instance = memnew(MeshInstance2D);318mesh_instance->set_mesh(mesh);319320EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();321ur->create_action(TTR("Convert to MeshInstance2D"), UndoRedo::MERGE_DISABLE, node);322SceneTreeDock::get_singleton()->replace_node(node, mesh_instance);323ur->commit_action(false);324}325326void Sprite2DEditor::_convert_to_polygon_2d_node() {327if (computed_outline_lines.is_empty()) {328err_dialog->set_text(TTR("Invalid geometry, can't create polygon."));329err_dialog->popup_centered();330return;331}332333Polygon2D *polygon_2d_instance = memnew(Polygon2D);334335int total_point_count = 0;336for (int i = 0; i < computed_outline_lines.size(); i++) {337total_point_count += computed_outline_lines[i].size();338}339340PackedVector2Array polygon;341polygon.resize(total_point_count);342Vector2 *polygon_write = polygon.ptrw();343344PackedVector2Array uvs;345uvs.resize(total_point_count);346Vector2 *uvs_write = uvs.ptrw();347348int current_point_index = 0;349350Array polys;351polys.resize(computed_outline_lines.size());352353for (int i = 0; i < computed_outline_lines.size(); i++) {354Vector<Vector2> outline = computed_outline_lines[i];355Vector<Vector2> uv_outline = outline_lines[i];356357PackedInt32Array pia;358pia.resize(outline.size());359int *pia_write = pia.ptrw();360361for (int pi = 0; pi < outline.size(); pi++) {362polygon_write[current_point_index] = outline[pi];363uvs_write[current_point_index] = uv_outline[pi];364pia_write[pi] = current_point_index;365current_point_index++;366}367368polys[i] = pia;369}370371polygon_2d_instance->set_uv(uvs);372polygon_2d_instance->set_polygon(polygon);373polygon_2d_instance->set_polygons(polys);374375EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();376ur->create_action(TTR("Convert to Polygon2D"), UndoRedo::MERGE_DISABLE, node);377SceneTreeDock::get_singleton()->replace_node(node, polygon_2d_instance);378ur->commit_action(false);379}380381void Sprite2DEditor::_create_collision_polygon_2d_node() {382if (computed_outline_lines.is_empty()) {383err_dialog->set_text(TTR("Invalid geometry, can't create collision polygon."));384err_dialog->popup_centered();385return;386}387388for (int i = 0; i < computed_outline_lines.size(); i++) {389Vector<Vector2> outline = computed_outline_lines[i];390391CollisionPolygon2D *collision_polygon_2d_instance = memnew(CollisionPolygon2D);392collision_polygon_2d_instance->set_polygon(outline);393394EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();395ur->create_action(TTR("Create CollisionPolygon2D Sibling"), UndoRedo::MERGE_DISABLE, node);396ur->add_do_method(this, "_add_as_sibling_or_child", node, collision_polygon_2d_instance);397ur->add_do_reference(collision_polygon_2d_instance);398ur->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);399ur->commit_action();400}401}402403void Sprite2DEditor::_create_light_occluder_2d_node() {404if (computed_outline_lines.is_empty()) {405err_dialog->set_text(TTR("Invalid geometry, can't create light occluder."));406err_dialog->popup_centered();407return;408}409410for (int i = 0; i < computed_outline_lines.size(); i++) {411Vector<Vector2> outline = computed_outline_lines[i];412413Ref<OccluderPolygon2D> polygon;414polygon.instantiate();415416PackedVector2Array a;417a.resize(outline.size());418Vector2 *aw = a.ptrw();419for (int io = 0; io < outline.size(); io++) {420aw[io] = outline[io];421}422polygon->set_polygon(a);423424LightOccluder2D *light_occluder_2d_instance = memnew(LightOccluder2D);425light_occluder_2d_instance->set_occluder_polygon(polygon);426427EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();428ur->create_action(TTR("Create LightOccluder2D Sibling"), UndoRedo::MERGE_DISABLE, node);429ur->add_do_method(this, "_add_as_sibling_or_child", node, light_occluder_2d_instance);430ur->add_do_reference(light_occluder_2d_instance);431ur->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);432ur->commit_action();433}434}435436void Sprite2DEditor::_add_as_sibling_or_child(Node *p_own_node, Node *p_new_node) {437// Can't make sibling if own node is scene root438if (p_own_node != get_tree()->get_edited_scene_root()) {439p_own_node->get_parent()->add_child(p_new_node, true);440Object::cast_to<Node2D>(p_new_node)->set_transform(Object::cast_to<Node2D>(p_own_node)->get_transform());441} else {442p_own_node->add_child(p_new_node, true);443}444445p_new_node->set_owner(get_tree()->get_edited_scene_root());446}447448void Sprite2DEditor::_sync_sprite_resize_mode() {449if (node != nullptr) {450node->_editor_set_dragging_to_resize_rect(resize_region_rect->is_pressed());451}452}453454void Sprite2DEditor::_update_sprite_resize_mode_button() {455if (node == nullptr) {456return;457}458resize_region_rect->set_disabled(!node->is_region_enabled());459resize_region_rect->set_pressed(node->_editor_is_dragging_to_resiz_rect());460resize_region_rect->set_tooltip_text(node->is_region_enabled() ? "" : TTRC("Sprite's region needs to be enabled in the inspector."));461}462463void Sprite2DEditor::_debug_uv_input(const Ref<InputEvent> &p_input) {464if (panner->gui_input(p_input, debug_uv->get_global_rect())) {465accept_event();466}467}468469void Sprite2DEditor::_debug_uv_draw() {470debug_uv->draw_set_transform(-draw_offset * draw_zoom, 0, Vector2(draw_zoom, draw_zoom));471472Ref<Texture2D> tex = node->get_texture();473ERR_FAIL_COND(tex.is_null());474475debug_uv->draw_texture(tex, Point2());476477Color color = Color(1.0, 0.8, 0.7);478479if (selected_menu_item == MENU_OPTION_CONVERT_TO_MESH_2D && uv_lines.size() > 0) {480debug_uv->draw_multiline(uv_lines, color);481482} 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) {483for (int i = 0; i < outline_lines.size(); i++) {484Vector<Vector2> outline = outline_lines[i];485486debug_uv->draw_polyline(outline, color);487debug_uv->draw_line(outline[0], outline[outline.size() - 1], color);488}489}490}491492void Sprite2DEditor::_center_view() {493Ref<Texture2D> tex = node->get_texture();494ERR_FAIL_COND(tex.is_null());495Vector2 zoom_factor = (debug_uv->get_size() - Vector2(1, 1) * 50 * EDSCALE) / tex->get_size();496zoom_widget->set_zoom(MIN(zoom_factor.x, zoom_factor.y));497// Recalculate scroll limits.498_update_zoom_and_pan(false);499500Vector2 offset = (tex->get_size() - debug_uv->get_size() / zoom_widget->get_zoom()) / 2;501h_scroll->set_value_no_signal(offset.x);502v_scroll->set_value_no_signal(offset.y);503_update_zoom_and_pan(false);504}505506void Sprite2DEditor::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {507h_scroll->set_value_no_signal(h_scroll->get_value() - p_scroll_vec.x / draw_zoom);508v_scroll->set_value_no_signal(v_scroll->get_value() - p_scroll_vec.y / draw_zoom);509_update_zoom_and_pan(false);510}511512void Sprite2DEditor::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {513const real_t prev_zoom = draw_zoom;514zoom_widget->set_zoom(draw_zoom * p_zoom_factor);515draw_offset += p_origin / prev_zoom - p_origin / zoom_widget->get_zoom();516h_scroll->set_value_no_signal(draw_offset.x);517v_scroll->set_value_no_signal(draw_offset.y);518_update_zoom_and_pan(false);519}520521void Sprite2DEditor::_update_zoom_and_pan(bool p_zoom_at_center) {522real_t previous_zoom = draw_zoom;523draw_zoom = zoom_widget->get_zoom();524draw_offset = Vector2(h_scroll->get_value(), v_scroll->get_value());525if (p_zoom_at_center) {526Vector2 center = debug_uv->get_size() / 2;527draw_offset += center / previous_zoom - center / draw_zoom;528}529530Ref<Texture2D> tex = node->get_texture();531ERR_FAIL_COND(tex.is_null());532533Point2 min_corner;534Point2 max_corner = tex->get_size();535Size2 page_size = debug_uv->get_size() / draw_zoom;536Vector2 margin = Vector2(50, 50) * EDSCALE / draw_zoom;537min_corner -= page_size - margin;538max_corner += page_size - margin;539540h_scroll->set_block_signals(true);541h_scroll->set_min(min_corner.x);542h_scroll->set_max(max_corner.x);543h_scroll->set_page(page_size.x);544h_scroll->set_value(draw_offset.x);545h_scroll->set_block_signals(false);546547v_scroll->set_block_signals(true);548v_scroll->set_min(min_corner.y);549v_scroll->set_max(max_corner.y);550v_scroll->set_page(page_size.y);551v_scroll->set_value(draw_offset.y);552v_scroll->set_block_signals(false);553554debug_uv->queue_redraw();555}556557void Sprite2DEditor::_notification(int p_what) {558switch (p_what) {559case NOTIFICATION_READY: {560v_scroll->set_anchors_and_offsets_preset(Control::PRESET_RIGHT_WIDE);561h_scroll->set_anchors_and_offsets_preset(Control::PRESET_BOTTOM_WIDE);562// Avoid scrollbar overlapping.563Size2 hmin = h_scroll->get_combined_minimum_size();564Size2 vmin = v_scroll->get_combined_minimum_size();565h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -vmin.width);566v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -hmin.height);567[[fallthrough]];568}569case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {570if (!EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {571break;572}573[[fallthrough]];574}575case NOTIFICATION_ENTER_TREE: {576panner->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")));577panner->setup_warped_panning(debug_uv_dialog, EDITOR_GET("editors/panning/warped_mouse_panning"));578} break;579case NOTIFICATION_THEME_CHANGED: {580options->set_button_icon(get_editor_theme_icon(SNAME("Sprite2D")));581582options->get_popup()->set_item_icon(MENU_OPTION_CONVERT_TO_MESH_2D, get_editor_theme_icon(SNAME("MeshInstance2D")));583options->get_popup()->set_item_icon(MENU_OPTION_CONVERT_TO_POLYGON_2D, get_editor_theme_icon(SNAME("Polygon2D")));584options->get_popup()->set_item_icon(MENU_OPTION_CREATE_COLLISION_POLY_2D, get_editor_theme_icon(SNAME("CollisionPolygon2D")));585options->get_popup()->set_item_icon(MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D, get_editor_theme_icon(SNAME("LightOccluder2D")));586587resize_region_rect->set_button_icon(get_editor_theme_icon(SNAME("KeepAspect")));588} break;589}590}591592void Sprite2DEditor::_bind_methods() {593ClassDB::bind_method("_add_as_sibling_or_child", &Sprite2DEditor::_add_as_sibling_or_child);594}595596void Sprite2DEditor::edit(Sprite2D *p_sprite) {597Callable callback_update_button = callable_mp(this, &Sprite2DEditor::_update_sprite_resize_mode_button);598StringName signal_name = SNAME("_editor_region_rect_enabled");599600if (node != nullptr && node->is_connected(signal_name, callback_update_button)) {601node->disconnect(signal_name, callback_update_button);602}603604node = p_sprite;605606if (node != nullptr && !node->is_connected(signal_name, callback_update_button)) {607node->connect(signal_name, callback_update_button);608}609610_update_sprite_resize_mode_button();611}612613Sprite2DEditor::Sprite2DEditor() {614// Top HBoxContainer definition615top_hb = memnew(HBoxContainer);616617CanvasItemEditor::get_singleton()->add_control_to_menu_panel(top_hb);618619// Options definition620options = memnew(MenuButton);621622top_hb->add_child(options);623624options->set_text(TTR("Sprite2D"));625options->set_flat(false);626options->set_theme_type_variation("FlatMenuButtonNoIconTint");627628options->get_popup()->add_item(TTR("Convert to MeshInstance2D"), MENU_OPTION_CONVERT_TO_MESH_2D);629options->get_popup()->add_item(TTR("Convert to Polygon2D"), MENU_OPTION_CONVERT_TO_POLYGON_2D);630options->get_popup()->add_item(TTR("Create CollisionPolygon2D Sibling"), MENU_OPTION_CREATE_COLLISION_POLY_2D);631options->get_popup()->add_item(TTR("Create LightOccluder2D Sibling"), MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D);632options->set_switch_on_hover(true);633634options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Sprite2DEditor::_menu_option));635636// Resize region rect definition637resize_region_rect = memnew(Button);638639resize_region_rect->set_theme_type_variation("FlatMenuButton");640resize_region_rect->set_toggle_mode(true);641resize_region_rect->set_shortcut(ED_SHORTCUT("canvas_item_editor/resize_region_rect", TTRC("Drag to Resize Region Rect"), KeyModifierMask::CMD_OR_CTRL | Key::R));642643resize_region_rect->connect(SceneStringName(pressed), callable_mp(this, &Sprite2DEditor::_sync_sprite_resize_mode));644645top_hb->add_child(resize_region_rect);646647// Other elements definition648err_dialog = memnew(AcceptDialog);649add_child(err_dialog);650651debug_uv_dialog = memnew(ConfirmationDialog);652debug_uv_dialog->set_size(Size2(960, 540) * EDSCALE);653VBoxContainer *vb = memnew(VBoxContainer);654debug_uv_dialog->add_child(vb);655debug_uv = memnew(Panel);656debug_uv->connect(SceneStringName(gui_input), callable_mp(this, &Sprite2DEditor::_debug_uv_input));657debug_uv->connect(SceneStringName(draw), callable_mp(this, &Sprite2DEditor::_debug_uv_draw));658debug_uv->set_clip_contents(true);659vb->add_margin_child(TTR("Preview:"), debug_uv, true);660661panner.instantiate();662panner->set_callbacks(callable_mp(this, &Sprite2DEditor::_pan_callback), callable_mp(this, &Sprite2DEditor::_zoom_callback));663664zoom_widget = memnew(EditorZoomWidget);665debug_uv->add_child(zoom_widget);666zoom_widget->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT, Control::PRESET_MODE_MINSIZE, 2 * EDSCALE);667zoom_widget->connect("zoom_changed", callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(true));668zoom_widget->set_shortcut_context(nullptr);669670v_scroll = memnew(VScrollBar);671debug_uv->add_child(v_scroll);672v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(false));673h_scroll = memnew(HScrollBar);674debug_uv->add_child(h_scroll);675h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(false));676677debug_uv_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Sprite2DEditor::_create_node));678679HBoxContainer *hb = memnew(HBoxContainer);680hb->add_child(memnew(Label(TTR("Simplification:"))));681simplification = memnew(SpinBox);682simplification->set_min(0.01);683simplification->set_max(10.00);684simplification->set_step(0.01);685simplification->set_value(2);686simplification->set_accessibility_name(TTRC("Simplification:"));687hb->add_child(simplification);688hb->add_spacer();689hb->add_child(memnew(Label(TTR("Shrink (Pixels):"))));690shrink_pixels = memnew(SpinBox);691shrink_pixels->set_min(0);692shrink_pixels->set_max(10);693shrink_pixels->set_step(1);694shrink_pixels->set_value(0);695shrink_pixels->set_accessibility_name(TTRC("Shrink (Pixels):"));696hb->add_child(shrink_pixels);697hb->add_spacer();698hb->add_child(memnew(Label(TTR("Grow (Pixels):"))));699grow_pixels = memnew(SpinBox);700grow_pixels->set_min(0);701grow_pixels->set_max(10);702grow_pixels->set_step(1);703grow_pixels->set_value(2);704grow_pixels->set_accessibility_name(TTRC("Grow (Pixels):"));705hb->add_child(grow_pixels);706hb->add_spacer();707update_preview = memnew(Button);708update_preview->set_text(TTR("Update Preview"));709update_preview->connect(SceneStringName(pressed), callable_mp(this, &Sprite2DEditor::_update_mesh_data));710hb->add_child(update_preview);711vb->add_margin_child(TTR("Settings:"), hb);712713add_child(debug_uv_dialog);714}715716void Sprite2DEditorPlugin::edit(Object *p_object) {717sprite_editor->edit(Object::cast_to<Sprite2D>(p_object));718}719720bool Sprite2DEditorPlugin::handles(Object *p_object) const {721return p_object->is_class("Sprite2D");722}723724void Sprite2DEditorPlugin::make_visible(bool p_visible) {725if (p_visible) {726sprite_editor->top_hb->show();727} else {728sprite_editor->top_hb->hide();729sprite_editor->edit(nullptr);730}731}732733Sprite2DEditorPlugin::Sprite2DEditorPlugin() {734sprite_editor = memnew(Sprite2DEditor);735EditorNode::get_singleton()->get_gui_base()->add_child(sprite_editor);736737make_visible(false);738//sprite_editor->options->hide();739}740741742