Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/texture/texture_region_editor_plugin.h
9903 views
1
/**************************************************************************/
2
/* texture_region_editor_plugin.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "editor/inspector/editor_inspector.h"
34
#include "editor/plugins/editor_plugin.h"
35
#include "scene/gui/dialogs.h"
36
37
class AtlasTexture;
38
class NinePatchRect;
39
class OptionButton;
40
class PanelContainer;
41
class Sprite2D;
42
class Sprite3D;
43
class StyleBoxTexture;
44
class ViewPanner;
45
46
class TextureRegionEditor : public AcceptDialog {
47
GDCLASS(TextureRegionEditor, AcceptDialog);
48
49
enum SnapMode {
50
SNAP_NONE,
51
SNAP_PIXEL,
52
SNAP_GRID,
53
SNAP_AUTOSLICE
54
};
55
56
friend class TextureRegionEditorPlugin;
57
OptionButton *snap_mode_button = nullptr;
58
Button *zoom_in = nullptr;
59
Button *zoom_reset = nullptr;
60
Button *zoom_out = nullptr;
61
HBoxContainer *hb_grid = nullptr; //For showing/hiding the grid controls when changing the SnapMode
62
SpinBox *sb_step_y = nullptr;
63
SpinBox *sb_step_x = nullptr;
64
SpinBox *sb_off_y = nullptr;
65
SpinBox *sb_off_x = nullptr;
66
SpinBox *sb_sep_y = nullptr;
67
SpinBox *sb_sep_x = nullptr;
68
69
PanelContainer *texture_preview = nullptr;
70
Panel *texture_overlay = nullptr;
71
72
VScrollBar *vscroll = nullptr;
73
HScrollBar *hscroll = nullptr;
74
75
Vector2 draw_ofs;
76
float draw_zoom = 1.0;
77
float min_draw_zoom = 1.0;
78
float max_draw_zoom = 1.0;
79
bool updating_scroll = false;
80
81
SnapMode snap_mode = SNAP_NONE;
82
Vector2 snap_offset;
83
Vector2 snap_step;
84
Vector2 snap_separation;
85
86
Sprite2D *node_sprite_2d = nullptr;
87
Sprite3D *node_sprite_3d = nullptr;
88
NinePatchRect *node_ninepatch = nullptr;
89
Ref<StyleBoxTexture> res_stylebox;
90
Ref<AtlasTexture> res_atlas_texture;
91
92
Rect2 rect;
93
Rect2 rect_prev;
94
float prev_margin = 0.0f;
95
int edited_margin = -1;
96
HashMap<RID, List<Rect2>> cache_map;
97
List<Rect2> autoslice_cache;
98
bool autoslice_is_dirty = true;
99
100
bool drag = false;
101
bool creating = false;
102
Vector2 drag_from;
103
int drag_index = -1;
104
bool request_center = false;
105
106
Ref<ViewPanner> panner;
107
void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
108
void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
109
void _scroll_changed(float);
110
Transform2D _get_offset_transform() const;
111
112
void _set_snap_mode(int p_mode);
113
void _set_snap_off_x(float p_val);
114
void _set_snap_off_y(float p_val);
115
void _set_snap_step_x(float p_val);
116
void _set_snap_step_y(float p_val);
117
void _set_snap_sep_x(float p_val);
118
void _set_snap_sep_y(float p_val);
119
120
void _zoom_on_position(float p_zoom, Point2 p_position = Point2());
121
void _zoom_in();
122
void _zoom_reset();
123
void _zoom_out();
124
125
void _apply_rect(const Rect2 &p_rect);
126
void _update_rect();
127
void _update_autoslice();
128
129
Ref<Texture2D> _get_edited_object_texture() const;
130
Rect2 _get_edited_object_region() const;
131
void _texture_changed();
132
void _node_removed(Node *p_node);
133
134
void _edit_region();
135
void _clear_edited_object();
136
137
void _draw_margin_line(Vector2 p_from, Vector2 p_to);
138
139
void _set_grid_parameters_clamping(bool p_enabled);
140
141
protected:
142
void _notification(int p_what);
143
virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
144
static void _bind_methods();
145
146
void _texture_preview_draw();
147
void _texture_overlay_draw();
148
void _texture_overlay_input(const Ref<InputEvent> &p_input);
149
150
Vector2 snap_point(Vector2 p_target) const;
151
152
public:
153
void edit(Object *p_obj);
154
155
TextureRegionEditor();
156
};
157
158
//
159
160
class EditorInspectorPluginTextureRegion : public EditorInspectorPlugin {
161
GDCLASS(EditorInspectorPluginTextureRegion, EditorInspectorPlugin);
162
163
TextureRegionEditor *texture_region_editor = nullptr;
164
165
void _region_edit(Object *p_object);
166
167
public:
168
virtual bool can_handle(Object *p_object) override;
169
virtual bool parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) override;
170
171
EditorInspectorPluginTextureRegion();
172
};
173
174
class TextureRegionEditorPlugin : public EditorPlugin {
175
GDCLASS(TextureRegionEditorPlugin, EditorPlugin);
176
177
public:
178
virtual String get_plugin_name() const override { return "TextureRegion"; }
179
180
TextureRegionEditorPlugin();
181
};
182
183