Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/inspector/editor_properties_array_dict.h
21423 views
1
/**************************************************************************/
2
/* editor_properties_array_dict.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/translations/editor_locale_dialog.h"
35
36
class Button;
37
class EditorSpinSlider;
38
class EditorVariantTypePopupMenu;
39
class MarginContainer;
40
41
class EditorPropertyArrayObject : public RefCounted {
42
GDCLASS(EditorPropertyArrayObject, RefCounted);
43
44
Variant array;
45
46
protected:
47
bool _set(const StringName &p_name, const Variant &p_value);
48
bool _get(const StringName &p_name, Variant &r_ret) const;
49
50
public:
51
enum {
52
NOT_CHANGING_TYPE = -1,
53
};
54
55
void set_array(const Variant &p_array);
56
Variant get_array();
57
};
58
59
class EditorPropertyDictionaryObject : public RefCounted {
60
GDCLASS(EditorPropertyDictionaryObject, RefCounted);
61
62
Variant new_item_key;
63
Variant new_item_value;
64
Dictionary dict;
65
66
protected:
67
bool _set(const StringName &p_name, const Variant &p_value);
68
bool _get(const StringName &p_name, Variant &r_ret) const;
69
70
public:
71
enum {
72
NOT_CHANGING_TYPE = -3,
73
NEW_KEY_INDEX,
74
NEW_VALUE_INDEX,
75
};
76
77
bool get_by_property_name(const String &p_name, Variant &r_ret) const;
78
void set_dict(const Dictionary &p_dict);
79
Dictionary get_dict();
80
81
void set_new_item_key(const Variant &p_new_item);
82
Variant get_new_item_key();
83
84
void set_new_item_value(const Variant &p_new_item);
85
Variant get_new_item_value();
86
87
String get_label_for_index(int p_index);
88
String get_property_name_for_index(int p_index);
89
String get_key_name_for_index(int p_index);
90
};
91
92
class EditorPropertyArray : public EditorProperty {
93
GDCLASS(EditorPropertyArray, EditorProperty);
94
95
struct Slot {
96
Ref<EditorPropertyArrayObject> object;
97
HBoxContainer *container = nullptr;
98
int index = -1;
99
Variant::Type type = Variant::VARIANT_MAX;
100
bool as_id = false;
101
EditorProperty *prop = nullptr;
102
Button *reorder_button = nullptr;
103
Button *edit_button = nullptr;
104
Button *remove_button = nullptr;
105
106
void set_index(int p_idx) {
107
String prop_name = "indices/" + itos(p_idx);
108
prop->set_object_and_property(object.ptr(), prop_name);
109
prop->set_label(itos(p_idx));
110
index = p_idx;
111
}
112
};
113
114
EditorVariantTypePopupMenu *change_type = nullptr;
115
116
bool preview_value = false;
117
int page_length = 20;
118
int page_index = 0;
119
int changing_type_index = EditorPropertyArrayObject::NOT_CHANGING_TYPE;
120
Button *edit = nullptr;
121
PanelContainer *container = nullptr;
122
VBoxContainer *property_vbox = nullptr;
123
EditorSpinSlider *size_slider = nullptr;
124
Button *button_add_item = nullptr;
125
EditorPaginator *paginator = nullptr;
126
Variant::Type array_type;
127
Variant::Type subtype;
128
PropertyHint subtype_hint;
129
String subtype_hint_string;
130
LocalVector<Slot> slots;
131
132
Slot reorder_slot;
133
int reorder_to_index = -1;
134
float reorder_mouse_y_delta = 0.0f;
135
void initialize_array(Variant &p_array);
136
137
void _page_changed(int p_page);
138
139
void _reorder_button_gui_input(const Ref<InputEvent> &p_event);
140
void _reorder_button_down(int p_index);
141
void _reorder_button_up();
142
void _create_new_property_slot();
143
144
void _resource_selected(const String &p_path, Ref<Resource> p_resource);
145
146
Node *get_base_node();
147
148
protected:
149
Ref<EditorPropertyArrayObject> object;
150
151
bool updating = false;
152
bool dropping = false;
153
154
void _notification(int p_what);
155
156
virtual void _add_element();
157
virtual void _length_changed(double p_page);
158
virtual void _edit_pressed();
159
virtual void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
160
virtual void _change_type(Object *p_button, int p_slot_index);
161
virtual void _change_type_menu(int p_index);
162
163
virtual void _object_id_selected(const StringName &p_property, ObjectID p_id);
164
virtual void _remove_pressed(int p_index);
165
166
virtual void _button_draw();
167
virtual void _button_add_item_draw();
168
virtual bool _is_drop_valid(const Dictionary &p_drag_data) const;
169
virtual bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
170
virtual void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
171
172
public:
173
void setup(Variant::Type p_array_type, const String &p_hint_string = "");
174
void set_preview_value(bool p_preview_value);
175
virtual void update_property() override;
176
virtual bool is_colored(ColorationMode p_mode) override;
177
EditorPropertyArray();
178
};
179
180
class EditorPropertyDictionary : public EditorProperty {
181
GDCLASS(EditorPropertyDictionary, EditorProperty);
182
183
struct Slot {
184
Ref<EditorPropertyDictionaryObject> object;
185
HBoxContainer *container = nullptr;
186
int index = -1;
187
Variant::Type type = Variant::VARIANT_MAX;
188
Variant::Type key_type = Variant::VARIANT_MAX;
189
bool as_id = false;
190
bool key_as_id = false;
191
EditorProperty *prop = nullptr;
192
EditorProperty *prop_key = nullptr;
193
Button *edit_button = nullptr;
194
Button *remove_button = nullptr;
195
String prop_name;
196
String key_name;
197
198
void set_index(int p_idx) {
199
index = p_idx;
200
prop_name = object->get_property_name_for_index(p_idx);
201
key_name = object->get_key_name_for_index(p_idx);
202
update_prop_or_index();
203
}
204
205
void set_prop(EditorProperty *p_prop) {
206
prop->add_sibling(p_prop);
207
prop->queue_free();
208
prop = p_prop;
209
update_prop_or_index();
210
}
211
212
void set_key_prop(EditorProperty *p_prop) {
213
if (prop_key) {
214
prop_key->add_sibling(p_prop);
215
prop_key->queue_free();
216
prop_key = p_prop;
217
update_prop_or_index();
218
}
219
}
220
221
void update_prop_or_index() {
222
prop->set_object_and_property(object.ptr(), prop_name);
223
if (prop_key) {
224
prop_key->set_object_and_property(object.ptr(), key_name);
225
} else {
226
prop->set_label(object->get_label_for_index(index));
227
}
228
}
229
};
230
231
EditorVariantTypePopupMenu *change_type = nullptr;
232
bool updating = false;
233
234
bool preview_value = false;
235
Ref<EditorPropertyDictionaryObject> object;
236
int page_length = 20;
237
int page_index = 0;
238
int changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE;
239
Button *edit = nullptr;
240
PanelContainer *container = nullptr;
241
VBoxContainer *property_vbox = nullptr;
242
PanelContainer *add_panel = nullptr;
243
Button *button_add_item = nullptr;
244
EditorPaginator *paginator = nullptr;
245
LocalVector<Slot> slots;
246
void _create_new_property_slot(int p_idx);
247
248
void _page_changed(int p_page);
249
void _edit_pressed();
250
void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
251
void _resource_selected(const String &p_path, Ref<Resource> p_resource);
252
void _change_type(Object *p_button, int p_slot_index);
253
void _change_type_menu(int p_index);
254
255
void _add_key_value();
256
void _object_id_selected(const StringName &p_property, ObjectID p_id);
257
void _remove_pressed(int p_slot_index);
258
259
Variant::Type key_subtype;
260
PropertyHint key_subtype_hint;
261
String key_subtype_hint_string;
262
Variant::Type value_subtype;
263
PropertyHint value_subtype_hint;
264
String value_subtype_hint_string;
265
void initialize_dictionary(Variant &p_dictionary);
266
267
protected:
268
void _notification(int p_what);
269
270
public:
271
void setup(PropertyHint p_hint, const String &p_hint_string = "");
272
void set_preview_value(bool p_preview_value);
273
virtual void update_property() override;
274
virtual bool is_colored(ColorationMode p_mode) override;
275
EditorPropertyDictionary();
276
};
277
278
class EditorPropertyLocalizableString : public EditorProperty {
279
GDCLASS(EditorPropertyLocalizableString, EditorProperty);
280
281
EditorLocaleDialog *locale_select = nullptr;
282
283
bool updating;
284
285
Ref<EditorPropertyDictionaryObject> object;
286
int page_length = 20;
287
int page_index = 0;
288
Button *edit = nullptr;
289
MarginContainer *container = nullptr;
290
VBoxContainer *property_vbox = nullptr;
291
EditorSpinSlider *size_slider = nullptr;
292
Button *button_add_item = nullptr;
293
EditorPaginator *paginator = nullptr;
294
295
void _page_changed(int p_page);
296
void _edit_pressed();
297
void _remove_item(Object *p_button, int p_index);
298
void _property_changed(const String &p_property, const Variant &p_value, const String &p_name = "", bool p_changing = false);
299
300
void _add_locale_popup();
301
void _add_locale(const String &p_locale);
302
void _object_id_selected(const StringName &p_property, ObjectID p_id);
303
304
public:
305
virtual void update_property() override;
306
EditorPropertyLocalizableString();
307
};
308
309