2015-09-28 05:06:06 +02:00
|
|
|
/*************************************************************************/
|
2017-01-16 08:04:19 +01:00
|
|
|
/* texture_region_editor_plugin.cpp */
|
2015-09-28 05:06:06 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2015-09-28 05:06:06 +02:00
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2015-09-28 05:06:06 +02:00
|
|
|
/* */
|
|
|
|
/* Author: Mariano Suligoy */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2017-01-16 08:04:19 +01:00
|
|
|
#include "texture_region_editor_plugin.h"
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2016-06-20 03:16:41 +02:00
|
|
|
#include "core/core_string_names.h"
|
2015-09-28 05:06:06 +02:00
|
|
|
#include "os/input.h"
|
|
|
|
#include "os/keyboard.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "scene/gui/check_box.h"
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void draw_margin_line(Control *edit_draw, Vector2 from, Vector2 to) {
|
|
|
|
Vector2 line = (to - from).normalized() * 10;
|
2016-06-20 03:16:41 +02:00
|
|
|
while ((to - from).length_squared() > 200) {
|
2017-09-26 04:43:20 +02:00
|
|
|
edit_draw->draw_line(from, from + line, EditorNode::get_singleton()->get_theme_base()->get_color("mono_color", "Editor"), 2);
|
2017-03-05 16:44:50 +01:00
|
|
|
from += line * 2;
|
2016-06-20 03:16:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_region_draw() {
|
2016-06-03 22:04:44 +02:00
|
|
|
Ref<Texture> base_tex = NULL;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (node_sprite)
|
2016-06-03 22:04:44 +02:00
|
|
|
base_tex = node_sprite->get_texture();
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (node_patch9)
|
2016-06-03 22:04:44 +02:00
|
|
|
base_tex = node_patch9->get_texture();
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (obj_styleBox.is_valid())
|
2016-06-04 18:40:53 +02:00
|
|
|
base_tex = obj_styleBox->get_texture();
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (atlas_tex.is_valid())
|
2016-06-10 12:31:38 +02:00
|
|
|
base_tex = atlas_tex->get_atlas();
|
2015-09-28 05:06:06 +02:00
|
|
|
if (base_tex.is_null())
|
|
|
|
return;
|
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D mtx;
|
2017-03-05 16:44:50 +01:00
|
|
|
mtx.elements[2] = -draw_ofs;
|
|
|
|
mtx.scale_basis(Vector2(draw_zoom, draw_zoom));
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
VS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(), mtx);
|
|
|
|
edit_draw->draw_texture(base_tex, Point2());
|
|
|
|
VS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(), Transform2D());
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2016-06-14 04:54:58 +02:00
|
|
|
if (snap_mode == SNAP_GRID) {
|
2017-09-26 04:43:20 +02:00
|
|
|
Color grid_color = get_color("grid_major_color", "Editor");
|
2015-09-28 05:06:06 +02:00
|
|
|
Size2 s = edit_draw->get_size();
|
2017-09-01 22:33:39 +02:00
|
|
|
int last_cell = 0;
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (snap_step.x != 0) {
|
2016-06-14 04:54:58 +02:00
|
|
|
if (snap_separation.x == 0)
|
2017-03-05 16:44:50 +01: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));
|
|
|
|
if (i == 0)
|
|
|
|
last_cell = cell;
|
|
|
|
if (last_cell != cell)
|
2017-09-26 04:43:20 +02:00
|
|
|
edit_draw->draw_line(Point2(i, 0), Point2(i, s.height), grid_color);
|
2017-03-05 16:44:50 +01:00
|
|
|
last_cell = cell;
|
2016-06-14 04:54:58 +02:00
|
|
|
}
|
|
|
|
else
|
2017-03-05 16:44:50 +01: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)));
|
|
|
|
if (i == 0)
|
|
|
|
last_cell = cell;
|
|
|
|
if (last_cell != cell)
|
2017-09-26 04:43:20 +02:00
|
|
|
edit_draw->draw_rect(Rect2(i - snap_separation.x * draw_zoom, 0, snap_separation.x * draw_zoom, s.height), grid_color);
|
2017-03-05 16:44:50 +01:00
|
|
|
last_cell = cell;
|
2016-06-14 04:54:58 +02:00
|
|
|
}
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (snap_step.y != 0) {
|
2016-06-14 04:54:58 +02:00
|
|
|
if (snap_separation.y == 0)
|
2017-03-05 16:44:50 +01: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));
|
|
|
|
if (i == 0)
|
|
|
|
last_cell = cell;
|
|
|
|
if (last_cell != cell)
|
2017-09-26 04:43:20 +02:00
|
|
|
edit_draw->draw_line(Point2(0, i), Point2(s.width, i), grid_color);
|
2017-03-05 16:44:50 +01:00
|
|
|
last_cell = cell;
|
2016-06-14 04:54:58 +02:00
|
|
|
}
|
|
|
|
else
|
2017-03-05 16:44:50 +01: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)));
|
|
|
|
if (i == 0)
|
|
|
|
last_cell = cell;
|
|
|
|
if (last_cell != cell)
|
2017-09-26 04:43:20 +02:00
|
|
|
edit_draw->draw_rect(Rect2(0, i - snap_separation.y * draw_zoom, s.width, snap_separation.y * draw_zoom), grid_color);
|
2017-03-05 16:44:50 +01:00
|
|
|
last_cell = cell;
|
2016-06-14 04:54:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (snap_mode == SNAP_AUTOSLICE) {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) {
|
2016-06-14 04:54:58 +02:00
|
|
|
Rect2 r = E->get();
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 endpoints[4] = {
|
2017-06-04 00:25:13 +02: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 04:54:58 +02:00
|
|
|
};
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
int next = (i + 1) % 4;
|
|
|
|
edit_draw->draw_line(endpoints[i] - draw_ofs, endpoints[next] - draw_ofs, Color(0.3, 0.7, 1, 1), 2);
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<Texture> select_handle = get_icon("EditorHandle", "EditorIcons");
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Rect2 scroll_rect(Point2(), mtx.basis_xform(base_tex->get_size()));
|
2015-09-28 05:06:06 +02:00
|
|
|
scroll_rect.expand_to(mtx.basis_xform(edit_draw->get_size()));
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 endpoints[4] = {
|
2017-06-04 00:25:13 +02:00
|
|
|
mtx.basis_xform(rect.position),
|
|
|
|
mtx.basis_xform(rect.position + Vector2(rect.size.x, 0)),
|
|
|
|
mtx.basis_xform(rect.position + rect.size),
|
|
|
|
mtx.basis_xform(rect.position + Vector2(0, rect.size.y))
|
2015-09-28 05:06:06 +02:00
|
|
|
};
|
2017-09-26 04:43:20 +02:00
|
|
|
Color color = get_color("mono_color", "Editor");
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int prev = (i + 3) % 4;
|
|
|
|
int next = (i + 1) % 4;
|
2015-09-28 05:06:06 +02:00
|
|
|
|
|
|
|
Vector2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized();
|
2017-03-05 16:44:50 +01:00
|
|
|
ofs *= 1.4144 * (select_handle->get_size().width / 2);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
edit_draw->draw_line(endpoints[i] - draw_ofs, endpoints[next] - draw_ofs, color, 2);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2016-06-14 04:54:58 +02:00
|
|
|
if (snap_mode != SNAP_AUTOSLICE)
|
2017-03-05 16:44:50 +01:00
|
|
|
edit_draw->draw_texture(select_handle, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor() - draw_ofs);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ofs = (endpoints[next] - endpoints[i]) / 2;
|
|
|
|
ofs += (endpoints[next] - endpoints[i]).tangent().normalized() * (select_handle->get_size().width / 2);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2016-06-14 04:54:58 +02:00
|
|
|
if (snap_mode != SNAP_AUTOSLICE)
|
2017-03-05 16:44:50 +01:00
|
|
|
edit_draw->draw_texture(select_handle, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor() - draw_ofs);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
|
|
|
scroll_rect.expand_to(endpoints[i]);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
scroll_rect = scroll_rect.grow(200);
|
|
|
|
updating_scroll = true;
|
2017-06-04 00:25:13 +02:00
|
|
|
hscroll->set_min(scroll_rect.position.x);
|
|
|
|
hscroll->set_max(scroll_rect.position.x + scroll_rect.size.x);
|
2015-09-28 05:06:06 +02:00
|
|
|
hscroll->set_page(edit_draw->get_size().x);
|
2017-01-04 05:16:14 +01:00
|
|
|
hscroll->set_value(draw_ofs.x);
|
2015-09-28 05:06:06 +02:00
|
|
|
hscroll->set_step(0.001);
|
|
|
|
|
2017-06-04 00:25:13 +02:00
|
|
|
vscroll->set_min(scroll_rect.position.y);
|
|
|
|
vscroll->set_max(scroll_rect.position.y + scroll_rect.size.y);
|
2015-09-28 05:06:06 +02:00
|
|
|
vscroll->set_page(edit_draw->get_size().y);
|
2017-01-04 05:16:14 +01:00
|
|
|
vscroll->set_value(draw_ofs.y);
|
2015-09-28 05:06:06 +02:00
|
|
|
vscroll->set_step(0.001);
|
2017-03-05 16:44:50 +01:00
|
|
|
updating_scroll = false;
|
2016-06-20 03:16:41 +02:00
|
|
|
|
|
|
|
float margins[4];
|
|
|
|
if (node_patch9 || obj_styleBox.is_valid()) {
|
|
|
|
if (node_patch9) {
|
|
|
|
margins[0] = node_patch9->get_patch_margin(MARGIN_TOP);
|
|
|
|
margins[1] = node_patch9->get_patch_margin(MARGIN_BOTTOM);
|
|
|
|
margins[2] = node_patch9->get_patch_margin(MARGIN_LEFT);
|
|
|
|
margins[3] = node_patch9->get_patch_margin(MARGIN_RIGHT);
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
Vector2 pos[4] = {
|
2017-03-05 16:44:50 +01:00
|
|
|
mtx.basis_xform(Vector2(0, margins[0])) + Vector2(0, endpoints[0].y - draw_ofs.y),
|
|
|
|
-mtx.basis_xform(Vector2(0, margins[1])) + Vector2(0, endpoints[2].y - draw_ofs.y),
|
|
|
|
mtx.basis_xform(Vector2(margins[2], 0)) + Vector2(endpoints[0].x - draw_ofs.x, 0),
|
|
|
|
-mtx.basis_xform(Vector2(margins[3], 0)) + Vector2(endpoints[2].x - draw_ofs.x, 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
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 03:16:41 +02:00
|
|
|
}
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) {
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D mtx;
|
2017-03-05 16:44:50 +01:00
|
|
|
mtx.elements[2] = -draw_ofs;
|
|
|
|
mtx.scale_basis(Vector2(draw_zoom, draw_zoom));
|
|
|
|
|
|
|
|
Vector2 endpoints[8] = {
|
2017-06-04 00:25:13 +02:00
|
|
|
mtx.xform(rect.position) + Vector2(-4, -4),
|
|
|
|
mtx.xform(rect.position + Vector2(rect.size.x / 2, 0)) + Vector2(0, -4),
|
|
|
|
mtx.xform(rect.position + Vector2(rect.size.x, 0)) + Vector2(4, -4),
|
|
|
|
mtx.xform(rect.position + Vector2(rect.size.x, rect.size.y / 2)) + Vector2(4, 0),
|
|
|
|
mtx.xform(rect.position + rect.size) + Vector2(4, 4),
|
|
|
|
mtx.xform(rect.position + Vector2(rect.size.x / 2, rect.size.y)) + Vector2(0, 4),
|
|
|
|
mtx.xform(rect.position + Vector2(0, rect.size.y)) + Vector2(-4, 4),
|
|
|
|
mtx.xform(rect.position + Vector2(0, rect.size.y / 2)) + Vector2(-4, 0)
|
2015-09-28 05:06:06 +02:00
|
|
|
};
|
|
|
|
|
2017-06-20 21:05:36 +02:00
|
|
|
Ref<InputEventMouseButton> mb = p_input;
|
2017-05-20 17:38:03 +02:00
|
|
|
if (mb.is_valid()) {
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (mb->get_button_index() == BUTTON_LEFT) {
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (mb->is_pressed()) {
|
2016-06-20 03:16:41 +02:00
|
|
|
if (node_patch9 || obj_styleBox.is_valid()) {
|
|
|
|
edited_margin = -1;
|
|
|
|
float margins[4];
|
|
|
|
if (node_patch9) {
|
|
|
|
margins[0] = node_patch9->get_patch_margin(MARGIN_TOP);
|
|
|
|
margins[1] = node_patch9->get_patch_margin(MARGIN_BOTTOM);
|
|
|
|
margins[2] = node_patch9->get_patch_margin(MARGIN_LEFT);
|
|
|
|
margins[3] = node_patch9->get_patch_margin(MARGIN_RIGHT);
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
Vector2 pos[4] = {
|
2017-06-04 00:25:13 +02:00
|
|
|
mtx.basis_xform(rect.position + Vector2(0, margins[0])) - draw_ofs,
|
|
|
|
mtx.basis_xform(rect.position + rect.size - Vector2(0, margins[1])) - draw_ofs,
|
|
|
|
mtx.basis_xform(rect.position + Vector2(margins[2], 0)) - draw_ofs,
|
|
|
|
mtx.basis_xform(rect.position + rect.size - Vector2(margins[3], 0)) - draw_ofs
|
2017-03-05 16:44:50 +01:00
|
|
|
};
|
2017-06-03 10:54:24 +02:00
|
|
|
if (Math::abs(mb->get_position().y - pos[0].y) < 8) {
|
2016-06-20 03:16:41 +02:00
|
|
|
edited_margin = 0;
|
|
|
|
prev_margin = margins[0];
|
2017-06-03 10:54:24 +02:00
|
|
|
} else if (Math::abs(mb->get_position().y - pos[1].y) < 8) {
|
2016-06-20 03:16:41 +02:00
|
|
|
edited_margin = 1;
|
|
|
|
prev_margin = margins[1];
|
2017-06-03 10:54:24 +02:00
|
|
|
} else if (Math::abs(mb->get_position().x - pos[2].x) < 8) {
|
2016-06-20 03:16:41 +02:00
|
|
|
edited_margin = 2;
|
|
|
|
prev_margin = margins[2];
|
2017-06-03 10:54:24 +02:00
|
|
|
} else if (Math::abs(mb->get_position().x - pos[3].x) < 8) {
|
2016-06-20 03:16:41 +02:00
|
|
|
edited_margin = 3;
|
|
|
|
prev_margin = margins[3];
|
|
|
|
}
|
|
|
|
if (edited_margin >= 0) {
|
2017-06-03 10:54:24 +02:00
|
|
|
drag_from = Vector2(mb->get_position().x, mb->get_position().y);
|
2017-03-05 16:44:50 +01:00
|
|
|
drag = true;
|
2016-06-20 03:16:41 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (edited_margin < 0 && snap_mode == SNAP_AUTOSLICE) {
|
2017-06-03 10:54:24 +02:00
|
|
|
Vector2 point = mtx.affine_inverse().xform(Vector2(mb->get_position().x, mb->get_position().y));
|
2017-03-05 16:44:50 +01:00
|
|
|
for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) {
|
2016-06-14 04:54:58 +02:00
|
|
|
if (E->get().has_point(point)) {
|
|
|
|
rect = E->get();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (Input::get_singleton()->is_key_pressed(KEY_CONTROL) && !(Input::get_singleton()->is_key_pressed(KEY_SHIFT | KEY_ALT))) {
|
2016-06-14 04:54:58 +02:00
|
|
|
Rect2 r;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (node_sprite)
|
|
|
|
r = node_sprite->get_region_rect();
|
|
|
|
else if (node_patch9)
|
|
|
|
r = node_patch9->get_region_rect();
|
|
|
|
else if (obj_styleBox.is_valid())
|
|
|
|
r = obj_styleBox->get_region_rect();
|
|
|
|
else if (atlas_tex.is_valid())
|
|
|
|
r = atlas_tex->get_region();
|
2017-06-04 00:25:13 +02:00
|
|
|
rect.expand_to(r.position);
|
|
|
|
rect.expand_to(r.position + r.size);
|
2016-06-14 04:54:58 +02:00
|
|
|
}
|
2017-08-23 22:25:14 +02:00
|
|
|
undo_redo->create_action(TTR("Set Region Rect"));
|
2017-03-05 16:44:50 +01: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());
|
|
|
|
} else if (node_patch9) {
|
|
|
|
undo_redo->add_do_method(node_patch9, "set_region_rect", rect);
|
|
|
|
undo_redo->add_undo_method(node_patch9, "set_region_rect", node_patch9->get_region_rect());
|
2016-06-20 03:16:41 +02:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2017-03-05 16:44:50 +01: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 03:16:41 +02:00
|
|
|
} else if (atlas_tex.is_valid()) {
|
2017-03-05 16:44:50 +01: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 04:54:58 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
undo_redo->add_do_method(edit_draw, "update");
|
|
|
|
undo_redo->add_undo_method(edit_draw, "update");
|
2016-06-14 04:54:58 +02:00
|
|
|
undo_redo->commit_action();
|
2016-06-20 03:16:41 +02:00
|
|
|
break;
|
2016-06-14 04:54:58 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-20 03:16:41 +02:00
|
|
|
} else if (edited_margin < 0) {
|
2017-06-03 10:54:24 +02:00
|
|
|
drag_from = mtx.affine_inverse().xform(Vector2(mb->get_position().x, mb->get_position().y));
|
2016-06-14 04:54:58 +02:00
|
|
|
if (snap_mode == SNAP_PIXEL)
|
2017-03-05 16:44:50 +01:00
|
|
|
drag_from = drag_from.snapped(Vector2(1, 1));
|
2016-06-14 04:54:58 +02:00
|
|
|
else if (snap_mode == SNAP_GRID)
|
2017-03-05 16:44:50 +01:00
|
|
|
drag_from = snap_point(drag_from);
|
|
|
|
drag = true;
|
|
|
|
if (node_sprite)
|
|
|
|
rect_prev = node_sprite->get_region_rect();
|
|
|
|
else if (node_patch9)
|
|
|
|
rect_prev = node_patch9->get_region_rect();
|
|
|
|
else if (obj_styleBox.is_valid())
|
|
|
|
rect_prev = obj_styleBox->get_region_rect();
|
|
|
|
else if (atlas_tex.is_valid())
|
|
|
|
rect_prev = atlas_tex->get_region();
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
Vector2 tuv = endpoints[i];
|
2017-06-03 10:54:24 +02:00
|
|
|
if (tuv.distance_to(Vector2(mb->get_position().x, mb->get_position().y)) < 8) {
|
2017-03-05 16:44:50 +01:00
|
|
|
drag_index = i;
|
2016-06-14 04:54:58 +02:00
|
|
|
}
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (drag_index == -1) {
|
2016-06-14 04:54:58 +02:00
|
|
|
creating = true;
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = Rect2(drag_from, Size2());
|
2016-06-14 04:54:58 +02:00
|
|
|
}
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (drag) {
|
2016-06-20 03:16:41 +02:00
|
|
|
if (edited_margin >= 0) {
|
|
|
|
undo_redo->create_action("Set Margin");
|
2017-03-05 16:44:50 +01:00
|
|
|
static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT };
|
2016-06-20 03:16:41 +02:00
|
|
|
if (node_patch9) {
|
2017-03-05 16:44:50 +01:00
|
|
|
undo_redo->add_do_method(node_patch9, "set_patch_margin", m[edited_margin], node_patch9->get_patch_margin(m[edited_margin]));
|
|
|
|
undo_redo->add_undo_method(node_patch9, "set_patch_margin", m[edited_margin], prev_margin);
|
2016-06-20 03:16:41 +02:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2017-03-05 16:44:50 +01: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 03:16:41 +02:00
|
|
|
obj_styleBox->emit_signal(CoreStringNames::get_singleton()->changed);
|
|
|
|
}
|
|
|
|
edited_margin = -1;
|
|
|
|
} else {
|
2016-06-14 04:54:58 +02:00
|
|
|
undo_redo->create_action("Set Region Rect");
|
2017-03-05 16:44:50 +01: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);
|
|
|
|
} 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);
|
|
|
|
} else if (node_patch9) {
|
2017-02-23 09:28:09 +01:00
|
|
|
// FIXME: Is this intentional?
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (node_patch9) {
|
|
|
|
undo_redo->add_do_method(node_patch9, "set_region_rect", node_patch9->get_region_rect());
|
|
|
|
undo_redo->add_undo_method(node_patch9, "set_region_rect", rect_prev);
|
2016-06-20 03:16:41 +02:00
|
|
|
} else if (obj_styleBox.is_valid()) {
|
2017-03-05 16:44:50 +01: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-03 22:04:44 +02:00
|
|
|
}
|
2016-06-20 03:16:41 +02:00
|
|
|
drag_index = -1;
|
2016-06-03 22:04:44 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
undo_redo->add_do_method(edit_draw, "update");
|
|
|
|
undo_redo->add_undo_method(edit_draw, "update");
|
2016-06-20 03:16:41 +02:00
|
|
|
undo_redo->commit_action();
|
2017-03-05 16:44:50 +01:00
|
|
|
drag = false;
|
2016-06-20 03:16:41 +02:00
|
|
|
creating = false;
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
} else if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) {
|
2015-09-28 05:06:06 +02:00
|
|
|
|
|
|
|
if (drag) {
|
2017-03-05 16:44:50 +01:00
|
|
|
drag = false;
|
2016-06-20 03:16:41 +02:00
|
|
|
if (edited_margin >= 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT };
|
2016-06-20 03:16:41 +02:00
|
|
|
if (node_patch9)
|
2017-03-05 16:44:50 +01:00
|
|
|
node_patch9->set_patch_margin(m[edited_margin], prev_margin);
|
2016-06-20 03:16:41 +02:00
|
|
|
if (obj_styleBox.is_valid())
|
2017-03-05 16:44:50 +01:00
|
|
|
obj_styleBox->set_margin_size(m[edited_margin], prev_margin);
|
2016-06-20 03:16:41 +02:00
|
|
|
edited_margin = -1;
|
|
|
|
} else {
|
|
|
|
apply_rect(rect_prev);
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = rect_prev;
|
2016-06-20 03:16:41 +02:00
|
|
|
edit_draw->update();
|
|
|
|
drag_index = -1;
|
|
|
|
}
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
2017-05-20 17:38:03 +02:00
|
|
|
} else if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) {
|
2016-06-26 20:25:03 +02:00
|
|
|
_zoom_in();
|
2017-05-20 17:38:03 +02:00
|
|
|
} else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) {
|
2016-06-26 20:25:03 +02:00
|
|
|
_zoom_out();
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
2017-05-20 17:38:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEventMouseMotion> mm = p_input;
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (mm.is_valid()) {
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (mm->get_button_mask() & BUTTON_MASK_MIDDLE || Input::get_singleton()->is_key_pressed(KEY_SPACE)) {
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
Vector2 draged(mm->get_relative().x, mm->get_relative().y);
|
2017-03-05 16:44:50 +01:00
|
|
|
hscroll->set_value(hscroll->get_value() - draged.x);
|
|
|
|
vscroll->set_value(vscroll->get_value() - draged.y);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
|
|
|
} else if (drag) {
|
|
|
|
|
2016-06-20 03:16:41 +02:00
|
|
|
if (edited_margin >= 0) {
|
2017-09-01 22:33:39 +02:00
|
|
|
float new_margin = 0;
|
2016-06-20 03:16:41 +02:00
|
|
|
if (edited_margin == 0)
|
2017-06-03 10:54:24 +02:00
|
|
|
new_margin = prev_margin + (mm->get_position().y - drag_from.y) / draw_zoom;
|
2016-06-20 03:16:41 +02:00
|
|
|
else if (edited_margin == 1)
|
2017-06-03 10:54:24 +02:00
|
|
|
new_margin = prev_margin - (mm->get_position().y - drag_from.y) / draw_zoom;
|
2016-06-20 03:16:41 +02:00
|
|
|
else if (edited_margin == 2)
|
2017-06-03 10:54:24 +02:00
|
|
|
new_margin = prev_margin + (mm->get_position().x - drag_from.x) / draw_zoom;
|
2016-06-20 03:16:41 +02:00
|
|
|
else if (edited_margin == 3)
|
2017-06-03 10:54:24 +02:00
|
|
|
new_margin = prev_margin - (mm->get_position().x - drag_from.x) / draw_zoom;
|
2017-09-01 22:33:39 +02:00
|
|
|
else
|
|
|
|
ERR_PRINT("Unexpected edited_margin");
|
|
|
|
|
2016-06-20 03:16:41 +02:00
|
|
|
if (new_margin < 0)
|
|
|
|
new_margin = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT };
|
2016-06-20 03:16:41 +02:00
|
|
|
if (node_patch9)
|
2017-03-05 16:44:50 +01:00
|
|
|
node_patch9->set_patch_margin(m[edited_margin], new_margin);
|
2016-06-20 03:16:41 +02:00
|
|
|
if (obj_styleBox.is_valid())
|
2017-03-05 16:44:50 +01:00
|
|
|
obj_styleBox->set_margin_size(m[edited_margin], new_margin);
|
2016-06-20 03:16:41 +02:00
|
|
|
} else {
|
2017-06-03 10:54:24 +02:00
|
|
|
Vector2 new_pos = mtx.affine_inverse().xform(mm->get_position());
|
2016-06-20 03:16:41 +02:00
|
|
|
if (snap_mode == SNAP_PIXEL)
|
2017-03-05 16:44:50 +01:00
|
|
|
new_pos = new_pos.snapped(Vector2(1, 1));
|
2016-06-20 03:16:41 +02:00
|
|
|
else if (snap_mode == SNAP_GRID)
|
2017-03-05 16:44:50 +01:00
|
|
|
new_pos = snap_point(new_pos);
|
2016-06-20 03:16:41 +02:00
|
|
|
|
|
|
|
if (creating) {
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = Rect2(drag_from, Size2());
|
2016-06-04 18:40:53 +02:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
2016-06-20 03:16:41 +02:00
|
|
|
edit_draw->update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (drag_index) {
|
|
|
|
case 0: {
|
2017-06-04 00:25:13 +02:00
|
|
|
Vector2 p = rect_prev.position + rect_prev.size;
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = Rect2(p, Size2());
|
2016-06-20 03:16:41 +02:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case 1: {
|
2017-06-04 00:25:13 +02:00
|
|
|
Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y);
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = Rect2(p, Size2(rect_prev.size.x, 0));
|
2016-06-20 03:16:41 +02:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case 2: {
|
2017-06-04 00:25:13 +02:00
|
|
|
Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y);
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = Rect2(p, Size2());
|
2016-06-20 03:16:41 +02:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case 3: {
|
2017-06-04 00:25:13 +02:00
|
|
|
Vector2 p = rect_prev.position;
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = Rect2(p, Size2(0, rect_prev.size.y));
|
2016-06-20 03:16:41 +02:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case 4: {
|
2017-06-04 00:25:13 +02:00
|
|
|
Vector2 p = rect_prev.position;
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = Rect2(p, Size2());
|
2016-06-20 03:16:41 +02:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case 5: {
|
2017-06-04 00:25:13 +02:00
|
|
|
Vector2 p = rect_prev.position;
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = Rect2(p, Size2(rect_prev.size.x, 0));
|
2016-06-20 03:16:41 +02:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case 6: {
|
2017-06-04 00:25:13 +02:00
|
|
|
Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0);
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = Rect2(p, Size2());
|
2016-06-20 03:16:41 +02:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case 7: {
|
2017-06-04 00:25:13 +02:00
|
|
|
Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0);
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = Rect2(p, Size2(0, rect_prev.size.y));
|
2016-06-20 03:16:41 +02:00
|
|
|
rect.expand_to(new_pos);
|
|
|
|
apply_rect(rect);
|
|
|
|
} break;
|
|
|
|
}
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_scroll_changed(float) {
|
2015-09-28 05:06:06 +02:00
|
|
|
if (updating_scroll)
|
|
|
|
return;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
draw_ofs.x = hscroll->get_value();
|
|
|
|
draw_ofs.y = vscroll->get_value();
|
2015-09-28 05:06:06 +02:00
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_set_snap_mode(int p_mode) {
|
|
|
|
snap_mode_button->get_popup()->set_item_checked(snap_mode, false);
|
2016-11-27 01:56:42 +01:00
|
|
|
snap_mode = p_mode;
|
2016-06-14 04:54:58 +02:00
|
|
|
snap_mode_button->set_text(snap_mode_button->get_popup()->get_item_text(p_mode));
|
2017-03-05 16:44:50 +01:00
|
|
|
snap_mode_button->get_popup()->set_item_checked(snap_mode, true);
|
2016-06-14 04:54:58 +02:00
|
|
|
|
|
|
|
if (snap_mode == SNAP_GRID)
|
|
|
|
hb_grid->show();
|
|
|
|
else
|
|
|
|
hb_grid->hide();
|
2015-09-28 05:06:06 +02:00
|
|
|
|
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_set_snap_off_x(float p_val) {
|
|
|
|
snap_offset.x = p_val;
|
2015-09-28 05:06:06 +02:00
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_set_snap_off_y(float p_val) {
|
|
|
|
snap_offset.y = p_val;
|
2015-09-28 05:06:06 +02:00
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_set_snap_step_x(float p_val) {
|
|
|
|
snap_step.x = p_val;
|
2015-09-28 05:06:06 +02:00
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_set_snap_step_y(float p_val) {
|
|
|
|
snap_step.y = p_val;
|
2015-09-28 05:06:06 +02:00
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_set_snap_sep_x(float p_val) {
|
2016-06-14 04:54:58 +02:00
|
|
|
snap_separation.x = p_val;
|
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_set_snap_sep_y(float p_val) {
|
2016-06-14 04:54:58 +02:00
|
|
|
snap_separation.y = p_val;
|
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_zoom_in() {
|
2016-06-14 04:54:58 +02:00
|
|
|
if (draw_zoom < 8) {
|
|
|
|
draw_zoom *= 2;
|
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_zoom_reset() {
|
2016-06-14 04:54:58 +02:00
|
|
|
if (draw_zoom == 1) return;
|
|
|
|
draw_zoom = 1;
|
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_zoom_out() {
|
2016-06-14 04:54:58 +02:00
|
|
|
if (draw_zoom > 0.25) {
|
|
|
|
draw_zoom /= 2;
|
|
|
|
edit_draw->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::apply_rect(const Rect2 &rect) {
|
|
|
|
if (node_sprite)
|
2016-06-20 03:16:41 +02:00
|
|
|
node_sprite->set_region_rect(rect);
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (node_patch9)
|
2016-06-20 03:16:41 +02:00
|
|
|
node_patch9->set_region_rect(rect);
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (obj_styleBox.is_valid())
|
2016-06-20 03:16:41 +02:00
|
|
|
obj_styleBox->set_region_rect(rect);
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (atlas_tex.is_valid())
|
2016-06-20 03:16:41 +02:00
|
|
|
atlas_tex->set_region(rect);
|
2016-06-03 22:04:44 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_READY: {
|
2016-06-14 04:54:58 +02:00
|
|
|
zoom_out->set_icon(get_icon("ZoomLess", "EditorIcons"));
|
|
|
|
zoom_reset->set_icon(get_icon("ZoomReset", "EditorIcons"));
|
|
|
|
zoom_in->set_icon(get_icon("ZoomMore", "EditorIcons"));
|
2017-03-05 16:44:50 +01:00
|
|
|
icon_zoom->set_texture(get_icon("Zoom", "EditorIcons"));
|
2015-09-28 05:06:06 +02:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_node_removed(Object *p_obj) {
|
|
|
|
if (p_obj == node_sprite || p_obj == node_patch9 || p_obj == obj_styleBox.ptr() || p_obj == atlas_tex.ptr()) {
|
2016-06-20 03:16:41 +02:00
|
|
|
node_patch9 = NULL;
|
|
|
|
node_sprite = NULL;
|
|
|
|
obj_styleBox = Ref<StyleBox>(NULL);
|
|
|
|
atlas_tex = Ref<AtlasTexture>(NULL);
|
2015-09-28 05:06:06 +02:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("_edit_region"), &TextureRegionEditor::_edit_region);
|
|
|
|
ClassDB::bind_method(D_METHOD("_region_draw"), &TextureRegionEditor::_region_draw);
|
|
|
|
ClassDB::bind_method(D_METHOD("_region_input"), &TextureRegionEditor::_region_input);
|
|
|
|
ClassDB::bind_method(D_METHOD("_scroll_changed"), &TextureRegionEditor::_scroll_changed);
|
|
|
|
ClassDB::bind_method(D_METHOD("_node_removed"), &TextureRegionEditor::_node_removed);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_snap_mode"), &TextureRegionEditor::_set_snap_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_snap_off_x"), &TextureRegionEditor::_set_snap_off_x);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_snap_off_y"), &TextureRegionEditor::_set_snap_off_y);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_snap_step_x"), &TextureRegionEditor::_set_snap_step_x);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_snap_step_y"), &TextureRegionEditor::_set_snap_step_y);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_snap_sep_x"), &TextureRegionEditor::_set_snap_sep_x);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_snap_sep_y"), &TextureRegionEditor::_set_snap_sep_y);
|
|
|
|
ClassDB::bind_method(D_METHOD("_zoom_in"), &TextureRegionEditor::_zoom_in);
|
|
|
|
ClassDB::bind_method(D_METHOD("_zoom_reset"), &TextureRegionEditor::_zoom_reset);
|
|
|
|
ClassDB::bind_method(D_METHOD("_zoom_out"), &TextureRegionEditor::_zoom_out);
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::edit(Object *p_obj) {
|
2017-03-30 01:30:24 +02:00
|
|
|
if (node_sprite)
|
|
|
|
node_sprite->remove_change_receptor(this);
|
|
|
|
if (node_patch9)
|
|
|
|
node_patch9->remove_change_receptor(this);
|
|
|
|
if (obj_styleBox.is_valid())
|
|
|
|
obj_styleBox->remove_change_receptor(this);
|
|
|
|
if (atlas_tex.is_valid())
|
|
|
|
atlas_tex->remove_change_receptor(this);
|
2016-06-04 18:40:53 +02:00
|
|
|
if (p_obj) {
|
2017-08-24 22:58:51 +02:00
|
|
|
node_sprite = Object::cast_to<Sprite>(p_obj);
|
|
|
|
node_patch9 = Object::cast_to<NinePatchRect>(p_obj);
|
|
|
|
if (Object::cast_to<StyleBoxTexture>(p_obj))
|
|
|
|
obj_styleBox = Ref<StyleBoxTexture>(Object::cast_to<StyleBoxTexture>(p_obj));
|
|
|
|
if (Object::cast_to<AtlasTexture>(p_obj))
|
|
|
|
atlas_tex = Ref<AtlasTexture>(Object::cast_to<AtlasTexture>(p_obj));
|
2016-10-11 11:31:25 +02:00
|
|
|
p_obj->add_change_receptor(this);
|
2016-06-20 03:16:41 +02:00
|
|
|
_edit_region();
|
2015-09-28 05:06:06 +02:00
|
|
|
} else {
|
2016-06-20 03:16:41 +02:00
|
|
|
node_sprite = NULL;
|
|
|
|
node_patch9 = NULL;
|
|
|
|
obj_styleBox = Ref<StyleBoxTexture>(NULL);
|
|
|
|
atlas_tex = Ref<AtlasTexture>(NULL);
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
2016-06-20 03:16:41 +02:00
|
|
|
edit_draw->update();
|
2016-06-03 22:04:44 +02:00
|
|
|
}
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2016-10-11 11:31:25 +02:00
|
|
|
void TextureRegionEditor::_changed_callback(Object *p_changed, const char *p_prop) {
|
2017-03-30 01:30:24 +02:00
|
|
|
|
|
|
|
if (!is_visible())
|
|
|
|
return;
|
|
|
|
if (p_prop == StringName("atlas") || p_prop == StringName("texture"))
|
2016-10-11 11:31:25 +02:00
|
|
|
_edit_region();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditor::_edit_region() {
|
2016-06-03 22:04:44 +02:00
|
|
|
Ref<Texture> texture = NULL;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (node_sprite)
|
2016-06-03 22:04:44 +02:00
|
|
|
texture = node_sprite->get_texture();
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (node_patch9)
|
2016-06-03 22:04:44 +02:00
|
|
|
texture = node_patch9->get_texture();
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (obj_styleBox.is_valid())
|
2016-06-04 18:40:53 +02:00
|
|
|
texture = obj_styleBox->get_texture();
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (atlas_tex.is_valid())
|
2016-06-10 12:31:38 +02:00
|
|
|
texture = atlas_tex->get_atlas();
|
2016-06-03 22:04:44 +02:00
|
|
|
|
|
|
|
if (texture.is_null()) {
|
2015-09-28 05:06:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-14 04:54:58 +02:00
|
|
|
autoslice_cache.clear();
|
2017-05-17 12:36:47 +02:00
|
|
|
Ref<Image> i;
|
|
|
|
i.instance();
|
|
|
|
if (i->load(texture->get_path()) == OK) {
|
2016-06-14 04:54:58 +02:00
|
|
|
BitMap bm;
|
|
|
|
bm.create_from_image_alpha(i);
|
2017-05-17 12:36:47 +02:00
|
|
|
for (int y = 0; y < i->get_height(); y++) {
|
|
|
|
for (int x = 0; x < i->get_width(); x++) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (bm.get_bit(Point2(x, y))) {
|
2016-06-14 04:54:58 +02:00
|
|
|
bool found = false;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) {
|
2016-06-14 04:54:58 +02:00
|
|
|
Rect2 grown = E->get().grow(1.5);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (grown.has_point(Point2(x, y))) {
|
|
|
|
E->get().expand_to(Point2(x, y));
|
|
|
|
E->get().expand_to(Point2(x + 1, y + 1));
|
2017-06-04 00:25:13 +02:00
|
|
|
x = E->get().position.x + E->get().size.x - 1;
|
2016-06-14 04:54:58 +02:00
|
|
|
bool merged = true;
|
|
|
|
while (merged) {
|
|
|
|
merged = false;
|
2016-07-23 02:11:12 +02:00
|
|
|
bool queue_erase = false;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (List<Rect2>::Element *F = autoslice_cache.front(); F; F = F->next()) {
|
|
|
|
if (queue_erase) {
|
2016-07-23 02:11:12 +02:00
|
|
|
autoslice_cache.erase(F->prev());
|
|
|
|
queue_erase = false;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (F == E)
|
2016-06-14 04:54:58 +02:00
|
|
|
continue;
|
|
|
|
if (E->get().grow(1).intersects(F->get())) {
|
2017-06-04 00:25:13 +02:00
|
|
|
E->get().expand_to(F->get().position);
|
|
|
|
E->get().expand_to(F->get().position + F->get().size);
|
2016-07-23 02:11:12 +02:00
|
|
|
if (F->prev()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
F = F->prev();
|
2016-07-23 02:11:12 +02:00
|
|
|
autoslice_cache.erase(F->next());
|
|
|
|
} else {
|
|
|
|
queue_erase = true;
|
2017-03-24 21:45:31 +01:00
|
|
|
//Can't delete the first rect in the list.
|
2016-07-23 02:11:12 +02:00
|
|
|
}
|
2016-06-14 04:54:58 +02:00
|
|
|
merged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Rect2 new_rect(x, y, 1, 1);
|
2016-06-14 04:54:58 +02:00
|
|
|
autoslice_cache.push_back(new_rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (node_sprite)
|
2016-06-20 03:16:41 +02:00
|
|
|
rect = node_sprite->get_region_rect();
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (node_patch9)
|
2016-06-20 03:16:41 +02:00
|
|
|
rect = node_patch9->get_region_rect();
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (obj_styleBox.is_valid())
|
2016-06-20 03:16:41 +02:00
|
|
|
rect = obj_styleBox->get_region_rect();
|
|
|
|
else if (atlas_tex.is_valid())
|
|
|
|
rect = atlas_tex->get_region();
|
2016-06-03 22:04:44 +02:00
|
|
|
|
2016-06-20 03:16:41 +02:00
|
|
|
edit_draw->update();
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
|
2016-06-14 04:54:58 +02:00
|
|
|
inline float _snap_scalar(float p_offset, float p_step, float separation, float p_target) {
|
|
|
|
if (p_step != 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
float a = Math::stepify(p_target - p_offset, p_step + separation) + p_offset;
|
2016-06-14 04:54:58 +02:00
|
|
|
float b = a;
|
|
|
|
if (p_target >= 0)
|
|
|
|
b -= separation;
|
|
|
|
else
|
|
|
|
b += p_step;
|
2017-03-05 16:44:50 +01:00
|
|
|
return (Math::abs(p_target - a) < Math::abs(p_target - b)) ? a : b;
|
2016-06-14 04:54:58 +02:00
|
|
|
}
|
|
|
|
return p_target;
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 22:04:44 +02:00
|
|
|
Vector2 TextureRegionEditor::snap_point(Vector2 p_target) const {
|
2016-06-14 04:54:58 +02:00
|
|
|
if (snap_mode == SNAP_GRID) {
|
|
|
|
p_target.x = _snap_scalar(snap_offset.x, snap_step.x, snap_separation.x, p_target.x);
|
|
|
|
p_target.y = _snap_scalar(snap_offset.y, snap_step.y, snap_separation.y, p_target.y);
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return p_target;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
TextureRegionEditor::TextureRegionEditor(EditorNode *p_editor) {
|
2016-06-03 22:04:44 +02:00
|
|
|
node_sprite = NULL;
|
|
|
|
node_patch9 = NULL;
|
2016-06-20 03:16:41 +02:00
|
|
|
obj_styleBox = Ref<StyleBoxTexture>(NULL);
|
|
|
|
atlas_tex = Ref<AtlasTexture>(NULL);
|
2017-03-05 16:44:50 +01:00
|
|
|
editor = p_editor;
|
2015-09-28 05:06:06 +02:00
|
|
|
undo_redo = editor->get_undo_redo();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
snap_step = Vector2(10, 10);
|
|
|
|
snap_separation = Vector2(0, 0);
|
2016-06-29 04:51:13 +02:00
|
|
|
edited_margin = -1;
|
|
|
|
drag_index = -1;
|
2017-03-05 16:44:50 +01:00
|
|
|
drag = false;
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
VBoxContainer *main_vb = memnew(VBoxContainer);
|
2016-06-20 03:16:41 +02:00
|
|
|
add_child(main_vb);
|
2017-09-22 00:12:33 +02:00
|
|
|
main_vb->set_anchors_and_margins_preset(Control::PRESET_WIDE);
|
2017-03-05 16:44:50 +01:00
|
|
|
HBoxContainer *hb_tools = memnew(HBoxContainer);
|
2015-09-28 05:06:06 +02:00
|
|
|
main_vb->add_child(hb_tools);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
hb_tools->add_child(memnew(Label(TTR("Snap Mode:"))));
|
2016-06-14 04:54:58 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
snap_mode_button = memnew(MenuButton);
|
2016-06-14 04:54:58 +02:00
|
|
|
hb_tools->add_child(snap_mode_button);
|
|
|
|
snap_mode_button->set_text(TTR("<None>"));
|
|
|
|
PopupMenu *p = snap_mode_button->get_popup();
|
2017-04-14 13:49:50 +02:00
|
|
|
p->set_hide_on_checkable_item_selection(false);
|
2017-03-05 16:44:50 +01:00
|
|
|
p->add_item(TTR("<None>"), 0);
|
|
|
|
p->add_item(TTR("Pixel Snap"), 1);
|
|
|
|
p->add_item(TTR("Grid Snap"), 2);
|
|
|
|
p->add_item(TTR("Auto Slice"), 3);
|
2016-06-14 04:54:58 +02:00
|
|
|
for (int i = 0; i < 4; i++)
|
2017-03-05 16:44:50 +01:00
|
|
|
p->set_item_as_checkable(i, true);
|
|
|
|
p->set_item_checked(0, true);
|
2017-01-08 22:18:54 +01:00
|
|
|
p->connect("id_pressed", this, "_set_snap_mode");
|
2017-03-05 16:44:50 +01:00
|
|
|
hb_grid = memnew(HBoxContainer);
|
2016-06-14 04:54:58 +02:00
|
|
|
hb_tools->add_child(hb_grid);
|
2017-03-05 16:44:50 +01:00
|
|
|
hb_grid->add_child(memnew(VSeparator));
|
2016-06-14 04:54:58 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
hb_grid->add_child(memnew(Label(TTR("Offset:"))));
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
sb_off_x = memnew(SpinBox);
|
2015-09-28 05:06:06 +02:00
|
|
|
sb_off_x->set_min(-256);
|
|
|
|
sb_off_x->set_max(256);
|
|
|
|
sb_off_x->set_step(1);
|
2017-01-04 05:16:14 +01:00
|
|
|
sb_off_x->set_value(snap_offset.x);
|
2015-09-28 05:06:06 +02:00
|
|
|
sb_off_x->set_suffix("px");
|
|
|
|
sb_off_x->connect("value_changed", this, "_set_snap_off_x");
|
2016-06-14 04:54:58 +02:00
|
|
|
hb_grid->add_child(sb_off_x);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
sb_off_y = memnew(SpinBox);
|
2015-09-28 05:06:06 +02:00
|
|
|
sb_off_y->set_min(-256);
|
|
|
|
sb_off_y->set_max(256);
|
|
|
|
sb_off_y->set_step(1);
|
2017-01-04 05:16:14 +01:00
|
|
|
sb_off_y->set_value(snap_offset.y);
|
2015-09-28 05:06:06 +02:00
|
|
|
sb_off_y->set_suffix("px");
|
|
|
|
sb_off_y->connect("value_changed", this, "_set_snap_off_y");
|
2016-06-14 04:54:58 +02:00
|
|
|
hb_grid->add_child(sb_off_y);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
hb_grid->add_child(memnew(VSeparator));
|
|
|
|
hb_grid->add_child(memnew(Label(TTR("Step:"))));
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
sb_step_x = memnew(SpinBox);
|
2015-09-28 05:06:06 +02:00
|
|
|
sb_step_x->set_min(-256);
|
|
|
|
sb_step_x->set_max(256);
|
|
|
|
sb_step_x->set_step(1);
|
2017-01-04 05:16:14 +01:00
|
|
|
sb_step_x->set_value(snap_step.x);
|
2015-09-28 05:06:06 +02:00
|
|
|
sb_step_x->set_suffix("px");
|
|
|
|
sb_step_x->connect("value_changed", this, "_set_snap_step_x");
|
2016-06-14 04:54:58 +02:00
|
|
|
hb_grid->add_child(sb_step_x);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
sb_step_y = memnew(SpinBox);
|
2015-09-28 05:06:06 +02:00
|
|
|
sb_step_y->set_min(-256);
|
|
|
|
sb_step_y->set_max(256);
|
|
|
|
sb_step_y->set_step(1);
|
2017-01-04 05:16:14 +01:00
|
|
|
sb_step_y->set_value(snap_step.y);
|
2015-09-28 05:06:06 +02:00
|
|
|
sb_step_y->set_suffix("px");
|
|
|
|
sb_step_y->connect("value_changed", this, "_set_snap_step_y");
|
2016-06-14 04:54:58 +02:00
|
|
|
hb_grid->add_child(sb_step_y);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
hb_grid->add_child(memnew(VSeparator));
|
|
|
|
hb_grid->add_child(memnew(Label(TTR("Separation:"))));
|
2016-06-14 04:54:58 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
sb_sep_x = memnew(SpinBox);
|
2016-06-14 04:54:58 +02:00
|
|
|
sb_sep_x->set_min(0);
|
|
|
|
sb_sep_x->set_max(256);
|
|
|
|
sb_sep_x->set_step(1);
|
2017-01-04 05:16:14 +01:00
|
|
|
sb_sep_x->set_value(snap_separation.x);
|
2016-06-14 04:54:58 +02:00
|
|
|
sb_sep_x->set_suffix("px");
|
|
|
|
sb_sep_x->connect("value_changed", this, "_set_snap_sep_x");
|
|
|
|
hb_grid->add_child(sb_sep_x);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
sb_sep_y = memnew(SpinBox);
|
2016-06-14 04:54:58 +02:00
|
|
|
sb_sep_y->set_min(0);
|
|
|
|
sb_sep_y->set_max(256);
|
|
|
|
sb_sep_y->set_step(1);
|
2017-01-04 05:16:14 +01:00
|
|
|
sb_sep_y->set_value(snap_separation.y);
|
2016-06-14 04:54:58 +02:00
|
|
|
sb_sep_y->set_suffix("px");
|
|
|
|
sb_sep_y->connect("value_changed", this, "_set_snap_sep_y");
|
|
|
|
hb_grid->add_child(sb_sep_y);
|
|
|
|
|
|
|
|
hb_grid->hide();
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HBoxContainer *main_hb = memnew(HBoxContainer);
|
2015-09-28 05:06:06 +02:00
|
|
|
main_vb->add_child(main_hb);
|
2017-03-05 16:44:50 +01:00
|
|
|
edit_draw = memnew(Control);
|
2015-09-28 05:06:06 +02:00
|
|
|
main_hb->add_child(edit_draw);
|
|
|
|
main_hb->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
edit_draw->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Control *separator = memnew(Control);
|
2016-06-14 04:54:58 +02:00
|
|
|
separator->set_h_size_flags(Control::SIZE_EXPAND_FILL);
|
|
|
|
hb_tools->add_child(separator);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
icon_zoom = memnew(TextureRect);
|
2015-09-28 05:06:06 +02:00
|
|
|
hb_tools->add_child(icon_zoom);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
zoom_out = memnew(ToolButton);
|
2016-06-14 04:54:58 +02:00
|
|
|
zoom_out->connect("pressed", this, "_zoom_out");
|
|
|
|
hb_tools->add_child(zoom_out);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
zoom_reset = memnew(ToolButton);
|
2016-06-14 04:54:58 +02:00
|
|
|
zoom_reset->connect("pressed", this, "_zoom_reset");
|
|
|
|
hb_tools->add_child(zoom_reset);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
zoom_in = memnew(ToolButton);
|
2016-06-14 04:54:58 +02:00
|
|
|
zoom_in->connect("pressed", this, "_zoom_in");
|
|
|
|
hb_tools->add_child(zoom_in);
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
vscroll = memnew(VScrollBar);
|
2015-09-28 05:06:06 +02:00
|
|
|
main_hb->add_child(vscroll);
|
2017-03-05 16:44:50 +01:00
|
|
|
vscroll->connect("value_changed", this, "_scroll_changed");
|
|
|
|
hscroll = memnew(HScrollBar);
|
2015-09-28 05:06:06 +02:00
|
|
|
main_vb->add_child(hscroll);
|
2017-03-05 16:44:50 +01:00
|
|
|
hscroll->connect("value_changed", this, "_scroll_changed");
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
edit_draw->connect("draw", this, "_region_draw");
|
|
|
|
edit_draw->connect("gui_input", this, "_region_input");
|
|
|
|
draw_zoom = 1.0;
|
|
|
|
updating_scroll = false;
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-01-09 19:50:08 +01:00
|
|
|
edit_draw->set_clip_contents(true);
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
|
2017-08-12 18:52:50 +02:00
|
|
|
void TextureRegionEditorPlugin::edit(Object *p_object) {
|
|
|
|
region_editor->edit(p_object);
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
|
2017-08-12 18:52:50 +02:00
|
|
|
bool TextureRegionEditorPlugin::handles(Object *p_object) const {
|
2017-09-30 14:44:01 +02:00
|
|
|
return p_object->is_class("Sprite") || p_object->is_class("NinePatchRect") || p_object->is_class("StyleBoxTexture") || p_object->is_class("AtlasTexture");
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditorPlugin::make_visible(bool p_visible) {
|
2015-09-28 05:06:06 +02:00
|
|
|
if (p_visible) {
|
2016-06-20 03:16:41 +02:00
|
|
|
region_button->show();
|
|
|
|
if (region_button->is_pressed())
|
|
|
|
region_editor->show();
|
2015-09-28 05:06:06 +02:00
|
|
|
} else {
|
2016-06-20 03:16:41 +02:00
|
|
|
region_button->hide();
|
2015-09-28 05:06:06 +02:00
|
|
|
region_editor->edit(NULL);
|
2016-06-20 03:16:41 +02:00
|
|
|
region_editor->hide();
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:04:44 +02:00
|
|
|
Dictionary TextureRegionEditorPlugin::get_state() const {
|
2016-01-10 23:02:32 +01:00
|
|
|
|
|
|
|
Dictionary state;
|
2017-03-05 16:44:50 +01:00
|
|
|
state["zoom"] = region_editor->draw_zoom;
|
|
|
|
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-10 23:02:32 +01:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TextureRegionEditorPlugin::set_state(const Dictionary &p_state) {
|
2016-01-10 23:02:32 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Dictionary state = p_state;
|
2016-01-10 23:02:32 +01:00
|
|
|
if (state.has("zoom")) {
|
2016-06-14 04:54:58 +02:00
|
|
|
region_editor->draw_zoom = p_state["zoom"];
|
2016-01-10 23:02:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state.has("snap_step")) {
|
|
|
|
Vector2 s = state["snap_step"];
|
2017-01-04 05:16:14 +01:00
|
|
|
region_editor->sb_step_x->set_value(s.x);
|
|
|
|
region_editor->sb_step_y->set_value(s.y);
|
2016-01-10 23:02:32 +01:00
|
|
|
region_editor->snap_step = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.has("snap_offset")) {
|
|
|
|
Vector2 ofs = state["snap_offset"];
|
2017-01-04 05:16:14 +01:00
|
|
|
region_editor->sb_off_x->set_value(ofs.x);
|
|
|
|
region_editor->sb_off_y->set_value(ofs.y);
|
2016-01-10 23:02:32 +01:00
|
|
|
region_editor->snap_offset = ofs;
|
|
|
|
}
|
|
|
|
|
2016-06-14 04:54:58 +02:00
|
|
|
if (state.has("snap_separation")) {
|
|
|
|
Vector2 sep = state["snap_separation"];
|
2017-01-04 05:16:14 +01:00
|
|
|
region_editor->sb_sep_x->set_value(sep.x);
|
|
|
|
region_editor->sb_sep_y->set_value(sep.y);
|
2016-06-14 04:54:58 +02:00
|
|
|
region_editor->snap_separation = sep;
|
2016-01-10 23:02:32 +01:00
|
|
|
}
|
|
|
|
|
2016-06-14 04:54:58 +02:00
|
|
|
if (state.has("snap_mode")) {
|
|
|
|
region_editor->_set_snap_mode(state["snap_mode"]);
|
2016-01-10 23:02:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
TextureRegionEditorPlugin::TextureRegionEditorPlugin(EditorNode *p_node) {
|
2015-09-28 05:06:06 +02:00
|
|
|
editor = p_node;
|
2017-03-05 16:44:50 +01:00
|
|
|
region_editor = memnew(TextureRegionEditor(p_node));
|
2016-06-20 03:16:41 +02:00
|
|
|
|
|
|
|
region_button = p_node->add_bottom_panel_item(TTR("Texture Region"), region_editor);
|
|
|
|
region_button->set_tooltip(TTR("Texture Region Editor"));
|
2015-09-28 05:06:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
region_editor->set_custom_minimum_size(Size2(0, 200));
|
2015-09-28 05:06:06 +02:00
|
|
|
region_editor->hide();
|
2016-06-20 03:16:41 +02:00
|
|
|
region_button->hide();
|
2015-09-28 05:06:06 +02:00
|
|
|
}
|