| 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(const EditorPropertyRangeHint &p_range_hint, bool p_link, bool p_is_int) { |
| 157 | radians_as_degrees = p_range_hint.radians_as_degrees; |
| 158 | |
| 159 | for (EditorSpinSlider *spin : spin_sliders) { |
| 160 | spin->set_min(p_range_hint.min); |
| 161 | spin->set_max(p_range_hint.max); |
| 162 | spin->set_step(p_range_hint.step); |
| 163 | if (p_range_hint.hide_control) { |
| 164 | spin->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE); |
| 165 | } |
| 166 | spin->set_allow_greater(true); |
| 167 | spin->set_allow_lesser(true); |
| 168 | spin->set_suffix(p_range_hint.suffix); |
| 169 | spin->set_editing_integer(p_is_int); |
| 170 | } |
| 171 | |
| 172 | if (!p_link) { |
| 173 | linked->hide(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | EditorPropertyVectorN::EditorPropertyVectorN(Variant::Type p_type, bool p_force_wide, bool p_horizontal) { |
| 178 | vector_type = p_type; |
| 179 | switch (vector_type) { |
| 180 | case Variant::VECTOR2: |
| 181 | case Variant::VECTOR2I: |
| 182 | component_count = 2; |
| 183 | break; |
| 184 | |
| 185 | case Variant::VECTOR3: |
| 186 | case Variant::VECTOR3I: |
| 187 | component_count = 3; |
| 188 | break; |
| 189 | |
| 190 | case Variant::VECTOR4: |
| 191 | case Variant::VECTOR4I: |
| 192 | component_count = 4; |
| 193 | break; |
| 194 | |
| 195 | default: // Needed to silence a warning. |
| 196 | ERR_PRINT("Not a Vector type."); |
| 197 | break; |
| 198 | } |
| 199 | bool horizontal = p_force_wide || p_horizontal; |
| 200 | |
| 201 | HBoxContainer *hb = memnew(HBoxContainer); |
| 202 | hb->set_h_size_flags(SIZE_EXPAND_FILL); |
| 203 | |
| 204 | BoxContainer *bc; |
| 205 | |
| 206 | if (p_force_wide) { |
| 207 | bc = memnew(HBoxContainer); |
| 208 | hb->add_child(bc); |
| 209 | } else if (horizontal) { |
| 210 | bc = memnew(HBoxContainer); |
| 211 | hb->add_child(bc); |
| 212 | set_bottom_editor(hb); |
| 213 | } else { |
| 214 | bc = memnew(VBoxContainer); |
| 215 | hb->add_child(bc); |
| 216 | } |
| 217 | bc->set_h_size_flags(SIZE_EXPAND_FILL); |
| 218 | |
| 219 | spin_sliders.resize(component_count); |
| 220 | EditorSpinSlider **spin = spin_sliders.ptrw(); |
| 221 | |
| 222 | for (int i = 0; i < component_count; i++) { |
| 223 | spin[i] = memnew(EditorSpinSlider); |
| 224 | bc->add_child(spin[i]); |
| 225 | spin[i]->set_flat(true); |
| 226 | spin[i]->set_label(String(COMPONENT_LABELS[i])); |
| 227 | spin[i]->set_accessibility_name(String(COMPONENT_LABELS[i])); |
| 228 | if (horizontal) { |
| 229 | spin[i]->set_h_size_flags(SIZE_EXPAND_FILL); |
| 230 | } |
| 231 | spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyVectorN::_value_changed).bind(String(COMPONENT_LABELS[i]))); |
| 232 | spin[i]->connect(SNAME("grabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(true)); |
| 233 | spin[i]->connect(SNAME("ungrabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(false)); |
| 234 | add_focusable(spin[i]); |
| 235 | } |
| 236 | |
| 237 | ratio.resize(component_count * (component_count - 1)); |
| 238 | ratio.fill(1.0); |
| 239 | |
| 240 | linked = memnew(TextureButton); |
| 241 | linked->set_toggle_mode(true); |
| 242 | linked->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED); |
| 243 | linked->set_tooltip_text(TTR("Lock/Unlock Component Ratio")); |
| 244 | linked->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyVectorN::_update_ratio)); |
| 245 | linked->connect(SceneStringName(toggled), callable_mp(this, &EditorPropertyVectorN::_store_link)); |
| 246 | hb->add_child(linked); |
| 247 | |
| 248 | add_child(hb); |
| 249 | if (!horizontal) { |
| 250 | set_label_reference(spin_sliders[0]); // Show text and buttons around this. |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | EditorPropertyVector2::EditorPropertyVector2(bool p_force_wide) : |
| 255 | EditorPropertyVectorN(Variant::VECTOR2, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector2_editing")) {} |
| 256 | |
| 257 | EditorPropertyVector2i::EditorPropertyVector2i(bool p_force_wide) : |
| 258 | EditorPropertyVectorN(Variant::VECTOR2I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector2_editing")) {} |
| 259 | |
| 260 | EditorPropertyVector3::EditorPropertyVector3(bool p_force_wide) : |
| 261 | EditorPropertyVectorN(Variant::VECTOR3, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {} |
| 262 | |
| 263 | EditorPropertyVector3i::EditorPropertyVector3i(bool p_force_wide) : |
| 264 | EditorPropertyVectorN(Variant::VECTOR3I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {} |
| 265 | |
| 266 | EditorPropertyVector4::EditorPropertyVector4(bool p_force_wide) : |
| 267 | EditorPropertyVectorN(Variant::VECTOR4, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {} |
| 268 | |
| 269 | EditorPropertyVector4i::EditorPropertyVector4i(bool p_force_wide) : |
| 270 | EditorPropertyVectorN(Variant::VECTOR4I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {} |