Skip to content

Commit 151d7b1

Browse files
committed
Relax grid parameter constraints in texture region editor
1 parent 7d151c8 commit 151d7b1

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

editor/plugins/texture_region_editor_plugin.cpp

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@
3737
#include "editor/editor_string_names.h"
3838
#include "editor/editor_undo_redo_manager.h"
3939
#include "editor/themes/editor_scale.h"
40-
#include "scene/gui/check_box.h"
40+
#include "scene/2d/sprite_2d.h"
41+
#include "scene/3d/sprite_3d.h"
42+
#include "scene/gui/nine_patch_rect.h"
4143
#include "scene/gui/option_button.h"
4244
#include "scene/gui/panel_container.h"
4345
#include "scene/gui/separator.h"
4446
#include "scene/gui/spin_box.h"
4547
#include "scene/gui/view_panner.h"
4648
#include "scene/resources/atlas_texture.h"
49+
#include "scene/resources/style_box_texture.h"
4750

4851
Transform2D TextureRegionEditor::_get_offset_transform() const {
4952
Transform2D mtx;
@@ -94,7 +97,7 @@ void TextureRegionEditor::_texture_overlay_draw() {
9497
last_cell = cell;
9598
}
9699
} else {
97-
for (int i = 0; i < s.width; i++) {
100+
for (int i = 0; i < s.width + snap_separation.x; i++) {
98101
int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / (snap_step.x + snap_separation.x)));
99102
if (i == 0) {
100103
last_cell = cell;
@@ -120,7 +123,7 @@ void TextureRegionEditor::_texture_overlay_draw() {
120123
last_cell = cell;
121124
}
122125
} else {
123-
for (int i = 0; i < s.height; i++) {
126+
for (int i = 0; i < s.height + snap_separation.y; i++) {
124127
int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / (snap_step.y + snap_separation.y)));
125128
if (i == 0) {
126129
last_cell = cell;
@@ -281,6 +284,17 @@ void TextureRegionEditor::_draw_margin_line(Vector2 p_from, Vector2 p_to) {
281284
}
282285
}
283286

287+
void TextureRegionEditor::_set_grid_parameters_clamping(bool p_enabled) {
288+
sb_off_x->set_allow_lesser(!p_enabled);
289+
sb_off_x->set_allow_greater(!p_enabled);
290+
sb_off_y->set_allow_lesser(!p_enabled);
291+
sb_off_y->set_allow_greater(!p_enabled);
292+
sb_step_x->set_allow_greater(!p_enabled);
293+
sb_step_y->set_allow_greater(!p_enabled);
294+
sb_sep_x->set_allow_greater(!p_enabled);
295+
sb_sep_y->set_allow_greater(!p_enabled);
296+
}
297+
284298
void TextureRegionEditor::_texture_overlay_input(const Ref<InputEvent> &p_input) {
285299
if (panner->gui_input(p_input)) {
286300
return;
@@ -842,6 +856,7 @@ void TextureRegionEditor::_notification(int p_what) {
842856
}
843857

844858
if (!is_visible()) {
859+
EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_offset", snap_offset);
845860
EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_step", snap_step);
846861
EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_separation", snap_separation);
847862
EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_mode", snap_mode);
@@ -973,6 +988,7 @@ void TextureRegionEditor::_texture_changed() {
973988
void TextureRegionEditor::_edit_region() {
974989
const Ref<Texture2D> object_texture = _get_edited_object_texture();
975990
if (object_texture.is_null()) {
991+
_set_grid_parameters_clamping(false);
976992
_zoom_reset();
977993
hscroll->hide();
978994
vscroll->hide();
@@ -1057,6 +1073,26 @@ void TextureRegionEditor::_edit_region() {
10571073
}
10581074
}
10591075

1076+
// Avoiding clamping with mismatched min/max.
1077+
_set_grid_parameters_clamping(false);
1078+
const Size2 tex_size = object_texture->get_size();
1079+
sb_off_x->set_min(-tex_size.x);
1080+
sb_off_x->set_max(tex_size.x);
1081+
sb_off_y->set_min(-tex_size.y);
1082+
sb_off_y->set_max(tex_size.y);
1083+
sb_step_x->set_max(tex_size.x);
1084+
sb_step_y->set_max(tex_size.y);
1085+
sb_sep_x->set_max(tex_size.x);
1086+
sb_sep_y->set_max(tex_size.y);
1087+
1088+
_set_grid_parameters_clamping(true);
1089+
sb_off_x->set_value(snap_offset.x);
1090+
sb_off_y->set_value(snap_offset.y);
1091+
sb_step_x->set_value(snap_step.x);
1092+
sb_step_y->set_value(snap_step.y);
1093+
sb_sep_x->set_value(snap_separation.x);
1094+
sb_sep_y->set_value(snap_separation.y);
1095+
10601096
_update_rect();
10611097
texture_preview->queue_redraw();
10621098
texture_overlay->queue_redraw();
@@ -1080,6 +1116,7 @@ TextureRegionEditor::TextureRegionEditor() {
10801116
set_ok_button_text(TTR("Close"));
10811117

10821118
// A power-of-two value works better as a default grid size.
1119+
snap_offset = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_offset", Vector2());
10831120
snap_step = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_step", Vector2(8, 8));
10841121
snap_separation = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_separation", Vector2());
10851122
snap_mode = (SnapMode)(int)EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_mode", SNAP_NONE);
@@ -1110,19 +1147,13 @@ TextureRegionEditor::TextureRegionEditor() {
11101147
hb_grid->add_child(memnew(Label(TTR("Offset:"))));
11111148

11121149
sb_off_x = memnew(SpinBox);
1113-
sb_off_x->set_min(-256);
1114-
sb_off_x->set_max(256);
11151150
sb_off_x->set_step(1);
1116-
sb_off_x->set_value(snap_offset.x);
11171151
sb_off_x->set_suffix("px");
11181152
sb_off_x->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_off_x));
11191153
hb_grid->add_child(sb_off_x);
11201154

11211155
sb_off_y = memnew(SpinBox);
1122-
sb_off_y->set_min(-256);
1123-
sb_off_y->set_max(256);
11241156
sb_off_y->set_step(1);
1125-
sb_off_y->set_value(snap_offset.y);
11261157
sb_off_y->set_suffix("px");
11271158
sb_off_y->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_off_y));
11281159
hb_grid->add_child(sb_off_y);
@@ -1131,19 +1162,15 @@ TextureRegionEditor::TextureRegionEditor() {
11311162
hb_grid->add_child(memnew(Label(TTR("Step:"))));
11321163

11331164
sb_step_x = memnew(SpinBox);
1134-
sb_step_x->set_min(-256);
1135-
sb_step_x->set_max(256);
1165+
sb_step_x->set_min(0);
11361166
sb_step_x->set_step(1);
1137-
sb_step_x->set_value(snap_step.x);
11381167
sb_step_x->set_suffix("px");
11391168
sb_step_x->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_step_x));
11401169
hb_grid->add_child(sb_step_x);
11411170

11421171
sb_step_y = memnew(SpinBox);
1143-
sb_step_y->set_min(-256);
1144-
sb_step_y->set_max(256);
1172+
sb_step_y->set_min(0);
11451173
sb_step_y->set_step(1);
1146-
sb_step_y->set_value(snap_step.y);
11471174
sb_step_y->set_suffix("px");
11481175
sb_step_y->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_step_y));
11491176
hb_grid->add_child(sb_step_y);
@@ -1153,24 +1180,29 @@ TextureRegionEditor::TextureRegionEditor() {
11531180

11541181
sb_sep_x = memnew(SpinBox);
11551182
sb_sep_x->set_min(0);
1156-
sb_sep_x->set_max(256);
11571183
sb_sep_x->set_step(1);
1158-
sb_sep_x->set_value(snap_separation.x);
11591184
sb_sep_x->set_suffix("px");
11601185
sb_sep_x->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_sep_x));
11611186
hb_grid->add_child(sb_sep_x);
11621187

11631188
sb_sep_y = memnew(SpinBox);
11641189
sb_sep_y->set_min(0);
1165-
sb_sep_y->set_max(256);
11661190
sb_sep_y->set_step(1);
1167-
sb_sep_y->set_value(snap_separation.y);
11681191
sb_sep_y->set_suffix("px");
11691192
sb_sep_y->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_sep_y));
11701193
hb_grid->add_child(sb_sep_y);
11711194

