Path: blob/master/editor/inspector/editor_properties_vector.cpp
20984 views
/**************************************************************************/1/* editor_properties_vector.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_properties_vector.h"3132#include "editor/editor_string_names.h"33#include "editor/gui/editor_spin_slider.h"34#include "editor/settings/editor_settings.h"35#include "editor/themes/editor_scale.h"36#include "scene/gui/box_container.h"37#include "scene/gui/texture_button.h"3839const String EditorPropertyVectorN::COMPONENT_LABELS[4] = { "x", "y", "z", "w" };4041void EditorPropertyVectorN::_set_read_only(bool p_read_only) {42for (EditorSpinSlider *spin : spin_sliders) {43spin->set_read_only(p_read_only);44}45}4647void EditorPropertyVectorN::_value_changed(double val, const String &p_name) {48if (linked->is_pressed()) {49int changed_component = -1;50for (int i = 0; i < component_count; i++) {51if (p_name == COMPONENT_LABELS[i]) {52changed_component = i;53break;54}55}56DEV_ASSERT(changed_component >= 0);5758for (int i = 0; i < component_count - 1; i++) {59int slider_idx = (changed_component + 1 + i) % component_count;60int ratio_idx = changed_component * (component_count - 1) + i;6162if (ratio[ratio_idx] == 0) {63continue;64}6566spin_sliders[slider_idx]->set_value_no_signal(spin_sliders[changed_component]->get_value() * ratio[ratio_idx]);67}68}6970Variant v;71Callable::CallError cerror;72Variant::construct(vector_type, v, nullptr, 0, cerror);7374for (int i = 0; i < component_count; i++) {75if (radians_as_degrees) {76v.set(i, Math::deg_to_rad(spin_sliders[i]->get_value()));77} else {78v.set(i, spin_sliders[i]->get_value());79}80}81emit_changed(get_edited_property(), v, linked->is_pressed() ? "" : p_name);82}8384void EditorPropertyVectorN::update_property() {85Variant val = get_edited_property_value();86for (int i = 0; i < component_count; i++) {87if (radians_as_degrees) {88spin_sliders[i]->set_value_no_signal(Math::rad_to_deg((real_t)val.get(i)));89} else {90spin_sliders[i]->set_value_no_signal(val.get(i));91}92}9394if (!is_grabbed) {95_update_ratio();96}97}9899void EditorPropertyVectorN::_update_ratio() {100linked->set_modulate(Color(1, 1, 1, linked->is_pressed() ? 1.0 : 0.5));101102double *ratio_write = ratio.ptrw();103for (int i = 0; i < ratio.size(); i++) {104int base_slider_idx = i / (component_count - 1);105int secondary_slider_idx = ((base_slider_idx + 1) + i % (component_count - 1)) % component_count;106107if (spin_sliders[base_slider_idx]->get_value() != 0) {108ratio_write[i] = spin_sliders[secondary_slider_idx]->get_value() / spin_sliders[base_slider_idx]->get_value();109}110}111}112113void EditorPropertyVectorN::_store_link(bool p_linked) {114if (!get_edited_object()) {115return;116}117const String key = vformat("%s:%s", get_edited_object()->get_class(), get_edited_property());118EditorSettings::get_singleton()->set_project_metadata("linked_properties", key, p_linked);119}120121void EditorPropertyVectorN::_grab_changed(bool p_grab) {122if (p_grab) {123_update_ratio();124}125is_grabbed = p_grab;126}127128void EditorPropertyVectorN::_notification(int p_what) {129switch (p_what) {130case NOTIFICATION_READY: {131if (linked->is_visible()) {132if (get_edited_object()) {133const String key = vformat("%s:%s", get_edited_object()->get_class(), get_edited_property());134linked->set_pressed_no_signal(EditorSettings::get_singleton()->get_project_metadata("linked_properties", key, true));135_update_ratio();136}137}138} break;139140case NOTIFICATION_THEME_CHANGED: {141int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));142143linked->set_texture_normal(get_editor_theme_icon(SNAME("Unlinked")));144linked->set_texture_pressed(get_editor_theme_icon(SNAME("Instance")));145linked->set_custom_minimum_size(Size2(icon_size + 8 * EDSCALE, 0));146147const Color *colors = _get_property_colors();148for (int i = 0; i < component_count; i++) {149spin_sliders[i]->add_theme_color_override("label_color", colors[i]);150}151} break;152}153}154155void EditorPropertyVectorN::setup(const EditorPropertyRangeHint &p_range_hint, bool p_link, bool p_is_int) {156radians_as_degrees = p_range_hint.radians_as_degrees;157158for (EditorSpinSlider *spin : spin_sliders) {159spin->set_min(p_range_hint.min);160spin->set_max(p_range_hint.max);161spin->set_step(p_range_hint.step);162if (p_range_hint.hide_control) {163spin->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);164}165spin->set_allow_greater(true);166spin->set_allow_lesser(true);167spin->set_suffix(p_range_hint.suffix);168spin->set_editing_integer(p_is_int);169}170171if (!p_link) {172linked->hide();173}174}175176EditorPropertyVectorN::EditorPropertyVectorN(Variant::Type p_type, bool p_force_wide, bool p_horizontal) {177vector_type = p_type;178switch (vector_type) {179case Variant::VECTOR2:180case Variant::VECTOR2I:181component_count = 2;182break;183184case Variant::VECTOR3:185case Variant::VECTOR3I:186component_count = 3;187break;188189case Variant::VECTOR4:190case Variant::VECTOR4I:191component_count = 4;192break;193194default: // Needed to silence a warning.195ERR_PRINT("Not a Vector type.");196break;197}198bool horizontal = p_force_wide || p_horizontal;199200HBoxContainer *hb = memnew(HBoxContainer);201hb->set_h_size_flags(SIZE_EXPAND_FILL);202203BoxContainer *bc;204205if (p_force_wide) {206bc = memnew(HBoxContainer);207hb->add_child(bc);208} else if (horizontal) {209bc = memnew(HBoxContainer);210hb->add_child(bc);211set_bottom_editor(hb);212} else {213bc = memnew(VBoxContainer);214hb->add_child(bc);215}216bc->set_h_size_flags(SIZE_EXPAND_FILL);217218spin_sliders.resize(component_count);219EditorSpinSlider **spin = spin_sliders.ptrw();220221for (int i = 0; i < component_count; i++) {222spin[i] = memnew(EditorSpinSlider);223bc->add_child(spin[i]);224spin[i]->set_flat(true);225spin[i]->set_label(String(COMPONENT_LABELS[i]));226spin[i]->set_accessibility_name(String(COMPONENT_LABELS[i]));227if (horizontal) {228spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);229}230spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyVectorN::_value_changed).bind(String(COMPONENT_LABELS[i])));231spin[i]->connect(SNAME("grabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(true));232spin[i]->connect(SNAME("ungrabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(false));233add_focusable(spin[i]);234}235236ratio.resize(component_count * (component_count - 1));237ratio.fill(1.0);238239linked = memnew(TextureButton);240linked->set_toggle_mode(true);241linked->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);242linked->set_tooltip_text(TTR("Lock/Unlock Component Ratio"));243linked->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyVectorN::_update_ratio));244linked->connect(SceneStringName(toggled), callable_mp(this, &EditorPropertyVectorN::_store_link));245hb->add_child(linked);246247add_child(hb);248if (!horizontal) {249set_label_reference(spin_sliders[0]); // Show text and buttons around this.250}251}252253EditorPropertyVector2::EditorPropertyVector2(bool p_force_wide) :254EditorPropertyVectorN(Variant::VECTOR2, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector2_editing")) {}255256EditorPropertyVector2i::EditorPropertyVector2i(bool p_force_wide) :257EditorPropertyVectorN(Variant::VECTOR2I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector2_editing")) {}258259EditorPropertyVector3::EditorPropertyVector3(bool p_force_wide) :260EditorPropertyVectorN(Variant::VECTOR3, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}261262EditorPropertyVector3i::EditorPropertyVector3i(bool p_force_wide) :263EditorPropertyVectorN(Variant::VECTOR3I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}264265EditorPropertyVector4::EditorPropertyVector4(bool p_force_wide) :266EditorPropertyVectorN(Variant::VECTOR4, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}267268EditorPropertyVector4i::EditorPropertyVector4i(bool p_force_wide) :269EditorPropertyVectorN(Variant::VECTOR4I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}270271272