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