Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/texture/texture_region_editor_plugin.cpp
9904 views
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::_texture_overlay_input(const Ref<InputEvent> &p_input) {
299
if (panner->gui_input(p_input, texture_overlay->get_global_rect())) {
300
return;
301
}
302
303
Transform2D mtx;
304
mtx.columns[2] = -draw_ofs * draw_zoom;
305
mtx.scale_basis(Vector2(draw_zoom, draw_zoom));
306
307
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
308
Ref<InputEventMouseButton> mb = p_input;
309
if (mb.is_valid()) {
310
if (mb->get_button_index() == MouseButton::LEFT) {
311
if (mb->is_pressed() && !panner->is_panning()) {
312
// Check if we click on any handle first.
313
{
314
const real_t handle_radius = 16 * EDSCALE;
315
const real_t handle_offset = 8 * EDSCALE;
316
317
// Position of selection handles.
318
const Vector2 endpoints[8] = {
319
mtx.xform(rect.position) + Vector2(-handle_offset, -handle_offset),
320
mtx.xform(rect.position + Vector2(rect.size.x / 2, 0)) + Vector2(0, -handle_offset),
321
mtx.xform(rect.position + Vector2(rect.size.x, 0)) + Vector2(handle_offset, -handle_offset),
322
mtx.xform(rect.position + Vector2(rect.size.x, rect.size.y / 2)) + Vector2(handle_offset, 0),
323
mtx.xform(rect.position + rect.size) + Vector2(handle_offset, handle_offset),
324
mtx.xform(rect.position + Vector2(rect.size.x / 2, rect.size.y)) + Vector2(0, handle_offset),
325
mtx.xform(rect.position + Vector2(0, rect.size.y)) + Vector2(-handle_offset, handle_offset),
326
mtx.xform(rect.position + Vector2(0, rect.size.y / 2)) + Vector2(-handle_offset, 0)
327
};
328
329
drag_from = mtx.affine_inverse().xform(mb->get_position());
330
if (snap_mode == SNAP_PIXEL) {
331
drag_from = drag_from.snappedf(1);
332
} else if (snap_mode == SNAP_GRID) {
333
drag_from = snap_point(drag_from);
334
}
335
drag = true;
336
337
rect_prev = _get_edited_object_region();
338
339
for (int i = 0; i < 8; i++) {
340
Vector2 tuv = endpoints[i];
341
if (tuv.distance_to(mb->get_position()) < handle_radius) {
342
drag_index = i;
343
}
344
}
345
}
346
347
// We didn't hit any handle, try other options.
348
if (drag_index < 0) {
349
if (node_ninepatch || res_stylebox.is_valid()) {
350
// For ninepatchable objects check if we are clicking on margin bars.
351
352
edited_margin = -1;
353
float margins[4] = { 0 };
354
if (node_ninepatch) {
355
margins[0] = node_ninepatch->get_patch_margin(SIDE_TOP);
356
margins[1] = node_ninepatch->get_patch_margin(SIDE_BOTTOM);
357
margins[2] = node_ninepatch->get_patch_margin(SIDE_LEFT);
358
margins[3] = node_ninepatch->get_patch_margin(SIDE_RIGHT);
359
} else if (res_stylebox.is_valid()) {
360
margins[0] = res_stylebox->get_texture_margin(SIDE_TOP);
361
margins[1] = res_stylebox->get_texture_margin(SIDE_BOTTOM);
362
margins[2] = res_stylebox->get_texture_margin(SIDE_LEFT);
363
margins[3] = res_stylebox->get_texture_margin(SIDE_RIGHT);
364
}
365
366
Vector2 pos[4] = {
367
mtx.basis_xform(rect.position + Vector2(0, margins[0])) - draw_ofs * draw_zoom,
368
mtx.basis_xform(rect.position + rect.size - Vector2(0, margins[1])) - draw_ofs * draw_zoom,
369
mtx.basis_xform(rect.position + Vector2(margins[2], 0)) - draw_ofs * draw_zoom,
370
mtx.basis_xform(rect.position + rect.size - Vector2(margins[3], 0)) - draw_ofs * draw_zoom
371
};
372
if (Math::abs(mb->get_position().y - pos[0].y) < 8) {
373
edited_margin = 0;
374
prev_margin = margins[0];
375
} else if (Math::abs(mb->get_position().y - pos[1].y) < 8) {
376
edited_margin = 1;
377
prev_margin = margins[1];
378
} else if (Math::abs(mb->get_position().x - pos[2].x) < 8) {
379
edited_margin = 2;
380
prev_margin = margins[2];
381
} else if (Math::abs(mb->get_position().x - pos[3].x) < 8) {
382
edited_margin = 3;
383
prev_margin = margins[3];
384
}
385
if (edited_margin >= 0) {
386
drag_from = mb->get_position();
387
drag = true;
388
}
389
}
390
391
if (edited_margin < 0 && snap_mode == SNAP_AUTOSLICE) {
392
// We didn't hit anything, but we're in the autoslice mode. Handle it.
393
394
Vector2 point = mtx.affine_inverse().xform(mb->get_position());
395
for (const Rect2 &E : autoslice_cache) {
396
if (E.has_point(point)) {
397
rect = E;
398
if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !(Input::get_singleton()->is_key_pressed(Key(Key::SHIFT | Key::ALT)))) {
399
Rect2 r;
400
if (node_sprite_2d) {
401
r = node_sprite_2d->get_region_rect();
402
} else if (node_sprite_3d) {
403
r = node_sprite_3d->get_region_rect();
404
} else if (node_ninepatch) {
405
r = node_ninepatch->get_region_rect();
406
} else if (res_stylebox.is_valid()) {
407
r = res_stylebox->get_region_rect();
408
} else if (res_atlas_texture.is_valid()) {
409
r = res_atlas_texture->get_region();
410
}
411
rect.expand_to(r.position);
412
rect.expand_to(r.get_end());
413
}
414
415
undo_redo->create_action(TTR("Set Region Rect"));
416
if (node_sprite_2d) {
417
undo_redo->add_do_method(node_sprite_2d, "set_region_rect", rect);
418
undo_redo->add_undo_method(node_sprite_2d, "set_region_rect", node_sprite_2d->get_region_rect());
419
} else if (node_sprite_3d) {
420
undo_redo->add_do_method(node_sprite_3d, "set_region_rect", rect);
421
undo_redo->add_undo_method(node_sprite_3d, "set_region_rect", node_sprite_3d->get_region_rect());
422
} else if (node_ninepatch) {
423
undo_redo->add_do_method(node_ninepatch, "set_region_rect", rect);
424
undo_redo->add_undo_method(node_ninepatch, "set_region_rect", node_ninepatch->get_region_rect());
425
} else if (res_stylebox.is_valid()) {
426
undo_redo->add_do_method(res_stylebox.ptr(), "set_region_rect", rect);
427
undo_redo->add_undo_method(res_stylebox.ptr(), "set_region_rect", res_stylebox->get_region_rect());
428
} else if (res_atlas_texture.is_valid()) {
429
undo_redo->add_do_method(res_atlas_texture.ptr(), "set_region", rect);
430
undo_redo->add_undo_method(res_atlas_texture.ptr(), "set_region", res_atlas_texture->get_region());
431
}
432
433
undo_redo->add_do_method(this, "_update_rect");
434
undo_redo->add_undo_method(this, "_update_rect");
435
undo_redo->add_do_method(texture_overlay, "queue_redraw");
436
undo_redo->add_undo_method(texture_overlay, "queue_redraw");
437
undo_redo->commit_action();
438
break;
439
}
440
}
441
} else if (edited_margin < 0) {
442
// We didn't hit anything and it's not autoslice, which means we try to create a new region.
443
444
if (drag_index == -1) {
445
creating = true;
446
rect = Rect2(drag_from, Size2());
447
}
448
}
449
}
450
451
} else if (!mb->is_pressed() && drag) {
452
if (edited_margin >= 0) {
453
undo_redo->create_action(TTR("Set Margin"));
454
static Side side[4] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT };
455
if (node_ninepatch) {
456
undo_redo->add_do_method(node_ninepatch, "set_patch_margin", side[edited_margin], node_ninepatch->get_patch_margin(side[edited_margin]));
457
undo_redo->add_undo_method(node_ninepatch, "set_patch_margin", side[edited_margin], prev_margin);
458
} else if (res_stylebox.is_valid()) {
459
undo_redo->add_do_method(res_stylebox.ptr(), "set_texture_margin", side[edited_margin], res_stylebox->get_texture_margin(side[edited_margin]));
460
undo_redo->add_undo_method(res_stylebox.ptr(), "set_texture_margin", side[edited_margin], prev_margin);
461
res_stylebox->emit_changed();
462
}
463
edited_margin = -1;
464
} else {
465
undo_redo->create_action(TTR("Set Region Rect"));
466
if (node_sprite_2d) {
467
undo_redo->add_do_method(node_sprite_2d, "set_region_rect", node_sprite_2d->get_region_rect());
468
undo_redo->add_undo_method(node_sprite_2d, "set_region_rect", rect_prev);
469
} else if (node_sprite_3d) {
470
undo_redo->add_do_method(node_sprite_3d, "set_region_rect", node_sprite_3d->get_region_rect());
471
undo_redo->add_undo_method(node_sprite_3d, "set_region_rect", rect_prev);
472
} else if (node_ninepatch) {
473
undo_redo->add_do_method(node_ninepatch, "set_region_rect", node_ninepatch->get_region_rect());
474
undo_redo->add_undo_method(node_ninepatch, "set_region_rect", rect_prev);
475
} else if (res_stylebox.is_valid()) {
476
undo_redo->add_do_method(res_stylebox.ptr(), "set_region_rect", res_stylebox->get_region_rect());
477
undo_redo->add_undo_method(res_stylebox.ptr(), "set_region_rect", rect_prev);
478
} else if (res_atlas_texture.is_valid()) {
479
undo_redo->add_do_method(res_atlas_texture.ptr(), "set_region", res_atlas_texture->get_region());
480
undo_redo->add_undo_method(res_atlas_texture.ptr(), "set_region", rect_prev);
481
}
482
drag_index = -1;
483
}
484
undo_redo->add_do_method(this, "_update_rect");
485
undo_redo->add_undo_method(this, "_update_rect");
486
undo_redo->add_do_method(texture_overlay, "queue_redraw");
487
undo_redo->add_undo_method(texture_overlay, "queue_redraw");
488
undo_redo->commit_action();
489
drag = false;
490
creating = false;
491
}
492
493
} else if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
494
if (drag) {
495
drag = false;
496
if (edited_margin >= 0) {
497
static Side side[4] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT };
498
if (node_ninepatch) {
499
node_ninepatch->set_patch_margin(side[edited_margin], prev_margin);
500
}
501
if (res_stylebox.is_valid()) {
502
res_stylebox->set_texture_margin(side[edited_margin], prev_margin);
503
}
504
edited_margin = -1;
505
} else {
506
_apply_rect(rect_prev);
507
rect = rect_prev;
508
texture_preview->queue_redraw();
509
texture_overlay->queue_redraw();
510
drag_index = -1;
511
}
512
}
513
}
514
}
515
516
Ref<InputEventMouseMotion> mm = p_input;
517
518
if (mm.is_valid()) {
519
if (drag) {
520
if (edited_margin >= 0) {
521
float new_margin = 0;
522
523
if (snap_mode != SNAP_GRID) {
524
if (edited_margin == 0) {
525
new_margin = prev_margin + (mm->get_position().y - drag_from.y) / draw_zoom;
526
} else if (edited_margin == 1) {
527
new_margin = prev_margin - (mm->get_position().y - drag_from.y) / draw_zoom;
528
} else if (edited_margin == 2) {
529
new_margin = prev_margin + (mm->get_position().x - drag_from.x) / draw_zoom;
530
} else if (edited_margin == 3) {
531
new_margin = prev_margin - (mm->get_position().x - drag_from.x) / draw_zoom;
532
} else {
533
ERR_PRINT("Unexpected edited_margin");
534
}
535
536
if (snap_mode == SNAP_PIXEL) {
537
new_margin = Math::round(new_margin);
538
}
539
} else {
540
Vector2 pos_snapped = snap_point(mtx.affine_inverse().xform(mm->get_position()));
541
Rect2 rect_rounded = Rect2(rect.position.round(), rect.size.round());
542
543
if (edited_margin == 0) {
544
new_margin = pos_snapped.y - rect_rounded.position.y;
545
} else if (edited_margin == 1) {
546
new_margin = rect_rounded.size.y + rect_rounded.position.y - pos_snapped.y;
547
} else if (edited_margin == 2) {
548
new_margin = pos_snapped.x - rect_rounded.position.x;
549
} else if (edited_margin == 3) {
550
new_margin = rect_rounded.size.x + rect_rounded.position.x - pos_snapped.x;
551
} else {
552
ERR_PRINT("Unexpected edited_margin");
553
}
554
}
555
556
if (new_margin < 0) {
557
new_margin = 0;
558
}
559
static Side side[4] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT };
560
if (node_ninepatch) {
561
node_ninepatch->set_patch_margin(side[edited_margin], new_margin);
562
}
563
if (res_stylebox.is_valid()) {
564
res_stylebox->set_texture_margin(side[edited_margin], new_margin);
565
}
566
} else {
567
Vector2 new_pos = mtx.affine_inverse().xform(mm->get_position());
568
if (snap_mode == SNAP_PIXEL) {
569
new_pos = new_pos.snappedf(1);
570
} else if (snap_mode == SNAP_GRID) {
571
new_pos = snap_point(new_pos);
572
}
573
574
if (creating) {
575
rect = Rect2(drag_from, Size2());
576
rect.expand_to(new_pos);
577
_apply_rect(rect);
578
texture_preview->queue_redraw();
579
texture_overlay->queue_redraw();
580
return;
581
}
582
583
switch (drag_index) {
584
case 0: {
585
Vector2 p = rect_prev.get_end();
586
rect = Rect2(p, Size2());
587
rect.expand_to(new_pos);
588
_apply_rect(rect);
589
} break;
590
case 1: {
591
Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y);
592
rect = Rect2(p, Size2(rect_prev.size.x, 0));
593
rect.expand_to(new_pos);
594
_apply_rect(rect);
595
} break;
596
case 2: {
597
Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y);
598
rect = Rect2(p, Size2());
599
rect.expand_to(new_pos);
600
_apply_rect(rect);
601
} break;
602
case 3: {
603
Vector2 p = rect_prev.position;
604
rect = Rect2(p, Size2(0, rect_prev.size.y));
605
rect.expand_to(new_pos);
606
_apply_rect(rect);
607
} break;
608
case 4: {
609
Vector2 p = rect_prev.position;
610
rect = Rect2(p, Size2());
611
rect.expand_to(new_pos);
612
_apply_rect(rect);
613
} break;
614
case 5: {
615
Vector2 p = rect_prev.position;
616
rect = Rect2(p, Size2(rect_prev.size.x, 0));
617
rect.expand_to(new_pos);
618
_apply_rect(rect);
619
} break;
620
case 6: {
621
Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0);
622
rect = Rect2(p, Size2());
623
rect.expand_to(new_pos);
624
_apply_rect(rect);
625
} break;
626
case 7: {
627
Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0);
628
rect = Rect2(p, Size2(0, rect_prev.size.y));
629
rect.expand_to(new_pos);
630
_apply_rect(rect);
631
} break;
632
}
633
}
634
texture_preview->queue_redraw();
635
texture_overlay->queue_redraw();
636
}
637
}
638
639
Ref<InputEventMagnifyGesture> magnify_gesture = p_input;
640
if (magnify_gesture.is_valid()) {
641
_zoom_on_position(draw_zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
642
}
643
644
Ref<InputEventPanGesture> pan_gesture = p_input;
645
if (pan_gesture.is_valid()) {
646
hscroll->set_value(hscroll->get_value() + hscroll->get_page() * pan_gesture->get_delta().x / 8);
647
vscroll->set_value(vscroll->get_value() + vscroll->get_page() * pan_gesture->get_delta().y / 8);
648
}
649
}
650
651
void TextureRegionEditor::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
652
p_scroll_vec /= draw_zoom;
653
hscroll->set_value(hscroll->get_value() - p_scroll_vec.x);
654
vscroll->set_value(vscroll->get_value() - p_scroll_vec.y);
655
}
656
657
void TextureRegionEditor::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
658
_zoom_on_position(draw_zoom * p_zoom_factor, p_origin);
659
}
660
661
void TextureRegionEditor::_scroll_changed(float) {
662
if (updating_scroll) {
663
return;
664
}
665
666
draw_ofs.x = hscroll->get_value();
667
draw_ofs.y = vscroll->get_value();
668
669
texture_preview->queue_redraw();
670
texture_overlay->queue_redraw();
671
}
672
673
void TextureRegionEditor::_set_snap_mode(int p_mode) {
674
snap_mode = (SnapMode)p_mode;
675
676
hb_grid->set_visible(snap_mode == SNAP_GRID);
677
if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) {
678
_update_autoslice();
679
}
680
681
texture_overlay->queue_redraw();
682
}
683
684
void TextureRegionEditor::_set_snap_off_x(float p_val) {
685
snap_offset.x = p_val;
686
texture_overlay->queue_redraw();
687
}
688
689
void TextureRegionEditor::_set_snap_off_y(float p_val) {
690
snap_offset.y = p_val;
691
texture_overlay->queue_redraw();
692
}
693
694
void TextureRegionEditor::_set_snap_step_x(float p_val) {
695
snap_step.x = p_val;
696
texture_overlay->queue_redraw();
697
}
698
699
void TextureRegionEditor::_set_snap_step_y(float p_val) {
700
snap_step.y = p_val;
701
texture_overlay->queue_redraw();
702
}
703
704
void TextureRegionEditor::_set_snap_sep_x(float p_val) {
705
snap_separation.x = p_val;
706
texture_overlay->queue_redraw();
707
}
708
709
void TextureRegionEditor::_set_snap_sep_y(float p_val) {
710
snap_separation.y = p_val;
711
texture_overlay->queue_redraw();
712
}
713
714
void TextureRegionEditor::_zoom_on_position(float p_zoom, Point2 p_position) {
715
if (p_zoom < min_draw_zoom || p_zoom > max_draw_zoom) {
716
return;
717
}
718
719
float prev_zoom = draw_zoom;
720
draw_zoom = p_zoom;
721
Point2 ofs = p_position;
722
ofs = ofs / prev_zoom - ofs / draw_zoom;
723
draw_ofs = (draw_ofs + ofs).round();
724
725
texture_preview->queue_redraw();
726
texture_overlay->queue_redraw();
727
}
728
729
void TextureRegionEditor::_zoom_in() {
730
_zoom_on_position(draw_zoom * 1.5, texture_overlay->get_size() / 2.0);
731
}
732
733
void TextureRegionEditor::_zoom_reset() {
734
_zoom_on_position(1.0, texture_overlay->get_size() / 2.0);
735
}
736
737
void TextureRegionEditor::_zoom_out() {
738
_zoom_on_position(draw_zoom / 1.5, texture_overlay->get_size() / 2.0);
739
}
740
741
void TextureRegionEditor::_apply_rect(const Rect2 &p_rect) {
742
if (node_sprite_2d) {
743
node_sprite_2d->set_region_rect(p_rect);
744
} else if (node_sprite_3d) {
745
node_sprite_3d->set_region_rect(p_rect);
746
} else if (node_ninepatch) {
747
node_ninepatch->set_region_rect(p_rect);
748
} else if (res_stylebox.is_valid()) {
749
res_stylebox->set_region_rect(p_rect);
750
} else if (res_atlas_texture.is_valid()) {
751
res_atlas_texture->set_region(p_rect);
752
}
753
}
754
755
void TextureRegionEditor::_update_rect() {
756
rect = _get_edited_object_region();
757
}
758
759
void TextureRegionEditor::_update_autoslice() {
760
autoslice_is_dirty = false;
761
autoslice_cache.clear();
762
763
const Ref<Texture2D> object_texture = _get_edited_object_texture();
764
if (object_texture.is_null()) {
765
return;
766
}
767
768
for (int y = 0; y < object_texture->get_height(); y++) {
769
for (int x = 0; x < object_texture->get_width(); x++) {
770
if (object_texture->is_pixel_opaque(x, y)) {
771
bool found = false;
772
for (Rect2 &E : autoslice_cache) {
773
Rect2 grown = E.grow(1.5);
774
if (grown.has_point(Point2(x, y))) {
775
E.expand_to(Point2(x, y));
776
E.expand_to(Point2(x + 1, y + 1));
777
x = E.position.x + E.size.x - 1;
778
bool merged = true;
779
while (merged) {
780
merged = false;
781
bool queue_erase = false;
782
for (List<Rect2>::Element *F = autoslice_cache.front(); F; F = F->next()) {
783
if (queue_erase) {
784
autoslice_cache.erase(F->prev());
785
queue_erase = false;
786
}
787
if (F->get() == E) {
788
continue;
789
}
790
if (E.grow(1).intersects(F->get())) {
791
E.expand_to(F->get().position);
792
E.expand_to(F->get().position + F->get().size);
793
if (F->prev()) {
794
F = F->prev();
795
autoslice_cache.erase(F->next());
796
} else {
797
queue_erase = true;
798
// Can't delete the first rect in the list.
799
}
800
merged = true;
801
}
802
}
803
}
804
found = true;
805
break;
806
}
807
}
808
if (!found) {
809
Rect2 new_rect(x, y, 1, 1);
810
autoslice_cache.push_back(new_rect);
811
}
812
}
813
}
814
}
815
cache_map[object_texture->get_rid()] = autoslice_cache;
816
}
817
818
void TextureRegionEditor::_notification(int p_what) {
819
switch (p_what) {
820
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
821
if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
822
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")));
823
panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning"));
824
}
825
} break;
826
827
case NOTIFICATION_ENTER_TREE: {
828
get_tree()->connect("node_removed", callable_mp(this, &TextureRegionEditor::_node_removed));
829
830
hb_grid->set_visible(snap_mode == SNAP_GRID);
831
if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) {
832
_update_autoslice();
833
}
834
835
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")));
836
panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning"));
837
} break;
838
839
case NOTIFICATION_EXIT_TREE: {
840
get_tree()->disconnect("node_removed", callable_mp(this, &TextureRegionEditor::_node_removed));
841
} break;
842
843
case NOTIFICATION_THEME_CHANGED: {
844
texture_preview->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("TextureRegionPreviewBG"), EditorStringName(EditorStyles)));
845
texture_overlay->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("TextureRegionPreviewFG"), EditorStringName(EditorStyles)));
846
847
zoom_out->set_button_icon(get_editor_theme_icon(SNAME("ZoomLess")));
848
zoom_reset->set_button_icon(get_editor_theme_icon(SNAME("ZoomReset")));
849
zoom_in->set_button_icon(get_editor_theme_icon(SNAME("ZoomMore")));
850
} break;
851
852
case NOTIFICATION_VISIBILITY_CHANGED: {
853
if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) {
854
_update_autoslice();
855
}
856
857
if (!is_visible()) {
858
EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_offset", snap_offset);
859
EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_step", snap_step);
860
EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_separation", snap_separation);
861
EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_mode", snap_mode);
862
}
863
} break;
864
865
case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
866
// This happens when the user leaves the Editor and returns,
867
// they could have changed the textures, so the cache is cleared.
868
cache_map.clear();
869
_edit_region();
870
} break;
871
}
872
}
873
874
void TextureRegionEditor::_node_removed(Node *p_node) {
875
if (p_node == node_sprite_2d || p_node == node_sprite_3d || p_node == node_ninepatch) {
876
_clear_edited_object();
877
hide();
878
}
879
}
880
881
void TextureRegionEditor::_clear_edited_object() {
882
if (node_sprite_2d) {
883
node_sprite_2d->disconnect(SceneStringName(texture_changed), callable_mp(this, &TextureRegionEditor::_texture_changed));
884
}
885
if (node_sprite_3d) {
886
node_sprite_3d->disconnect(SceneStringName(texture_changed), callable_mp(this, &TextureRegionEditor::_texture_changed));
887
}
888
if (node_ninepatch) {
889
node_ninepatch->disconnect(SceneStringName(texture_changed), callable_mp(this, &TextureRegionEditor::_texture_changed));
890
}
891
if (res_stylebox.is_valid()) {
892
res_stylebox->disconnect_changed(callable_mp(this, &TextureRegionEditor::_texture_changed));
893
}
894
if (res_atlas_texture.is_valid()) {
895
res_atlas_texture->disconnect_changed(callable_mp(this, &TextureRegionEditor::_texture_changed));
896
}
897
898
node_sprite_2d = nullptr;
899
node_sprite_3d = nullptr;
900
node_ninepatch = nullptr;
901
res_stylebox = Ref<StyleBoxTexture>();
902
res_atlas_texture = Ref<AtlasTexture>();
903
}
904
905
void TextureRegionEditor::edit(Object *p_obj) {
906
_clear_edited_object();
907
908
if (p_obj) {
909
node_sprite_2d = Object::cast_to<Sprite2D>(p_obj);
910
node_sprite_3d = Object::cast_to<Sprite3D>(p_obj);
911
node_ninepatch = Object::cast_to<NinePatchRect>(p_obj);
912
913
bool is_resource = false;
914
if (Object::cast_to<StyleBoxTexture>(p_obj)) {
915
res_stylebox = Ref<StyleBoxTexture>(p_obj);
916
is_resource = true;
917
}
918
if (Object::cast_to<AtlasTexture>(p_obj)) {
919
res_atlas_texture = Ref<AtlasTexture>(p_obj);
920
is_resource = true;
921
}
922
923
if (is_resource) {
924
Object::cast_to<Resource>(p_obj)->connect_changed(callable_mp(this, &TextureRegionEditor::_texture_changed));
925
} else {
926
p_obj->connect(SceneStringName(texture_changed), callable_mp(this, &TextureRegionEditor::_texture_changed));
927
}
928
_edit_region();
929
}
930
931
texture_preview->queue_redraw();
932
texture_overlay->queue_redraw();
933
popup_centered_ratio(0.5);
934
request_center = true;
935
}
936
937
Ref<Texture2D> TextureRegionEditor::_get_edited_object_texture() const {
938
if (node_sprite_2d) {
939
return node_sprite_2d->get_texture();
940
}
941
if (node_sprite_3d) {
942
return node_sprite_3d->get_texture();
943
}
944
if (node_ninepatch) {
945
return node_ninepatch->get_texture();
946
}
947
if (res_stylebox.is_valid()) {
948
return res_stylebox->get_texture();
949
}
950
if (res_atlas_texture.is_valid()) {
951
return res_atlas_texture->get_atlas();
952
}
953
954
return Ref<Texture2D>();
955
}
956
957
Rect2 TextureRegionEditor::_get_edited_object_region() const {
958
Rect2 region;
959
960
if (node_sprite_2d) {
961
region = node_sprite_2d->get_region_rect();
962
} else if (node_sprite_3d) {
963
region = node_sprite_3d->get_region_rect();
964
} else if (node_ninepatch) {
965
region = node_ninepatch->get_region_rect();
966
} else if (res_stylebox.is_valid()) {
967
region = res_stylebox->get_region_rect();
968
} else if (res_atlas_texture.is_valid()) {
969
region = res_atlas_texture->get_region();
970
}
971
972
const Ref<Texture2D> object_texture = _get_edited_object_texture();
973
if (region == Rect2() && object_texture.is_valid()) {
974
region = Rect2(Vector2(), object_texture->get_size());
975
}
976
977
return region;
978
}
979
980
void TextureRegionEditor::_texture_changed() {
981
if (!is_visible()) {
982
return;
983
}
984
_edit_region();
985
}
986
987
void TextureRegionEditor::_edit_region() {
988
const Ref<Texture2D> object_texture = _get_edited_object_texture();
989
if (object_texture.is_null()) {
990
_set_grid_parameters_clamping(false);
991
_zoom_reset();
992
hscroll->hide();
993
vscroll->hide();
994
texture_preview->queue_redraw();
995
texture_overlay->queue_redraw();
996
return;
997
}
998
999
CanvasItem::TextureFilter filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
1000
if (node_sprite_2d) {
1001
filter = node_sprite_2d->get_texture_filter_in_tree();
1002
} else if (node_sprite_3d) {
1003
StandardMaterial3D::TextureFilter filter_3d = node_sprite_3d->get_texture_filter();
1004
1005
switch (filter_3d) {
1006
case StandardMaterial3D::TEXTURE_FILTER_NEAREST:
1007
filter = CanvasItem::TEXTURE_FILTER_NEAREST;
1008
break;
1009
case StandardMaterial3D::TEXTURE_FILTER_LINEAR:
1010
filter = CanvasItem::TEXTURE_FILTER_LINEAR;
1011
break;
1012
case StandardMaterial3D::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
1013
filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
1014
break;
1015
case StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
1016
filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
1017
break;
1018
case StandardMaterial3D::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC:
1019
filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC;
1020
break;
1021
case StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC:
1022
filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC;
1023
break;
1024
default:
1025
// fallback to project default
1026
filter = CanvasItem::TEXTURE_FILTER_PARENT_NODE;
1027
break;
1028
}
1029
} else if (node_ninepatch) {
1030
filter = node_ninepatch->get_texture_filter_in_tree();
1031
}
1032
1033
// occurs when get_texture_filter_in_tree reaches the scene root
1034
if (filter == CanvasItem::TEXTURE_FILTER_PARENT_NODE) {
1035
SubViewport *root = EditorNode::get_singleton()->get_scene_root();
1036
1037
if (root != nullptr) {
1038
Viewport::DefaultCanvasItemTextureFilter filter_default = root->get_default_canvas_item_texture_filter();
1039
1040
// depending on default filter, set filter to match, otherwise fall back on nearest w/ mipmaps
1041
switch (filter_default) {
1042
case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST:
1043
filter = CanvasItem::TEXTURE_FILTER_NEAREST;
1044
break;
1045
case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR:
1046
filter = CanvasItem::TEXTURE_FILTER_LINEAR;
1047
break;
1048
case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
1049
filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
1050
break;
1051
case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
1052
default:
1053
filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
1054
break;
1055
}
1056
} else {
1057
filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
1058
}
1059
}
1060
1061
texture_preview->set_texture_filter(filter);
1062
texture_preview->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_DISABLED);
1063
1064
if (cache_map.has(object_texture->get_rid())) {
1065
autoslice_cache = cache_map[object_texture->get_rid()];
1066
autoslice_is_dirty = false;
1067
} else {
1068
if (is_visible() && snap_mode == SNAP_AUTOSLICE) {
1069
_update_autoslice();
1070
} else {
1071
autoslice_is_dirty = true;
1072
}
1073
}
1074
1075
// Avoiding clamping with mismatched min/max.
1076
_set_grid_parameters_clamping(false);
1077
const Size2 tex_size = object_texture->get_size();
1078
sb_off_x->set_min(-tex_size.x);
1079
sb_off_x->set_max(tex_size.x);
1080
sb_off_y->set_min(-tex_size.y);
1081
sb_off_y->set_max(tex_size.y);
1082
sb_step_x->set_max(tex_size.x);
1083
sb_step_y->set_max(tex_size.y);
1084
sb_sep_x->set_max(tex_size.x);
1085
sb_sep_y->set_max(tex_size.y);
1086
1087
_set_grid_parameters_clamping(true);
1088
sb_off_x->set_value(snap_offset.x);
1089
sb_off_y->set_value(snap_offset.y);
1090
sb_step_x->set_value(snap_step.x);
1091
sb_step_y->set_value(snap_step.y);
1092
sb_sep_x->set_value(snap_separation.x);
1093
sb_sep_y->set_value(snap_separation.y);
1094
1095
_update_rect();
1096
texture_preview->queue_redraw();
1097
texture_overlay->queue_redraw();
1098
}
1099
1100
Vector2 TextureRegionEditor::snap_point(Vector2 p_target) const {
1101
if (snap_mode == SNAP_GRID) {
1102
p_target.x = Math::snap_scalar_separation(snap_offset.x, snap_step.x, p_target.x, snap_separation.x);
1103
p_target.y = Math::snap_scalar_separation(snap_offset.y, snap_step.y, p_target.y, snap_separation.y);
1104
}
1105
1106
return p_target;
1107
}
1108
1109
void TextureRegionEditor::shortcut_input(const Ref<InputEvent> &p_event) {
1110
const Ref<InputEventKey> k = p_event;
1111
if (k.is_valid() && k->is_pressed()) {
1112
bool handled = false;
1113
1114
if (ED_IS_SHORTCUT("ui_undo", p_event)) {
1115
EditorNode::get_singleton()->undo();
1116
handled = true;
1117
}
1118
1119
if (ED_IS_SHORTCUT("ui_redo", p_event)) {
1120
EditorNode::get_singleton()->redo();
1121
handled = true;
1122
}
1123
1124
if (handled) {
1125
set_input_as_handled();
1126
}
1127
}
1128
}
1129
1130
void TextureRegionEditor::_bind_methods() {
1131
ClassDB::bind_method(D_METHOD("_update_rect"), &TextureRegionEditor::_update_rect);
1132
}
1133
1134
TextureRegionEditor::TextureRegionEditor() {
1135
set_title(TTR("Region Editor"));
1136
set_process_shortcut_input(true);
1137
set_ok_button_text(TTR("Close"));
1138
1139
// A power-of-two value works better as a default grid size.
1140
snap_offset = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_offset", Vector2());
1141
snap_step = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_step", Vector2(8, 8));
1142
snap_separation = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_separation", Vector2());
1143
snap_mode = (SnapMode)(int)EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_mode", SNAP_NONE);
1144
1145
panner.instantiate();
1146
panner->set_callbacks(callable_mp(this, &TextureRegionEditor::_pan_callback), callable_mp(this, &TextureRegionEditor::_zoom_callback));
1147
1148
VBoxContainer *vb = memnew(VBoxContainer);
1149
add_child(vb);
1150
1151
HBoxContainer *hb_tools = memnew(HBoxContainer);
1152
vb->add_child(hb_tools);
1153
hb_tools->add_child(memnew(Label(TTR("Snap Mode:"))));
1154
1155
snap_mode_button = memnew(OptionButton);
1156
hb_tools->add_child(snap_mode_button);
1157
snap_mode_button->set_accessibility_name(TTRC("Snap Mode:"));
1158
snap_mode_button->add_item(TTR("None"), 0);
1159
snap_mode_button->add_item(TTR("Pixel Snap"), 1);
1160
snap_mode_button->add_item(TTR("Grid Snap"), 2);
1161
snap_mode_button->add_item(TTR("Auto Slice"), 3);
1162
snap_mode_button->select(snap_mode);
1163
snap_mode_button->connect(SceneStringName(item_selected), callable_mp(this, &TextureRegionEditor::_set_snap_mode));
1164
1165
hb_grid = memnew(HBoxContainer);
1166
hb_tools->add_child(hb_grid);
1167
1168
hb_grid->add_child(memnew(VSeparator));
1169
hb_grid->add_child(memnew(Label(TTR("Offset:"))));
1170
1171
sb_off_x = memnew(SpinBox);
1172
sb_off_x->set_step(1);
1173
sb_off_x->set_suffix("px");
1174
sb_off_x->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_off_x));
1175
sb_off_x->set_accessibility_name(TTRC("Offset X"));
1176
hb_grid->add_child(sb_off_x);
1177
1178
sb_off_y = memnew(SpinBox);
1179
sb_off_y->set_step(1);
1180
sb_off_y->set_suffix("px");
1181
sb_off_y->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_off_y));
1182
sb_off_y->set_accessibility_name(TTRC("Offset Y"));
1183
hb_grid->add_child(sb_off_y);
1184
1185
hb_grid->add_child(memnew(VSeparator));
1186
hb_grid->add_child(memnew(Label(TTR("Step:"))));
1187
1188
sb_step_x = memnew(SpinBox);
1189
sb_step_x->set_min(0);
1190
sb_step_x->set_step(1);
1191
sb_step_x->set_suffix("px");
1192
sb_step_x->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_step_x));
1193
sb_step_x->set_accessibility_name(TTRC("Step X"));
1194
hb_grid->add_child(sb_step_x);
1195
1196
sb_step_y = memnew(SpinBox);
1197
sb_step_y->set_min(0);
1198
sb_step_y->set_step(1);
1199
sb_step_y->set_suffix("px");
1200
sb_step_y->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_step_y));
1201
sb_step_y->set_accessibility_name(TTRC("Step Y"));
1202
hb_grid->add_child(sb_step_y);
1203
1204
hb_grid->add_child(memnew(VSeparator));
1205
hb_grid->add_child(memnew(Label(TTR("Separation:"))));
1206
1207
sb_sep_x = memnew(SpinBox);
1208
sb_sep_x->set_min(0);
1209
sb_sep_x->set_step(1);
1210
sb_sep_x->set_suffix("px");
1211
sb_sep_x->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_sep_x));
1212
sb_sep_x->set_accessibility_name(TTRC("Separation X"));
1213
hb_grid->add_child(sb_sep_x);
1214
1215
sb_sep_y = memnew(SpinBox);
1216
sb_sep_y->set_min(0);
1217
sb_sep_y->set_step(1);
1218
sb_sep_y->set_suffix("px");
1219
sb_sep_y->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_set_snap_sep_y));
1220
sb_sep_y->set_accessibility_name(TTRC("Separation Y"));
1221
hb_grid->add_child(sb_sep_y);
1222
1223
hb_grid->hide();
1224
1225
// Restore grid snap parameters.
1226
_set_grid_parameters_clamping(false);
1227
sb_off_x->set_value(snap_offset.x);
1228
sb_off_y->set_value(snap_offset.y);
1229
sb_step_x->set_value(snap_step.x);
1230
sb_step_y->set_value(snap_step.y);
1231
sb_sep_x->set_value(snap_separation.x);
1232
sb_sep_y->set_value(snap_separation.y);
1233
1234
// Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
1235
draw_zoom = MAX(1.0f, EDSCALE);
1236
max_draw_zoom = 128.0f * MAX(1.0f, EDSCALE);
1237
min_draw_zoom = 0.01f * MAX(1.0f, EDSCALE);
1238
1239
texture_preview = memnew(PanelContainer);
1240
vb->add_child(texture_preview);
1241
texture_preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1242
texture_preview->set_clip_contents(true);
1243
texture_preview->connect(SceneStringName(draw), callable_mp(this, &TextureRegionEditor::_texture_preview_draw));
1244
1245
texture_overlay = memnew(Panel);
1246
texture_preview->add_child(texture_overlay);
1247
texture_overlay->set_focus_mode(Control::FOCUS_CLICK);
1248
texture_overlay->connect(SceneStringName(draw), callable_mp(this, &TextureRegionEditor::_texture_overlay_draw));
1249
texture_overlay->connect(SceneStringName(gui_input), callable_mp(this, &TextureRegionEditor::_texture_overlay_input));
1250
texture_overlay->connect(SceneStringName(focus_exited), callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
1251
1252
HBoxContainer *zoom_hb = memnew(HBoxContainer);
1253
texture_overlay->add_child(zoom_hb);
1254
zoom_hb->set_begin(Point2(5, 5));
1255
1256
zoom_out = memnew(Button);
1257
zoom_out->set_flat(true);
1258
zoom_out->set_tooltip_text(TTR("Zoom Out"));
1259
zoom_out->connect(SceneStringName(pressed), callable_mp(this, &TextureRegionEditor::_zoom_out));
1260
zoom_hb->add_child(zoom_out);
1261
1262
zoom_reset = memnew(Button);
1263
zoom_reset->set_flat(true);
1264
zoom_reset->set_tooltip_text(TTR("Zoom Reset"));
1265
zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &TextureRegionEditor::_zoom_reset));
1266
zoom_hb->add_child(zoom_reset);
1267
1268
zoom_in = memnew(Button);
1269
zoom_in->set_flat(true);
1270
zoom_in->set_tooltip_text(TTR("Zoom In"));
1271
zoom_in->connect(SceneStringName(pressed), callable_mp(this, &TextureRegionEditor::_zoom_in));
1272
zoom_hb->add_child(zoom_in);
1273
1274
vscroll = memnew(VScrollBar);
1275
vscroll->set_anchors_and_offsets_preset(Control::PRESET_RIGHT_WIDE);
1276
vscroll->set_step(0.001);
1277
vscroll->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_scroll_changed));
1278
texture_overlay->add_child(vscroll);
1279
1280
hscroll = memnew(HScrollBar);
1281
hscroll->set_anchors_and_offsets_preset(Control::PRESET_BOTTOM_WIDE);
1282
hscroll->set_step(0.001);
1283
hscroll->connect(SceneStringName(value_changed), callable_mp(this, &TextureRegionEditor::_scroll_changed));
1284
texture_overlay->add_child(hscroll);
1285
}
1286
1287
////////////////////////
1288
1289
bool EditorInspectorPluginTextureRegion::can_handle(Object *p_object) {
1290
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);
1291
}
1292
1293
void EditorInspectorPluginTextureRegion::_region_edit(Object *p_object) {
1294
texture_region_editor->edit(p_object);
1295
}
1296
1297
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) {
1298
if ((p_type == Variant::RECT2 || p_type == Variant::RECT2I)) {
1299
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")) {
1300
EditorInspectorActionButton *button = memnew(EditorInspectorActionButton(TTRC("Edit Region"), SNAME("RegionEdit")));
1301
button->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorPluginTextureRegion::_region_edit).bind(p_object));
1302
add_property_editor(p_path, button, true);
1303
}
1304
}
1305
return false; //not exclusive
1306
}
1307
1308
EditorInspectorPluginTextureRegion::EditorInspectorPluginTextureRegion() {
1309
texture_region_editor = memnew(TextureRegionEditor);
1310
EditorNode::get_singleton()->get_gui_base()->add_child(texture_region_editor);
1311
}
1312
1313
TextureRegionEditorPlugin::TextureRegionEditorPlugin() {
1314
Ref<EditorInspectorPluginTextureRegion> inspector_plugin;
1315
inspector_plugin.instantiate();
1316
add_inspector_plugin(inspector_plugin);
1317
}
1318
1319