summaryrefslogtreecommitdiff
path: root/editor/plugins/control_editor_plugin.h
blob: 14886e77a419a8bd2a09ec848371e7a53f565d4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*************************************************************************/
/*  control_editor_plugin.h                                              */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* 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.                */
/*************************************************************************/

#ifndef CONTROL_EDITOR_PLUGIN_H
#define CONTROL_EDITOR_PLUGIN_H

#include "editor/editor_inspector.h"
#include "editor/editor_plugin.h"
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
#include "scene/gui/check_box.h"
#include "scene/gui/control.h"
#include "scene/gui/label.h"
#include "scene/gui/margin_container.h"
#include "scene/gui/option_button.h"
#include "scene/gui/panel_container.h"
#include "scene/gui/popup.h"
#include "scene/gui/separator.h"
#include "scene/gui/texture_rect.h"

class EditorUndoRedoManager;
class GridContainer;

// Inspector controls.
class ControlPositioningWarning : public MarginContainer {
	GDCLASS(ControlPositioningWarning, MarginContainer);

	Control *control_node = nullptr;

	PanelContainer *bg_panel = nullptr;
	GridContainer *grid = nullptr;
	TextureRect *title_icon = nullptr;
	TextureRect *hint_icon = nullptr;
	Label *title_label = nullptr;
	Label *hint_label = nullptr;
	Control *hint_filler_left = nullptr;
	Control *hint_filler_right = nullptr;

	void _update_warning();
	void _update_toggler();
	virtual void gui_input(const Ref<InputEvent> &p_event) override;

protected:
	void _notification(int p_notification);

public:
	void set_control(Control *p_node);

	ControlPositioningWarning();
};

class EditorPropertyAnchorsPreset : public EditorProperty {
	GDCLASS(EditorPropertyAnchorsPreset, EditorProperty);
	OptionButton *options = nullptr;

	void _option_selected(int p_which);

protected:
	virtual void _set_read_only(bool p_read_only) override;

public:
	void setup(const Vector<String> &p_options);
	virtual void update_property() override;
	EditorPropertyAnchorsPreset();
};

class EditorPropertySizeFlags : public EditorProperty {
	GDCLASS(EditorPropertySizeFlags, EditorProperty);

	enum FlagPreset {
		SIZE_FLAGS_PRESET_FILL,
		SIZE_FLAGS_PRESET_SHRINK_BEGIN,
		SIZE_FLAGS_PRESET_SHRINK_CENTER,
		SIZE_FLAGS_PRESET_SHRINK_END,
		SIZE_FLAGS_PRESET_CUSTOM,
	};

	OptionButton *flag_presets = nullptr;
	CheckBox *flag_expand = nullptr;
	VBoxContainer *flag_options = nullptr;
	Vector<CheckBox *> flag_checks;

	bool vertical = false;

	bool keep_selected_preset = false;

	void _preset_selected(int p_which);
	void _expand_toggled();
	void _flag_toggled();

protected:
	virtual void _set_read_only(bool p_read_only) override;

public:
	void setup(const Vector<String> &p_options, bool p_vertical);
	virtual void update_property() override;
	EditorPropertySizeFlags();
};

class EditorInspectorPluginControl : public EditorInspectorPlugin {
	GDCLASS(EditorInspectorPluginControl, EditorInspectorPlugin);

public:
	virtual bool can_handle(Object *p_object) override;
	virtual void parse_group(Object *p_object, const String &p_group) override;
	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 uint32_t p_usage, const bool p_wide = false) override;
};

// Toolbar controls.
class ControlEditorPopupButton : public Button {
	GDCLASS(ControlEditorPopupButton, Button);

	Ref<Texture2D> arrow_icon;

	PopupPanel *popup_panel = nullptr;
	VBoxContainer *popup_vbox = nullptr;

	void _popup_visibility_changed(bool p_visible);

protected:
	void _notification(int p_what);

public:
	virtual Size2 get_minimum_size() const override;
	virtual void toggled(bool p_pressed) override;

	VBoxContainer *get_popup_hbox() const { return popup_vbox; }

	ControlEditorPopupButton();
};

class ControlEditorPresetPicker : public MarginContainer {
	GDCLASS(ControlEditorPresetPicker, MarginContainer);

	virtual void _preset_button_pressed(const int p_preset) {}

protected:
	static constexpr int grid_separation = 0;
	HashMap<int, Button *> preset_buttons;

	void _add_row_button(HBoxContainer *p_row, const int p_preset, const String &p_name);
	void _add_separator(BoxContainer *p_box, Separator *p_separator);

public:
	ControlEditorPresetPicker() {}
};

class AnchorPresetPicker : public ControlEditorPresetPicker {
	GDCLASS(AnchorPresetPicker, ControlEditorPresetPicker);

	virtual void _preset_button_pressed(const int p_preset) override;

protected:
	void _notification(int p_notification);
	static void _bind_methods();

public:
	AnchorPresetPicker();
};

class SizeFlagPresetPicker : public ControlEditorPresetPicker {
	GDCLASS(SizeFlagPresetPicker, ControlEditorPresetPicker);

	CheckBox *expand_button = nullptr;

	bool vertical = false;

	virtual void _preset_button_pressed(const int p_preset) override;

protected:
	void _notification(int p_notification);
	static void _bind_methods();

public:
	void set_allowed_flags(Vector<SizeFlags> &p_flags);

	SizeFlagPresetPicker(bool p_vertical);
};

class ControlEditorToolbar : public HBoxContainer {
	GDCLASS(ControlEditorToolbar, HBoxContainer);

	Ref<EditorUndoRedoManager> undo_redo;
	EditorSelection *editor_selection = nullptr;

	ControlEditorPopupButton *anchors_button = nullptr;
	ControlEditorPopupButton *containers_button = nullptr;
	Button *anchor_mode_button = nullptr;

	SizeFlagPresetPicker *container_h_picker = nullptr;
	SizeFlagPresetPicker *container_v_picker = nullptr;

	bool anchors_mode = false;

	void _anchors_preset_selected(int p_preset);
	void _anchors_to_current_ratio();
	void _anchor_mode_toggled(bool p_status);
	void _container_flags_selected(int p_flags, bool p_vertical);

	Vector2 _position_to_anchor(const Control *p_control, Vector2 position);
	bool _is_node_locked(const Node *p_node);
	List<Control *> _get_edited_controls();
	void _selection_changed();

protected:
	void _notification(int p_notification);

	static ControlEditorToolbar *singleton;

public:
	bool is_anchors_mode_enabled() { return anchors_mode; };

	static ControlEditorToolbar *get_singleton() { return singleton; }

	ControlEditorToolbar();
};

// Editor plugin.
class ControlEditorPlugin : public EditorPlugin {
	GDCLASS(ControlEditorPlugin, EditorPlugin);

	ControlEditorToolbar *toolbar = nullptr;

public:
	virtual String get_name() const override { return "Control"; }

	ControlEditorPlugin();
};

#endif // CONTROL_EDITOR_PLUGIN_H