| 1 | /**************************************************************************/ |
| 2 | /* texture_region_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 "texture_region_editor_plugin.h" |
| 32 | |
| 33 | #include "core/input/input.h" |
| 34 | #include "core/os/keyboard.h" |
| 35 | #include "editor/editor_node.h" |
| 36 | #include "editor/editor_string_names.h" |
| 37 | #include "editor/editor_undo_redo_manager.h" |
| 38 | #include "editor/settings/editor_settings.h" |
| 39 | #include "editor/themes/editor_scale.h" |
| 40 | #include "scene/2d/sprite_2d.h" |
| 41 | #include "scene/3d/sprite_3d.h" |
| 42 | #include "scene/gui/nine_patch_rect.h" |
| 43 | #include "scene/gui/option_button.h" |
| 44 | #include "scene/gui/panel_container.h" |
| 45 | #include "scene/gui/separator.h" |
| 46 | #include "scene/gui/spin_box.h" |
| 47 | #include "scene/gui/view_panner.h" |
| 48 | #include "scene/resources/atlas_texture.h" |
| 49 | #include "scene/resources/style_box_texture.h" |
| 50 | |
| 51 | Transform2D TextureRegionEditor::_get_offset_transform() const { |
| 52 | Transform2D mtx; |
| 53 | mtx.columns[2] = -draw_ofs * draw_zoom; |
| 54 | mtx.scale_basis(Vector2(draw_zoom, draw_zoom)); |
| 55 | |
| 56 | return mtx; |
| 57 | } |
| 58 | |
| 59 | void TextureRegionEditor::_texture_preview_draw() { |
| 60 | const Ref<Texture2D> object_texture = _get_edited_object_texture(); |
| 61 | if (object_texture.is_null()) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | Transform2D mtx = _get_offset_transform(); |
| 66 | |
| 67 | RS::get_singleton()->canvas_item_add_set_transform(texture_preview->get_canvas_item(), mtx); |
| 68 | texture_preview->draw_rect(Rect2(Point2(), object_texture->get_size()), Color(0.5, 0.5, 0.5, 0.5), false); |
| 69 | texture_preview->draw_texture(object_texture, Point2()); |
| 70 | RS::get_singleton()->canvas_item_add_set_transform(texture_preview->get_canvas_item(), Transform2D()); |
| 71 | } |
| 72 | |
| 73 | void TextureRegionEditor::_texture_overlay_draw() { |
| 74 | const Ref<Texture2D> object_texture = _get_edited_object_texture(); |
| 75 | if (object_texture.is_null()) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | Transform2D mtx = _get_offset_transform(); |
| 80 | const Color color = get_theme_color(SNAME("mono_color"), EditorStringName(Editor)); |
| 81 | |
| 82 | if (snap_mode == SNAP_GRID) { |
| 83 | const Color grid_color = Color(color.r, color.g, color.b, color.a * 0.15); |
| 84 | Size2 s = texture_overlay->get_size(); |
| 85 | int last_cell = 0; |
| 86 | |
| 87 | if (snap_step.x != 0) { |
| 88 | if (snap_separation.x == 0) { |
| 89 | for (int i = 0; i < s.width; i++) { |
| 90 | int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / snap_step.x)); |
| 91 | if (i == 0) { |
| 92 | last_cell = cell; |
| 93 | } |
| 94 | if (last_cell != cell) { |
| 95 | texture_overlay->draw_line(Point2(i, 0), Point2(i, s.height), grid_color); |
| 96 | } |
| 97 | last_cell = cell; |
| 98 | } |
| 99 | } else { |
| 100 | for (int i = 0; i < s.width + snap_separation.x; i++) { |
| 101 | int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / (snap_step.x + snap_separation.x))); |
| 102 | if (i == 0) { |
| 103 | last_cell = cell; |
| 104 | } |
| 105 | if (last_cell != cell) { |
| 106 | texture_overlay->draw_rect(Rect2(i - snap_separation.x * draw_zoom, 0, snap_separation.x * draw_zoom, s.height), grid_color); |
| 107 | } |
| 108 | last_cell = cell; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (snap_step.y != 0) { |
| 114 | if (snap_separation.y == 0) { |
| 115 | for (int i = 0; i < s.height; i++) { |
| 116 | int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / snap_step.y)); |
| 117 | if (i == 0) { |
| 118 | last_cell = cell; |
| 119 | } |
| 120 | if (last_cell != cell) { |
| 121 | texture_overlay->draw_line(Point2(0, i), Point2(s.width, i), grid_color); |
| 122 | } |
| 123 | last_cell = cell; |
| 124 | } |
| 125 | } else { |
| 126 | for (int i = 0; i < s.height + snap_separation.y; i++) { |
| 127 | int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / (snap_step.y + snap_separation.y))); |
| 128 | if (i == 0) { |
| 129 | last_cell = cell; |
| 130 | } |
| 131 | if (last_cell != cell) { |
| 132 | texture_overlay->draw_rect(Rect2(0, i - snap_separation.y * draw_zoom, s.width, snap_separation.y * draw_zoom), grid_color); |
| 133 | } |
| 134 | last_cell = cell; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } else if (snap_mode == SNAP_AUTOSLICE) { |
| 139 | for (const Rect2 &r : autoslice_cache) { |
| 140 | const Vector2 endpoints[4] = { |
| 141 | mtx.basis_xform(r.position), |
| 142 | mtx.basis_xform(r.position + Vector2(r.size.x, 0)), |
| 143 | mtx.basis_xform(r.position + r.size), |
| 144 | mtx.basis_xform(r.position + Vector2(0, r.size.y)) |
| 145 | }; |
| 146 | for (int i = 0; i < 4; i++) { |
| 147 | int next = (i + 1) % 4; |
| 148 | texture_overlay->draw_line(endpoints[i] - draw_ofs * draw_zoom, endpoints[next] - draw_ofs * draw_zoom, Color(0.3, 0.7, 1, 1), 2); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | Ref<Texture2D> select_handle = get_editor_theme_icon(SNAME("EditorHandle")); |
| 154 | |
| 155 | Rect2 scroll_rect(Point2(), object_texture->get_size()); |
| 156 | |
| 157 | const Vector2 raw_endpoints[4] = { |
| 158 | rect.position, |
| 159 | rect.position + Vector2(rect.size.x, 0), |
| 160 | rect.position + rect.size, |
| 161 | rect.position + Vector2(0, rect.size.y) |
| 162 | }; |
| 163 | const Vector2 endpoints[4] = { |
| 164 | mtx.basis_xform(raw_endpoints[0]), |
| 165 | mtx.basis_xform(raw_endpoints[1]), |
| 166 | mtx.basis_xform(raw_endpoints[2]), |
| 167 | mtx.basis_xform(raw_endpoints[3]) |
| 168 | }; |
| 169 | for (int i = 0; i < 4; i++) { |
| 170 | int prev = (i + 3) % 4; |
| 171 | int next = (i + 1) % 4; |
| 172 | |
| 173 | Vector2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized(); |
| 174 | ofs *= Math::SQRT2 * (select_handle->get_size().width / 2); |
| 175 | |
| 176 | texture_overlay->draw_line(endpoints[i] - draw_ofs * draw_zoom, endpoints[next] - draw_ofs * draw_zoom, color, 2); |
| 177 | |
| 178 | if (snap_mode != SNAP_AUTOSLICE) { |
| 179 | texture_overlay->draw_texture(select_handle, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor() - draw_ofs * draw_zoom); |
| 180 | } |
| 181 | |
| 182 | ofs = (endpoints[next] - endpoints[i]) / 2; |
| 183 | ofs += (endpoints[next] - endpoints[i]).orthogonal().normalized() * (select_handle->get_size().width / 2); |
| 184 | |
| 185 | if (snap_mode != SNAP_AUTOSLICE) { |
| 186 | texture_overlay->draw_texture(select_handle, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor() - draw_ofs * draw_zoom); |
| 187 | } |
| 188 | |
| 189 | scroll_rect.expand_to(raw_endpoints[i]); |
| 190 | } |
| 191 | |
| 192 | const Size2 scroll_margin = texture_overlay->get_size() / draw_zoom; |
| 193 | scroll_rect.position -= scroll_margin; |
| 194 | scroll_rect.size += scroll_margin * 2; |
| 195 | |
| 196 | updating_scroll = true; |
| 197 | |
| 198 | hscroll->set_min(scroll_rect.position.x); |
| 199 | hscroll->set_max(scroll_rect.position.x + scroll_rect.size.x); |
| 200 | if (Math::abs(scroll_rect.position.x - (scroll_rect.position.x + scroll_rect.size.x)) <= scroll_margin.x) { |
| 201 | hscroll->hide(); |
| 202 | } else { |
| 203 | hscroll->show(); |
| 204 | hscroll->set_page(scroll_margin.x); |
| 205 | hscroll->set_value(draw_ofs.x); |
| 206 | } |
| 207 | |
| 208 | vscroll->set_min(scroll_rect.position.y); |
| 209 | vscroll->set_max(scroll_rect.position.y + scroll_rect.size.y); |
| 210 | if (Math::abs(scroll_rect.position.y - (scroll_rect.position.y + scroll_rect.size.y)) <= scroll_margin.y) { |
| 211 | vscroll->hide(); |
| 212 | draw_ofs.y = scroll_rect.position.y; |
| 213 | } else { |
| 214 | vscroll->show(); |
| 215 | vscroll->set_page(scroll_margin.y); |
| 216 | vscroll->set_value(draw_ofs.y); |
| 217 | } |
| 218 | |
| 219 | Size2 hmin = hscroll->get_combined_minimum_size(); |
| 220 | Size2 vmin = vscroll->get_combined_minimum_size(); |
| 221 | |
| 222 | // Avoid scrollbar overlapping. |
| 223 | hscroll->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, vscroll->is_visible() ? -vmin.width : 0); |
| 224 | vscroll->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, hscroll->is_visible() ? -hmin.height : 0); |
| 225 | |
| 226 | updating_scroll = false; |
| 227 | |
| 228 | if (request_center && hscroll->get_min() < 0) { |
| 229 | hscroll->set_value((hscroll->get_min() + hscroll->get_max() - hscroll->get_page()) / 2); |
| 230 | vscroll->set_value((vscroll->get_min() + vscroll->get_max() - vscroll->get_page()) / 2); |
| 231 | // This ensures that the view is updated correctly. |
| 232 | callable_mp(this, &TextureRegionEditor::_pan_callback).call_deferred(Vector2(1, 0), Ref<InputEvent>()); |
| 233 | callable_mp(this, &TextureRegionEditor::_scroll_changed).call_deferred(0.0); |
| 234 | request_center = false; |
| 235 | } |
| 236 | |
| 237 | if (node_ninepatch || res_stylebox.is_valid()) { |
| 238 | float margins[4] = { 0 }; |
| 239 | if (node_ninepatch) { |
| 240 | margins[0] = node_ninepatch->get_patch_margin(SIDE_TOP); |
| 241 | margins[1] = node_ninepatch->get_patch_margin(SIDE_BOTTOM); |
| 242 | margins[2] = node_ninepatch->get_patch_margin(SIDE_LEFT); |
| 243 | margins[3] = node_ninepatch->get_patch_margin(SIDE_RIGHT); |
| 244 | } else if (res_stylebox.is_valid()) { |
| 245 | margins[0] = res_stylebox->get_texture_margin(SIDE_TOP); |
| 246 | margins[1] = res_stylebox->get_texture_margin(SIDE_BOTTOM); |
| 247 | margins[2] = res_stylebox->get_texture_margin(SIDE_LEFT); |
| 248 | margins[3] = res_stylebox->get_texture_margin(SIDE_RIGHT); |
| 249 | } |
| 250 | |
| 251 | Vector2 pos[4] = { |
| 252 | mtx.basis_xform(Vector2(0, margins[0])) + Vector2(0, endpoints[0].y - draw_ofs.y * draw_zoom), |
| 253 | -mtx.basis_xform(Vector2(0, margins[1])) + Vector2(0, endpoints[2].y - draw_ofs.y * draw_zoom), |
| 254 | mtx.basis_xform(Vector2(margins[2], 0)) + Vector2(endpoints[0].x - draw_ofs.x * draw_zoom, 0), |
| 255 | -mtx.basis_xform(Vector2(margins[3], 0)) + Vector2(endpoints[2].x - draw_ofs.x * draw_zoom, 0) |
| 256 | }; |
| 257 | |
| 258 | _draw_margin_line(pos[0], pos[0] + Vector2(texture_overlay->get_size().x, 0)); |
| 259 | _draw_margin_line(pos[1], pos[1] + Vector2(texture_overlay->get_size().x, 0)); |
| 260 | _draw_margin_line(pos[2], pos[2] + Vector2(0, texture_overlay->get_size().y)); |
| 261 | _draw_margin_line(pos[3], pos[3] + Vector2(0, texture_overlay->get_size().y)); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void TextureRegionEditor::_draw_margin_line(Vector2 p_from, Vector2 p_to) { |
| 266 | // Margin line is a dashed line with a normalized dash length. This method works |
| 267 | // for both vertical and horizontal lines. |
| 268 | |
| 269 | Vector2 dash_size = (p_to - p_from).normalized() * 10; |
| 270 | const int dash_thickness = Math::round(2 * EDSCALE); |
| 271 | const Color dash_color = get_theme_color(SNAME("mono_color"), EditorStringName(Editor)); |
| 272 | const Color dash_bg_color = dash_color.inverted() * Color(1, 1, 1, 0.5); |
| 273 | const int line_threshold = 200; |
| 274 | |
| 275 | // Draw a translucent background line to make the foreground line visible on any background. |
| 276 | texture_overlay->draw_line(p_from, p_to, dash_bg_color, dash_thickness); |
| 277 | |
| 278 | Vector2 dash_start = p_from; |
| 279 | while (dash_start.distance_squared_to(p_to) > line_threshold) { |
| 280 | texture_overlay->draw_line(dash_start, dash_start + dash_size, dash_color, dash_thickness); |
| 281 | |
| 282 | // Skip two size lengths, one for the drawn dash and one for the gap. |
| 283 | dash_start += dash_size * 2; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void TextureRegionEditor::_set_grid_parameters_clamping(bool p_enabled) { |
| 288 | sb_off_x->set_allow_lesser(!p_enabled); |
| 289 | sb_off_x->set_allow_greater(!p_enabled); |
| 290 | sb_off_y->set_allow_lesser(!p_enabled); |
| 291 | sb_off_y->set_allow_greater(!p_enabled); |
| 292 | sb_step_x->set_allow_greater(!p_enabled); |
| 293 | sb_step_y->set_allow_greater(!p_enabled); |
| 294 | sb_sep_x->set_allow_greater(!p_enabled); |
| 295 | sb_sep_y->set_allow_greater(!p_enabled); |
| 296 | } |
| 297 | |
| 298 | void TextureRegionEditor::_commit_drag() { |
| 299 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 300 | if (edited_margin >= 0) { |
| 301 | undo_redo->create_action(TTR("Set Margin")); |
| 302 | static Side side[4] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT }; |
| 303 | if (node_ninepatch) { |
| 304 | undo_redo->add_do_method(node_ninepatch, "set_patch_margin", side[edited_margin], node_ninepatch->get_patch_margin(side[edited_margin])); |
| 305 | undo_redo->add_undo_method(node_ninepatch, "set_patch_margin", side[edited_margin], prev_margin); |
| 306 | } else if (res_stylebox.is_valid()) { |
| 307 | undo_redo->add_do_method(res_stylebox.ptr(), "set_texture_margin", side[edited_margin], res_stylebox->get_texture_margin(side[edited_margin])); |
| 308 | undo_redo->add_undo_method(res_stylebox.ptr(), "set_texture_margin", side[edited_margin], prev_margin); |
| 309 | res_stylebox->emit_changed(); |
| 310 | } |
| 311 | edited_margin = -1; |
| 312 | } else { |
| 313 | undo_redo->create_action(TTR("Set Region Rect")); |
| 314 | if (node_sprite_2d) { |
| 315 | undo_redo->add_do_method(node_sprite_2d, "set_region_rect", node_sprite_2d->get_region_rect()); |
| 316 | undo_redo->add_undo_method(node_sprite_2d, "set_region_rect", rect_prev); |
| 317 | } else if (node_sprite_3d) { |
| 318 | undo_redo->add_do_method(node_sprite_3d, "set_region_rect", node_sprite_3d->get_region_rect()); |
| 319 | undo_redo->add_undo_method(node_sprite_3d, "set_region_rect", rect_prev); |
| 320 | } else if (node_ninepatch) { |
| 321 | undo_redo->add_do_method(node_ninepatch, "set_region_rect", node_ninepatch->get_region_rect()); |
| 322 | undo_redo->add_undo_method(node_ninepatch, "set_region_rect", rect_prev); |
| 323 | } else if (res_stylebox.is_valid()) { |
| 324 | undo_redo->add_do_method(res_stylebox.ptr(), "set_region_rect", res_stylebox->get_region_rect()); |
| 325 | undo_redo->add_undo_method(res_stylebox.ptr(), "set_region_rect", rect_prev); |
| 326 | } else if (res_atlas_texture.is_valid()) { |
| 327 | undo_redo->add_do_method(res_atlas_texture.ptr(), "set_region", res_atlas_texture->get_region()); |
| 328 | undo_redo->add_undo_method(res_atlas_texture.ptr(), "set_region", rect_prev); |
| 329 | } |
| 330 | drag_index = -1; |
| 331 | } |
| 332 | |
| 333 | undo_redo->add_do_method(this, "_update_rect"); |
| 334 | undo_redo->add_undo_method(this, "_update_rect"); |
| 335 | undo_redo->add_do_method(texture_overlay, "queue_redraw"); |
| 336 | undo_redo->add_undo_method(texture_overlay, "queue_redraw"); |
| 337 | undo_redo->commit_action(); |
| 338 | drag = false; |
| 339 | creating = false; |
| 340 | } |
| 341 | |
| 342 | void TextureRegionEditor::_texture_overlay_input(const Ref<InputEvent> &p_input) { |
| 343 | if (panner->gui_input(p_input, texture_overlay->get_global_rect())) { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | Transform2D mtx; |
| 348 | mtx.columns[2] = -draw_ofs * draw_zoom; |
| 349 | mtx.scale_basis(Vector2(draw_zoom, draw_zoom)); |
| 350 | |
| 351 | bool cancel_drag = false; |
| 352 | |
| 353 | Ref<InputEventMouseButton> mb = p_input; |
| 354 | if (mb.is_valid()) { |
| 355 | if (mb->get_button_index() == MouseButton::LEFT) { |
| 356 | if (mb->is_pressed() && !panner->is_panning()) { |
| 357 | // Check if we click on any handle first. |
| 358 | { |
| 359 | const real_t handle_radius = 16 * EDSCALE; |
| 360 | const real_t handle_offset = 8 * EDSCALE; |
| 361 | |
| 362 | // Position of selection handles. |
| 363 | const Vector2 endpoints[8] = { |
| 364 | mtx.xform(rect.position) + Vector2(-handle_offset, -handle_offset), |
| 365 | mtx.xform(rect.position + Vector2(rect.size.x / 2, 0)) + Vector2(0, -handle_offset), |
| 366 | mtx.xform(rect.position + Vector2(rect.size.x, 0)) + Vector2(handle_offset, -handle_offset), |
| 367 | mtx.xform(rect.position + Vector2(rect.size.x, rect.size.y / 2)) + Vector2(handle_offset, 0), |
| 368 | mtx.xform(rect.position + rect.size) + Vector2(handle_offset, handle_offset), |
| 369 | mtx.xform(rect.position + Vector2(rect.size.x / 2, rect.size.y)) + Vector2(0, handle_offset), |
| 370 | mtx.xform(rect.position + Vector2(0, rect.size.y)) + Vector2(-handle_offset, handle_offset), |
| 371 | mtx.xform(rect.position + Vector2(0, rect.size.y / 2)) + Vector2(-handle_offset, 0) |
| 372 | }; |
| 373 | |
| 374 | drag_from = mtx.affine_inverse().xform(mb->get_position()); |
| 375 | if (snap_mode == SNAP_PIXEL) { |
| 376 | drag_from = drag_from.snappedf(1); |
| 377 | } else if (snap_mode == SNAP_GRID) { |
| 378 | drag_from = snap_point(drag_from); |
| 379 | } |
| 380 | drag = true; |
| 381 | |
| 382 | rect_prev = _get_edited_object_region(); |
| 383 | |
| 384 | for (int i = 0; i < 8; i++) { |
| 385 | Vector2 tuv = endpoints[i]; |
| 386 | if (tuv.distance_to(mb->get_position()) < handle_radius) { |
| 387 | drag_index = i; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // We didn't hit any handle, try other options. |
| 393 | if (drag_index < 0) { |
| 394 | if (node_ninepatch || res_stylebox.is_valid()) { |
| 395 | // For ninepatchable objects check if we are clicking on margin bars. |
| 396 | |
| 397 | edited_margin = -1; |
| 398 | float margins[4] = { 0 }; |
| 399 | if (node_ninepatch) { |
| 400 | margins[0] = node_ninepatch->get_patch_margin(SIDE_TOP); |
| 401 | margins[1] = node_ninepatch->get_patch_margin(SIDE_BOTTOM); |
| 402 | margins[2] = node_ninepatch->get_patch_margin(SIDE_LEFT); |
| 403 | margins[3] = node_ninepatch->get_patch_margin(SIDE_RIGHT); |
| 404 | } else if (res_stylebox.is_valid()) { |
| 405 | margins[0] = res_stylebox->get_texture_margin(SIDE_TOP); |
| 406 | margins[1] = res_stylebox->get_texture_margin(SIDE_BOTTOM); |
| 407 | margins[2] = res_stylebox->get_texture_margin(SIDE_LEFT); |
| 408 | margins[3] = res_stylebox->get_texture_margin(SIDE_RIGHT); |
| 409 | } |
| 410 | |
| 411 | Vector2 pos[4] = { |
| 412 | mtx.basis_xform(rect.position + Vector2(0, margins[0])) - draw_ofs * draw_zoom, |
| 413 | mtx.basis_xform(rect.position + rect.size - Vector2(0, margins[1])) - draw_ofs * draw_zoom, |
| 414 | mtx.basis_xform(rect.position + Vector2(margins[2], 0)) - draw_ofs * draw_zoom, |
| 415 | mtx.basis_xform(rect.position + rect.size - Vector2(margins[3], 0)) - draw_ofs * draw_zoom |
| 416 | }; |
| 417 | if (Math::abs(mb->get_position().y - pos[0].y) < 8) { |
| 418 | edited_margin = 0; |
| 419 | prev_margin = margins[0]; |
| 420 | } else if (Math::abs(mb->get_position().y - pos[1].y) < 8) { |
| 421 | edited_margin = 1; |
| 422 | prev_margin = margins[1]; |
| 423 | } else if (Math::abs(mb->get_position().x - pos[2].x) < 8) { |
| 424 | edited_margin = 2; |
| 425 | prev_margin = margins[2]; |
| 426 | } else if (Math::abs(mb->get_position().x - pos[3].x) < 8) { |
| 427 | edited_margin = 3; |
| 428 | prev_margin = margins[3]; |
| 429 | } |
| 430 | if (edited_margin >= 0) { |
| 431 | drag_from = mb->get_position(); |
| 432 | drag = true; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | if (edited_margin < 0 && snap_mode == SNAP_AUTOSLICE) { |
| 437 | // We didn't hit anything, but we're in the autoslice mode. Handle it. |
| 438 | |
| 439 | Vector2 point = mtx.affine_inverse().xform(mb->get_position()); |
| 440 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 441 | for (const Rect2 &E : autoslice_cache) { |
| 442 | if (E.has_point(point)) { |
| 443 | rect = E; |
| 444 | if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !(Input::get_singleton()->is_key_pressed(Key(Key::SHIFT | Key::ALT)))) { |
| 445 | Rect2 r; |
| 446 | if (node_sprite_2d) { |
| 447 | r = node_sprite_2d->get_region_rect(); |
| 448 | } else if (node_sprite_3d) { |
| 449 | r = node_sprite_3d->get_region_rect(); |
| 450 | } else if (node_ninepatch) { |
| 451 | r = node_ninepatch->get_region_rect(); |
| 452 | } else if (res_stylebox.is_valid()) { |
| 453 | r = res_stylebox->get_region_rect(); |
| 454 | } else if (res_atlas_texture.is_valid()) { |
| 455 | r = res_atlas_texture->get_region(); |
| 456 | } |
| 457 | rect.expand_to(r.position); |
| 458 | rect.expand_to(r.get_end()); |
| 459 | } |
| 460 | |
| 461 | undo_redo->create_action(TTR("Set Region Rect")); |
| 462 | if (node_sprite_2d) { |
| 463 | undo_redo->add_do_method(node_sprite_2d, "set_region_rect", rect); |
| 464 | undo_redo->add_undo_method(node_sprite_2d, "set_region_rect", node_sprite_2d->get_region_rect()); |
| 465 | } else if (node_sprite_3d) { |
| 466 | undo_redo->add_do_method(node_sprite_3d, "set_region_rect", rect); |
| 467 | undo_redo->add_undo_method(node_sprite_3d, "set_region_rect", node_sprite_3d->get_region_rect()); |
| 468 | } else if (node_ninepatch) { |
| 469 | undo_redo->add_do_method(node_ninepatch, "set_region_rect", rect); |
| 470 | undo_redo->add_undo_method(node_ninepatch, "set_region_rect", node_ninepatch->get_region_rect()); |
| 471 | } else if (res_stylebox.is_valid()) { |
| 472 | undo_redo->add_do_method(res_stylebox.ptr(), "set_region_rect", rect); |
| 473 | undo_redo->add_undo_method(res_stylebox.ptr(), "set_region_rect", res_stylebox->get_region_rect()); |
| 474 | } else if (res_atlas_texture.is_valid()) { |
| 475 | undo_redo->add_do_method(res_atlas_texture.ptr(), "set_region", rect); |
| 476 | undo_redo->add_undo_method(res_atlas_texture.ptr(), "set_region", res_atlas_texture->get_region()); |
| 477 | } |
| 478 | |
| 479 | undo_redo->add_do_method(this, "_update_rect"); |
| 480 | undo_redo->add_undo_method(this, "_update_rect"); |
| 481 | undo_redo->add_do_method(texture_overlay, "queue_redraw"); |
| 482 | undo_redo->add_undo_method(texture_overlay, "queue_redraw"); |
| 483 | undo_redo->commit_action(); |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | } else if (edited_margin < 0) { |
| 488 | // We didn't hit anything and it's not autoslice, which means we try to create a new region. |
| 489 | |
| 490 | if (drag_index == -1) { |
| 491 | creating = true; |
| 492 | rect = Rect2(drag_from, Size2()); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | } else if (!mb->is_pressed() && drag) { |
| 498 | _commit_drag(); |
| 499 | } |
| 500 | } else if (drag && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) { |
| 501 | cancel_drag = true; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if (drag && p_input.is_valid() && p_input->is_action_pressed(SNAME("ui_cancel"), false, true)) { |
| 506 | cancel_drag = true; |
| 507 | } |
| 508 | |
| 509 | if (cancel_drag) { |
| 510 | drag = false; |
| 511 | if (edited_margin >= 0) { |
| 512 | static Side side[4] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT }; |
| 513 | if (node_ninepatch) { |
| 514 | node_ninepatch->set_patch_margin(side[edited_margin], prev_margin); |
| 515 | } |
| 516 | if (res_stylebox.is_valid()) { |
| 517 | res_stylebox->set_texture_margin(side[edited_margin], prev_margin); |
| 518 | } |
| 519 | edited_margin = -1; |
| 520 | } else { |
| 521 | _apply_rect(rect_prev); |
| 522 | rect = rect_prev; |
| 523 | texture_preview->queue_redraw(); |
| 524 | texture_overlay->queue_redraw(); |
| 525 | drag_index = -1; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | Ref<InputEventMouseMotion> mm = p_input; |
| 530 | |
| 531 | if (drag && mm.is_valid()) { |
| 532 | if (mm->get_button_mask().has_flag(MouseButtonMask::LEFT)) { |
| 533 | if (edited_margin >= 0) { |
| 534 | float new_margin = 0; |
| 535 | |
| 536 | if (snap_mode != SNAP_GRID) { |
| 537 | if (edited_margin == 0) { |
| 538 | new_margin = prev_margin + (mm->get_position().y - drag_from.y) / draw_zoom; |
| 539 | } else if (edited_margin == 1) { |
| 540 | new_margin = prev_margin - (mm->get_position().y - drag_from.y) / draw_zoom; |
| 541 | } else if (edited_margin == 2) { |
| 542 | new_margin = prev_margin + (mm->get_position().x - drag_from.x) / draw_zoom; |
| 543 | } else if (edited_margin == 3) { |
| 544 | new_margin = prev_margin - (mm->get_position().x - drag_from.x) / draw_zoom; |
| 545 | } else { |
| 546 | ERR_PRINT("Unexpected edited_margin"); |
| 547 | } |
| 548 | |
| 549 | if (snap_mode == SNAP_PIXEL) { |
| 550 | new_margin = Math::round(new_margin); |
| 551 | } |
| 552 | } else { |
| 553 | Vector2 pos_snapped = snap_point(mtx.affine_inverse().xform(mm->get_position())); |
| 554 | Rect2 rect_rounded = Rect2(rect.position.round(), rect.size.round()); |
| 555 | |
| 556 | if (edited_margin == 0) { |
| 557 | new_margin = pos_snapped.y - rect_rounded.position.y; |
| 558 | } else if (edited_margin == 1) { |
| 559 | new_margin = rect_rounded.size.y + rect_rounded.position.y - pos_snapped.y; |
| 560 | } else if (edited_margin == 2) { |
| 561 | new_margin = pos_snapped.x - rect_rounded.position.x; |
| 562 | } else if (edited_margin == 3) { |
| 563 | new_margin = rect_rounded.size.x + rect_rounded.position.x - pos_snapped.x; |
| 564 | } else { |
| 565 | ERR_PRINT("Unexpected edited_margin"); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | if (new_margin < 0) { |
| 570 | new_margin = 0; |
| 571 | } |
| 572 | static Side side[4] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT }; |
| 573 | if (node_ninepatch) { |
| 574 | node_ninepatch->set_patch_margin(side[edited_margin], new_margin); |
| 575 | } |
| 576 | if (res_stylebox.is_valid()) { |
| 577 | res_stylebox->set_texture_margin(side[edited_margin], new_margin); |
| 578 | } |
| 579 | } else { |
| 580 | Vector2 new_pos = mtx.affine_inverse().xform(mm->get_position()); |
| 581 | if (snap_mode == SNAP_PIXEL) { |
| 582 | new_pos = new_pos.snappedf(1); |
| 583 | } else if (snap_mode == SNAP_GRID) { |
| 584 | new_pos = snap_point(new_pos); |
| 585 | } |
| 586 | |
| 587 | if (creating) { |
| 588 | rect = Rect2(drag_from, Size2()); |
| 589 | rect.expand_to(new_pos); |
| 590 | _apply_rect(rect); |
| 591 | texture_preview->queue_redraw(); |
| 592 | texture_overlay->queue_redraw(); |
| 593 | return; |
| 594 | } |
| 595 | |
| 596 | switch (drag_index) { |
| 597 | case 0: { |
| 598 | Vector2 p = rect_prev.get_end(); |
| 599 | rect = Rect2(p, Size2()); |
| 600 | rect.expand_to(new_pos); |
| 601 | _apply_rect(rect); |
| 602 | } break; |
| 603 | case 1: { |
| 604 | Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y); |
| 605 | rect = Rect2(p, Size2(rect_prev.size.x, 0)); |
| 606 | rect.expand_to(new_pos); |
| 607 | _apply_rect(rect); |
| 608 | } break; |
| 609 | case 2: { |
| 610 | Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y); |
| 611 | rect = Rect2(p, Size2()); |
| 612 | rect.expand_to(new_pos); |
| 613 | _apply_rect(rect); |
| 614 | } break; |
| 615 | case 3: { |
| 616 | Vector2 p = rect_prev.position; |
| 617 | rect = Rect2(p, Size2(0, rect_prev.size.y)); |
| 618 | rect.expand_to(new_pos); |
| 619 | _apply_rect(rect); |
| 620 | } break; |
| 621 | case 4: { |
| 622 | Vector2 p = rect_prev.position; |
| 623 | rect = Rect2(p, Size2()); |
| 624 | rect.expand_to(new_pos); |
| 625 | _apply_rect(rect); |
| 626 | } break; |
| 627 | case 5: { |
| 628 | Vector2 p = rect_prev.position; |
| 629 | rect = Rect2(p, Size2(rect_prev.size.x, 0)); |
| 630 | rect.expand_to(new_pos); |
| 631 | _apply_rect(rect); |
| 632 | } break; |
| 633 | case 6: { |
| 634 | Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0); |
| 635 | rect = Rect2(p, Size2()); |
| 636 | rect.expand_to(new_pos); |
| 637 | _apply_rect(rect); |
| 638 | } break; |
| 639 | case 7: { |
| 640 | Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0); |
| 641 | rect = Rect2(p, Size2(0, rect_prev.size.y)); |
| 642 | rect.expand_to(new_pos); |
| 643 | _apply_rect(rect); |
| 644 | } break; |
| 645 | } |
| 646 | } |
| 647 | texture_preview->queue_redraw(); |
| 648 | texture_overlay->queue_redraw(); |
| 649 | } else { |
| 650 | _commit_drag(); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | Ref<InputEventMagnifyGesture> magnify_gesture = p_input; |
| 655 | if (magnify_gesture.is_valid()) { |
| 656 | _zoom_on_position(draw_zoom * magnify_gesture->get_factor(), magnify_gesture->get_position()); |
| 657 | } |
| 658 | |
| 659 | Ref<InputEventPanGesture> pan_gesture = p_input; |
| 660 | if (pan_gesture.is_valid()) { |
| 661 | hscroll->set_value(hscroll->get_value() + hscroll->get_page() * pan_gesture->get_delta().x / 8); |
| 662 | vscroll->set_value(vscroll->get_value() + vscroll->get_page() * pan_gesture->get_delta().y / 8); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | void TextureRegionEditor::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) { |
| 667 | p_scroll_vec /= draw_zoom; |
| 668 | hscroll->set_value(hscroll->get_value() - p_scroll_vec.x); |
| 669 | vscroll->set_value(vscroll->get_value() - p_scroll_vec.y); |
| 670 | } |
| 671 | |
| 672 | void TextureRegionEditor::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) { |
| 673 | _zoom_on_position(draw_zoom * p_zoom_factor, p_origin); |
| 674 | } |
| 675 | |
| 676 | void TextureRegionEditor::_input_from_window(const Ref<InputEvent> &p_event) { |
| 677 | if (!drag && p_event.is_valid() && p_event->is_action_pressed(SNAME("ui_cancel"), false, true)) { |
| 678 | hide(); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | void TextureRegionEditor::_scroll_changed(float) { |
| 683 | if (updating_scroll) { |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | draw_ofs.x = hscroll->get_value(); |
| 688 | draw_ofs.y = vscroll->get_value(); |
| 689 | |
| 690 | texture_preview->queue_redraw(); |
| 691 | texture_overlay->queue_redraw(); |
| 692 | } |
| 693 | |
| 694 | void TextureRegionEditor::_set_snap_mode(int p_mode) { |
| 695 | snap_mode = (SnapMode)p_mode; |
| 696 | |
| 697 | hb_grid->set_visible(snap_mode == SNAP_GRID); |
| 698 | if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) { |
| 699 | _update_autoslice(); |
| 700 | } |
| 701 | |
| 702 | texture_overlay->queue_redraw(); |
| 703 | } |
| 704 | |
| 705 | void TextureRegionEditor::_set_snap_off_x(float p_val) { |
| 706 | snap_offset.x = p_val; |
| 707 | texture_overlay->queue_redraw(); |
| 708 | } |
| 709 | |
| 710 | void TextureRegionEditor::_set_snap_off_y(float p_val) { |
| 711 | snap_offset.y = p_val; |
| 712 | texture_overlay->queue_redraw(); |
| 713 | } |
| 714 | |
| 715 | void TextureRegionEditor::_set_snap_step_x(float p_val) { |
| 716 | snap_step.x = p_val; |
| 717 | texture_overlay->queue_redraw(); |
| 718 | } |
| 719 | |
| 720 | void TextureRegionEditor::_set_snap_step_y(float p_val) { |
| 721 | snap_step.y = p_val; |
| 722 | texture_overlay->queue_redraw(); |
| 723 | } |
| 724 | |
| 725 | void TextureRegionEditor::_set_snap_sep_x(float p_val) { |
| 726 | snap_separation.x = p_val; |
| 727 | texture_overlay->queue_redraw(); |
| 728 | } |
| 729 | |
| 730 | void TextureRegionEditor::_set_snap_sep_y(float p_val) { |
| 731 | snap_separation.y = p_val; |
| 732 | texture_overlay->queue_redraw(); |
| 733 | } |
| 734 | |
| 735 | void TextureRegionEditor::_zoom_on_position(float p_zoom, Point2 p_position) { |
| 736 | if (p_zoom < min_draw_zoom || p_zoom > max_draw_zoom) { |
| 737 | return; |
| 738 | } |
| 739 | |
| 740 | float prev_zoom = draw_zoom; |
| 741 | draw_zoom = p_zoom; |
| 742 | Point2 ofs = p_position; |
| 743 | ofs = ofs / prev_zoom - ofs / draw_zoom; |
| 744 | draw_ofs = (draw_ofs + ofs).round(); |
| 745 | |
| 746 | texture_preview->queue_redraw(); |
| 747 | texture_overlay->queue_redraw(); |
| 748 | } |
| 749 | |
| 750 | void TextureRegionEditor::_zoom_in() { |
| 751 | _zoom_on_position(draw_zoom * 1.5, texture_overlay->get_size() / 2.0); |
| 752 | } |
| 753 | |
| 754 | void TextureRegionEditor::_zoom_reset() { |
| 755 | _zoom_on_position(1.0, texture_overlay->get_size() / 2.0); |
| 756 | } |
| 757 | |
| 758 | void TextureRegionEditor::_zoom_out() { |
| 759 | _zoom_on_position(draw_zoom / 1.5, texture_overlay->get_size() / 2.0); |
| 760 | } |
| 761 | |
| 762 | void TextureRegionEditor::_apply_rect(const Rect2 &p_rect) { |
| 763 | if (node_sprite_2d) { |
| 764 | node_sprite_2d->set_region_rect(p_rect); |
| 765 | } else if (node_sprite_3d) { |
| 766 | node_sprite_3d->set_region_rect(p_rect); |
| 767 | } else if (node_ninepatch) { |
| 768 | node_ninepatch->set_region_rect(p_rect); |
| 769 | } else if (res_stylebox.is_valid()) { |
| 770 | res_stylebox->set_region_rect(p_rect); |
| 771 | } else if (res_atlas_texture.is_valid()) { |
| 772 | res_atlas_texture->set_region(p_rect); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | void TextureRegionEditor::_update_rect() { |
| 777 | rect = _get_edited_object_region(); |
| 778 | } |
| 779 | |
| 780 | void TextureRegionEditor::_update_autoslice() { |
| 781 | autoslice_is_dirty = false; |
| 782 | autoslice_cache.clear(); |
| 783 | |
| 784 | const Ref<Texture2D> object_texture = _get_edited_object_texture(); |
| 785 | if (object_texture.is_null()) { |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | for (int y = 0; y < object_texture->get_height(); y++) { |
| 790 | for (int x = 0; x < object_texture->get_width(); x++) { |
| 791 | if (object_texture->is_pixel_opaque(x, y)) { |
| 792 | bool found = false; |
| 793 | for (Rect2 &E : autoslice_cache) { |
| 794 | Rect2 grown = E.grow(1.5); |
| 795 | if (grown.has_point(Point2(x, y))) { |
| 796 | E.expand_to(Point2(x, y)); |
| 797 | E.expand_to(Point2(x + 1, y + 1)); |
| 798 | x = E.position.x + E.size.x - 1; |
| 799 | bool merged = true; |
| 800 | while (merged) { |
| 801 | merged = false; |
| 802 | bool queue_erase = false; |
| 803 | for (List<Rect2>::Element *F = autoslice_cache.front(); F; F = F->next()) { |
| 804 | if (queue_erase) { |
| 805 | autoslice_cache.erase(F->prev()); |
| 806 | queue_erase = false; |
| 807 | } |
| 808 | if (F->get() == E) { |
| 809 | continue; |
| 810 | } |
| 811 | if (E.grow(1).intersects(F->get())) { |
| 812 | E.expand_to(F->get().position); |
| 813 | E.expand_to(F->get().position + F->get().size); |
| 814 | if (F->prev()) { |
| 815 | F = F->prev(); |
| 816 | autoslice_cache.erase(F->next()); |
| 817 | } else { |
| 818 | queue_erase = true; |
| 819 | // Can't delete the first rect in the list. |
| 820 | } |
| 821 | merged = true; |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | found = true; |
| 826 | break; |
| 827 | } |
| 828 | } |
| 829 | if (!found) { |
| 830 | Rect2 new_rect(x, y, 1, 1); |
| 831 | autoslice_cache.push_back(new_rect); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | cache_map[object_texture->get_rid()] = autoslice_cache; |
| 837 | } |
| 838 | |
| 839 | void TextureRegionEditor::_notification(int p_what) { |
| 840 | switch (p_what) { |
| 841 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 842 | if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) { |
| 843 | 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"))); |
| 844 | panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning")); |
| 845 | } |
| 846 | } break; |
| 847 | |
| 848 | case NOTIFICATION_ENTER_TREE: { |
| 849 | get_tree()->connect("node_removed", callable_mp(this, &TextureRegionEditor::_node_removed)); |
| 850 | |
| 851 | hb_grid->set_visible(snap_mode == SNAP_GRID); |
| 852 | if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) { |
| 853 | _update_autoslice(); |
| 854 | } |
| 855 | |
| 856 | 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"))); |
| 857 | panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning")); |
| 858 | } break; |
| 859 | |
| 860 | case NOTIFICATION_EXIT_TREE: { |
| 861 | get_tree()->disconnect("node_removed", callable_mp(this, &TextureRegionEditor::_node_removed)); |
| 862 | } break; |
| 863 | |
| 864 | case NOTIFICATION_THEME_CHANGED: { |
| 865 | texture_preview->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("TextureRegionPreviewBG"), EditorStringName(EditorStyles))); |
| 866 | texture_overlay->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("TextureRegionPreviewFG"), EditorStringName(EditorStyles))); |
| 867 | |
| 868 | zoom_out->set_button_icon(get_editor_theme_icon(SNAME("ZoomLess"))); |
| 869 | zoom_reset->set_button_icon(get_editor_theme_icon(SNAME("ZoomReset"))); |
| 870 | zoom_in->set_button_icon(get_editor_theme_icon(SNAME("ZoomMore"))); |
| 871 | } break; |
| 872 | |
| 873 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 874 | if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) { |
| 875 | _update_autoslice(); |
| 876 | } |
| 877 | |
| 878 | if (!is_visible()) { |
| 879 | EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_offset", snap_offset); |
| 880 | EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_step", snap_step); |
| 881 | EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_separation", snap_separation); |
| 882 | EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_mode", snap_mode); |
| 883 | } |
| 884 | } break; |
| 885 | |
| 886 | case NOTIFICATION_WM_WINDOW_FOCUS_OUT: { |
| 887 | if (drag) { |
| 888 | _commit_drag(); |
| 889 | } |
| 890 | } break; |
| 891 | |
| 892 | case NOTIFICATION_WM_WINDOW_FOCUS_IN: { |
| 893 | // This happens when the user leaves the Editor and returns, |
| 894 | // they could have changed the textures, so the cache is cleared. |
| 895 | cache_map.clear(); |
| 896 | _edit_region(); |
| 897 | } break; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | void TextureRegionEditor::_node_removed(Node *p_node) { |
| 902 | if (p_node == node_sprite_2d || p_node == node_sprite_3d || p_node == node_ninepatch) { |
| 903 | _clear_edited_object(); |
| 904 | hide(); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | void TextureRegionEditor::_clear_edited_object() { |
| 909 | if (node_sprite_2d) { |
| 910 | node_sprite_2d->disconnect(SceneStringName(texture_changed), callable_mp(this, &TextureRegionEditor::_texture_changed)); |
| 911 | node_sprite_2d->disconnect(SceneStringName(item_rect_changed), callable_mp(this, &TextureRegionEditor::_edit_region)); |
| 912 | } |
| 913 | if (node_sprite_3d) { |
| 914 | node_sprite_3d->disconnect(SceneStringName(texture_changed), callable_mp(this, &TextureRegionEditor::_texture_changed)); |
| 915 | node_sprite_3d->disconnect(SceneStringName(item_rect_changed), callable_mp(this, &TextureRegionEditor::_edit_region)); |
| 916 | } |
| 917 | if (node_ninepatch) { |
| 918 | node_ninepatch->disconnect(SceneStringName(texture_changed), callable_mp(this, &TextureRegionEditor::_texture_changed)); |
| 919 | node_ninepatch->disconnect(SceneStringName(item_rect_changed), callable_mp(this, &TextureRegionEditor::_edit_region)); |
| 920 | } |
| 921 | if (res_stylebox.is_valid()) { |
| 922 | res_stylebox->disconnect_changed(callable_mp(this, &TextureRegionEditor::_texture_changed)); |
| 923 | res_stylebox->disconnect_changed(callable_mp(this, &TextureRegionEditor::_edit_region)); |
| 924 | } |
| 925 | if (res_atlas_texture.is_valid()) { |
| 926 | res_atlas_texture->disconnect_changed(callable_mp(this, &TextureRegionEditor::_texture_changed)); |
| 927 | res_atlas_texture->disconnect_changed(callable_mp(this, &TextureRegionEditor::_edit_region)); |
| 928 | } |
| 929 | |
| 930 | node_sprite_2d = nullptr; |
| 931 | node_sprite_3d = nullptr; |
| 932 | node_ninepatch = nullptr; |
| 933 | res_stylebox = Ref<StyleBoxTexture>(); |
| 934 | res_atlas_texture = Ref<AtlasTexture>(); |
| 935 | } |
| 936 | |
| 937 | void TextureRegionEditor::edit(Object *p_obj) { |
| 938 | _clear_edited_object(); |
| 939 | |
| 940 | if (p_obj) { |
| 941 | node_sprite_2d = Object::cast_to<Sprite2D>(p_obj); |
| 942 | node_sprite_3d = Object::cast_to<Sprite3D>(p_obj); |
| 943 | node_ninepatch = Object::cast_to<NinePatchRect>(p_obj); |
| 944 | |
| 945 | bool is_resource = false; |
| 946 | if (Object::cast_to<StyleBoxTexture>(p_obj)) { |
| 947 | res_stylebox = Ref<StyleBoxTexture>(p_obj); |
| 948 | is_resource = true; |
| 949 | } |
| 950 | if (Object::cast_to<AtlasTexture>(p_obj)) { |
| 951 | res_atlas_texture = Ref<AtlasTexture>(p_obj); |
| 952 | is_resource = true; |
| 953 | } |
| 954 | |
| 955 | if (is_resource) { |
| 956 | Object::cast_to<Resource>(p_obj)->connect_changed(callable_mp(this, &TextureRegionEditor::_texture_changed)); |
| 957 | Object::cast_to<Resource>(p_obj)->connect_changed(callable_mp(this, &TextureRegionEditor::_edit_region)); |
| 958 | } else { |
| 959 | p_obj->connect(SceneStringName(texture_changed), callable_mp(this, &TextureRegionEditor::_texture_changed)); |
| 960 | p_obj->connect(SceneStringName(item_rect_changed), callable_mp(this, &TextureRegionEditor::_edit_region)); |
| 961 | } |
| 962 | _edit_region(); |
| 963 | } |
| 964 | |
| 965 | texture_preview->queue_redraw(); |
| 966 | texture_overlay->queue_redraw(); |
| 967 | popup_centered_ratio(0.5); |
| 968 | request_center = true; |
| 969 | } |
| 970 | |
| 971 | Ref<Texture2D> TextureRegionEditor::_get_edited_object_texture() const { |
| 972 | if (node_sprite_2d) { |
| 973 | return node_sprite_2d->get_texture(); |
| 974 | } |
| 975 | if (node_sprite_3d) { |
| 976 | return node_sprite_3d->get_texture(); |
| 977 | } |
| 978 | if (node_ninepatch) { |
| 979 | return node_ninepatch->get_texture(); |
| 980 | } |
| 981 | if (res_stylebox.is_valid()) { |
| 982 | return res_stylebox->get_texture(); |
| 983 | } |
| 984 | if (res_atlas_texture.is_valid()) { |
| 985 | return res_atlas_texture->get_atlas(); |
| 986 | } |
| 987 | |
| 988 | return Ref<Texture2D>(); |
| 989 | } |
| 990 | |
| 991 | Rect2 TextureRegionEditor::_get_edited_object_region() const { |
| 992 | Rect2 region; |
| 993 | |
| 994 | if (node_sprite_2d) { |
| 995 | region = node_sprite_2d->get_region_rect(); |
| 996 | } else if (node_sprite_3d) { |
| 997 | region = node_sprite_3d->get_region_rect(); |
| 998 | } else if (node_ninepatch) { |
| 999 | region = node_ninepatch->get_region_rect(); |
| 1000 | } else if (res_stylebox.is_valid()) { |
| 1001 | region = res_stylebox->get_region_rect(); |
| 1002 | } else if (res_atlas_texture.is_valid()) { |
| 1003 | region = res_atlas_texture->get_region(); |
| 1004 | } |
| 1005 | |
| 1006 | const Ref<Texture2D> object_texture = _get_edited_object_texture(); |
| 1007 | if (region == Rect2() && object_texture.is_valid()) { |
| 1008 | region = Rect2(Vector2(), object_texture->get_size()); |
| 1009 | } |
| 1010 | |
| 1011 | return region; |
| 1012 | } |
| 1013 | |
| 1014 | void TextureRegionEditor::_texture_changed() { |
| 1015 | if (!is_visible()) { |
| 1016 | return; |
| 1017 | } |
| 1018 | _edit_region(); |
| 1019 | } |
| 1020 | |
| 1021 | void TextureRegionEditor::_edit_region() { |
| 1022 | const Ref<Texture2D> object_texture = _get_edited_object_texture(); |
| 1023 | if (object_texture.is_null()) { |
| 1024 | _set_grid_parameters_clamping(false); |
| 1025 | _zoom_reset(); |
| 1026 | hscroll->hide(); |
| 1027 | vscroll->hide(); |
| 1028 | texture_preview->queue_redraw(); |
| 1029 | texture_overlay->queue_redraw(); |
| 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | CanvasItem::TextureFilter filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS; |
| 1034 | if (node_sprite_2d) { |
| 1035 | filter = node_sprite_2d->get_texture_filter_in_tree(); |
| 1036 | } else if (node_sprite_3d) { |
| 1037 | StandardMaterial3D::TextureFilter filter_3d = node_sprite_3d->get_texture_filter(); |
| 1038 | |
| 1039 | switch (filter_3d) { |
| 1040 | case StandardMaterial3D::TEXTURE_FILTER_NEAREST: |
| 1041 | filter = CanvasItem::TEXTURE_FILTER_NEAREST; |
| 1042 | break; |
| 1043 | case StandardMaterial3D::TEXTURE_FILTER_LINEAR: |
| 1044 | filter = CanvasItem::TEXTURE_FILTER_LINEAR; |
| 1045 | break; |
| 1046 | case StandardMaterial3D::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: |
| 1047 | filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS; |
| 1048 | break; |
| 1049 | case StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: |
| 1050 | filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS; |
| 1051 | break; |
| 1052 | case StandardMaterial3D::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: |
| 1053 | filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC; |
| 1054 | break; |
| 1055 | case StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: |
| 1056 | filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC; |
| 1057 | break; |
| 1058 | default: |
| 1059 | // fallback to project default |
| 1060 | filter = CanvasItem::TEXTURE_FILTER_PARENT_NODE; |
| 1061 | break; |
| 1062 | } |
| 1063 | } else if (node_ninepatch) { |
| 1064 | filter = node_ninepatch->get_texture_filter_in_tree(); |
| 1065 | } |
| 1066 | |
| 1067 | // occurs when get_texture_filter_in_tree reaches the scene root |
| 1068 | if (filter == CanvasItem::TEXTURE_FILTER_PARENT_NODE) { |
| 1069 | SubViewport *root = EditorNode::get_singleton()->get_scene_root(); |
| 1070 | |
| 1071 | if (root != nullptr) { |
| 1072 | Viewport::DefaultCanvasItemTextureFilter filter_default = root->get_default_canvas_item_texture_filter(); |
| 1073 | |
| 1074 | // depending on default filter, set filter to match, otherwise fall back on nearest w/ mipmaps |
| 1075 | switch (filter_default) { |
| 1076 | case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST: |
| 1077 | filter = CanvasItem::TEXTURE_FILTER_NEAREST; |
| 1078 | break; |
| 1079 | case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR: |
| 1080 | filter = CanvasItem::TEXTURE_FILTER_LINEAR; |
| 1081 | break; |
| 1082 | case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: |
| 1083 | filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS; |
| 1084 | break; |
| 1085 | case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: |
| 1086 | default: |
| 1087 | filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS; |
| 1088 | break; |
| 1089 | } |
| 1090 | } else { |
| 1091 | filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | texture_preview->set_texture_filter(filter); |
| 1096 | texture_preview->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_DISABLED); |
| 1097 | |
| 1098 | if (cache_map.has(object_texture->get_rid())) { |
| 1099 | autoslice_cache = cache_map[object_texture->get_rid()]; |
| 1100 | autoslice_is_dirty = false; |
| 1101 | } else { |
| 1102 | if (is_visible() && snap_mode == SNAP_AUTOSLICE) { |
| 1103 | _update_autoslice(); |
| 1104 | } else { |
| 1105 | autoslice_is_dirty = true; |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | // Avoiding clamping with mismatched min/max. |
| 1110 | _set_grid_parameters_clamping(false); |
| 1111 | const Size2 tex_size = object_texture->get_size(); |
| 1112 | sb_off_x->set_min(-tex_size.x); |
| 1113 | sb_off_x->set_max(tex_size.x); |
| 1114 | sb_off_y->set_min(-tex_size.y); |
| 1115 | sb_off_y->set_max(tex_size.y); |
| 1116 | sb_step_x->set_max(tex_size.x); |
| 1117 | sb_step_y->set_max(tex_size.y); |
| 1118 | sb_sep_x->set_max(tex_size.x); |
| 1119 | sb_sep_y->set_max(tex_size.y); |
| 1120 | |
| 1121 | _set_grid_parameters_clamping(true); |
| 1122 | sb_off_x->set_value(snap_offset.x); |
| 1123 | sb_off_y->set_value(snap_offset.y); |
| 1124 | sb_step_x->set_value(snap_step.x); |
| 1125 | sb_step_y->set_value(snap_step.y); |
| 1126 | sb_sep_x->set_value(snap_separation.x); |
| 1127 | sb_sep_y->set_value(snap_separation.y); |
| 1128 | |
| 1129 | _update_rect(); |
| 1130 | texture_preview->queue_redraw(); |
| 1131 | texture_overlay->queue_redraw(); |
| 1132 | } |
| 1133 | |
| 1134 | Vector2 TextureRegionEditor::snap_point(Vector2 p_target) const { |
| 1135 | if (snap_mode == SNAP_GRID) { |
| 1136 | p_target.x = Math::snap_scalar_separation(snap_offset.x, snap_step.x, p_target.x, snap_separation.x); |
| 1137 | p_target.y = Math::snap_scalar_separation(snap_offset.y, snap_step.y, p_target.y, snap_separation.y); |
| 1138 | } |
| 1139 | |
| 1140 | return p_target; |
| 1141 | } |
| 1142 | |
| 1143 | void TextureRegionEditor::shortcut_input(const Ref<InputEvent> &p_event) { |
| 1144 | const Ref<InputEventKey> k = p_event; |
| 1145 | if (k.is_valid() && k->is_pressed()) { |
| 1146 | bool handled = false; |
| 1147 | |
| 1148 | if (ED_IS_SHORTCUT("ui_undo", p_event)) { |
| 1149 | EditorNode::get_singleton()->undo(); |
| 1150 | handled = true; |
| 1151 | } |
| 1152 | |
| 1153 | if (ED_IS_SHORTCUT("ui_redo", p_event)) { |
| 1154 | EditorNode::get_singleton()->redo(); |
| 1155 | handled = true; |
| 1156 | } |
| 1157 | |
| 1158 | if (handled) { |
| 1159 | set_input_as_handled(); |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | void TextureRegionEditor::_bind_methods() { |
| 1165 | ClassDB::bind_method(D_METHOD("_update_rect"), &TextureRegionEditor::_update_rect); |
| 1166 | } |
| 1167 | |
| 1168 | TextureRegionEditor::TextureRegionEditor() { |
| 1169 | set_title(TTR("Region Editor")); |
| 1170 | set_flag(FLAG_MAXIMIZE_DISABLED, false); |
| 1171 | set_process_shortcut_input(true); |
| 1172 | set_ok_button_text(TTR("Close")); |
| 1173 | // Handled manually, to allow canceling dragging. |
| 1174 | set_close_on_escape(false); |
| 1175 | |
| 1176 | // A power-of-two value works better as a default grid size. |
| 1177 | snap_offset = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_offset", Vector2()); |
| 1178 | snap_step = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_step", Vector2(8, 8)); |
| 1179 | snap_separation = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_separation", Vector2()); |
| 1180 | snap_mode = (SnapMode)(int)EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_mode", SNAP_NONE); |
| 1181 | |
| 1182 | panner.instantiate(); |
| 1183 | panner->set_callbacks(callable_mp(this, &TextureRegionEditor::_pan_callback), callable_mp(this, &TextureRegionEditor::_zoom_callback)); |
| 1184 | |
| 1185 | VBoxContainer *vb = memnew(VBoxContainer); |
| 1186 | add_child(vb); |
| 1187 | |
| 1188 | HBoxContainer *hb_tools = memnew(HBoxContainer); |
| 1189 | vb->add_child(hb_tools); |
| 1190 | hb_tools->add_child(memnew(Label(TTR("Snap Mode:")))); |
| 1191 | |
| 1192 | snap_mode_button = memnew(OptionButton); |
| 1193 | hb_tools->add_child(snap_mode_button); |
| 1194 | snap_mode_button->set_accessibility_name(TTRC("Snap Mode:")); |
| 1195 | snap_mode_button->add_item(TTR("None"), 0); |
| 1196 | snap_mode_button->add_item(TTR("Pixel Snap"), 1); |
| 1197 | snap_mode_button->add_item(TTR("Grid Snap"), 2); |
| 1198 | snap_mode_button->add_item(TTR("Auto Slice"), 3); |
| 1199 | snap_mode_button->select(snap_mode); |
| 1200 | snap_mode_button->connect(SceneStringName(item_selected), callable_mp(this, &TextureRegionEditor::_set_snap_mode)); |
| 1201 | |
| 1202 | hb_grid = memnew(HBoxContainer); |
| 1203 | hb_tools->add_child(hb_grid); |
| 1204 | |
| 1205 | hb_grid->add_child(memnew(VSeparator)); |
| 1206 | hb_grid->add_child(memnew(Label(TTR("Offset:")))); |
| 1207 | |
| 1208 | sb_off_x = memnew(SpinBox); |
| 1209 | sb_off_x->set_step(1); |
| 1210 | sb_off_x->set_suffix("px"); |
| 1211 | sb_off_x->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_off_x)); |
| 1212 | sb_off_x->set_accessibility_name(TTRC("Offset X")); |
| 1213 | hb_grid->add_child(sb_off_x); |
| 1214 | |
| 1215 | sb_off_y = memnew(SpinBox); |
| 1216 | sb_off_y->set_step(1); |
| 1217 | sb_off_y->set_suffix("px"); |
| 1218 | sb_off_y->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_off_y)); |
| 1219 | sb_off_y->set_accessibility_name(TTRC("Offset Y")); |
| 1220 | hb_grid->add_child(sb_off_y); |
| 1221 | |
| 1222 | hb_grid->add_child(memnew(VSeparator)); |
| 1223 | hb_grid->add_child(memnew(Label(TTR("Step:")))); |
| 1224 | |
| 1225 | sb_step_x = memnew(SpinBox); |
| 1226 | sb_step_x->set_min(0); |
| 1227 | sb_step_x->set_step(1); |
| 1228 | sb_step_x->set_suffix("px"); |
| 1229 | sb_step_x->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_step_x)); |
| 1230 | sb_step_x->set_accessibility_name(TTRC("Step X")); |
| 1231 | hb_grid->add_child(sb_step_x); |
| 1232 | |
| 1233 | sb_step_y = memnew(SpinBox); |
| 1234 | sb_step_y->set_min(0); |
| 1235 | sb_step_y->set_step(1); |
| 1236 | sb_step_y->set_suffix("px"); |
| 1237 | sb_step_y->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_step_y)); |
| 1238 | sb_step_y->set_accessibility_name(TTRC("Step Y")); |
| 1239 | hb_grid->add_child(sb_step_y); |
| 1240 | |
| 1241 | hb_grid->add_child(memnew(VSeparator)); |
| 1242 | hb_grid->add_child(memnew(Label(TTR("Separation:")))); |
| 1243 | |
| 1244 | sb_sep_x = memnew(SpinBox); |
| 1245 | sb_sep_x->set_min(0); |
| 1246 | sb_sep_x->set_step(1); |
| 1247 | sb_sep_x->set_suffix("px"); |
| 1248 | sb_sep_x->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_sep_x)); |
| 1249 | sb_sep_x->set_accessibility_name(TTRC("Separation X")); |
| 1250 | hb_grid->add_child(sb_sep_x); |
| 1251 | |
| 1252 | sb_sep_y = memnew(SpinBox); |
| 1253 | sb_sep_y->set_min(0); |
| 1254 | sb_sep_y->set_step(1); |
| 1255 | sb_sep_y->set_suffix("px"); |
| 1256 | sb_sep_y->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_sep_y)); |
| 1257 | sb_sep_y->set_accessibility_name(TTRC("Separation Y")); |
| 1258 | hb_grid->add_child(sb_sep_y); |
| 1259 | |
| 1260 | hb_grid->hide(); |
| 1261 | |
| 1262 | // Restore grid snap parameters. |
| 1263 | _set_grid_parameters_clamping(false); |
| 1264 | sb_off_x->set_value(snap_offset.x); |
| 1265 | sb_off_y->set_value(snap_offset.y); |
| 1266 | sb_step_x->set_value(snap_step.x); |
| 1267 | sb_step_y->set_value(snap_step.y); |
| 1268 | sb_sep_x->set_value(snap_separation.x); |
| 1269 | sb_sep_y->set_value(snap_separation.y); |
| 1270 | |
| 1271 | // Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad. |
| 1272 | draw_zoom = MAX(1.0f, EDSCALE); |
| 1273 | max_draw_zoom = 128.0f * MAX(1.0f, EDSCALE); |
| 1274 | min_draw_zoom = 0.01f * MAX(1.0f, EDSCALE); |
| 1275 | |
| 1276 | texture_preview = memnew(PanelContainer); |
| 1277 | vb->add_child(texture_preview); |
| 1278 | texture_preview->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 1279 | texture_preview->set_clip_contents(true); |
| 1280 | texture_preview->connect(SceneStringName(draw), callable_mp(this, &TextureRegionEditor::_texture_preview_draw)); |
| 1281 | |
| 1282 | texture_overlay = memnew(Panel); |
| 1283 | texture_preview->add_child(texture_overlay); |
| 1284 | texture_overlay->set_focus_mode(Control::FOCUS_CLICK); |
| 1285 | texture_overlay->connect(SceneStringName(draw), callable_mp(this, &TextureRegionEditor::_texture_overlay_draw)); |
| 1286 | texture_overlay->connect(SceneStringName(gui_input), callable_mp(this, &TextureRegionEditor::_texture_overlay_input)); |
| 1287 | texture_overlay->connect(SceneStringName(focus_exited), callable_mp(panner.ptr(), &ViewPanner::release_pan_key)); |
| 1288 | |
| 1289 | HBoxContainer *zoom_hb = memnew(HBoxContainer); |
| 1290 | texture_overlay->add_child(zoom_hb); |
| 1291 | zoom_hb->set_begin(Point2(5, 5)); |
| 1292 | |
| 1293 | zoom_out = memnew(Button); |
| 1294 | zoom_out->set_flat(true); |
| 1295 | zoom_out->set_tooltip_text(TTR("Zoom Out")); |
| 1296 | zoom_out->connect(SceneStringName(pressed), callable_mp(this, &TextureRegionEditor::_zoom_out)); |
| 1297 | zoom_hb->add_child(zoom_out); |
| 1298 | |
| 1299 | zoom_reset = memnew(Button); |
| 1300 | zoom_reset->set_flat(true); |
| 1301 | zoom_reset->set_tooltip_text(TTR("Zoom Reset")); |
| 1302 | zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &TextureRegionEditor::_zoom_reset)); |
| 1303 | zoom_hb->add_child(zoom_reset); |
| 1304 | |
| 1305 | zoom_in = memnew(Button); |
| 1306 | zoom_in->set_flat(true); |
| 1307 | zoom_in->set_tooltip_text(TTR("Zoom In")); |
| 1308 | zoom_in->connect(SceneStringName(pressed), callable_mp(this, &TextureRegionEditor::_zoom_in)); |
| 1309 | zoom_hb->add_child(zoom_in); |
| 1310 | |
| 1311 | vscroll = memnew(VScrollBar); |
| 1312 | vscroll->set_anchors_and_offsets_preset(Control::PRESET_RIGHT_WIDE); |
| 1313 | vscroll->set_step(0.001); |
| 1314 | vscroll->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_scroll_changed)); |
| 1315 | texture_overlay->add_child(vscroll); |
| 1316 | |
| 1317 | hscroll = memnew(HScrollBar); |
| 1318 | hscroll->set_anchors_and_offsets_preset(Control::PRESET_BOTTOM_WIDE); |
| 1319 | hscroll->set_step(0.001); |
| 1320 | hscroll->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_scroll_changed)); |
| 1321 | texture_overlay->add_child(hscroll); |
| 1322 | } |
| 1323 | |
| 1324 | //////////////////////// |
| 1325 | |
| 1326 | bool EditorInspectorPluginTextureRegion::can_handle(Object *p_object) { |
| 1327 | return Object::cast_to<Sprite2D>(p_object) || Object::cast_to<Sprite3D>(p_object) || Object::cast_to<NinePatchRect>(p_object) || Object::cast_to<StyleBoxTexture>(p_object) || Object::cast_to<AtlasTexture>(p_object); |
| 1328 | } |
| 1329 | |
| 1330 | void EditorInspectorPluginTextureRegion::_region_edit(Object *p_object) { |
| 1331 | texture_region_editor->edit(p_object); |
| 1332 | } |
| 1333 | |
| 1334 | bool EditorInspectorPluginTextureRegion::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) { |
| 1335 | if ((p_type == Variant::RECT2 || p_type == Variant::RECT2I)) { |
| 1336 | if (((Object::cast_to<Sprite2D>(p_object) || Object::cast_to<Sprite3D>(p_object) || Object::cast_to<NinePatchRect>(p_object) || Object::cast_to<StyleBoxTexture>(p_object)) && p_path == "region_rect") || (Object::cast_to<AtlasTexture>(p_object) && p_path == "region")) { |
| 1337 | EditorInspectorActionButton *button = memnew(EditorInspectorActionButton(TTRC("Edit Region"), SNAME("RegionEdit"))); |
| 1338 | button->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorPluginTextureRegion::_region_edit).bind(p_object)); |
| 1339 | add_property_editor(p_path, button, true); |
| 1340 | } |
| 1341 | } |
| 1342 | return false; //not exclusive |
| 1343 | } |
| 1344 | |
| 1345 | EditorInspectorPluginTextureRegion::EditorInspectorPluginTextureRegion() { |
| 1346 | texture_region_editor = memnew(TextureRegionEditor); |
| 1347 | EditorNode::get_singleton()->get_gui_base()->add_child(texture_region_editor); |
| 1348 | } |
| 1349 | |
| 1350 | TextureRegionEditorPlugin::TextureRegionEditorPlugin() { |
| 1351 | Ref<EditorInspectorPluginTextureRegion> inspector_plugin; |
| 1352 | inspector_plugin.instantiate(); |
| 1353 | add_inspector_plugin(inspector_plugin); |
| 1354 | } |