11721195
hb_grid->hide();
11731196

1197+
// Restore grid snap parameters.
1198+
_set_grid_parameters_clamping(false);
1199+
sb_off_x->set_value(snap_offset.x);
1200+
sb_off_y->set_value(snap_offset.y);
1201+
sb_step_x->set_value(snap_step.x);
1202+
sb_step_y->set_value(snap_step.y);
1203+
sb_sep_x->set_value(snap_separation.x);
1204+
sb_sep_y->set_value(snap_separation.y);
1205+
11741206
// Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
11751207
draw_zoom = MAX(1.0f, EDSCALE);
11761208
max_draw_zoom = 128.0f * MAX(1.0f, EDSCALE);

editor/plugins/texture_region_editor_plugin.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,17 @@
3131
#ifndef TEXTURE_REGION_EDITOR_PLUGIN_H
3232
#define TEXTURE_REGION_EDITOR_PLUGIN_H
3333

34-
#include "canvas_item_editor_plugin.h"
3534
#include "editor/editor_inspector.h"
3635
#include "editor/editor_plugin.h"
37-
#include "scene/2d/sprite_2d.h"
38-
#include "scene/3d/sprite_3d.h"
3936
#include "scene/gui/dialogs.h"
40-
#include "scene/gui/nine_patch_rect.h"
41-
#include "scene/resources/style_box_texture.h"
4237

4338
class AtlasTexture;
39+
class NinePatchRect;
4440
class OptionButton;
4541
class PanelContainer;
42+
class Sprite2D;
43+
class Sprite3D;
44+
class StyleBoxTexture;
4645
class ViewPanner;
4746

4847
class TextureRegionEditor : public AcceptDialog {
@@ -138,6 +137,8 @@ class TextureRegionEditor : public AcceptDialog {
138137

139138
void _draw_margin_line(Vector2 p_from, Vector2 p_to);
140139

140+
void _set_grid_parameters_clamping(bool p_enabled);
141+
141142
protected:
142143
void _notification(int p_what);
143144
static void _bind_methods();

0 commit comments

Comments
 (0)