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