Merge pull request #77699 from KoBeWi/an_indestructible_ratio_that_we_all_deserve

Preserve vector ratio when dragging through 0
This commit is contained in:
Rémi Verschelde 2023-06-10 12:10:30 +02:00
commit c3a45e771b
No known key found for this signature in database
GPG Key ID: C3336907360768E1
2 changed files with 26 additions and 14 deletions

View File

@ -57,6 +57,11 @@ void EditorPropertyVectorN::_value_changed(double val, const String &p_name) {
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;
if (ratio[ratio_idx] == 0) {
continue;
}
spin_sliders[slider_idx]->set_value_no_signal(spin_sliders[changed_component]->get_value() * ratio[ratio_idx]);
}
}
@ -84,28 +89,24 @@ void EditorPropertyVectorN::update_property() {
spin_sliders[i]->set_value_no_signal(val.get(i));
}
}
_update_ratio();
if (!is_grabbed) {
_update_ratio();
}
}
void EditorPropertyVectorN::_update_ratio() {
linked->set_modulate(Color(1, 1, 1, linked->is_pressed() ? 1.0 : 0.5));
bool non_zero = true;
for (int i = 0; i < component_count; i++) {
if (spin_sliders[i]->get_value() == 0) {
non_zero = false;
break;
}
}
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) {
if (spin_sliders[base_slider_idx]->get_value() != 0) {
ratio_write[i] = spin_sliders[secondary_slider_idx]->get_value() / spin_sliders[base_slider_idx]->get_value();
} else {
ratio_write[i] = 1.0;
ratio_write[i] = 0;
}
}
}
@ -118,6 +119,13 @@ void EditorPropertyVectorN::_store_link(bool p_linked) {
EditorSettings::get_singleton()->set_project_metadata("linked_properties", key, p_linked);
}
void EditorPropertyVectorN::_grab_changed(bool p_grab) {
if (p_grab) {
_update_ratio();
}
is_grabbed = p_grab;
}
void EditorPropertyVectorN::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_READY: {
@ -204,14 +212,16 @@ EditorPropertyVectorN::EditorPropertyVectorN(Variant::Type p_type, bool p_force_
for (int i = 0; i < component_count; i++) {
spin[i] = memnew(EditorSpinSlider);
bc->add_child(spin[i]);
spin[i]->set_flat(true);
spin[i]->set_label(String(COMPONENT_LABELS[i]));
bc->add_child(spin[i]);
add_focusable(spin[i]);
spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyVectorN::_value_changed).bind(String(COMPONENT_LABELS[i])));
if (horizontal) {
spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
}
spin[i]->connect(SNAME("value_changed"), callable_mp(this, &EditorPropertyVectorN::_value_changed).bind(String(COMPONENT_LABELS[i])));
spin[i]->connect(SNAME("grabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(true));
spin[i]->connect(SNAME("ungrabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(false));
add_focusable(spin[i]);
}
ratio.resize(component_count * (component_count - 1));

View File

@ -47,11 +47,13 @@ class EditorPropertyVectorN : public EditorProperty {
Vector<EditorSpinSlider *> spin_sliders;
TextureButton *linked = nullptr;
Vector<double> ratio;
bool is_grabbed = false;
bool angle_in_radians = false;
void _update_ratio();
void _store_link(bool p_linked);
void _grab_changed(bool p_grab);
void _value_changed(double p_val, const String &p_name);
protected: