From bf02aabfe13afd7aa03a57b6e2118fef37328f3b Mon Sep 17 00:00:00 2001 From: kobewi Date: Fri, 2 Jun 2023 16:05:28 +0200 Subject: [PATCH] Simplify linked vector ratio logic --- editor/editor_properties_vector.cpp | 32 +++++++++++------------------ editor/editor_properties_vector.h | 1 - 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/editor/editor_properties_vector.cpp b/editor/editor_properties_vector.cpp index 28ea0dc36d7..79e2a219027 100644 --- a/editor/editor_properties_vector.cpp +++ b/editor/editor_properties_vector.cpp @@ -37,15 +37,6 @@ const String EditorPropertyVectorN::COMPONENT_LABELS[4] = { "x", "y", "z", "w" }; -int EditorPropertyVectorN::_get_ratio_component(int p_idx, int p_component) const { - int i = p_idx / (component_count - 1); - if (p_component == 1) { - return i; - } else { - return (i + p_idx % (component_count - 1) + 1) % component_count; - } -} - void EditorPropertyVectorN::_set_read_only(bool p_read_only) { for (EditorSpinSlider *spin : spin_sliders) { spin->set_read_only(p_read_only); @@ -54,21 +45,20 @@ void EditorPropertyVectorN::_set_read_only(bool p_read_only) { void EditorPropertyVectorN::_value_changed(double val, const String &p_name) { if (linked->is_pressed()) { - // TODO: The logic here can be simplified. _get_ratio_component() only exists, - // because the exact code was difficult to figure out. + int changed_component = -1; for (int i = 0; i < component_count; i++) { if (p_name == COMPONENT_LABELS[i]) { - for (int j = 0; j < ratio.size(); j++) { - if (_get_ratio_component(j, 1) == i) { - for (int k = 0; k < component_count - 1; k++) { - spin_sliders[_get_ratio_component(j + k, 0)]->set_value_no_signal(spin_sliders[_get_ratio_component(j + k, 1)]->get_value() * ratio[j + k]); - } - break; - } - } + changed_component = i; break; } } + DEV_ASSERT(changed_component >= 0); + + for (int i = 0; i < component_count - 1; i++) { + int slider_idx = (changed_component + 1 + i) % component_count; + int ratio_idx = changed_component * (component_count - 1) + i; + spin_sliders[slider_idx]->set_value_no_signal(spin_sliders[changed_component]->get_value() * ratio[ratio_idx]); + } } Variant v; @@ -110,8 +100,10 @@ void EditorPropertyVectorN::_update_ratio() { double *ratio_write = ratio.ptrw(); for (int i = 0; i < ratio.size(); i++) { + int base_slider_idx = i / (component_count - 1); + int secondary_slider_idx = ((base_slider_idx + 1) + i % (component_count - 1)) % component_count; if (non_zero) { - ratio_write[i] = spin_sliders[_get_ratio_component(i, 0)]->get_value() / spin_sliders[_get_ratio_component(i, 1)]->get_value(); + ratio_write[i] = spin_sliders[secondary_slider_idx]->get_value() / spin_sliders[base_slider_idx]->get_value(); } else { ratio_write[i] = 1.0; } diff --git a/editor/editor_properties_vector.h b/editor/editor_properties_vector.h index c4b650a4900..2fce9f7e295 100644 --- a/editor/editor_properties_vector.h +++ b/editor/editor_properties_vector.h @@ -51,7 +51,6 @@ class EditorPropertyVectorN : public EditorProperty { bool angle_in_radians = false; void _update_ratio(); - int _get_ratio_component(int p_idx, int p_component) const; void _store_link(bool p_linked); void _value_changed(double p_val, const String &p_name);