Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/gui/control_editor_plugin.h
9903 views
1
/**************************************************************************/
2
/* control_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/box_container.h"
36
#include "scene/gui/button.h"
37
#include "scene/gui/margin_container.h"
38
39
class CheckBox;
40
class CheckButton;
41
class EditorSelection;
42
class GridContainer;
43
class Label;
44
class OptionButton;
45
class PanelContainer;
46
class PopupPanel;
47
class Separator;
48
class TextureRect;
49
50
// Inspector controls.
51
class ControlPositioningWarning : public MarginContainer {
52
GDCLASS(ControlPositioningWarning, MarginContainer);
53
54
Control *control_node = nullptr;
55
56
PanelContainer *bg_panel = nullptr;
57
GridContainer *grid = nullptr;
58
TextureRect *title_icon = nullptr;
59
TextureRect *hint_icon = nullptr;
60
Label *title_label = nullptr;
61
Label *hint_label = nullptr;
62
Control *hint_filler_left = nullptr;
63
Control *hint_filler_right = nullptr;
64
65
void _update_warning();
66
void _update_toggler();
67
virtual void gui_input(const Ref<InputEvent> &p_event) override;
68
69
protected:
70
void _notification(int p_notification);
71
72
public:
73
void set_control(Control *p_node);
74
75
ControlPositioningWarning();
76
};
77
78
class EditorPropertyAnchorsPreset : public EditorProperty {
79
GDCLASS(EditorPropertyAnchorsPreset, EditorProperty);
80
OptionButton *options = nullptr;
81
82
void _option_selected(int p_which);
83
84
protected:
85
virtual void _set_read_only(bool p_read_only) override;
86
void _notification(int p_what);
87
88
public:
89
void setup(const Vector<String> &p_options);
90
virtual void update_property() override;
91
EditorPropertyAnchorsPreset();
92
};
93
94
class EditorPropertySizeFlags : public EditorProperty {
95
GDCLASS(EditorPropertySizeFlags, EditorProperty);
96
97
enum FlagPreset {
98
SIZE_FLAGS_PRESET_FILL,
99
SIZE_FLAGS_PRESET_SHRINK_BEGIN,
100
SIZE_FLAGS_PRESET_SHRINK_CENTER,
101
SIZE_FLAGS_PRESET_SHRINK_END,
102
SIZE_FLAGS_PRESET_CUSTOM,
103
};
104
105
OptionButton *flag_presets = nullptr;
106
CheckBox *flag_expand = nullptr;
107
VBoxContainer *flag_options = nullptr;
108
Vector<CheckBox *> flag_checks;
109
110
bool vertical = false;
111
112
bool keep_selected_preset = false;
113
114
void _preset_selected(int p_which);
115
void _expand_toggled();
116
void _flag_toggled();
117
118
protected:
119
virtual void _set_read_only(bool p_read_only) override;
120
121
public:
122
void setup(const Vector<String> &p_options, bool p_vertical);
123
virtual void update_property() override;
124
EditorPropertySizeFlags();
125
};
126
127
class EditorInspectorPluginControl : public EditorInspectorPlugin {
128
GDCLASS(EditorInspectorPluginControl, EditorInspectorPlugin);
129
130
bool inside_control_category = false;
131
132
public:
133
virtual bool can_handle(Object *p_object) override;
134
virtual void parse_category(Object *p_object, const String &p_category) override;
135
virtual void parse_group(Object *p_object, const String &p_group) override;
136
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 = false) override;
137
};
138
139
// Toolbar controls.
140
class ControlEditorPopupButton : public Button {
141
GDCLASS(ControlEditorPopupButton, Button);
142
143
Ref<Texture2D> arrow_icon;
144
145
PopupPanel *popup_panel = nullptr;
146
VBoxContainer *popup_vbox = nullptr;
147
148
void _popup_visibility_changed(bool p_visible);
149
150
protected:
151
void _notification(int p_what);
152
153
public:
154
virtual Size2 get_minimum_size() const override;
155
virtual void toggled(bool p_pressed) override;
156
157
VBoxContainer *get_popup_hbox() const { return popup_vbox; }
158
159
ControlEditorPopupButton();
160
};
161
162
class ControlEditorPresetPicker : public MarginContainer {
163
GDCLASS(ControlEditorPresetPicker, MarginContainer);
164
165
virtual void _preset_button_pressed(const int p_preset) {}
166
167
protected:
168
static constexpr int grid_separation = 0;
169
HashMap<int, Button *> preset_buttons;
170
171
void _add_row_button(HBoxContainer *p_row, const int p_preset, const String &p_name);
172
void _add_separator(BoxContainer *p_box, Separator *p_separator);
173
};
174
175
class AnchorPresetPicker : public ControlEditorPresetPicker {
176
GDCLASS(AnchorPresetPicker, ControlEditorPresetPicker);
177
178
virtual void _preset_button_pressed(const int p_preset) override;
179
180
protected:
181
void _notification(int p_notification);
182
static void _bind_methods();
183
184
public:
185
AnchorPresetPicker();
186
};
187
188
class SizeFlagPresetPicker : public ControlEditorPresetPicker {
189
GDCLASS(SizeFlagPresetPicker, ControlEditorPresetPicker);
190
191
CheckButton *expand_button = nullptr;
192
193
bool vertical = false;
194
195
virtual void _preset_button_pressed(const int p_preset) override;
196
void _expand_button_pressed();
197
198
protected:
199
void _notification(int p_notification);
200
static void _bind_methods();
201
202
public:
203
void set_allowed_flags(Vector<SizeFlags> &p_flags);
204
void set_expand_flag(bool p_expand);
205
206
SizeFlagPresetPicker(bool p_vertical);
207
};
208
209
class ControlEditorToolbar : public HBoxContainer {
210
GDCLASS(ControlEditorToolbar, HBoxContainer);
211
212
EditorSelection *editor_selection = nullptr;
213
214
ControlEditorPopupButton *anchors_button = nullptr;
215
ControlEditorPopupButton *containers_button = nullptr;
216
Button *anchor_mode_button = nullptr;
217
218
SizeFlagPresetPicker *container_h_picker = nullptr;
219
SizeFlagPresetPicker *container_v_picker = nullptr;
220
221
bool anchors_mode = false;
222
223
void _anchors_preset_selected(int p_preset);
224
void _anchors_to_current_ratio();
225
void _anchor_mode_toggled(bool p_status);
226
void _container_flags_selected(int p_flags, bool p_vertical);
227
void _expand_flag_toggled(bool p_expand, bool p_vertical);
228
229
Vector2 _position_to_anchor(const Control *p_control, Vector2 position);
230
bool _is_node_locked(const Node *p_node);
231
List<Control *> _get_edited_controls();
232
void _selection_changed();
233
234
protected:
235
void _notification(int p_notification);
236
237
static ControlEditorToolbar *singleton;
238
239
public:
240
bool is_anchors_mode_enabled() { return anchors_mode; }
241
242
static ControlEditorToolbar *get_singleton() { return singleton; }
243
244
ControlEditorToolbar();
245
};
246
247
// Editor plugin.
248
class ControlEditorPlugin : public EditorPlugin {
249
GDCLASS(ControlEditorPlugin, EditorPlugin);
250
251
ControlEditorToolbar *toolbar = nullptr;
252
253
public:
254
virtual String get_plugin_name() const override { return "Control"; }
255
256
ControlEditorPlugin();
257
};
258
259