Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/inspector/editor_properties_array_dict.h
9902 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
104
void set_index(int p_idx) {
105
String prop_name = "indices/" + itos(p_idx);
106
prop->set_object_and_property(object.ptr(), prop_name);
107
prop->set_label(itos(p_idx));
108
index = p_idx;
109
}
110
};
111
112
EditorVariantTypePopupMenu *change_type = nullptr;
113
114
bool preview_value = false;
115
int page_length = 20;
116
int page_index = 0;
117
int changing_type_index = EditorPropertyArrayObject::NOT_CHANGING_TYPE;
118
Button *edit = nullptr;
119
PanelContainer *container = nullptr;
120
VBoxContainer *property_vbox = nullptr;
121
EditorSpinSlider *size_slider = nullptr;
122
Button *button_add_item = nullptr;
123
EditorPaginator *paginator = nullptr;
124
Variant::Type array_type;
125
Variant::Type subtype;
126
PropertyHint subtype_hint;
127
String subtype_hint_string;
128
LocalVector<Slot> slots;
129
130
Slot reorder_slot;
131
int reorder_to_index = -1;
132
float reorder_mouse_y_delta = 0.0f;
133
void initialize_array(Variant &p_array);
134
135
void _page_changed(int p_page);
136
137
void _reorder_button_gui_input(const Ref<InputEvent> &p_event);
138
void _reorder_button_down(int p_index);
139
void _reorder_button_up();
140
void _create_new_property_slot();
141
142
Node *get_base_node();
143
144
protected:
145
Ref<EditorPropertyArrayObject> object;
146
147
bool updating = false;
148
bool dropping = false;
149
150
void _notification(int p_what);
151
152
virtual void _add_element();
153
virtual void _length_changed(double p_page);
154
virtual void _edit_pressed();
155
virtual void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
156
virtual void _change_type(Object *p_button, int p_slot_index);
157
virtual void _change_type_menu(int p_index);
158
159
virtual void _object_id_selected(const StringName &p_property, ObjectID p_id);
160
virtual void _remove_pressed(int p_index);
161
162
virtual void _button_draw();
163
virtual void _button_add_item_draw();
164
virtual bool _is_drop_valid(const Dictionary &p_drag_data) const;
165
virtual bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
166
virtual void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
167
168
public:
169
void setup(Variant::Type p_array_type, const String &p_hint_string = "");
170
void set_preview_value(bool p_preview_value);
171
virtual void update_property() override;
172
virtual bool is_colored(ColorationMode p_mode) override;
173
EditorPropertyArray();
174
};
175
176
class EditorPropertyDictionary : public EditorProperty {
177
GDCLASS(EditorPropertyDictionary, EditorProperty);
178
179
struct Slot {
180
Ref<EditorPropertyDictionaryObject> object;
181
HBoxContainer *container = nullptr;
182
int index = -1;
183
Variant::Type type = Variant::VARIANT_MAX;
184
Variant::Type key_type = Variant::VARIANT_MAX;
185
bool as_id = false;
186
bool key_as_id = false;
187
EditorProperty *prop = nullptr;
188
EditorProperty *prop_key = nullptr;
189
String prop_name;
190
String key_name;
191
192
void set_index(int p_idx) {
193
index = p_idx;
194
prop_name = object->get_property_name_for_index(p_idx);
195
key_name = object->get_key_name_for_index(p_idx);
196
update_prop_or_index();
197
}
198
199
void set_prop(EditorProperty *p_prop) {
200
prop->add_sibling(p_prop);
201
prop->queue_free();
202
prop = p_prop;
203
update_prop_or_index();
204
}
205
206
void set_key_prop(EditorProperty *p_prop) {
207
if (prop_key) {
208
prop_key->add_sibling(p_prop);
209
prop_key->queue_free();
210
prop_key = p_prop;
211
update_prop_or_index();
212
}
213
}
214
215
void update_prop_or_index() {
216
prop->set_object_and_property(object.ptr(), prop_name);
217
if (prop_key) {
218
prop_key->set_object_and_property(object.ptr(), key_name);
219
} else {
220
prop->set_label(object->get_label_for_index(index));
221
}
222
}
223
};
224
225
EditorVariantTypePopupMenu *change_type = nullptr;
226
bool updating = false;
227
228
bool preview_value = false;
229
Ref<EditorPropertyDictionaryObject> object;
230
int page_length = 20;
231
int page_index = 0;
232
int changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE;
233
Button *edit = nullptr;
234
PanelContainer *container = nullptr;
235
VBoxContainer *property_vbox = nullptr;
236
PanelContainer *add_panel = nullptr;
237
EditorSpinSlider *size_sliderv = nullptr;
238
Button *button_add_item = nullptr;
239
EditorPaginator *paginator = nullptr;
240
LocalVector<Slot> slots;
241
void _create_new_property_slot(int p_idx);
242
243
void _page_changed(int p_page);
244
void _edit_pressed();
245
void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
246
void _change_type(Object *p_button, int p_slot_index);
247
void _change_type_menu(int p_index);
248
249
void _add_key_value();
250
void _object_id_selected(const StringName &p_property, ObjectID p_id);
251
void _remove_pressed(int p_slot_index);
252
253
Variant::Type key_subtype;
254
PropertyHint key_subtype_hint;
255
String key_subtype_hint_string;
256
Variant::Type value_subtype;
257
PropertyHint value_subtype_hint;
258
String value_subtype_hint_string;
259
void initialize_dictionary(Variant &p_dictionary);
260
261
protected:
262
void _notification(int p_what);
263
264
public:
265
void setup(PropertyHint p_hint, const String &p_hint_string = "");
266
void set_preview_value(bool p_preview_value);
267
virtual void update_property() override;
268
virtual bool is_colored(ColorationMode p_mode) override;
269
EditorPropertyDictionary();
270
};
271
272
class EditorPropertyLocalizableString : public EditorProperty {
273
GDCLASS(EditorPropertyLocalizableString, EditorProperty);
274
275
EditorLocaleDialog *locale_select = nullptr;
276
277
bool updating;
278
279
Ref<EditorPropertyDictionaryObject> object;
280
int page_length = 20;
281
int page_index = 0;
282
Button *edit = nullptr;
283
MarginContainer *container = nullptr;
284
VBoxContainer *property_vbox = nullptr;
285
EditorSpinSlider *size_slider = nullptr;
286
Button *button_add_item = nullptr;
287
EditorPaginator *paginator = nullptr;
288
289
void _page_changed(int p_page);
290
void _edit_pressed();
291
void _remove_item(Object *p_button, int p_index);
292
void _property_changed(const String &p_property, const Variant &p_value, const String &p_name = "", bool p_changing = false);
293
294
void _add_locale_popup();
295
void _add_locale(const String &p_locale);
296
void _object_id_selected(const StringName &p_property, ObjectID p_id);
297
298
public:
299
virtual void update_property() override;
300
EditorPropertyLocalizableString();
301
};
302
303