From 673c649adbaac7e484abf2c96e24d6e846827455 Mon Sep 17 00:00:00 2001 From: Silc Renew Date: Sun, 21 Aug 2022 16:03:38 +0900 Subject: Implement Quaternion Editor --- core/core_constants.cpp | 1 + core/object/object.h | 1 + doc/classes/@GlobalScope.xml | 5 +- editor/editor_properties.cpp | 143 +++++++++++++++++++++++++++++++++++++++--- editor/editor_properties.h | 19 +++++- editor/editor_spin_slider.cpp | 4 ++ editor/editor_spin_slider.h | 2 + scene/3d/node_3d.cpp | 2 +- 8 files changed, 165 insertions(+), 12 deletions(-) diff --git a/core/core_constants.cpp b/core/core_constants.cpp index dc0ab72a86..299b60872d 100644 --- a/core/core_constants.cpp +++ b/core/core_constants.cpp @@ -612,6 +612,7 @@ void register_global_constants() { BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_LOCALE_ID); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_LOCALIZABLE_STRING); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_NODE_TYPE); + BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_HIDE_QUATERNION_EDIT); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_MAX); BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_NONE); diff --git a/core/object/object.h b/core/object/object.h index 154ef176d3..b6869aff21 100644 --- a/core/object/object.h +++ b/core/object/object.h @@ -91,6 +91,7 @@ enum PropertyHint { PROPERTY_HINT_LOCALE_ID, PROPERTY_HINT_LOCALIZABLE_STRING, PROPERTY_HINT_NODE_TYPE, ///< a node object type + PROPERTY_HINT_HIDE_QUATERNION_EDIT, /// Only Node3D::transform should hide the quaternion editor. PROPERTY_HINT_MAX, // When updating PropertyHint, also sync the hardcoded list in VisualScriptEditorVariableEdit }; diff --git a/doc/classes/@GlobalScope.xml b/doc/classes/@GlobalScope.xml index 5b3f4d4e7b..0661a46a4a 100644 --- a/doc/classes/@GlobalScope.xml +++ b/doc/classes/@GlobalScope.xml @@ -2695,7 +2695,10 @@ - + + Hints that a quaternion property should disable the temporary euler editor. + + diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp index f434df3a1e..4555aba1d7 100644 --- a/editor/editor_properties.cpp +++ b/editor/editor_properties.cpp @@ -2613,8 +2613,47 @@ void EditorPropertyQuaternion::_set_read_only(bool p_read_only) { for (int i = 0; i < 4; i++) { spin[i]->set_read_only(p_read_only); } + for (int i = 0; i < 3; i++) { + euler[i]->set_read_only(p_read_only); + } }; +void EditorPropertyQuaternion::_edit_custom_value() { + if (edit_button->is_pressed()) { + edit_custom_bc->show(); + for (int i = 0; i < 3; i++) { + euler[i]->grab_focus(); + } + } else { + edit_custom_bc->hide(); + for (int i = 0; i < 4; i++) { + spin[i]->grab_focus(); + } + } + update_property(); +} + +void EditorPropertyQuaternion::_custom_value_changed(double val) { + if (setting) { + return; + } + + edit_euler.x = euler[0]->get_value(); + edit_euler.y = euler[1]->get_value(); + edit_euler.z = euler[2]->get_value(); + + Vector3 v; + v.x = Math::deg2rad(edit_euler.x); + v.y = Math::deg2rad(edit_euler.y); + v.z = Math::deg2rad(edit_euler.z); + + Quaternion temp_q = Quaternion(v); + spin[0]->set_value(temp_q.x); + spin[1]->set_value(temp_q.y); + spin[2]->set_value(temp_q.z); + spin[3]->set_value(temp_q.w); +} + void EditorPropertyQuaternion::_value_changed(double val, const String &p_name) { if (setting) { return; @@ -2625,9 +2664,18 @@ void EditorPropertyQuaternion::_value_changed(double val, const String &p_name) p.y = spin[1]->get_value(); p.z = spin[2]->get_value(); p.w = spin[3]->get_value(); + emit_changed(get_edited_property(), p, p_name); } +bool EditorPropertyQuaternion::is_grabbing_euler() { + bool is_grabbing = false; + for (int i = 0; i < 3; i++) { + is_grabbing |= euler[i]->is_grabbing(); + } + return is_grabbing; +} + void EditorPropertyQuaternion::update_property() { Quaternion val = get_edited_object()->get(get_edited_property()); setting = true; @@ -2635,9 +2683,22 @@ void EditorPropertyQuaternion::update_property() { spin[1]->set_value(val.y); spin[2]->set_value(val.z); spin[3]->set_value(val.w); + if (!is_grabbing_euler()) { + Vector3 v = val.normalized().get_euler_yxz(); + edit_euler.x = Math::rad2deg(v.x); + edit_euler.y = Math::rad2deg(v.y); + edit_euler.z = Math::rad2deg(v.z); + euler[0]->set_value(edit_euler.x); + euler[1]->set_value(edit_euler.y); + euler[2]->set_value(edit_euler.z); + } setting = false; } +void EditorPropertyQuaternion::_warning_pressed() { + warning_dialog->popup_centered(); +} + void EditorPropertyQuaternion::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: @@ -2646,6 +2707,13 @@ void EditorPropertyQuaternion::_notification(int p_what) { for (int i = 0; i < 4; i++) { spin[i]->add_theme_color_override("label_color", colors[i]); } + for (int i = 0; i < 3; i++) { + euler[i]->add_theme_color_override("label_color", colors[i]); + } + edit_button->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons"))); + euler_label->add_theme_color_override(SNAME("font_color"), get_theme_color(SNAME("property_color"), SNAME("Editor"))); + warning->set_icon(get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons"))); + warning->add_theme_color_override(SNAME("font_color"), get_theme_color(SNAME("warning_color"), SNAME("Editor"))); } break; } } @@ -2653,7 +2721,7 @@ void EditorPropertyQuaternion::_notification(int p_what) { void EditorPropertyQuaternion::_bind_methods() { } -void EditorPropertyQuaternion::setup(double p_min, double p_max, double p_step, bool p_no_slider, const String &p_suffix) { +void EditorPropertyQuaternion::setup(double p_min, double p_max, double p_step, bool p_no_slider, const String &p_suffix, bool p_hide_editor) { for (int i = 0; i < 4; i++) { spin[i]->set_min(p_min); spin[i]->set_max(p_max); @@ -2665,28 +2733,50 @@ void EditorPropertyQuaternion::setup(double p_min, double p_max, double p_step, // a generic way to store 4 values, so we'll still respect the suffix. spin[i]->set_suffix(p_suffix); } + + for (int i = 0; i < 3; i++) { + euler[i]->set_min(-360); + euler[i]->set_max(360); + euler[i]->set_step(0.1); + euler[i]->set_hide_slider(false); + euler[i]->set_allow_greater(true); + euler[i]->set_allow_lesser(true); + euler[i]->set_suffix(U"\u00B0"); + } + + if (p_hide_editor) { + edit_button->hide(); + } } EditorPropertyQuaternion::EditorPropertyQuaternion() { bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector_types_editing"); - BoxContainer *bc; - + VBoxContainer *bc = memnew(VBoxContainer); + edit_custom_bc = memnew(VBoxContainer); + BoxContainer *edit_custom_layout; if (horizontal) { - bc = memnew(HBoxContainer); - add_child(bc); + default_layout = memnew(HBoxContainer); + edit_custom_layout = memnew(HBoxContainer); set_bottom_editor(bc); } else { - bc = memnew(VBoxContainer); - add_child(bc); + default_layout = memnew(VBoxContainer); + edit_custom_layout = memnew(VBoxContainer); } + edit_custom_bc->hide(); + add_child(bc); + edit_custom_bc->set_h_size_flags(SIZE_EXPAND_FILL); + default_layout->set_h_size_flags(SIZE_EXPAND_FILL); + edit_custom_layout->set_h_size_flags(SIZE_EXPAND_FILL); + bc->add_child(default_layout); + bc->add_child(edit_custom_bc); static const char *desc[4] = { "x", "y", "z", "w" }; for (int i = 0; i < 4; i++) { spin[i] = memnew(EditorSpinSlider); spin[i]->set_flat(true); spin[i]->set_label(desc[i]); - bc->add_child(spin[i]); + default_layout->add_child(spin[i]); add_focusable(spin[i]); spin[i]->connect("value_changed", callable_mp(this, &EditorPropertyQuaternion::_value_changed).bind(desc[i])); if (horizontal) { @@ -2694,6 +2784,41 @@ EditorPropertyQuaternion::EditorPropertyQuaternion() { } } + warning = memnew(Button); + warning->set_text(TTR("Temporary Euler may be changed implicitly!")); + warning->set_clip_text(true); + warning->connect("pressed", callable_mp(this, &EditorPropertyQuaternion::_warning_pressed)); + warning_dialog = memnew(AcceptDialog); + add_child(warning_dialog); + warning_dialog->set_text(TTR("Temporary Euler will not be stored in the object with the original value. Instead, it will be stored as Quaternion with irreversible conversion.\nThis is due to the fact that the result of Euler->Quaternion can be determined uniquely, but the result of Quaternion->Euler can be multi-existent.")); + + euler_label = memnew(Label); + euler_label->set_text("Temporary Euler"); + + edit_custom_bc->add_child(warning); + edit_custom_bc->add_child(edit_custom_layout); + edit_custom_layout->add_child(euler_label); + + for (int i = 0; i < 3; i++) { + euler[i] = memnew(EditorSpinSlider); + euler[i]->set_flat(true); + euler[i]->set_label(desc[i]); + edit_custom_layout->add_child(euler[i]); + add_focusable(euler[i]); + euler[i]->connect("value_changed", callable_mp(this, &EditorPropertyQuaternion::_custom_value_changed)); + if (horizontal) { + euler[i]->set_h_size_flags(SIZE_EXPAND_FILL); + } + } + + edit_button = memnew(Button); + edit_button->set_flat(true); + edit_button->set_toggle_mode(true); + default_layout->add_child(edit_button); + edit_button->connect("pressed", callable_mp(this, &EditorPropertyQuaternion::_edit_custom_value)); + + add_focusable(edit_button); + if (!horizontal) { set_label_reference(spin[0]); //show text and buttons around this } @@ -4357,7 +4482,7 @@ EditorProperty *EditorInspectorDefaultPlugin::get_editor_for_property(Object *p_ case Variant::QUATERNION: { EditorPropertyQuaternion *editor = memnew(EditorPropertyQuaternion); EditorPropertyRangeHint hint = _parse_range_hint(p_hint, p_hint_text, default_float_step); - editor->setup(hint.min, hint.max, hint.step, hint.hide_slider, hint.suffix); + editor->setup(hint.min, hint.max, hint.step, hint.hide_slider, hint.suffix, p_hint == PROPERTY_HINT_HIDE_QUATERNION_EDIT); return editor; } break; case Variant::AABB: { diff --git a/editor/editor_properties.h b/editor/editor_properties.h index c1dfb5cb1e..16cee62ae8 100644 --- a/editor/editor_properties.h +++ b/editor/editor_properties.h @@ -625,9 +625,26 @@ public: class EditorPropertyQuaternion : public EditorProperty { GDCLASS(EditorPropertyQuaternion, EditorProperty); + BoxContainer *default_layout = nullptr; EditorSpinSlider *spin[4]; bool setting = false; + + Button *warning = nullptr; + AcceptDialog *warning_dialog = nullptr; + + Label *euler_label = nullptr; + VBoxContainer *edit_custom_bc = nullptr; + EditorSpinSlider *euler[3]; + Button *edit_button = nullptr; + + Vector3 edit_euler = Vector3(); + void _value_changed(double p_val, const String &p_name); + void _edit_custom_value(); + void _custom_value_changed(double p_val); + void _warning_pressed(); + + bool is_grabbing_euler(); protected: virtual void _set_read_only(bool p_read_only) override; @@ -636,7 +653,7 @@ protected: public: virtual void update_property() override; - void setup(double p_min, double p_max, double p_step, bool p_no_slider, const String &p_suffix = String()); + void setup(double p_min, double p_max, double p_step, bool p_no_slider, const String &p_suffix = String(), bool p_hide_editor = false); EditorPropertyQuaternion(); }; diff --git a/editor/editor_spin_slider.cpp b/editor/editor_spin_slider.cpp index 20e9d7a3df..5b98460e8e 100644 --- a/editor/editor_spin_slider.cpp +++ b/editor/editor_spin_slider.cpp @@ -605,6 +605,10 @@ bool EditorSpinSlider::is_flat() const { return flat; } +bool EditorSpinSlider::is_grabbing() const { + return grabbing_grabber || grabbing_spinner; +} + void EditorSpinSlider::_focus_entered() { _ensure_input_popup(); Rect2 gr = get_screen_rect(); diff --git a/editor/editor_spin_slider.h b/editor/editor_spin_slider.h index f0adf5b7a1..afcaa3e4b6 100644 --- a/editor/editor_spin_slider.h +++ b/editor/editor_spin_slider.h @@ -110,6 +110,8 @@ public: void set_flat(bool p_enable); bool is_flat() const; + bool is_grabbing() const; + void setup_and_show() { _focus_entered(); } LineEdit *get_line_edit(); diff --git a/scene/3d/node_3d.cpp b/scene/3d/node_3d.cpp index 4ae834cf7a..1c82f8f23d 100644 --- a/scene/3d/node_3d.cpp +++ b/scene/3d/node_3d.cpp @@ -1054,7 +1054,7 @@ void Node3D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "global_transform", PROPERTY_HINT_NONE, "suffix:m", PROPERTY_USAGE_NONE), "set_global_transform", "get_global_transform"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "position", PROPERTY_HINT_RANGE, "-99999,99999,0.001,or_greater,or_lesser,no_slider,suffix:m", PROPERTY_USAGE_EDITOR), "set_position", "get_position"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_lesser,or_greater,radians", PROPERTY_USAGE_EDITOR), "set_rotation", "get_rotation"); - ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "quaternion", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_quaternion", "get_quaternion"); + ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "quaternion", PROPERTY_HINT_HIDE_QUATERNION_EDIT, "", PROPERTY_USAGE_EDITOR), "set_quaternion", "get_quaternion"); ADD_PROPERTY(PropertyInfo(Variant::BASIS, "basis", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_basis", "get_basis"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "scale", PROPERTY_HINT_LINK, "", PROPERTY_USAGE_EDITOR), "set_scale", "get_scale"); ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_edit_mode", PROPERTY_HINT_ENUM, "Euler,Quaternion,Basis"), "set_rotation_edit_mode", "get_rotation_edit_mode"); -- cgit v1.2.3