Path: blob/master/editor/inspector/editor_properties_vector.cpp
9896 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(double p_min, double p_max, double p_step, bool p_hide_slider, bool p_link, const String &p_suffix, bool p_radians_as_degrees, bool p_is_int) {156radians_as_degrees = p_radians_as_degrees;157158for (EditorSpinSlider *spin : spin_sliders) {159spin->set_min(p_min);160spin->set_max(p_max);161spin->set_step(p_step);162spin->set_hide_slider(p_hide_slider);163spin->set_allow_greater(true);164spin->set_allow_lesser(true);165spin->set_suffix(p_suffix);166spin->set_editing_integer(p_is_int);167}168169if (!p_link) {170linked->hide();171}172}173174EditorPropertyVectorN::EditorPropertyVectorN(Variant::Type p_type, bool p_force_wide, bool p_horizontal) {175vector_type = p_type;176switch (vector_type) {177case Variant::VECTOR2:178case Variant::VECTOR2I:179component_count = 2;180break;181182case Variant::VECTOR3:183case Variant::VECTOR3I:184component_count = 3;185break;186187case Variant::VECTOR4:188case Variant::VECTOR4I:189component_count = 4;190break;191192default: // Needed to silence a warning.193ERR_PRINT("Not a Vector type.");194break;195}196bool horizontal = p_force_wide || p_horizontal;197198HBoxContainer *hb = memnew(HBoxContainer);199hb->set_h_size_flags(SIZE_EXPAND_FILL);200201BoxContainer *bc;202203if (p_force_wide) {204bc = memnew(HBoxContainer);205hb->add_child(bc);206} else if (horizontal) {207bc = memnew(HBoxContainer);208hb->add_child(bc);209set_bottom_editor(hb);210} else {211bc = memnew(VBoxContainer);212hb->add_child(bc);213}214bc->set_h_size_flags(SIZE_EXPAND_FILL);215216spin_sliders.resize(component_count);217EditorSpinSlider **spin = spin_sliders.ptrw();218219for (int i = 0; i < component_count; i++) {220spin[i] = memnew(EditorSpinSlider);221bc->add_child(spin[i]);222spin[i]->set_flat(true);223spin[i]->set_label(String(COMPONENT_LABELS[i]));224spin[i]->set_accessibility_name(String(COMPONENT_LABELS[i]));225if (horizontal) {226spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);227}228spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyVectorN::_value_changed).bind(String(COMPONENT_LABELS[i])));229spin[i]->connect(SNAME("grabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(true));230spin[i]->connect(SNAME("ungrabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(false));231add_focusable(spin[i]);232}233234ratio.resize(component_count * (component_count - 1));235ratio.fill(1.0);236237linked = memnew(TextureButton);238linked->set_toggle_mode(true);239linked->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);240linked->set_tooltip_text(TTR("Lock/Unlock Component Ratio"));241linked->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyVectorN::_update_ratio));242linked->connect(SceneStringName(toggled), callable_mp(this, &EditorPropertyVectorN::_store_link));243hb->add_child(linked);244245add_child(hb);246if (!horizontal) {247set_label_reference(spin_sliders[0]); // Show text and buttons around this.248}249}250251EditorPropertyVector2::EditorPropertyVector2(bool p_force_wide) :252EditorPropertyVectorN(Variant::VECTOR2, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector2_editing")) {}253254EditorPropertyVector2i::EditorPropertyVector2i(bool p_force_wide) :255EditorPropertyVectorN(Variant::VECTOR2I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector2_editing")) {}256257EditorPropertyVector3::EditorPropertyVector3(bool p_force_wide) :258EditorPropertyVectorN(Variant::VECTOR3, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}259260EditorPropertyVector3i::EditorPropertyVector3i(bool p_force_wide) :261EditorPropertyVectorN(Variant::VECTOR3I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}262263EditorPropertyVector4::EditorPropertyVector4(bool p_force_wide) :264EditorPropertyVectorN(Variant::VECTOR4, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}265266EditorPropertyVector4i::EditorPropertyVector4i(bool p_force_wide) :267EditorPropertyVectorN(Variant::VECTOR4I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}268269270