Path: blob/master/editor/scene/2d/sprite_2d_editor_plugin.cpp
9903 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 "thirdparty/clipper2/include/clipper2/clipper.h"4950#define PRECISION 15152void Sprite2DEditor::_node_removed(Node *p_node) {53if (p_node == node) {54node = nullptr;55options->hide();56}57}5859Vector<Vector2> expand(const Vector<Vector2> &points, const Rect2i &rect, float epsilon = 2.0) {60int size = points.size();61ERR_FAIL_COND_V(size < 2, Vector<Vector2>());6263Clipper2Lib::PathD subj(points.size());64for (int i = 0; i < points.size(); i++) {65subj[i] = Clipper2Lib::PointD(points[i].x, points[i].y);66}6768Clipper2Lib::PathsD solution = Clipper2Lib::InflatePaths({ subj }, epsilon, Clipper2Lib::JoinType::Miter, Clipper2Lib::EndType::Polygon, 2.0, PRECISION, 0.0);69// Here the miter_limit = 2.0 and arc_tolerance = 0.0 are Clipper2 defaults,70// and PRECISION is used to scale points up internally, to attain the desired precision.7172ERR_FAIL_COND_V(solution.size() == 0, points);7374// Clamp into the specified rect.75Clipper2Lib::RectD clamp(rect.position.x,76rect.position.y,77rect.position.x + rect.size.width,78rect.position.y + rect.size.height);79Clipper2Lib::PathsD out = Clipper2Lib::RectClip(clamp, solution[0], PRECISION);80// Here PRECISION is used to scale points up internally, to attain the desired precision.8182ERR_FAIL_COND_V(out.size() == 0, points);8384const Clipper2Lib::PathD &p2 = out[0];8586Vector<Vector2> outPoints;8788int lasti = p2.size() - 1;89Vector2 prev = Vector2(p2[lasti].x, p2[lasti].y);90for (uint64_t i = 0; i < p2.size(); i++) {91Vector2 cur = Vector2(p2[i].x, p2[i].y);92if (cur.distance_to(prev) > 0.5) {93outPoints.push_back(cur);94prev = cur;95}96}97return outPoints;98}99100void Sprite2DEditor::_menu_option(int p_option) {101if (!node) {102return;103}104105selected_menu_item = (Menu)p_option;106107switch (p_option) {108case MENU_OPTION_CONVERT_TO_MESH_2D: {109debug_uv_dialog->set_ok_button_text(TTR("Create MeshInstance2D"));110debug_uv_dialog->set_title(TTR("MeshInstance2D Preview"));111112_popup_debug_uv_dialog();113} break;114case MENU_OPTION_CONVERT_TO_POLYGON_2D: {115debug_uv_dialog->set_ok_button_text(TTR("Create Polygon2D"));116debug_uv_dialog->set_title(TTR("Polygon2D Preview"));117118_popup_debug_uv_dialog();119} break;120case MENU_OPTION_CREATE_COLLISION_POLY_2D: {121debug_uv_dialog->set_ok_button_text(TTR("Create CollisionPolygon2D"));122debug_uv_dialog->set_title(TTR("CollisionPolygon2D Preview"));123124_popup_debug_uv_dialog();125} break;126case MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D: {127debug_uv_dialog->set_ok_button_text(TTR("Create LightOccluder2D"));128debug_uv_dialog->set_title(TTR("LightOccluder2D Preview"));129130_popup_debug_uv_dialog();131} break;132}133}134135void Sprite2DEditor::_popup_debug_uv_dialog() {136String error_message;137if (node->get_owner() != get_tree()->get_edited_scene_root() && node != get_tree()->get_edited_scene_root()) {138error_message = TTR("Can't convert a sprite from a foreign scene.");139}140Ref<Texture2D> texture = node->get_texture();141if (texture.is_null()) {142error_message = TTR("Can't convert an empty sprite to mesh.");143}144145if (!error_message.is_empty()) {146err_dialog->set_text(error_message);147err_dialog->popup_centered();148return;149}150151_update_mesh_data();152debug_uv_dialog->popup_centered();153get_tree()->connect("process_frame", callable_mp(this, &Sprite2DEditor::_center_view), CONNECT_ONE_SHOT);154debug_uv->set_texture_filter(node->get_texture_filter_in_tree());155debug_uv->queue_redraw();156}157158void Sprite2DEditor::_update_mesh_data() {159ERR_FAIL_NULL(node);160Ref<Texture2D> texture = node->get_texture();161ERR_FAIL_COND(texture.is_null());162Ref<Image> image = texture->get_image();163ERR_FAIL_COND(image.is_null());164165if (image->is_compressed()) {166image->decompress();167}168169Rect2 rect = node->is_region_enabled() ? node->get_region_rect() : Rect2(Point2(), image->get_size());170rect.size /= Vector2(node->get_hframes(), node->get_vframes());171rect.position += node->get_frame_coords() * rect.size;172173Ref<BitMap> bm;174bm.instantiate();175bm->create_from_image_alpha(image);176177int shrink = shrink_pixels->get_value();178if (shrink > 0) {179bm->shrink_mask(shrink, rect);180}181182int grow = grow_pixels->get_value();183if (grow > 0) {184bm->grow_mask(grow, rect);185}186187float epsilon = simplification->get_value();188189Vector<Vector<Vector2>> lines = bm->clip_opaque_to_polygons(rect, epsilon);190191uv_lines.clear();192193computed_vertices.clear();194computed_uv.clear();195computed_indices.clear();196197Size2 img_size = image->get_size();198for (int i = 0; i < lines.size(); i++) {199lines.write[i] = expand(lines[i], rect, epsilon);200}201202if (selected_menu_item == MENU_OPTION_CONVERT_TO_MESH_2D) {203for (int j = 0; j < lines.size(); j++) {204int index_ofs = computed_vertices.size();205206for (int i = 0; i < lines[j].size(); i++) {207Vector2 vtx = lines[j][i];208computed_uv.push_back((vtx + rect.position) / img_size);209210if (node->is_flipped_h()) {211vtx.x = rect.size.x - vtx.x;212}213if (node->is_flipped_v()) {214vtx.y = rect.size.y - vtx.y;215}216vtx += node->get_offset();217if (node->is_centered()) {218vtx -= rect.size / 2.0;219}220221computed_vertices.push_back(vtx);222}223224Vector<int> poly = Geometry2D::triangulate_polygon(lines[j]);225226for (int i = 0; i < poly.size(); i += 3) {227for (int k = 0; k < 3; k++) {228int idx = i + k;229int idxn = i + (k + 1) % 3;230uv_lines.push_back(lines[j][poly[idx]] + rect.position);231uv_lines.push_back(lines[j][poly[idxn]] + rect.position);232233computed_indices.push_back(poly[idx] + index_ofs);234}235}236}237}238239outline_lines.clear();240computed_outline_lines.clear();241242if (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) {243outline_lines.resize(lines.size());244computed_outline_lines.resize(lines.size());245for (int pi = 0; pi < lines.size(); pi++) {246Vector<Vector2> ol;247Vector<Vector2> col;248249ol.resize(lines[pi].size());250col.resize(lines[pi].size());251252for (int i = 0; i < lines[pi].size(); i++) {253Vector2 vtx = lines[pi][i];254ol.write[i] = vtx + rect.position;255256if (node->is_flipped_h()) {257vtx.x = rect.size.x - vtx.x;258}259if (node->is_flipped_v()) {260vtx.y = rect.size.y - vtx.y;261}262// Don't bake offset to Polygon2D which has offset property.263if (selected_menu_item != MENU_OPTION_CONVERT_TO_POLYGON_2D) {264vtx += node->get_offset();265}266if (node->is_centered()) {267vtx -= rect.size / 2.0;268}269270col.write[i] = vtx;271}272273outline_lines.write[pi] = ol;274computed_outline_lines.write[pi] = col;275}276}277278debug_uv->queue_redraw();279}280281void Sprite2DEditor::_create_node() {282switch (selected_menu_item) {283case MENU_OPTION_CONVERT_TO_MESH_2D: {284_convert_to_mesh_2d_node();285} break;286case MENU_OPTION_CONVERT_TO_POLYGON_2D: {287_convert_to_polygon_2d_node();288} break;289case MENU_OPTION_CREATE_COLLISION_POLY_2D: {290_create_collision_polygon_2d_node();291} break;292case MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D: {293_create_light_occluder_2d_node();294} break;295}296}297298void Sprite2DEditor::_convert_to_mesh_2d_node() {299if (computed_vertices.size() < 3) {300err_dialog->set_text(TTR("Invalid geometry, can't replace by mesh."));301err_dialog->popup_centered();302return;303}304305Ref<ArrayMesh> mesh;306mesh.instantiate();307308Array a;309a.resize(Mesh::ARRAY_MAX);310a[Mesh::ARRAY_VERTEX] = computed_vertices;311a[Mesh::ARRAY_TEX_UV] = computed_uv;312a[Mesh::ARRAY_INDEX] = computed_indices;313314mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);315316MeshInstance2D *mesh_instance = memnew(MeshInstance2D);317mesh_instance->set_mesh(mesh);318319EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();320ur->create_action(TTR("Convert to MeshInstance2D"), UndoRedo::MERGE_DISABLE, node);321SceneTreeDock::get_singleton()->replace_node(node, mesh_instance);322ur->commit_action(false);323}324325void Sprite2DEditor::_convert_to_polygon_2d_node() {326if (computed_outline_lines.is_empty()) {327err_dialog->set_text(TTR("Invalid geometry, can't create polygon."));328err_dialog->popup_centered();329return;330}331332Polygon2D *polygon_2d_instance = memnew(Polygon2D);333334int total_point_count = 0;335for (int i = 0; i < computed_outline_lines.size(); i++) {336total_point_count += computed_outline_lines[i].size();337}338339PackedVector2Array polygon;340polygon.resize(total_point_count);341Vector2 *polygon_write = polygon.ptrw();342343PackedVector2Array uvs;344uvs.resize(total_point_count);345Vector2 *uvs_write = uvs.ptrw();346347int current_point_index = 0;348349Array polys;350polys.resize(computed_outline_lines.size());351352for (int i = 0; i < computed_outline_lines.size(); i++) {353Vector<Vector2> outline = computed_outline_lines[i];354Vector<Vector2> uv_outline = outline_lines[i];355356PackedInt32Array pia;357pia.resize(outline.size());358int *pia_write = pia.ptrw();359360for (int pi = 0; pi < outline.size(); pi++) {361polygon_write[current_point_index] = outline[pi];362uvs_write[current_point_index] = uv_outline[pi];363pia_write[pi] = current_point_index;364current_point_index++;365}366367polys[i] = pia;368}369370polygon_2d_instance->set_uv(uvs);371polygon_2d_instance->set_polygon(polygon);372polygon_2d_instance->set_polygons(polys);373374EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();375ur->create_action(TTR("Convert to Polygon2D"), UndoRedo::MERGE_DISABLE, node);376SceneTreeDock::get_singleton()->replace_node(node, polygon_2d_instance);377ur->commit_action(false);378}379380void Sprite2DEditor::_create_collision_polygon_2d_node() {381if (computed_outline_lines.is_empty()) {382err_dialog->set_text(TTR("Invalid geometry, can't create collision polygon."));383err_dialog->popup_centered();384return;385}386387for (int i = 0; i < computed_outline_lines.size(); i++) {388Vector<Vector2> outline = computed_outline_lines[i];389390CollisionPolygon2D *collision_polygon_2d_instance = memnew(CollisionPolygon2D);391collision_polygon_2d_instance->set_polygon(outline);392393EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();394ur->create_action(TTR("Create CollisionPolygon2D Sibling"), UndoRedo::MERGE_DISABLE, node);395ur->add_do_method(this, "_add_as_sibling_or_child", node, collision_polygon_2d_instance);396ur->add_do_reference(collision_polygon_2d_instance);397ur->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);398ur->commit_action();399}400}401402void Sprite2DEditor::_create_light_occluder_2d_node() {403if (computed_outline_lines.is_empty()) {404err_dialog->set_text(TTR("Invalid geometry, can't create light occluder."));405err_dialog->popup_centered();406return;407}408409for (int i = 0; i < computed_outline_lines.size(); i++) {410Vector<Vector2> outline = computed_outline_lines[i];411412Ref<OccluderPolygon2D> polygon;413polygon.instantiate();414415PackedVector2Array a;416a.resize(outline.size());417Vector2 *aw = a.ptrw();418for (int io = 0; io < outline.size(); io++) {419aw[io] = outline[io];420}421polygon->set_polygon(a);422423LightOccluder2D *light_occluder_2d_instance = memnew(LightOccluder2D);424light_occluder_2d_instance->set_occluder_polygon(polygon);425426EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();427ur->create_action(TTR("Create LightOccluder2D Sibling"), UndoRedo::MERGE_DISABLE, node);428ur->add_do_method(this, "_add_as_sibling_or_child", node, light_occluder_2d_instance);429ur->add_do_reference(light_occluder_2d_instance);430ur->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);431ur->commit_action();432}433}434435void Sprite2DEditor::_add_as_sibling_or_child(Node *p_own_node, Node *p_new_node) {436// Can't make sibling if own node is scene root437if (p_own_node != get_tree()->get_edited_scene_root()) {438p_own_node->get_parent()->add_child(p_new_node, true);439Object::cast_to<Node2D>(p_new_node)->set_transform(Object::cast_to<Node2D>(p_own_node)->get_transform());440} else {441p_own_node->add_child(p_new_node, true);442}443444p_new_node->set_owner(get_tree()->get_edited_scene_root());445}446447void Sprite2DEditor::_sync_sprite_resize_mode() {448if (node != nullptr) {449node->_editor_set_dragging_to_resize_rect(resize_region_rect->is_pressed());450}451}452453void Sprite2DEditor::_update_sprite_resize_mode_button() {454if (node == nullptr) {455return;456}457resize_region_rect->set_disabled(!node->is_region_enabled());458resize_region_rect->set_pressed(node->_editor_is_dragging_to_resiz_rect());459resize_region_rect->set_tooltip_text(node->is_region_enabled() ? "" : TTRC("Sprite's region needs to be enabled in the inspector."));460}461462void Sprite2DEditor::_debug_uv_input(const Ref<InputEvent> &p_input) {463if (panner->gui_input(p_input, debug_uv->get_global_rect())) {464accept_event();465}466}467468void Sprite2DEditor::_debug_uv_draw() {469debug_uv->draw_set_transform(-draw_offset * draw_zoom, 0, Vector2(draw_zoom, draw_zoom));470471Ref<Texture2D> tex = node->get_texture();472ERR_FAIL_COND(tex.is_null());473474debug_uv->draw_texture(tex, Point2());475476Color color = Color(1.0, 0.8, 0.7);477478if (selected_menu_item == MENU_OPTION_CONVERT_TO_MESH_2D && uv_lines.size() > 0) {479debug_uv->draw_multiline(uv_lines, color);480481} 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) {482for (int i = 0; i < outline_lines.size(); i++) {483Vector<Vector2> outline = outline_lines[i];484485debug_uv->draw_polyline(outline, color);486debug_uv->draw_line(outline[0], outline[outline.size() - 1], color);487}488}489}490491void Sprite2DEditor::_center_view() {492Ref<Texture2D> tex = node->get_texture();493ERR_FAIL_COND(tex.is_null());494Vector2 zoom_factor = (debug_uv->get_size() - Vector2(1, 1) * 50 * EDSCALE) / tex->get_size();495zoom_widget->set_zoom(MIN(zoom_factor.x, zoom_factor.y));496// Recalculate scroll limits.497_update_zoom_and_pan(false);498499Vector2 offset = (tex->get_size() - debug_uv->get_size() / zoom_widget->get_zoom()) / 2;500h_scroll->set_value_no_signal(offset.x);501v_scroll->set_value_no_signal(offset.y);502_update_zoom_and_pan(false);503}504505void Sprite2DEditor::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {506h_scroll->set_value_no_signal(h_scroll->get_value() - p_scroll_vec.x / draw_zoom);507v_scroll->set_value_no_signal(v_scroll->get_value() - p_scroll_vec.y / draw_zoom);508_update_zoom_and_pan(false);509}510511void Sprite2DEditor::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {512const real_t prev_zoom = draw_zoom;513zoom_widget->set_zoom(draw_zoom * p_zoom_factor);514draw_offset += p_origin / prev_zoom - p_origin / zoom_widget->get_zoom();515h_scroll->set_value_no_signal(draw_offset.x);516v_scroll->set_value_no_signal(draw_offset.y);517_update_zoom_and_pan(false);518}519520void Sprite2DEditor::_update_zoom_and_pan(bool p_zoom_at_center) {521real_t previous_zoom = draw_zoom;522draw_zoom = zoom_widget->get_zoom();523draw_offset = Vector2(h_scroll->get_value(), v_scroll->get_value());524if (p_zoom_at_center) {525Vector2 center = debug_uv->get_size() / 2;526draw_offset += center / previous_zoom - center / draw_zoom;527}528529Ref<Texture2D> tex = node->get_texture();530ERR_FAIL_COND(tex.is_null());531532Point2 min_corner;533Point2 max_corner = tex->get_size();534Size2 page_size = debug_uv->get_size() / draw_zoom;535Vector2 margin = Vector2(50, 50) * EDSCALE / draw_zoom;536min_corner -= page_size - margin;537max_corner += page_size - margin;538539h_scroll->set_block_signals(true);540h_scroll->set_min(min_corner.x);541h_scroll->set_max(max_corner.x);542h_scroll->set_page(page_size.x);543h_scroll->set_value(draw_offset.x);544h_scroll->set_block_signals(false);545546v_scroll->set_block_signals(true);547v_scroll->set_min(min_corner.y);548v_scroll->set_max(max_corner.y);549v_scroll->set_page(page_size.y);550v_scroll->set_value(draw_offset.y);551v_scroll->set_block_signals(false);552553debug_uv->queue_redraw();554}555556void Sprite2DEditor::_notification(int p_what) {557switch (p_what) {558case NOTIFICATION_READY: {559v_scroll->set_anchors_and_offsets_preset(Control::PRESET_RIGHT_WIDE);560h_scroll->set_anchors_and_offsets_preset(Control::PRESET_BOTTOM_WIDE);561// Avoid scrollbar overlapping.562Size2 hmin = h_scroll->get_combined_minimum_size();563Size2 vmin = v_scroll->get_combined_minimum_size();564h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -vmin.width);565v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -hmin.height);566[[fallthrough]];567}568case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {569if (!EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {570break;571}572[[fallthrough]];573}574case NOTIFICATION_ENTER_TREE: {575panner->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")));576panner->setup_warped_panning(debug_uv_dialog, EDITOR_GET("editors/panning/warped_mouse_panning"));577} break;578case NOTIFICATION_THEME_CHANGED: {579options->set_button_icon(get_editor_theme_icon(SNAME("Sprite2D")));580581options->get_popup()->set_item_icon(MENU_OPTION_CONVERT_TO_MESH_2D, get_editor_theme_icon(SNAME("MeshInstance2D")));582options->get_popup()->set_item_icon(MENU_OPTION_CONVERT_TO_POLYGON_2D, get_editor_theme_icon(SNAME("Polygon2D")));583options->get_popup()->set_item_icon(MENU_OPTION_CREATE_COLLISION_POLY_2D, get_editor_theme_icon(SNAME("CollisionPolygon2D")));584options->get_popup()->set_item_icon(MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D, get_editor_theme_icon(SNAME("LightOccluder2D")));585586resize_region_rect->set_button_icon(get_editor_theme_icon(SNAME("KeepAspect")));587} break;588}589}590591void Sprite2DEditor::_bind_methods() {592ClassDB::bind_method("_add_as_sibling_or_child", &Sprite2DEditor::_add_as_sibling_or_child);593}594595void Sprite2DEditor::edit(Sprite2D *p_sprite) {596Callable callback_update_button = callable_mp(this, &Sprite2DEditor::_update_sprite_resize_mode_button);597StringName signal_name = SNAME("_editor_region_rect_enabled");598599if (node != nullptr && node->is_connected(signal_name, callback_update_button)) {600node->disconnect(signal_name, callback_update_button);601}602603node = p_sprite;604605if (node != nullptr && !node->is_connected(signal_name, callback_update_button)) {606node->connect(signal_name, callback_update_button);607}608609_update_sprite_resize_mode_button();610}611612Sprite2DEditor::Sprite2DEditor() {613// Top HBoxContainer definition614top_hb = memnew(HBoxContainer);615616CanvasItemEditor::get_singleton()->add_control_to_menu_panel(top_hb);617618// Options definition619options = memnew(MenuButton);620621top_hb->add_child(options);622623options->set_text(TTR("Sprite2D"));624options->set_flat(false);625options->set_theme_type_variation("FlatMenuButton");626627options->get_popup()->add_item(TTR("Convert to MeshInstance2D"), MENU_OPTION_CONVERT_TO_MESH_2D);628options->get_popup()->add_item(TTR("Convert to Polygon2D"), MENU_OPTION_CONVERT_TO_POLYGON_2D);629options->get_popup()->add_item(TTR("Create CollisionPolygon2D Sibling"), MENU_OPTION_CREATE_COLLISION_POLY_2D);630options->get_popup()->add_item(TTR("Create LightOccluder2D Sibling"), MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D);631options->set_switch_on_hover(true);632633options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Sprite2DEditor::_menu_option));634635// Resize region rect definition636resize_region_rect = memnew(Button);637638resize_region_rect->set_theme_type_variation("FlatMenuButton");639resize_region_rect->set_toggle_mode(true);640resize_region_rect->set_shortcut(ED_SHORTCUT("canvas_item_editor/resize_region_rect", TTRC("Drag to Resize Region Rect"), KeyModifierMask::CMD_OR_CTRL | Key::R));641642resize_region_rect->connect(SceneStringName(pressed), callable_mp(this, &Sprite2DEditor::_sync_sprite_resize_mode));643644top_hb->add_child(resize_region_rect);645646// Other elements definition647err_dialog = memnew(AcceptDialog);648add_child(err_dialog);649650debug_uv_dialog = memnew(ConfirmationDialog);651debug_uv_dialog->set_size(Size2(960, 540) * EDSCALE);652VBoxContainer *vb = memnew(VBoxContainer);653debug_uv_dialog->add_child(vb);654debug_uv = memnew(Panel);655debug_uv->connect(SceneStringName(gui_input), callable_mp(this, &Sprite2DEditor::_debug_uv_input));656debug_uv->connect(SceneStringName(draw), callable_mp(this, &Sprite2DEditor::_debug_uv_draw));657debug_uv->set_clip_contents(true);658vb->add_margin_child(TTR("Preview:"), debug_uv, true);659660panner.instantiate();661panner->set_callbacks(callable_mp(this, &Sprite2DEditor::_pan_callback), callable_mp(this, &Sprite2DEditor::_zoom_callback));662663zoom_widget = memnew(EditorZoomWidget);664debug_uv->add_child(zoom_widget);665zoom_widget->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT, Control::PRESET_MODE_MINSIZE, 2 * EDSCALE);666zoom_widget->connect("zoom_changed", callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(true));667zoom_widget->set_shortcut_context(nullptr);668669v_scroll = memnew(VScrollBar);670debug_uv->add_child(v_scroll);671v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(false));672h_scroll = memnew(HScrollBar);673debug_uv->add_child(h_scroll);674h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(false));675676debug_uv_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Sprite2DEditor::_create_node));677678HBoxContainer *hb = memnew(HBoxContainer);679hb->add_child(memnew(Label(TTR("Simplification:"))));680simplification = memnew(SpinBox);681simplification->set_min(0.01);682simplification->set_max(10.00);683simplification->set_step(0.01);684simplification->set_value(2);685simplification->set_accessibility_name(TTRC("Simplification:"));686hb->add_child(simplification);687hb->add_spacer();688hb->add_child(memnew(Label(TTR("Shrink (Pixels):"))));689shrink_pixels = memnew(SpinBox);690shrink_pixels->set_min(0);691shrink_pixels->set_max(10);692shrink_pixels->set_step(1);693shrink_pixels->set_value(0);694shrink_pixels->set_accessibility_name(TTRC("Shrink (Pixels):"));695hb->add_child(shrink_pixels);696hb->add_spacer();697hb->add_child(memnew(Label(TTR("Grow (Pixels):"))));698grow_pixels = memnew(SpinBox);699grow_pixels->set_min(0);700grow_pixels->set_max(10);701grow_pixels->set_step(1);702grow_pixels->set_value(2);703grow_pixels->set_accessibility_name(TTRC("Grow (Pixels):"));704hb->add_child(grow_pixels);705hb->add_spacer();706update_preview = memnew(Button);707update_preview->set_text(TTR("Update Preview"));708update_preview->connect(SceneStringName(pressed), callable_mp(this, &Sprite2DEditor::_update_mesh_data));709hb->add_child(update_preview);710vb->add_margin_child(TTR("Settings:"), hb);711712add_child(debug_uv_dialog);713}714715void Sprite2DEditorPlugin::edit(Object *p_object) {716sprite_editor->edit(Object::cast_to<Sprite2D>(p_object));717}718719bool Sprite2DEditorPlugin::handles(Object *p_object) const {720return p_object->is_class("Sprite2D");721}722723void Sprite2DEditorPlugin::make_visible(bool p_visible) {724if (p_visible) {725sprite_editor->top_hb->show();726} else {727sprite_editor->top_hb->hide();728sprite_editor->edit(nullptr);729}730}731732Sprite2DEditorPlugin::Sprite2DEditorPlugin() {733sprite_editor = memnew(Sprite2DEditor);734EditorNode::get_singleton()->get_gui_base()->add_child(sprite_editor);735736make_visible(false);737//sprite_editor->options->hide();738}739740741