2015-09-28 11:06:06 +08:00
|
|
|
/*************************************************************************/
|
2017-01-16 15:04:19 +08:00
|
|
|
/* texture_region_editor_plugin.cpp */
|
2015-09-28 11:06:06 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2015-09-28 11:06:06 +08:00
|
|
|
/*************************************************************************/
|
2020-01-01 18:16:22 +08:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2015-09-28 11:06:06 +08:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2017-01-16 15:04:19 +08:00
|
|
|
#include "texture_region_editor_plugin.h"
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2016-06-20 09:16:41 +08:00
|
|
|
#include "core/core_string_names.h"
|
2020-04-28 21:19:37 +08:00
|
|
|
#include "core/input/input.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/os/keyboard.h"
|
2019-12-24 15:17:23 +08:00
|
|
|
#include "editor/editor_scale.h"
|
2017-03-05 23:44:50 +08:00
|
|
|
#include "scene/gui/check_box.h"
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2018-01-05 07:50:27 +08:00
|
|
|
/**
|
|
|
|
@author Mariano Suligoy
|
|
|
|
*/
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void draw_margin_line(Control *edit_draw, Vector2 from, Vector2 to) {
|
|
|
|
Vector2 line = (to - from).normalized() * 10;
|
2016-06-20 09:16:41 +08:00
|
|
|
while ((to - from).length_squared() > 200) {
|
2020-03-12 20:37:40 +08:00
|
|
|
edit_draw->draw_line(from, from + line, EditorNode::get_singleton()->get_theme_base()->get_theme_color("mono_color", "Editor"), 2);
|
2017-03-05 23:44:50 +08:00
|
|
|
from += line * 2;
|
2016-06-20 09:16:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_region_draw() {
|
2020-04-02 07:20:12 +08:00
|
|
|
Ref<Texture2D> base_tex = nullptr;
|
2020-05-14 22:41:43 +08:00
|
|
|
if (node_sprite) {
|
2016-06-04 04:04:44 +08:00
|
|
|
base_tex = node_sprite->get_texture();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_sprite_3d) {
|
2019-01-22 02:52:57 +08:00
|
|
|
base_tex = node_sprite_3d->get_texture();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_ninepatch) {
|
2017-10-24 13:59:04 +08:00
|
|
|
base_tex = node_ninepatch->get_texture();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2016-06-05 00:40:53 +08:00
|
|
|
base_tex = obj_styleBox->get_texture();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (atlas_tex.is_valid()) {
|
2016-06-10 18:31:38 +08:00
|
|
|
base_tex = atlas_tex->get_atlas();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-03-02 13:25:28 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (base_tex.is_null()) {
|
2015-09-28 11:06:06 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-01-11 11:52:51 +08:00
|
|
|
Transform2D mtx;
|
2019-03-16 21:29:32 +08:00
|
|
|
mtx.elements[2] = -draw_ofs * draw_zoom;
|
2017-03-05 23:44:50 +08:00
|
|
|
mtx.scale_basis(Vector2(draw_zoom, draw_zoom));
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
RS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(), mtx);
|
2017-03-05 23:44:50 +08:00
|
|
|
edit_draw->draw_texture(base_tex, Point2());
|
2020-03-28 02:21:27 +08:00
|
|
|
RS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(), Transform2D());
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2016-06-14 10:54:58 +08:00
|
|
|
if (snap_mode == SNAP_GRID) {
|
2018-08-18 23:32:09 +08:00
|
|
|
Color grid_color = Color(1.0, 1.0, 1.0, 0.15);
|
2015-09-28 11:06:06 +08:00
|
|
|
Size2 s = edit_draw->get_size();
|
2017-09-02 04:33:39 +08:00
|
|
|
int last_cell = 0;
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (snap_step.x != 0) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (snap_separation.x == 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < s.width; i++) {
|
|
|
|
int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / snap_step.x));
|
2020-05-14 22:41:43 +08:00
|
|
|
if (i == 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
last_cell = cell;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (last_cell != cell) {
|
2017-09-26 10:43:20 +08:00
|
|
|
edit_draw->draw_line(Point2(i, 0), Point2(i, s.height), grid_color);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
last_cell = cell;
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < s.width; i++) {
|
|
|
|
int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / (snap_step.x + snap_separation.x)));
|
2020-05-14 22:41:43 +08:00
|
|
|
if (i == 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
last_cell = cell;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (last_cell != cell) {
|
2017-09-26 10:43:20 +08:00
|
|
|
edit_draw->draw_rect(Rect2(i - snap_separation.x * draw_zoom, 0, snap_separation.x * draw_zoom, s.height), grid_color);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
last_cell = cell;
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (snap_step.y != 0) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (snap_separation.y == 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < s.height; i++) {
|
|
|
|
int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / snap_step.y));
|
2020-05-14 22:41:43 +08:00
|
|
|
if (i == 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
last_cell = cell;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (last_cell != cell) {
|
2017-09-26 10:43:20 +08:00
|
|
|
edit_draw->draw_line(Point2(0, i), Point2(s.width, i), grid_color);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
last_cell = cell;
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < s.height; i++) {
|
|
|
|
int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / (snap_step.y + snap_separation.y)));
|
2020-05-14 22:41:43 +08:00
|
|
|
if (i == 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
last_cell = cell;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (last_cell != cell) {
|
2017-09-26 10:43:20 +08:00
|
|
|
edit_draw->draw_rect(Rect2(0, i - snap_separation.y * draw_zoom, s.width, snap_separation.y * draw_zoom), grid_color);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
last_cell = cell;
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
|
|
|
} else if (snap_mode == SNAP_AUTOSLICE) {
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) {
|
2016-06-14 10:54:58 +08:00
|
|
|
Rect2 r = E->get();
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector2 endpoints[4] = {
|
2017-06-04 06:25:13 +08:00
|
|
|
mtx.basis_xform(r.position),
|
|
|
|
mtx.basis_xform(r.position + Vector2(r.size.x, 0)),
|
|
|
|
mtx.basis_xform(r.position + r.size),
|
|
|
|
mtx.basis_xform(r.position + Vector2(0, r.size.y))
|
2016-06-14 10:54:58 +08:00
|
|
|
};
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
int next = (i + 1) % 4;
|
2019-03-16 21:29:32 +08:00
|
|
|
edit_draw->draw_line(endpoints[i] - draw_ofs * draw_zoom, endpoints[next] - draw_ofs * draw_zoom, Color(0.3, 0.7, 1, 1), 2);
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
Ref<Texture2D> select_handle = get_theme_icon("EditorHandle", "EditorIcons");
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2020-01-05 19:52:21 +08:00
|
|
|
Rect2 scroll_rect(Point2(), base_tex->get_size());
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2020-01-05 19:52:21 +08:00
|
|
|
const Vector2 raw_endpoints[4] = {
|
|
|
|
rect.position,
|
|
|
|
rect.position + Vector2(rect.size.x, 0),
|
|
|
|
rect.position + rect.size,
|
|
|
|
rect.position + Vector2(0, rect.size.y)
|
|
|
|
};
|
|
|
|
const Vector2 endpoints[4] = {
|
|
|
|
mtx.basis_xform(raw_endpoints[0]),
|
|
|
|
mtx.basis_xform(raw_endpoints[1]),
|
|
|
|
mtx.basis_xform(raw_endpoints[2]),
|
|
|
|
mtx.basis_xform(raw_endpoints[3])
|
2015-09-28 11:06:06 +08:00
|
|
|
};
|
2020-03-12 20:37:40 +08:00
|
|
|
Color color = get_theme_color("mono_color", "Editor");
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
int prev = (i + 3) % 4;
|
|
|
|
int next = (i + 1) % 4;
|
2015-09-28 11:06:06 +08:00
|
|
|
|
|
|
|
Vector2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized();
|
2019-12-10 12:13:02 +08:00
|
|
|
ofs *= Math_SQRT2 * (select_handle->get_size().width / 2);
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2019-03-16 21:29:32 +08:00
|
|
|
edit_draw->draw_line(endpoints[i] - draw_ofs * draw_zoom, endpoints[next] - draw_ofs * draw_zoom, color, 2);
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (snap_mode != SNAP_AUTOSLICE) {
|
2019-03-16 21:29:32 +08:00
|
|
|
edit_draw->draw_texture(select_handle, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor() - draw_ofs * draw_zoom);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ofs = (endpoints[next] - endpoints[i]) / 2;
|
|
|
|
ofs += (endpoints[next] - endpoints[i]).tangent().normalized() * (select_handle->get_size().width / 2);
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (snap_mode != SNAP_AUTOSLICE) {
|
2019-03-16 21:29:32 +08:00
|
|
|
edit_draw->draw_texture(select_handle, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor() - draw_ofs * draw_zoom);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2020-01-05 19:52:21 +08:00
|
|
|
scroll_rect.expand_to(raw_endpoints[i]);
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
2020-01-05 19:52:21 +08:00
|
|
|
const Size2 scroll_margin = edit_draw->get_size() / draw_zoom;
|
|
|
|
scroll_rect.position -= scroll_margin;
|
|
|
|
scroll_rect.size += scroll_margin * 2;
|
2019-04-25 21:51:27 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
updating_scroll = true;
|
2020-01-14 19:25:55 +08:00
|
|
|
|
2017-06-04 06:25:13 +08:00
|
|
|
hscroll->set_min(scroll_rect.position.x);
|
|
|
|
hscroll->set_max(scroll_rect.position.x + scroll_rect.size.x);
|
2020-01-05 19:52:21 +08:00
|
|
|
if (ABS(scroll_rect.position.x - (scroll_rect.position.x + scroll_rect.size.x)) <= scroll_margin.x) {
|
2018-11-24 12:38:26 +08:00
|
|
|
hscroll->hide();
|
|
|
|
} else {
|
|
|
|
hscroll->show();
|
2020-01-05 19:52:21 +08:00
|
|
|
hscroll->set_page(scroll_margin.x);
|
2018-11-24 12:38:26 +08:00
|
|
|
hscroll->set_value(draw_ofs.x);
|
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-06-04 06:25:13 +08:00
|
|
|
vscroll->set_min(scroll_rect.position.y);
|
|
|
|
vscroll->set_max(scroll_rect.position.y + scroll_rect.size.y);
|
2020-01-05 19:52:21 +08:00
|
|
|
if (ABS(scroll_rect.position.y - (scroll_rect.position.y + scroll_rect.size.y)) <= scroll_margin.y) {
|
2018-11-24 12:38:26 +08:00
|
|
|
vscroll->hide();
|
|
|
|
draw_ofs.y = scroll_rect.position.y;
|
|
|
|
} else {
|
|
|
|
vscroll->show();
|
2020-01-05 19:52:21 +08:00
|
|
|
vscroll->set_page(scroll_margin.y);
|
2018-11-24 12:38:26 +08:00
|
|
|
vscroll->set_value(draw_ofs.y);
|
|
|
|
}
|
2020-01-14 19:25:55 +08:00
|
|
|
|
|
|
|
Size2 hmin = hscroll->get_combined_minimum_size();
|
|
|
|
Size2 vmin = vscroll->get_combined_minimum_size();
|
|
|
|
|
|
|
|
// Avoid scrollbar overlapping.
|
|
|
|
hscroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, vscroll->is_visible() ? -vmin.width : 0);
|
|
|
|
vscroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, hscroll->is_visible() ? -hmin.height : 0);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
updating_scroll = false;
|
2016-06-20 09:16:41 +08:00
|
|
|
|
2017-10-24 13:59:04 +08:00
|
|
|
if (node_ninepatch || obj_styleBox.is_valid()) {
|
2019-07-20 14:09:57 +08:00
|
|
|
float margins[4] = { 0 };
|
2017-10-24 13:59:04 +08:00
|
|
|
if (node_ninepatch) {
|
|
|
|
margins[0] = node_ninepatch->get_patch_margin(MARGIN_TOP);
|
|
|
|
margins[1] = node_ninepatch->get_patch_margin(MARGIN_BOTTOM);
|
|
|
|
margins[2] = node_ninepatch->get_patch_margin(MARGIN_LEFT);
|
|
|
|
margins[3] = node_ninepatch->get_patch_margin(MARGIN_RIGHT);
|
2016-06-20 09:16:41 +08:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
|
|
|
margins[0] = obj_styleBox->get_margin_size(MARGIN_TOP);
|
|
|
|
margins[1] = obj_styleBox->get_margin_size(MARGIN_BOTTOM);
|
|
|
|
margins[2] = obj_styleBox->get_margin_size(MARGIN_LEFT);
|
|
|
|
margins[3] = obj_styleBox->get_margin_size(MARGIN_RIGHT);
|
|
|
|
}
|
2019-07-20 14:09:57 +08:00
|
|
|
|
2016-06-20 09:16:41 +08:00
|
|
|
Vector2 pos[4] = {
|
2019-04-05 21:33:53 +08:00
|
|
|
mtx.basis_xform(Vector2(0, margins[0])) + Vector2(0, endpoints[0].y - draw_ofs.y * draw_zoom),
|
|
|
|
-mtx.basis_xform(Vector2(0, margins[1])) + Vector2(0, endpoints[2].y - draw_ofs.y * draw_zoom),
|
|
|
|
mtx.basis_xform(Vector2(margins[2], 0)) + Vector2(endpoints[0].x - draw_ofs.x * draw_zoom, 0),
|
|
|
|
-mtx.basis_xform(Vector2(margins[3], 0)) + Vector2(endpoints[2].x - draw_ofs.x * draw_zoom, 0)
|
2017-03-05 23:44:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
draw_margin_line(edit_draw, pos[0], pos[0] + Vector2(edit_draw->get_size().x, 0));
|
|
|
|
draw_margin_line(edit_draw, pos[1], pos[1] + Vector2(edit_draw->get_size().x, 0));
|
|
|
|
draw_margin_line(edit_draw, pos[2], pos[2] + Vector2(0, edit_draw->get_size().y));
|
|
|
|
draw_margin_line(edit_draw, pos[3], pos[3] + Vector2(0, edit_draw->get_size().y));
|
2016-06-20 09:16:41 +08:00
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
2017-05-20 23:38:03 +08:00
|
|
|
void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) {
|
2017-01-11 11:52:51 +08:00
|
|
|
Transform2D mtx;
|
2019-03-16 21:29:32 +08:00
|
|
|
mtx.elements[2] = -draw_ofs * draw_zoom;
|
2017-03-05 23:44:50 +08:00
|
|
|
mtx.scale_basis(Vector2(draw_zoom, draw_zoom));
|
|
|
|
|
2020-01-09 13:39:48 +08:00
|
|
|
const real_t handle_radius = 8 * EDSCALE;
|
|
|
|
const real_t handle_offset = 4 * EDSCALE;
|
|
|
|
|
|
|
|
// Position of selection handles.
|
|
|
|
const Vector2 endpoints[8] = {
|
|
|
|
mtx.xform(rect.position) + Vector2(-handle_offset, -handle_offset),
|
|
|
|
mtx.xform(rect.position + Vector2(rect.size.x / 2, 0)) + Vector2(0, -handle_offset),
|
|
|
|
mtx.xform(rect.position + Vector2(rect.size.x, 0)) + Vector2(handle_offset, -handle_offset),
|
|
|
|
mtx.xform(rect.position + Vector2(rect.size.x, rect.size.y / 2)) + Vector2(handle_offset, 0),
|
|
|
|
mtx.xform(rect.position + rect.size) + Vector2(handle_offset, handle_offset),
|
|
|
|
mtx.xform(rect.position + Vector2(rect.size.x / 2, rect.size.y)) + Vector2(0, handle_offset),
|
|
|
|
mtx.xform(rect.position + Vector2(0, rect.size.y)) + Vector2(-handle_offset, handle_offset),
|
|
|
|
mtx.xform(rect.position + Vector2(0, rect.size.y / 2)) + Vector2(-handle_offset, 0)
|
2015-09-28 11:06:06 +08:00
|
|
|
};
|
|
|
|
|
2017-06-21 03:05:36 +08:00
|
|
|
Ref<InputEventMouseButton> mb = p_input;
|
2017-05-20 23:38:03 +08:00
|
|
|
if (mb.is_valid()) {
|
|
|
|
if (mb->get_button_index() == BUTTON_LEFT) {
|
|
|
|
if (mb->is_pressed()) {
|
2017-10-24 13:59:04 +08:00
|
|
|
if (node_ninepatch || obj_styleBox.is_valid()) {
|
2016-06-20 09:16:41 +08:00
|
|
|
edited_margin = -1;
|
2019-07-20 14:09:57 +08:00
|
|
|
float margins[4] = { 0 };
|
2017-10-24 13:59:04 +08:00
|
|
|
if (node_ninepatch) {
|
|
|
|
margins[0] = node_ninepatch->get_patch_margin(MARGIN_TOP);
|
|
|
|
margins[1] = node_ninepatch->get_patch_margin(MARGIN_BOTTOM);
|
|
|
|
margins[2] = node_ninepatch->get_patch_margin(MARGIN_LEFT);
|
|
|
|
margins[3] = node_ninepatch->get_patch_margin(MARGIN_RIGHT);
|
2016-06-20 09:16:41 +08:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
|
|
|
margins[0] = obj_styleBox->get_margin_size(MARGIN_TOP);
|
|
|
|
margins[1] = obj_styleBox->get_margin_size(MARGIN_BOTTOM);
|
|
|
|
margins[2] = obj_styleBox->get_margin_size(MARGIN_LEFT);
|
|
|
|
margins[3] = obj_styleBox->get_margin_size(MARGIN_RIGHT);
|
|
|
|
}
|
2019-07-20 14:09:57 +08:00
|
|
|
|
2016-06-20 09:16:41 +08:00
|
|
|
Vector2 pos[4] = {
|
2019-03-16 21:29:32 +08:00
|
|
|
mtx.basis_xform(rect.position + Vector2(0, margins[0])) - draw_ofs * draw_zoom,
|
|
|
|
mtx.basis_xform(rect.position + rect.size - Vector2(0, margins[1])) - draw_ofs * draw_zoom,
|
|
|
|
mtx.basis_xform(rect.position + Vector2(margins[2], 0)) - draw_ofs * draw_zoom,
|
|
|
|
mtx.basis_xform(rect.position + rect.size - Vector2(margins[3], 0)) - draw_ofs * draw_zoom
|
2017-03-05 23:44:50 +08:00
|
|
|
};
|
2017-06-03 16:54:24 +08:00
|
|
|
if (Math::abs(mb->get_position().y - pos[0].y) < 8) {
|
2016-06-20 09:16:41 +08:00
|
|
|
edited_margin = 0;
|
|
|
|
prev_margin = margins[0];
|
2017-06-03 16:54:24 +08:00
|
|
|
} else if (Math::abs(mb->get_position().y - pos[1].y) < 8) {
|
2016-06-20 09:16:41 +08:00
|
|
|
edited_margin = 1;
|
|
|
|
prev_margin = margins[1];
|
2017-06-03 16:54:24 +08:00
|
|
|
} else if (Math::abs(mb->get_position().x - pos[2].x) < 8) {
|
2016-06-20 09:16:41 +08:00
|
|
|
edited_margin = 2;
|
|
|
|
prev_margin = margins[2];
|
2017-06-03 16:54:24 +08:00
|
|
|
} else if (Math::abs(mb->get_position().x - pos[3].x) < 8) {
|
2016-06-20 09:16:41 +08:00
|
|
|
edited_margin = 3;
|
|
|
|
prev_margin = margins[3];
|
|
|
|
}
|
|
|
|
if (edited_margin >= 0) {
|
2017-06-03 16:54:24 +08:00
|
|
|
drag_from = Vector2(mb->get_position().x, mb->get_position().y);
|
2017-03-05 23:44:50 +08:00
|
|
|
drag = true;
|
2016-06-20 09:16:41 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
if (edited_margin < 0 && snap_mode == SNAP_AUTOSLICE) {
|
2017-06-03 16:54:24 +08:00
|
|
|
Vector2 point = mtx.affine_inverse().xform(Vector2(mb->get_position().x, mb->get_position().y));
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) {
|
2016-06-14 10:54:58 +08:00
|
|
|
if (E->get().has_point(point)) {
|
|
|
|
rect = E->get();
|
2020-04-28 21:19:37 +08:00
|
|
|
if (Input::get_singleton()->is_key_pressed(KEY_CONTROL) && !(Input::get_singleton()->is_key_pressed(KEY_SHIFT | KEY_ALT))) {
|
2016-06-14 10:54:58 +08:00
|
|
|
Rect2 r;
|
2020-05-14 22:41:43 +08:00
|
|
|
if (node_sprite) {
|
2017-03-05 23:44:50 +08:00
|
|
|
r = node_sprite->get_region_rect();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_sprite_3d) {
|
2019-01-22 02:52:57 +08:00
|
|
|
r = node_sprite_3d->get_region_rect();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_ninepatch) {
|
2017-10-24 13:59:04 +08:00
|
|
|
r = node_ninepatch->get_region_rect();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
r = obj_styleBox->get_region_rect();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (atlas_tex.is_valid()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
r = atlas_tex->get_region();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-06-04 06:25:13 +08:00
|
|
|
rect.expand_to(r.position);
|
|
|
|
rect.expand_to(r.position + r.size);
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
2017-08-24 04:25:14 +08:00
|
|
|
undo_redo->create_action(TTR("Set Region Rect"));
|
2017-03-05 23:44:50 +08:00
|
|
|
if (node_sprite) {
|
|
|
|
undo_redo->add_do_method(node_sprite, "set_region_rect", rect);
|
|
|
|
undo_redo->add_undo_method(node_sprite, "set_region_rect", node_sprite->get_region_rect());
|
2019-01-22 02:52:57 +08:00
|
|
|
} else if (node_sprite_3d) {
|
|
|
|
undo_redo->add_do_method(node_sprite_3d, "set_region_rect", rect);
|
|
|
|
undo_redo->add_undo_method(node_sprite_3d, "set_region_rect", node_sprite_3d->get_region_rect());
|
2017-10-24 13:59:04 +08:00
|
|
|
} else if (node_ninepatch) {
|
|
|
|
undo_redo->add_do_method(node_ninepatch, "set_region_rect", rect);
|
|
|
|
undo_redo->add_undo_method(node_ninepatch, "set_region_rect", node_ninepatch->get_region_rect());
|
2016-06-20 09:16:41 +08:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
undo_redo->add_do_method(obj_styleBox.ptr(), "set_region_rect", rect);
|
|
|
|
undo_redo->add_undo_method(obj_styleBox.ptr(), "set_region_rect", obj_styleBox->get_region_rect());
|
2016-06-20 09:16:41 +08:00
|
|
|
} else if (atlas_tex.is_valid()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
undo_redo->add_do_method(atlas_tex.ptr(), "set_region", rect);
|
|
|
|
undo_redo->add_undo_method(atlas_tex.ptr(), "set_region", atlas_tex->get_region());
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
2018-11-24 12:38:26 +08:00
|
|
|
undo_redo->add_do_method(this, "_update_rect");
|
|
|
|
undo_redo->add_undo_method(this, "_update_rect");
|
2017-03-05 23:44:50 +08:00
|
|
|
undo_redo->add_do_method(edit_draw, "update");
|
|
|
|
undo_redo->add_undo_method(edit_draw, "update");
|
2016-06-14 10:54:58 +08:00
|
|
|
undo_redo->commit_action();
|
2016-06-20 09:16:41 +08:00
|
|
|
break;
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-20 09:16:41 +08:00
|
|
|
} else if (edited_margin < 0) {
|
2017-06-03 16:54:24 +08:00
|
|
|
drag_from = mtx.affine_inverse().xform(Vector2(mb->get_position().x, mb->get_position().y));
|
2020-05-14 22:41:43 +08:00
|
|
|
if (snap_mode == SNAP_PIXEL) {
|
2017-03-05 23:44:50 +08:00
|
|
|
drag_from = drag_from.snapped(Vector2(1, 1));
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (snap_mode == SNAP_GRID) {
|
2017-03-05 23:44:50 +08:00
|
|
|
drag_from = snap_point(drag_from);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
drag = true;
|
2020-05-14 22:41:43 +08:00
|
|
|
if (node_sprite) {
|
2017-03-05 23:44:50 +08:00
|
|
|
rect_prev = node_sprite->get_region_rect();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_sprite_3d) {
|
2019-01-22 02:52:57 +08:00
|
|
|
rect_prev = node_sprite_3d->get_region_rect();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_ninepatch) {
|
2017-10-24 13:59:04 +08:00
|
|
|
rect_prev = node_ninepatch->get_region_rect();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
rect_prev = obj_styleBox->get_region_rect();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (atlas_tex.is_valid()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
rect_prev = atlas_tex->get_region();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
Vector2 tuv = endpoints[i];
|
2020-01-09 13:39:48 +08:00
|
|
|
if (tuv.distance_to(Vector2(mb->get_position().x, mb->get_position().y)) < handle_radius) {
|
2017-03-05 23:44:50 +08:00
|
|
|
drag_index = i;
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (drag_index == -1) {
|
2016-06-14 10:54:58 +08:00
|
|
|
creating = true;
|
2017-03-05 23:44:50 +08:00
|
|
|
rect = Rect2(drag_from, Size2());
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (drag) {
|
2016-06-20 09:16:41 +08:00
|
|
|
if (edited_margin >= 0) {
|
2018-11-24 12:38:26 +08:00
|
|
|
undo_redo->create_action(TTR("Set Margin"));
|
2017-03-05 23:44:50 +08:00
|
|
|
static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT };
|
2017-10-24 13:59:04 +08:00
|
|
|
if (node_ninepatch) {
|
|
|
|
undo_redo->add_do_method(node_ninepatch, "set_patch_margin", m[edited_margin], node_ninepatch->get_patch_margin(m[edited_margin]));
|
|
|
|
undo_redo->add_undo_method(node_ninepatch, "set_patch_margin", m[edited_margin], prev_margin);
|
2016-06-20 09:16:41 +08:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
undo_redo->add_do_method(obj_styleBox.ptr(), "set_margin_size", m[edited_margin], obj_styleBox->get_margin_size(m[edited_margin]));
|
|
|
|
undo_redo->add_undo_method(obj_styleBox.ptr(), "set_margin_size", m[edited_margin], prev_margin);
|
2016-06-20 09:16:41 +08:00
|
|
|
obj_styleBox->emit_signal(CoreStringNames::get_singleton()->changed);
|
|
|
|
}
|
|
|
|
edited_margin = -1;
|
|
|
|
} else {
|
2018-11-24 12:38:26 +08:00
|
|
|
undo_redo->create_action(TTR("Set Region Rect"));
|
2017-03-05 23:44:50 +08:00
|
|
|
if (node_sprite) {
|
|
|
|
undo_redo->add_do_method(node_sprite, "set_region_rect", node_sprite->get_region_rect());
|
|
|
|
undo_redo->add_undo_method(node_sprite, "set_region_rect", rect_prev);
|
2019-01-22 02:52:57 +08:00
|
|
|
} else if (node_sprite_3d) {
|
|
|
|
undo_redo->add_do_method(node_sprite_3d, "set_region_rect", node_sprite_3d->get_region_rect());
|
|
|
|
undo_redo->add_undo_method(node_sprite_3d, "set_region_rect", rect_prev);
|
2017-03-05 23:44:50 +08:00
|
|
|
} else if (atlas_tex.is_valid()) {
|
|
|
|
undo_redo->add_do_method(atlas_tex.ptr(), "set_region", atlas_tex->get_region());
|
|
|
|
undo_redo->add_undo_method(atlas_tex.ptr(), "set_region", rect_prev);
|
2017-10-24 13:59:04 +08:00
|
|
|
} else if (node_ninepatch) {
|
|
|
|
undo_redo->add_do_method(node_ninepatch, "set_region_rect", node_ninepatch->get_region_rect());
|
|
|
|
undo_redo->add_undo_method(node_ninepatch, "set_region_rect", rect_prev);
|
2016-06-20 09:16:41 +08:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
undo_redo->add_do_method(obj_styleBox.ptr(), "set_region_rect", obj_styleBox->get_region_rect());
|
|
|
|
undo_redo->add_undo_method(obj_styleBox.ptr(), "set_region_rect", rect_prev);
|
2016-06-04 04:04:44 +08:00
|
|
|
}
|
2016-06-20 09:16:41 +08:00
|
|
|
drag_index = -1;
|
2016-06-04 04:04:44 +08:00
|
|
|
}
|
2018-11-24 12:38:26 +08:00
|
|
|
undo_redo->add_do_method(this, "_update_rect");
|
|
|
|
undo_redo->add_undo_method(this, "_update_rect");
|
2017-03-05 23:44:50 +08:00
|
|
|
undo_redo->add_do_method(edit_draw, "update");
|
|
|
|
undo_redo->add_undo_method(edit_draw, "update");
|
2016-06-20 09:16:41 +08:00
|
|
|
undo_redo->commit_action();
|
2017-03-05 23:44:50 +08:00
|
|
|
drag = false;
|
2016-06-20 09:16:41 +08:00
|
|
|
creating = false;
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
2017-05-20 23:38:03 +08:00
|
|
|
} else if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) {
|
2015-09-28 11:06:06 +08:00
|
|
|
if (drag) {
|
2017-03-05 23:44:50 +08:00
|
|
|
drag = false;
|
2016-06-20 09:16:41 +08:00
|
|
|
if (edited_margin >= 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT };
|
2020-05-14 22:41:43 +08:00
|
|
|
if (node_ninepatch) {
|
2017-10-24 13:59:04 +08:00
|
|
|
node_ninepatch->set_patch_margin(m[edited_margin], prev_margin);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (obj_styleBox.is_valid()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
obj_styleBox->set_margin_size(m[edited_margin], prev_margin);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-06-20 09:16:41 +08:00
|
|
|
edited_margin = -1;
|
|
|
|
} else {
|
|
|
|
apply_rect(rect_prev);
|
2017-03-05 23:44:50 +08:00
|
|
|
rect = rect_prev;
|
2016-06-20 09:16:41 +08:00
|
|
|
edit_draw->update();
|
|
|
|
drag_index = -1;
|
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
2017-05-20 23:38:03 +08:00
|
|
|
} else if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) {
|
2019-03-16 21:29:32 +08:00
|
|
|
_zoom_on_position(draw_zoom * ((0.95 + (0.05 * mb->get_factor())) / 0.95), mb->get_position());
|
2017-05-20 23:38:03 +08:00
|
|
|
} else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) {
|
2019-03-16 21:29:32 +08:00
|
|
|
_zoom_on_position(draw_zoom * (1 - (0.05 * mb->get_factor())), mb->get_position());
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
2017-05-20 23:38:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEventMouseMotion> mm = p_input;
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-05-20 23:38:03 +08:00
|
|
|
if (mm.is_valid()) {
|
2020-04-28 21:19:37 +08:00
|
|
|
if (mm->get_button_mask() & BUTTON_MASK_MIDDLE || Input::get_singleton()->is_key_pressed(KEY_SPACE)) {
|
2019-11-22 15:35:03 +08:00
|
|
|
Vector2 dragged(mm->get_relative().x / draw_zoom, mm->get_relative().y / draw_zoom);
|
|
|
|
hscroll->set_value(hscroll->get_value() - dragged.x);
|
|
|
|
vscroll->set_value(vscroll->get_value() - dragged.y);
|
2015-09-28 11:06:06 +08:00
|
|
|
|
|
|
|
} else if (drag) {
|
2016-06-20 09:16:41 +08:00
|
|
|
if (edited_margin >= 0) {
|
2017-09-02 04:33:39 +08:00
|
|
|
float new_margin = 0;
|
2020-05-14 22:41:43 +08:00
|
|
|
if (edited_margin == 0) {
|
2017-06-03 16:54:24 +08:00
|
|
|
new_margin = prev_margin + (mm->get_position().y - drag_from.y) / draw_zoom;
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (edited_margin == 1) {
|
2017-06-03 16:54:24 +08:00
|
|
|
new_margin = prev_margin - (mm->get_position().y - drag_from.y) / draw_zoom;
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (edited_margin == 2) {
|
2017-06-03 16:54:24 +08:00
|
|
|
new_margin = prev_margin + (mm->get_position().x - drag_from.x) / draw_zoom;
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (edited_margin == 3) {
|
2017-06-03 16:54:24 +08:00
|
|
|
new_margin = prev_margin - (mm->get_position().x - drag_from.x) / draw_zoom;
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2017-09-02 04:33:39 +08:00
|
|
|
ERR_PRINT("Unexpected edited_margin");
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-09-02 04:33:39 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (new_margin < 0) {
|
2016-06-20 09:16:41 +08:00
|
|
|
new_margin = 0;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT };
|
2020-05-14 22:41:43 +08:00
|
|
|
if (node_ninepatch) {
|
2017-10-24 13:59:04 +08:00
|
|
|
node_ninepatch->set_patch_margin(m[edited_margin], new_margin);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (obj_styleBox.is_valid()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
obj_styleBox->set_margin_size(m[edited_margin], new_margin);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-06-20 09:16:41 +08:00
|
|
|
} else {
|
2017-06-03 16:54:24 +08:00
|
|
|
Vector2 new_pos = mtx.affine_inverse().xform(mm->get_position());
|
2020-05-14 22:41:43 +08:00
|
|
|
if (snap_mode == SNAP_PIXEL) {
|
2017-03-05 23:44:50 +08:00
|
|
|
new_pos = new_pos.snapped(Vector2(1, 1));
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (snap_mode == SNAP_GRID) {
|
2017-03-05 23:44:50 +08:00
|
|
|
new_pos = snap_point(new_pos);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-06-20 09:16:41 +08:00
|
|
|
|
|
|
|
if (creating) {
|
2017-03-05 23:44:50 +08:00
|
|
|
rect = Rect2(drag_from, Size2());
|
2016-06-05 00:40:53 +08:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
2016-06-20 09:16:41 +08:00
|
|
|
edit_draw->update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
switch (drag_index) {
|
|
|
|
case 0: {
|
2017-06-04 06:25:13 +08:00
|
|
|
Vector2 p = rect_prev.position + rect_prev.size;
|
2017-03-05 23:44:50 +08:00
|
|
|
rect = Rect2(p, Size2());
|
2016-06-20 09:16:41 +08:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 1: {
|
2017-06-04 06:25:13 +08:00
|
|
|
Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y);
|
2017-03-05 23:44:50 +08:00
|
|
|
rect = Rect2(p, Size2(rect_prev.size.x, 0));
|
2016-06-20 09:16:41 +08:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 2: {
|
2017-06-04 06:25:13 +08:00
|
|
|
Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y);
|
2017-03-05 23:44:50 +08:00
|
|
|
rect = Rect2(p, Size2());
|
2016-06-20 09:16:41 +08:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 3: {
|
2017-06-04 06:25:13 +08:00
|
|
|
Vector2 p = rect_prev.position;
|
2017-03-05 23:44:50 +08:00
|
|
|
rect = Rect2(p, Size2(0, rect_prev.size.y));
|
2016-06-20 09:16:41 +08:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 4: {
|
2017-06-04 06:25:13 +08:00
|
|
|
Vector2 p = rect_prev.position;
|
2017-03-05 23:44:50 +08:00
|
|
|
rect = Rect2(p, Size2());
|
2016-06-20 09:16:41 +08:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 5: {
|
2017-06-04 06:25:13 +08:00
|
|
|
Vector2 p = rect_prev.position;
|
2017-03-05 23:44:50 +08:00
|
|
|
rect = Rect2(p, Size2(rect_prev.size.x, 0));
|
2016-06-20 09:16:41 +08:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 6: {
|
2017-06-04 06:25:13 +08:00
|
|
|
Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0);
|
2017-03-05 23:44:50 +08:00
|
|
|
rect = Rect2(p, Size2());
|
2016-06-20 09:16:41 +08:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case 7: {
|
2017-06-04 06:25:13 +08:00
|
|
|
Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0);
|
2017-03-05 23:44:50 +08:00
|
|
|
rect = Rect2(p, Size2(0, rect_prev.size.y));
|
2016-06-20 09:16:41 +08:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
}
|
2020-01-30 11:39:13 +08:00
|
|
|
|
|
|
|
Ref<InputEventMagnifyGesture> magnify_gesture = p_input;
|
|
|
|
if (magnify_gesture.is_valid()) {
|
|
|
|
_zoom_on_position(draw_zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEventPanGesture> pan_gesture = p_input;
|
|
|
|
if (pan_gesture.is_valid()) {
|
|
|
|
hscroll->set_value(hscroll->get_value() + hscroll->get_page() * pan_gesture->get_delta().x / 8);
|
|
|
|
vscroll->set_value(vscroll->get_value() + vscroll->get_page() * pan_gesture->get_delta().y / 8);
|
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_scroll_changed(float) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (updating_scroll) {
|
2015-09-28 11:06:06 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
draw_ofs.x = hscroll->get_value();
|
|
|
|
draw_ofs.y = vscroll->get_value();
|
2015-09-28 11:06:06 +08:00
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_set_snap_mode(int p_mode) {
|
2016-11-27 08:56:42 +08:00
|
|
|
snap_mode = p_mode;
|
2016-06-14 10:54:58 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (snap_mode == SNAP_GRID) {
|
2016-06-14 10:54:58 +08:00
|
|
|
hb_grid->show();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2016-06-14 10:54:58 +08:00
|
|
|
hb_grid->hide();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-12-25 03:00:30 +08:00
|
|
|
if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) {
|
|
|
|
_update_autoslice();
|
|
|
|
}
|
|
|
|
|
2015-09-28 11:06:06 +08:00
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_set_snap_off_x(float p_val) {
|
|
|
|
snap_offset.x = p_val;
|
2015-09-28 11:06:06 +08:00
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_set_snap_off_y(float p_val) {
|
|
|
|
snap_offset.y = p_val;
|
2015-09-28 11:06:06 +08:00
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_set_snap_step_x(float p_val) {
|
|
|
|
snap_step.x = p_val;
|
2015-09-28 11:06:06 +08:00
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_set_snap_step_y(float p_val) {
|
|
|
|
snap_step.y = p_val;
|
2015-09-28 11:06:06 +08:00
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_set_snap_sep_x(float p_val) {
|
2016-06-14 10:54:58 +08:00
|
|
|
snap_separation.x = p_val;
|
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_set_snap_sep_y(float p_val) {
|
2016-06-14 10:54:58 +08:00
|
|
|
snap_separation.y = p_val;
|
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2019-03-16 21:29:32 +08:00
|
|
|
void TextureRegionEditor::_zoom_on_position(float p_zoom, Point2 p_position) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (p_zoom < 0.25 || p_zoom > 8) {
|
2019-03-16 21:29:32 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2019-03-16 21:29:32 +08:00
|
|
|
|
|
|
|
float prev_zoom = draw_zoom;
|
|
|
|
draw_zoom = p_zoom;
|
|
|
|
Point2 ofs = p_position;
|
|
|
|
ofs = ofs / prev_zoom - ofs / draw_zoom;
|
|
|
|
draw_ofs.x = Math::round(draw_ofs.x + ofs.x);
|
|
|
|
draw_ofs.y = Math::round(draw_ofs.y + ofs.y);
|
|
|
|
|
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_zoom_in() {
|
2019-03-16 21:29:32 +08:00
|
|
|
_zoom_on_position(draw_zoom * 1.5, edit_draw->get_size() / 2.0);
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_zoom_reset() {
|
2019-03-16 21:29:32 +08:00
|
|
|
_zoom_on_position(1.0, edit_draw->get_size() / 2.0);
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_zoom_out() {
|
2019-03-16 21:29:32 +08:00
|
|
|
_zoom_on_position(draw_zoom / 1.5, edit_draw->get_size() / 2.0);
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
|
|
|
|
2018-11-24 12:38:26 +08:00
|
|
|
void TextureRegionEditor::apply_rect(const Rect2 &p_rect) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (node_sprite) {
|
2018-11-24 12:38:26 +08:00
|
|
|
node_sprite->set_region_rect(p_rect);
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_sprite_3d) {
|
2019-01-22 02:52:57 +08:00
|
|
|
node_sprite_3d->set_region_rect(p_rect);
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_ninepatch) {
|
2018-11-24 12:38:26 +08:00
|
|
|
node_ninepatch->set_region_rect(p_rect);
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2018-11-24 12:38:26 +08:00
|
|
|
obj_styleBox->set_region_rect(p_rect);
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (atlas_tex.is_valid()) {
|
2018-11-24 12:38:26 +08:00
|
|
|
atlas_tex->set_region(p_rect);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-11-24 12:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureRegionEditor::_update_rect() {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (node_sprite) {
|
2018-11-24 12:38:26 +08:00
|
|
|
rect = node_sprite->get_region_rect();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_sprite_3d) {
|
2019-01-22 02:52:57 +08:00
|
|
|
rect = node_sprite_3d->get_region_rect();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_ninepatch) {
|
2018-11-24 12:38:26 +08:00
|
|
|
rect = node_ninepatch->get_region_rect();
|
2019-09-18 01:26:20 +08:00
|
|
|
if (rect == Rect2()) {
|
|
|
|
rect = Rect2(Vector2(), node_ninepatch->get_texture()->get_size());
|
|
|
|
}
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2018-11-24 12:38:26 +08:00
|
|
|
rect = obj_styleBox->get_region_rect();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (atlas_tex.is_valid()) {
|
2018-11-24 12:38:26 +08:00
|
|
|
rect = atlas_tex->get_region();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-06-04 04:04:44 +08:00
|
|
|
}
|
|
|
|
|
2017-12-25 03:00:30 +08:00
|
|
|
void TextureRegionEditor::_update_autoslice() {
|
|
|
|
autoslice_is_dirty = false;
|
|
|
|
autoslice_cache.clear();
|
2018-05-18 05:02:16 +08:00
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
Ref<Texture2D> texture = nullptr;
|
2020-05-14 22:41:43 +08:00
|
|
|
if (node_sprite) {
|
2017-12-25 03:00:30 +08:00
|
|
|
texture = node_sprite->get_texture();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_sprite_3d) {
|
2019-01-22 02:52:57 +08:00
|
|
|
texture = node_sprite_3d->get_texture();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_ninepatch) {
|
2017-12-25 03:00:30 +08:00
|
|
|
texture = node_ninepatch->get_texture();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2017-12-25 03:00:30 +08:00
|
|
|
texture = obj_styleBox->get_texture();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (atlas_tex.is_valid()) {
|
2017-12-25 03:00:30 +08:00
|
|
|
texture = atlas_tex->get_atlas();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-12-25 03:00:30 +08:00
|
|
|
|
|
|
|
if (texture.is_null()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int y = 0; y < texture->get_height(); y++) {
|
|
|
|
for (int x = 0; x < texture->get_width(); x++) {
|
|
|
|
if (texture->is_pixel_opaque(x, y)) {
|
|
|
|
bool found = false;
|
|
|
|
for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) {
|
|
|
|
Rect2 grown = E->get().grow(1.5);
|
|
|
|
if (grown.has_point(Point2(x, y))) {
|
|
|
|
E->get().expand_to(Point2(x, y));
|
|
|
|
E->get().expand_to(Point2(x + 1, y + 1));
|
|
|
|
x = E->get().position.x + E->get().size.x - 1;
|
|
|
|
bool merged = true;
|
|
|
|
while (merged) {
|
|
|
|
merged = false;
|
|
|
|
bool queue_erase = false;
|
|
|
|
for (List<Rect2>::Element *F = autoslice_cache.front(); F; F = F->next()) {
|
|
|
|
if (queue_erase) {
|
|
|
|
autoslice_cache.erase(F->prev());
|
|
|
|
queue_erase = false;
|
|
|
|
}
|
2020-05-14 22:41:43 +08:00
|
|
|
if (F == E) {
|
2017-12-25 03:00:30 +08:00
|
|
|
continue;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-12-25 03:00:30 +08:00
|
|
|
if (E->get().grow(1).intersects(F->get())) {
|
|
|
|
E->get().expand_to(F->get().position);
|
|
|
|
E->get().expand_to(F->get().position + F->get().size);
|
|
|
|
if (F->prev()) {
|
|
|
|
F = F->prev();
|
|
|
|
autoslice_cache.erase(F->next());
|
|
|
|
} else {
|
|
|
|
queue_erase = true;
|
|
|
|
// Can't delete the first rect in the list.
|
|
|
|
}
|
|
|
|
merged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
Rect2 new_rect(x, y, 1, 1);
|
|
|
|
autoslice_cache.push_back(new_rect);
|
2018-05-18 05:02:16 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-25 03:00:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cache_map[texture->get_rid()] = autoslice_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureRegionEditor::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
2018-11-24 12:38:26 +08:00
|
|
|
case NOTIFICATION_ENTER_TREE:
|
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2020-03-12 20:37:40 +08:00
|
|
|
edit_draw->add_theme_style_override("panel", get_theme_stylebox("bg", "Tree"));
|
2018-11-24 12:38:26 +08:00
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
case NOTIFICATION_READY: {
|
2020-03-12 20:37:40 +08:00
|
|
|
zoom_out->set_icon(get_theme_icon("ZoomLess", "EditorIcons"));
|
|
|
|
zoom_reset->set_icon(get_theme_icon("ZoomReset", "EditorIcons"));
|
|
|
|
zoom_in->set_icon(get_theme_icon("ZoomMore", "EditorIcons"));
|
2020-01-23 08:55:52 +08:00
|
|
|
|
|
|
|
vscroll->set_anchors_and_margins_preset(PRESET_RIGHT_WIDE);
|
|
|
|
hscroll->set_anchors_and_margins_preset(PRESET_BOTTOM_WIDE);
|
2015-09-28 11:06:06 +08:00
|
|
|
} break;
|
2017-12-25 03:00:30 +08:00
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) {
|
|
|
|
_update_autoslice();
|
|
|
|
}
|
|
|
|
} break;
|
2020-06-30 07:47:18 +08:00
|
|
|
case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
|
2017-12-25 03:00:30 +08:00
|
|
|
// This happens when the user leaves the Editor and returns,
|
2018-11-24 12:38:26 +08:00
|
|
|
// they could have changed the textures, so the cache is cleared.
|
2017-12-25 03:00:30 +08:00
|
|
|
cache_map.clear();
|
|
|
|
_edit_region();
|
|
|
|
} break;
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_node_removed(Object *p_obj) {
|
2019-01-22 02:52:57 +08:00
|
|
|
if (p_obj == node_sprite || p_obj == node_sprite_3d || p_obj == node_ninepatch || p_obj == obj_styleBox.ptr() || p_obj == atlas_tex.ptr()) {
|
2020-04-02 07:20:12 +08:00
|
|
|
node_sprite = nullptr;
|
|
|
|
node_sprite_3d = nullptr;
|
|
|
|
node_ninepatch = nullptr;
|
|
|
|
obj_styleBox = Ref<StyleBox>(nullptr);
|
|
|
|
atlas_tex = Ref<AtlasTexture>(nullptr);
|
2015-09-28 11:06:06 +08:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("_edit_region"), &TextureRegionEditor::_edit_region);
|
|
|
|
ClassDB::bind_method(D_METHOD("_node_removed"), &TextureRegionEditor::_node_removed);
|
2019-03-16 21:29:32 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("_zoom_on_position"), &TextureRegionEditor::_zoom_on_position);
|
2018-11-24 12:38:26 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("_update_rect"), &TextureRegionEditor::_update_rect);
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
2018-05-18 05:02:16 +08:00
|
|
|
bool TextureRegionEditor::is_stylebox() {
|
|
|
|
return obj_styleBox.is_valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextureRegionEditor::is_atlas_texture() {
|
|
|
|
return atlas_tex.is_valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextureRegionEditor::is_ninepatch() {
|
2020-04-02 07:20:12 +08:00
|
|
|
return node_ninepatch != nullptr;
|
2018-05-18 05:02:16 +08:00
|
|
|
}
|
|
|
|
|
2019-01-22 02:52:57 +08:00
|
|
|
Sprite3D *TextureRegionEditor::get_sprite_3d() {
|
|
|
|
return node_sprite_3d;
|
|
|
|
}
|
|
|
|
|
2020-03-27 05:49:16 +08:00
|
|
|
Sprite2D *TextureRegionEditor::get_sprite() {
|
2018-05-18 05:02:16 +08:00
|
|
|
return node_sprite;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::edit(Object *p_obj) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (node_sprite) {
|
2017-03-30 07:30:24 +08:00
|
|
|
node_sprite->remove_change_receptor(this);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (node_sprite_3d) {
|
2019-01-22 02:52:57 +08:00
|
|
|
node_sprite_3d->remove_change_receptor(this);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (node_ninepatch) {
|
2017-10-24 13:59:04 +08:00
|
|
|
node_ninepatch->remove_change_receptor(this);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (obj_styleBox.is_valid()) {
|
2017-03-30 07:30:24 +08:00
|
|
|
obj_styleBox->remove_change_receptor(this);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (atlas_tex.is_valid()) {
|
2017-03-30 07:30:24 +08:00
|
|
|
atlas_tex->remove_change_receptor(this);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-06-05 00:40:53 +08:00
|
|
|
if (p_obj) {
|
2020-03-27 05:49:16 +08:00
|
|
|
node_sprite = Object::cast_to<Sprite2D>(p_obj);
|
2019-01-22 02:52:57 +08:00
|
|
|
node_sprite_3d = Object::cast_to<Sprite3D>(p_obj);
|
2017-10-24 13:59:04 +08:00
|
|
|
node_ninepatch = Object::cast_to<NinePatchRect>(p_obj);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (Object::cast_to<StyleBoxTexture>(p_obj)) {
|
2017-08-25 04:58:51 +08:00
|
|
|
obj_styleBox = Ref<StyleBoxTexture>(Object::cast_to<StyleBoxTexture>(p_obj));
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (Object::cast_to<AtlasTexture>(p_obj)) {
|
2017-08-25 04:58:51 +08:00
|
|
|
atlas_tex = Ref<AtlasTexture>(Object::cast_to<AtlasTexture>(p_obj));
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-10-11 17:31:25 +08:00
|
|
|
p_obj->add_change_receptor(this);
|
2016-06-20 09:16:41 +08:00
|
|
|
_edit_region();
|
2015-09-28 11:06:06 +08:00
|
|
|
} else {
|
2020-04-02 07:20:12 +08:00
|
|
|
node_sprite = nullptr;
|
|
|
|
node_sprite_3d = nullptr;
|
|
|
|
node_ninepatch = nullptr;
|
|
|
|
obj_styleBox = Ref<StyleBoxTexture>(nullptr);
|
|
|
|
atlas_tex = Ref<AtlasTexture>(nullptr);
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
2016-06-20 09:16:41 +08:00
|
|
|
edit_draw->update();
|
2019-01-22 02:52:57 +08:00
|
|
|
if ((node_sprite && !node_sprite->is_region()) || (node_sprite_3d && !node_sprite_3d->is_region())) {
|
2018-05-18 05:02:16 +08:00
|
|
|
set_process(true);
|
|
|
|
}
|
|
|
|
if (!p_obj) {
|
|
|
|
set_process(false);
|
|
|
|
}
|
2016-06-04 04:04:44 +08:00
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2016-10-11 17:31:25 +08:00
|
|
|
void TextureRegionEditor::_changed_callback(Object *p_changed, const char *p_prop) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!is_visible()) {
|
2017-03-30 07:30:24 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2020-11-05 10:01:55 +08:00
|
|
|
String prop = p_prop;
|
|
|
|
if (prop == "atlas" || prop == "texture" || prop == "region") {
|
2016-10-11 17:31:25 +08:00
|
|
|
_edit_region();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-10-11 17:31:25 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditor::_edit_region() {
|
2020-04-02 07:20:12 +08:00
|
|
|
Ref<Texture2D> texture = nullptr;
|
2020-05-14 22:41:43 +08:00
|
|
|
if (node_sprite) {
|
2016-06-04 04:04:44 +08:00
|
|
|
texture = node_sprite->get_texture();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_sprite_3d) {
|
2019-01-22 02:52:57 +08:00
|
|
|
texture = node_sprite_3d->get_texture();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (node_ninepatch) {
|
2017-10-24 13:59:04 +08:00
|
|
|
texture = node_ninepatch->get_texture();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2016-06-05 00:40:53 +08:00
|
|
|
texture = obj_styleBox->get_texture();
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (atlas_tex.is_valid()) {
|
2016-06-10 18:31:38 +08:00
|
|
|
texture = atlas_tex->get_atlas();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-06-04 04:04:44 +08:00
|
|
|
|
|
|
|
if (texture.is_null()) {
|
2018-11-24 12:38:26 +08:00
|
|
|
_zoom_reset();
|
|
|
|
hscroll->hide();
|
|
|
|
vscroll->hide();
|
2018-03-02 13:25:28 +08:00
|
|
|
edit_draw->update();
|
2015-09-28 11:06:06 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-25 03:00:30 +08:00
|
|
|
if (cache_map.has(texture->get_rid())) {
|
|
|
|
autoslice_cache = cache_map[texture->get_rid()];
|
|
|
|
autoslice_is_dirty = false;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (is_visible() && snap_mode == SNAP_AUTOSLICE) {
|
|
|
|
_update_autoslice();
|
|
|
|
} else {
|
|
|
|
autoslice_is_dirty = true;
|
2016-06-14 10:54:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:38:26 +08:00
|
|
|
_update_rect();
|
2016-06-20 09:16:41 +08:00
|
|
|
edit_draw->update();
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
2016-06-04 04:04:44 +08:00
|
|
|
Vector2 TextureRegionEditor::snap_point(Vector2 p_target) const {
|
2016-06-14 10:54:58 +08:00
|
|
|
if (snap_mode == SNAP_GRID) {
|
2019-10-31 20:40:58 +08:00
|
|
|
p_target.x = Math::snap_scalar_separation(snap_offset.x, snap_step.x, p_target.x, snap_separation.x);
|
|
|
|
p_target.y = Math::snap_scalar_separation(snap_offset.y, snap_step.y, p_target.y, snap_separation.y);
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return p_target;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
TextureRegionEditor::TextureRegionEditor(EditorNode *p_editor) {
|
2020-04-02 07:20:12 +08:00
|
|
|
node_sprite = nullptr;
|
|
|
|
node_sprite_3d = nullptr;
|
|
|
|
node_ninepatch = nullptr;
|
|
|
|
obj_styleBox = Ref<StyleBoxTexture>(nullptr);
|
|
|
|
atlas_tex = Ref<AtlasTexture>(nullptr);
|
2017-03-05 23:44:50 +08:00
|
|
|
editor = p_editor;
|
2015-09-28 11:06:06 +08:00
|
|
|
undo_redo = editor->get_undo_redo();
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
snap_step = Vector2(10, 10);
|
|
|
|
snap_separation = Vector2(0, 0);
|
2018-03-01 04:55:13 +08:00
|
|
|
snap_mode = SNAP_NONE;
|
2016-06-29 10:51:13 +08:00
|
|
|
edited_margin = -1;
|
|
|
|
drag_index = -1;
|
2017-03-05 23:44:50 +08:00
|
|
|
drag = false;
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
HBoxContainer *hb_tools = memnew(HBoxContainer);
|
2018-11-24 12:38:26 +08:00
|
|
|
add_child(hb_tools);
|
2017-03-05 23:44:50 +08:00
|
|
|
hb_tools->add_child(memnew(Label(TTR("Snap Mode:"))));
|
2016-06-14 10:54:58 +08:00
|
|
|
|
2018-11-24 12:38:26 +08:00
|
|
|
snap_mode_button = memnew(OptionButton);
|
2016-06-14 10:54:58 +08:00
|
|
|
hb_tools->add_child(snap_mode_button);
|
2018-11-24 12:38:26 +08:00
|
|
|
snap_mode_button->add_item(TTR("None"), 0);
|
|
|
|
snap_mode_button->add_item(TTR("Pixel Snap"), 1);
|
|
|
|
snap_mode_button->add_item(TTR("Grid Snap"), 2);
|
|
|
|
snap_mode_button->add_item(TTR("Auto Slice"), 3);
|
|
|
|
snap_mode_button->select(0);
|
2020-02-22 01:28:45 +08:00
|
|
|
snap_mode_button->connect("item_selected", callable_mp(this, &TextureRegionEditor::_set_snap_mode));
|
2018-11-24 12:38:26 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
hb_grid = memnew(HBoxContainer);
|
2018-11-24 12:38:26 +08:00
|
|
|
hb_tools->add_child(hb_grid);
|
2016-06-14 10:54:58 +08:00
|
|
|
|
2018-11-24 12:38:26 +08:00
|
|
|
hb_grid->add_child(memnew(VSeparator));
|
2017-03-05 23:44:50 +08:00
|
|
|
hb_grid->add_child(memnew(Label(TTR("Offset:"))));
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
sb_off_x = memnew(SpinBox);
|
2015-09-28 11:06:06 +08:00
|
|
|
sb_off_x->set_min(-256);
|
|
|
|
sb_off_x->set_max(256);
|
|
|
|
sb_off_x->set_step(1);
|
2017-01-04 12:16:14 +08:00
|
|
|
sb_off_x->set_value(snap_offset.x);
|
2015-09-28 11:06:06 +08:00
|
|
|
sb_off_x->set_suffix("px");
|
2020-02-22 01:28:45 +08:00
|
|
|
sb_off_x->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_off_x));
|
2016-06-14 10:54:58 +08:00
|
|
|
hb_grid->add_child(sb_off_x);
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
sb_off_y = memnew(SpinBox);
|
2015-09-28 11:06:06 +08:00
|
|
|
sb_off_y->set_min(-256);
|
|
|
|
sb_off_y->set_max(256);
|
|
|
|
sb_off_y->set_step(1);
|
2017-01-04 12:16:14 +08:00
|
|
|
sb_off_y->set_value(snap_offset.y);
|
2015-09-28 11:06:06 +08:00
|
|
|
sb_off_y->set_suffix("px");
|
2020-02-22 01:28:45 +08:00
|
|
|
sb_off_y->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_off_y));
|
2016-06-14 10:54:58 +08:00
|
|
|
hb_grid->add_child(sb_off_y);
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
hb_grid->add_child(memnew(VSeparator));
|
|
|
|
hb_grid->add_child(memnew(Label(TTR("Step:"))));
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
sb_step_x = memnew(SpinBox);
|
2015-09-28 11:06:06 +08:00
|
|
|
sb_step_x->set_min(-256);
|
|
|
|
sb_step_x->set_max(256);
|
|
|
|
sb_step_x->set_step(1);
|
2017-01-04 12:16:14 +08:00
|
|
|
sb_step_x->set_value(snap_step.x);
|
2015-09-28 11:06:06 +08:00
|
|
|
sb_step_x->set_suffix("px");
|
2020-02-22 01:28:45 +08:00
|
|
|
sb_step_x->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_step_x));
|
2016-06-14 10:54:58 +08:00
|
|
|
hb_grid->add_child(sb_step_x);
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
sb_step_y = memnew(SpinBox);
|
2015-09-28 11:06:06 +08:00
|
|
|
sb_step_y->set_min(-256);
|
|
|
|
sb_step_y->set_max(256);
|
|
|
|
sb_step_y->set_step(1);
|
2017-01-04 12:16:14 +08:00
|
|
|
sb_step_y->set_value(snap_step.y);
|
2015-09-28 11:06:06 +08:00
|
|
|
sb_step_y->set_suffix("px");
|
2020-02-22 01:28:45 +08:00
|
|
|
sb_step_y->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_step_y));
|
2016-06-14 10:54:58 +08:00
|
|
|
hb_grid->add_child(sb_step_y);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
hb_grid->add_child(memnew(VSeparator));
|
2018-08-13 07:24:20 +08:00
|
|
|
hb_grid->add_child(memnew(Label(TTR("Sep.:"))));
|
2016-06-14 10:54:58 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
sb_sep_x = memnew(SpinBox);
|
2016-06-14 10:54:58 +08:00
|
|
|
sb_sep_x->set_min(0);
|
|
|
|
sb_sep_x->set_max(256);
|
|
|
|
sb_sep_x->set_step(1);
|
2017-01-04 12:16:14 +08:00
|
|
|
sb_sep_x->set_value(snap_separation.x);
|
2016-06-14 10:54:58 +08:00
|
|
|
sb_sep_x->set_suffix("px");
|
2020-02-22 01:28:45 +08:00
|
|
|
sb_sep_x->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_sep_x));
|
2016-06-14 10:54:58 +08:00
|
|
|
hb_grid->add_child(sb_sep_x);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
sb_sep_y = memnew(SpinBox);
|
2016-06-14 10:54:58 +08:00
|
|
|
sb_sep_y->set_min(0);
|
|
|
|
sb_sep_y->set_max(256);
|
|
|
|
sb_sep_y->set_step(1);
|
2017-01-04 12:16:14 +08:00
|
|
|
sb_sep_y->set_value(snap_separation.y);
|
2016-06-14 10:54:58 +08:00
|
|
|
sb_sep_y->set_suffix("px");
|
2020-02-22 01:28:45 +08:00
|
|
|
sb_sep_y->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_sep_y));
|
2016-06-14 10:54:58 +08:00
|
|
|
hb_grid->add_child(sb_sep_y);
|
|
|
|
|
|
|
|
hb_grid->hide();
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2018-11-24 12:38:26 +08:00
|
|
|
edit_draw = memnew(Panel);
|
|
|
|
add_child(edit_draw);
|
|
|
|
edit_draw->set_v_size_flags(SIZE_EXPAND_FILL);
|
2020-02-22 01:28:45 +08:00
|
|
|
edit_draw->connect("draw", callable_mp(this, &TextureRegionEditor::_region_draw));
|
|
|
|
edit_draw->connect("gui_input", callable_mp(this, &TextureRegionEditor::_region_input));
|
2018-11-24 12:38:26 +08:00
|
|
|
|
|
|
|
draw_zoom = 1.0;
|
|
|
|
edit_draw->set_clip_contents(true);
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2018-11-24 12:38:26 +08:00
|
|
|
HBoxContainer *zoom_hb = memnew(HBoxContainer);
|
|
|
|
edit_draw->add_child(zoom_hb);
|
|
|
|
zoom_hb->set_begin(Point2(5, 5));
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2020-06-20 02:49:04 +08:00
|
|
|
zoom_out = memnew(Button);
|
|
|
|
zoom_out->set_flat(true);
|
2018-11-24 12:38:26 +08:00
|
|
|
zoom_out->set_tooltip(TTR("Zoom Out"));
|
2020-02-22 01:28:45 +08:00
|
|
|
zoom_out->connect("pressed", callable_mp(this, &TextureRegionEditor::_zoom_out));
|
2018-11-24 12:38:26 +08:00
|
|
|
zoom_hb->add_child(zoom_out);
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2020-06-20 02:49:04 +08:00
|
|
|
zoom_reset = memnew(Button);
|
|
|
|
zoom_reset->set_flat(true);
|
2020-01-21 03:58:42 +08:00
|
|
|
zoom_reset->set_tooltip(TTR("Zoom Reset"));
|
2020-02-22 01:28:45 +08:00
|
|
|
zoom_reset->connect("pressed", callable_mp(this, &TextureRegionEditor::_zoom_reset));
|
2018-11-24 12:38:26 +08:00
|
|
|
zoom_hb->add_child(zoom_reset);
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2020-06-20 02:49:04 +08:00
|
|
|
zoom_in = memnew(Button);
|
|
|
|
zoom_in->set_flat(true);
|
2020-01-21 03:58:42 +08:00
|
|
|
zoom_in->set_tooltip(TTR("Zoom In"));
|
2020-02-22 01:28:45 +08:00
|
|
|
zoom_in->connect("pressed", callable_mp(this, &TextureRegionEditor::_zoom_in));
|
2018-11-24 12:38:26 +08:00
|
|
|
zoom_hb->add_child(zoom_in);
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
vscroll = memnew(VScrollBar);
|
2018-11-24 12:38:26 +08:00
|
|
|
vscroll->set_step(0.001);
|
|
|
|
edit_draw->add_child(vscroll);
|
2020-02-22 01:28:45 +08:00
|
|
|
vscroll->connect("value_changed", callable_mp(this, &TextureRegionEditor::_scroll_changed));
|
2017-03-05 23:44:50 +08:00
|
|
|
hscroll = memnew(HScrollBar);
|
2018-11-24 12:38:26 +08:00
|
|
|
hscroll->set_step(0.001);
|
|
|
|
edit_draw->add_child(hscroll);
|
2020-02-22 01:28:45 +08:00
|
|
|
hscroll->connect("value_changed", callable_mp(this, &TextureRegionEditor::_scroll_changed));
|
2015-09-28 11:06:06 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
updating_scroll = false;
|
2020-04-06 09:48:23 +08:00
|
|
|
autoslice_is_dirty = true;
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
2017-08-13 00:52:50 +08:00
|
|
|
void TextureRegionEditorPlugin::edit(Object *p_object) {
|
|
|
|
region_editor->edit(p_object);
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
2017-08-13 00:52:50 +08:00
|
|
|
bool TextureRegionEditorPlugin::handles(Object *p_object) const {
|
2020-03-27 05:49:16 +08:00
|
|
|
return p_object->is_class("Sprite2D") || p_object->is_class("Sprite3D") || p_object->is_class("NinePatchRect") || p_object->is_class("StyleBoxTexture") || p_object->is_class("AtlasTexture");
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
|
2019-07-14 02:55:32 +08:00
|
|
|
void TextureRegionEditorPlugin::_editor_visiblity_changed() {
|
|
|
|
manually_hidden = !region_editor->is_visible_in_tree();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditorPlugin::make_visible(bool p_visible) {
|
2015-09-28 11:06:06 +08:00
|
|
|
if (p_visible) {
|
2018-02-17 19:08:44 +08:00
|
|
|
texture_region_button->show();
|
2019-07-14 02:55:32 +08:00
|
|
|
bool is_node_configured = region_editor->is_stylebox() || region_editor->is_atlas_texture() || region_editor->is_ninepatch() || (region_editor->get_sprite() && region_editor->get_sprite()->is_region()) || (region_editor->get_sprite_3d() && region_editor->get_sprite_3d()->is_region());
|
|
|
|
if ((is_node_configured && !manually_hidden) || texture_region_button->is_pressed()) {
|
2018-05-18 05:02:16 +08:00
|
|
|
editor->make_bottom_panel_item_visible(region_editor);
|
|
|
|
}
|
2015-09-28 11:06:06 +08:00
|
|
|
} else {
|
2018-08-13 07:24:20 +08:00
|
|
|
if (region_editor->is_visible_in_tree()) {
|
|
|
|
editor->hide_bottom_panel();
|
2019-07-14 02:55:32 +08:00
|
|
|
manually_hidden = false;
|
2018-08-13 07:24:20 +08:00
|
|
|
}
|
2018-02-17 19:08:44 +08:00
|
|
|
texture_region_button->hide();
|
2020-04-02 07:20:12 +08:00
|
|
|
region_editor->edit(nullptr);
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-04 04:04:44 +08:00
|
|
|
Dictionary TextureRegionEditorPlugin::get_state() const {
|
2016-01-11 06:02:32 +08:00
|
|
|
Dictionary state;
|
2017-03-05 23:44:50 +08:00
|
|
|
state["snap_offset"] = region_editor->snap_offset;
|
|
|
|
state["snap_step"] = region_editor->snap_step;
|
|
|
|
state["snap_separation"] = region_editor->snap_separation;
|
|
|
|
state["snap_mode"] = region_editor->snap_mode;
|
2016-01-11 06:02:32 +08:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void TextureRegionEditorPlugin::set_state(const Dictionary &p_state) {
|
|
|
|
Dictionary state = p_state;
|
2016-01-11 06:02:32 +08:00
|
|
|
if (state.has("snap_step")) {
|
|
|
|
Vector2 s = state["snap_step"];
|
2017-01-04 12:16:14 +08:00
|
|
|
region_editor->sb_step_x->set_value(s.x);
|
|
|
|
region_editor->sb_step_y->set_value(s.y);
|
2016-01-11 06:02:32 +08:00
|
|
|
region_editor->snap_step = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.has("snap_offset")) {
|
|
|
|
Vector2 ofs = state["snap_offset"];
|
2017-01-04 12:16:14 +08:00
|
|
|
region_editor->sb_off_x->set_value(ofs.x);
|
|
|
|
region_editor->sb_off_y->set_value(ofs.y);
|
2016-01-11 06:02:32 +08:00
|
|
|
region_editor->snap_offset = ofs;
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:54:58 +08:00
|
|
|
if (state.has("snap_separation")) {
|
|
|
|
Vector2 sep = state["snap_separation"];
|
2017-01-04 12:16:14 +08:00
|
|
|
region_editor->sb_sep_x->set_value(sep.x);
|
|
|
|
region_editor->sb_sep_y->set_value(sep.y);
|
2016-06-14 10:54:58 +08:00
|
|
|
region_editor->snap_separation = sep;
|
2016-01-11 06:02:32 +08:00
|
|
|
}
|
|
|
|
|
2016-06-14 10:54:58 +08:00
|
|
|
if (state.has("snap_mode")) {
|
|
|
|
region_editor->_set_snap_mode(state["snap_mode"]);
|
2018-11-24 12:38:26 +08:00
|
|
|
region_editor->snap_mode_button->select(state["snap_mode"]);
|
2016-01-11 06:02:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-14 02:55:32 +08:00
|
|
|
void TextureRegionEditorPlugin::_bind_methods() {
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
TextureRegionEditorPlugin::TextureRegionEditorPlugin(EditorNode *p_node) {
|
2019-07-14 02:55:32 +08:00
|
|
|
manually_hidden = false;
|
2015-09-28 11:06:06 +08:00
|
|
|
editor = p_node;
|
2016-06-20 09:16:41 +08:00
|
|
|
|
2019-07-14 02:55:32 +08:00
|
|
|
region_editor = memnew(TextureRegionEditor(p_node));
|
2018-05-18 05:02:16 +08:00
|
|
|
region_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
|
2015-09-28 11:06:06 +08:00
|
|
|
region_editor->hide();
|
2020-02-22 01:28:45 +08:00
|
|
|
region_editor->connect("visibility_changed", callable_mp(this, &TextureRegionEditorPlugin::_editor_visiblity_changed));
|
2018-08-13 07:24:20 +08:00
|
|
|
|
|
|
|
texture_region_button = p_node->add_bottom_panel_item(TTR("TextureRegion"), region_editor);
|
2018-02-17 19:08:44 +08:00
|
|
|
texture_region_button->hide();
|
2015-09-28 11:06:06 +08:00
|
|
|
}
|