2
0
Эх сурвалжийг харах

Relax grid parameter constraints in texture region editor

Haoyu Qiu 1 жил өмнө
parent
commit
151d7b10e0

+ 51 - 19
editor/plugins/texture_region_editor_plugin.cpp

@@ -37,13 +37,16 @@
 #include "editor/editor_string_names.h"
 #include "editor/editor_undo_redo_manager.h"
 #include "editor/themes/editor_scale.h"
-#include "scene/gui/check_box.h"
+#include "scene/2d/sprite_2d.h"
+#include "scene/3d/sprite_3d.h"
+#include "scene/gui/nine_patch_rect.h"
 #include "scene/gui/option_button.h"
 #include "scene/gui/panel_container.h"
 #include "scene/gui/separator.h"
 #include "scene/gui/spin_box.h"
 #include "scene/gui/view_panner.h"
 #include "scene/resources/atlas_texture.h"
+#include "scene/resources/style_box_texture.h"
 
 Transform2D TextureRegionEditor::_get_offset_transform() const {
 	Transform2D mtx;
@@ -94,7 +97,7 @@ void TextureRegionEditor::_texture_overlay_draw() {
 					last_cell = cell;
 				}
 			} else {
-				for (int i = 0; i < s.width; i++) {
+				for (int i = 0; i < s.width + snap_separation.x; 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;
@@ -120,7 +123,7 @@ void TextureRegionEditor::_texture_overlay_draw() {
 					last_cell = cell;
 				}
 			} else {
-				for (int i = 0; i < s.height; i++) {
+				for (int i = 0; i < s.height + snap_separation.y; 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;
@@ -281,6 +284,17 @@ void TextureRegionEditor::_draw_margin_line(Vector2 p_from, Vector2 p_to) {
 	}
 }
 
+void TextureRegionEditor::_set_grid_parameters_clamping(bool p_enabled) {
+	sb_off_x->set_allow_lesser(!p_enabled);
+	sb_off_x->set_allow_greater(!p_enabled);
+	sb_off_y->set_allow_lesser(!p_enabled);
+	sb_off_y->set_allow_greater(!p_enabled);
+	sb_step_x->set_allow_greater(!p_enabled);
+	sb_step_y->set_allow_greater(!p_enabled);
+	sb_sep_x->set_allow_greater(!p_enabled);
+	sb_sep_y->set_allow_greater(!p_enabled);
+}
+
 void TextureRegionEditor::_texture_overlay_input(const Ref<InputEvent> &p_input) {
 	if (panner->gui_input(p_input)) {
 		return;
@@ -842,6 +856,7 @@ void TextureRegionEditor::_notification(int p_what) {
 			}
 
 			if (!is_visible()) {
+				EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_offset", snap_offset);
 				EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_step", snap_step);
 				EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_separation", snap_separation);
 				EditorSettings::get_singleton()->set_project_metadata("texture_region_editor", "snap_mode", snap_mode);
@@ -973,6 +988,7 @@ void TextureRegionEditor::_texture_changed() {
 void TextureRegionEditor::_edit_region() {
 	const Ref<Texture2D> object_texture = _get_edited_object_texture();
 	if (object_texture.is_null()) {
+		_set_grid_parameters_clamping(false);
 		_zoom_reset();
 		hscroll->hide();
 		vscroll->hide();
@@ -1057,6 +1073,26 @@ void TextureRegionEditor::_edit_region() {
 		}
 	}
 
+	// Avoiding clamping with mismatched min/max.
+	_set_grid_parameters_clamping(false);
+	const Size2 tex_size = object_texture->get_size();
+	sb_off_x->set_min(-tex_size.x);
+	sb_off_x->set_max(tex_size.x);
+	sb_off_y->set_min(-tex_size.y);
+	sb_off_y->set_max(tex_size.y);
+	sb_step_x->set_max(tex_size.x);
+	sb_step_y->set_max(tex_size.y);
+	sb_sep_x->set_max(tex_size.x);
+	sb_sep_y->set_max(tex_size.y);
+
+	_set_grid_parameters_clamping(true);
+	sb_off_x->set_value(snap_offset.x);
+	sb_off_y->set_value(snap_offset.y);
+	sb_step_x->set_value(snap_step.x);
+	sb_step_y->set_value(snap_step.y);
+	sb_sep_x->set_value(snap_separation.x);
+	sb_sep_y->set_value(snap_separation.y);
+
 	_update_rect();
 	texture_preview->queue_redraw();
 	texture_overlay->queue_redraw();
@@ -1080,6 +1116,7 @@ TextureRegionEditor::TextureRegionEditor() {
 	set_ok_button_text(TTR("Close"));
 
 	// A power-of-two value works better as a default grid size.
+	snap_offset = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_offset", Vector2());
 	snap_step = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_step", Vector2(8, 8));
 	snap_separation = EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_separation", Vector2());
 	snap_mode = (SnapMode)(int)EditorSettings::get_singleton()->get_project_metadata("texture_region_editor", "snap_mode", SNAP_NONE);
@@ -1110,19 +1147,13 @@ TextureRegionEditor::TextureRegionEditor() {
 	hb_grid->add_child(memnew(Label(TTR("Offset:"))));
 
 	sb_off_x = memnew(SpinBox);
-	sb_off_x->set_min(-256);
-	sb_off_x->set_max(256);
 	sb_off_x->set_step(1);
-	sb_off_x->set_value(snap_offset.x);
 	sb_off_x->set_suffix("px");
 	sb_off_x->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_off_x));
 	hb_grid->add_child(sb_off_x);
 
 	sb_off_y = memnew(SpinBox);
-	sb_off_y->set_min(-256);
-	sb_off_y->set_max(256);
 	sb_off_y->set_step(1);
-	sb_off_y->set_value(snap_offset.y);
 	sb_off_y->set_suffix("px");
 	sb_off_y->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_off_y));
 	hb_grid->add_child(sb_off_y);
@@ -1131,19 +1162,15 @@ TextureRegionEditor::TextureRegionEditor() {
 	hb_grid->add_child(memnew(Label(TTR("Step:"))));
 
 	sb_step_x = memnew(SpinBox);
-	sb_step_x->set_min(-256);
-	sb_step_x->set_max(256);
+	sb_step_x->set_min(0);
 	sb_step_x->set_step(1);
-	sb_step_x->set_value(snap_step.x);
 	sb_step_x->set_suffix("px");
 	sb_step_x->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_step_x));
 	hb_grid->add_child(sb_step_x);
 
 	sb_step_y = memnew(SpinBox);
-	sb_step_y->set_min(-256);
-	sb_step_y->set_max(256);
+	sb_step_y->set_min(0);
 	sb_step_y->set_step(1);
-	sb_step_y->set_value(snap_step.y);
 	sb_step_y->set_suffix("px");
 	sb_step_y->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_step_y));
 	hb_grid->add_child(sb_step_y);
@@ -1153,24 +1180,29 @@ TextureRegionEditor::TextureRegionEditor() {
 
 	sb_sep_x = memnew(SpinBox);
 	sb_sep_x->set_min(0);
-	sb_sep_x->set_max(256);
 	sb_sep_x->set_step(1);
-	sb_sep_x->set_value(snap_separation.x);
 	sb_sep_x->set_suffix("px");
 	sb_sep_x->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_sep_x));
 	hb_grid->add_child(sb_sep_x);
 
 	sb_sep_y = memnew(SpinBox);
 	sb_sep_y->set_min(0);
-	sb_sep_y->set_max(256);
 	sb_sep_y->set_step(1);
-	sb_sep_y->set_value(snap_separation.y);
 	sb_sep_y->set_suffix("px");
 	sb_sep_y->connect("value_changed", callable_mp(this, &TextureRegionEditor::_set_snap_sep_y));
 	hb_grid->add_child(sb_sep_y);
 
 	hb_grid->hide();
 
+	// Restore grid snap parameters.
+	_set_grid_parameters_clamping(false);
+	sb_off_x->set_value(snap_offset.x);
+	sb_off_y->set_value(snap_offset.y);
+	sb_step_x->set_value(snap_step.x);
+	sb_step_y->set_value(snap_step.y);
+	sb_sep_x->set_value(snap_separation.x);
+	sb_sep_y->set_value(snap_separation.y);
+
 	// Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
 	draw_zoom = MAX(1.0f, EDSCALE);
 	max_draw_zoom = 128.0f * MAX(1.0f, EDSCALE);

+ 6 - 5
editor/plugins/texture_region_editor_plugin.h

@@ -31,18 +31,17 @@
 #ifndef TEXTURE_REGION_EDITOR_PLUGIN_H
 #define TEXTURE_REGION_EDITOR_PLUGIN_H
 
-#include "canvas_item_editor_plugin.h"
 #include "editor/editor_inspector.h"
 #include "editor/editor_plugin.h"
-#include "scene/2d/sprite_2d.h"
-#include "scene/3d/sprite_3d.h"
 #include "scene/gui/dialogs.h"
-#include "scene/gui/nine_patch_rect.h"
-#include "scene/resources/style_box_texture.h"
 
 class AtlasTexture;
+class NinePatchRect;
 class OptionButton;
 class PanelContainer;
+class Sprite2D;
+class Sprite3D;
+class StyleBoxTexture;
 class ViewPanner;
 
 class TextureRegionEditor : public AcceptDialog {
@@ -138,6 +137,8 @@ class TextureRegionEditor : public AcceptDialog {
 
 	void _draw_margin_line(Vector2 p_from, Vector2 p_to);
 
+	void _set_grid_parameters_clamping(bool p_enabled);
+
 protected:
 	void _notification(int p_what);
 	static void _bind_methods();