Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/inspector/editor_properties_vector.cpp
9896 views
1
/**************************************************************************/
2
/* editor_properties_vector.cpp */
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
#include "editor_properties_vector.h"
32
33
#include "editor/editor_string_names.h"
34
#include "editor/gui/editor_spin_slider.h"
35
#include "editor/settings/editor_settings.h"
36
#include "editor/themes/editor_scale.h"
37
#include "scene/gui/box_container.h"
38
#include "scene/gui/texture_button.h"
39
40
const String EditorPropertyVectorN::COMPONENT_LABELS[4] = { "x", "y", "z", "w" };
41
42
void EditorPropertyVectorN::_set_read_only(bool p_read_only) {
43
for (EditorSpinSlider *spin : spin_sliders) {
44
spin->set_read_only(p_read_only);
45
}
46
}
47
48
void EditorPropertyVectorN::_value_changed(double val, const String &p_name) {
49
if (linked->is_pressed()) {
50
int changed_component = -1;
51
for (int i = 0; i < component_count; i++) {
52
if (p_name == COMPONENT_LABELS[i]) {
53
changed_component = i;
54
break;
55
}
56
}
57
DEV_ASSERT(changed_component >= 0);
58
59
for (int i = 0; i < component_count - 1; i++) {
60
int slider_idx = (changed_component + 1 + i) % component_count;
61
int ratio_idx = changed_component * (component_count - 1) + i;
62
63
if (ratio[ratio_idx] == 0) {
64
continue;
65
}
66
67
spin_sliders[slider_idx]->set_value_no_signal(spin_sliders[changed_component]->get_value() * ratio[ratio_idx]);
68
}
69
}
70
71
Variant v;
72
Callable::CallError cerror;
73
Variant::construct(vector_type, v, nullptr, 0, cerror);
74
75
for (int i = 0; i < component_count; i++) {
76
if (radians_as_degrees) {
77
v.set(i, Math::deg_to_rad(spin_sliders[i]->get_value()));
78
} else {
79
v.set(i, spin_sliders[i]->get_value());
80
}
81
}
82
emit_changed(get_edited_property(), v, linked->is_pressed() ? "" : p_name);
83
}
84
85
void EditorPropertyVectorN::update_property() {
86
Variant val = get_edited_property_value();
87
for (int i = 0; i < component_count; i++) {
88
if (radians_as_degrees) {
89
spin_sliders[i]->set_value_no_signal(Math::rad_to_deg((real_t)val.get(i)));
90
} else {
91
spin_sliders[i]->set_value_no_signal(val.get(i));
92
}
93
}
94
95
if (!is_grabbed) {
96
_update_ratio();
97
}
98
}
99
100
void EditorPropertyVectorN::_update_ratio() {
101
linked->set_modulate(Color(1, 1, 1, linked->is_pressed() ? 1.0 : 0.5));
102
103
double *ratio_write = ratio.ptrw();
104
for (int i = 0; i < ratio.size(); i++) {
105
int base_slider_idx = i / (component_count - 1);
106
int secondary_slider_idx = ((base_slider_idx + 1) + i % (component_count - 1)) % component_count;
107
108
if (spin_sliders[base_slider_idx]->get_value() != 0) {
109
ratio_write[i] = spin_sliders[secondary_slider_idx]->get_value() / spin_sliders[base_slider_idx]->get_value();
110
}
111
}
112
}
113
114
void EditorPropertyVectorN::_store_link(bool p_linked) {
115
if (!get_edited_object()) {
116
return;
117
}
118
const String key = vformat("%s:%s", get_edited_object()->get_class(), get_edited_property());
119
EditorSettings::get_singleton()->set_project_metadata("linked_properties", key, p_linked);
120
}
121
122
void EditorPropertyVectorN::_grab_changed(bool p_grab) {
123
if (p_grab) {
124
_update_ratio();
125
}
126
is_grabbed = p_grab;
127
}
128
129
void EditorPropertyVectorN::_notification(int p_what) {
130
switch (p_what) {
131
case NOTIFICATION_READY: {
132
if (linked->is_visible()) {
133
if (get_edited_object()) {
134
const String key = vformat("%s:%s", get_edited_object()->get_class(), get_edited_property());
135
linked->set_pressed_no_signal(EditorSettings::get_singleton()->get_project_metadata("linked_properties", key, true));
136
_update_ratio();
137
}
138
}
139
} break;
140
141
case NOTIFICATION_THEME_CHANGED: {
142
int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
143
144
linked->set_texture_normal(get_editor_theme_icon(SNAME("Unlinked")));
145
linked->set_texture_pressed(get_editor_theme_icon(SNAME("Instance")));
146
linked->set_custom_minimum_size(Size2(icon_size + 8 * EDSCALE, 0));
147
148
const Color *colors = _get_property_colors();
149
for (int i = 0; i < component_count; i++) {
150
spin_sliders[i]->add_theme_color_override("label_color", colors[i]);
151
}
152
} break;
153
}
154
}
155
156
void EditorPropertyVectorN::setup(double p_min, double p_max, double p_step, bool p_hide_slider, bool p_link, const String &p_suffix, bool p_radians_as_degrees, bool p_is_int) {
157
radians_as_degrees = p_radians_as_degrees;
158
159
for (EditorSpinSlider *spin : spin_sliders) {
160
spin->set_min(p_min);
161
spin->set_max(p_max);
162
spin->set_step(p_step);
163
spin->set_hide_slider(p_hide_slider);
164
spin->set_allow_greater(true);
165
spin->set_allow_lesser(true);
166
spin->set_suffix(p_suffix);
167
spin->set_editing_integer(p_is_int);
168
}
169
170
if (!p_link) {
171
linked->hide();
172
}
173
}
174
175
EditorPropertyVectorN::EditorPropertyVectorN(Variant::Type p_type, bool p_force_wide, bool p_horizontal) {
176
vector_type = p_type;
177
switch (vector_type) {
178
case Variant::VECTOR2:
179
case Variant::VECTOR2I:
180
component_count = 2;
181
break;
182
183
case Variant::VECTOR3:
184
case Variant::VECTOR3I:
185
component_count = 3;
186
break;
187
188
case Variant::VECTOR4:
189
case Variant::VECTOR4I:
190
component_count = 4;
191
break;
192
193
default: // Needed to silence a warning.
194
ERR_PRINT("Not a Vector type.");
195
break;
196
}
197
bool horizontal = p_force_wide || p_horizontal;
198
199
HBoxContainer *hb = memnew(HBoxContainer);
200
hb->set_h_size_flags(SIZE_EXPAND_FILL);
201
202
BoxContainer *bc;
203
204
if (p_force_wide) {
205
bc = memnew(HBoxContainer);
206
hb->add_child(bc);
207
} else if (horizontal) {
208
bc = memnew(HBoxContainer);
209
hb->add_child(bc);
210
set_bottom_editor(hb);
211
} else {
212
bc = memnew(VBoxContainer);
213
hb->add_child(bc);
214
}
215
bc->set_h_size_flags(SIZE_EXPAND_FILL);
216
217
spin_sliders.resize(component_count);
218
EditorSpinSlider **spin = spin_sliders.ptrw();
219
220
for (int i = 0; i < component_count; i++) {
221
spin[i] = memnew(EditorSpinSlider);
222
bc->add_child(spin[i]);
223
spin[i]->set_flat(true);
224
spin[i]->set_label(String(COMPONENT_LABELS[i]));
225
spin[i]->set_accessibility_name(String(COMPONENT_LABELS[i]));
226
if (horizontal) {
227
spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
228
}
229
spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyVectorN::_value_changed).bind(String(COMPONENT_LABELS[i])));
230
spin[i]->connect(SNAME("grabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(true));
231
spin[i]->connect(SNAME("ungrabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(false));
232
add_focusable(spin[i]);
233
}
234
235
ratio.resize(component_count * (component_count - 1));
236
ratio.fill(1.0);
237
238
linked = memnew(TextureButton);
239
linked->set_toggle_mode(true);
240
linked->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);
241
linked->set_tooltip_text(TTR("Lock/Unlock Component Ratio"));
242
linked->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyVectorN::_update_ratio));
243
linked->connect(SceneStringName(toggled), callable_mp(this, &EditorPropertyVectorN::_store_link));
244
hb->add_child(linked);
245
246
add_child(hb);
247
if (!horizontal) {
248
set_label_reference(spin_sliders[0]); // Show text and buttons around this.
249
}
250
}
251
252
EditorPropertyVector2::EditorPropertyVector2(bool p_force_wide) :
253
EditorPropertyVectorN(Variant::VECTOR2, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector2_editing")) {}
254
255
EditorPropertyVector2i::EditorPropertyVector2i(bool p_force_wide) :
256
EditorPropertyVectorN(Variant::VECTOR2I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector2_editing")) {}
257
258
EditorPropertyVector3::EditorPropertyVector3(bool p_force_wide) :
259
EditorPropertyVectorN(Variant::VECTOR3, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}
260
261
EditorPropertyVector3i::EditorPropertyVector3i(bool p_force_wide) :
262
EditorPropertyVectorN(Variant::VECTOR3I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}
263
264
EditorPropertyVector4::EditorPropertyVector4(bool p_force_wide) :
265
EditorPropertyVectorN(Variant::VECTOR4, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}
266
267
EditorPropertyVector4i::EditorPropertyVector4i(bool p_force_wide) :
268
EditorPropertyVectorN(Variant::VECTOR4I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}
269
270