Path: blob/master/editor/inspector/editor_properties_array_dict.h
21423 views
/**************************************************************************/1/* editor_properties_array_dict.h */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#pragma once3132#include "editor/inspector/editor_inspector.h"33#include "editor/translations/editor_locale_dialog.h"3435class Button;36class EditorSpinSlider;37class EditorVariantTypePopupMenu;38class MarginContainer;3940class EditorPropertyArrayObject : public RefCounted {41GDCLASS(EditorPropertyArrayObject, RefCounted);4243Variant array;4445protected:46bool _set(const StringName &p_name, const Variant &p_value);47bool _get(const StringName &p_name, Variant &r_ret) const;4849public:50enum {51NOT_CHANGING_TYPE = -1,52};5354void set_array(const Variant &p_array);55Variant get_array();56};5758class EditorPropertyDictionaryObject : public RefCounted {59GDCLASS(EditorPropertyDictionaryObject, RefCounted);6061Variant new_item_key;62Variant new_item_value;63Dictionary dict;6465protected:66bool _set(const StringName &p_name, const Variant &p_value);67bool _get(const StringName &p_name, Variant &r_ret) const;6869public:70enum {71NOT_CHANGING_TYPE = -3,72NEW_KEY_INDEX,73NEW_VALUE_INDEX,74};7576bool get_by_property_name(const String &p_name, Variant &r_ret) const;77void set_dict(const Dictionary &p_dict);78Dictionary get_dict();7980void set_new_item_key(const Variant &p_new_item);81Variant get_new_item_key();8283void set_new_item_value(const Variant &p_new_item);84Variant get_new_item_value();8586String get_label_for_index(int p_index);87String get_property_name_for_index(int p_index);88String get_key_name_for_index(int p_index);89};9091class EditorPropertyArray : public EditorProperty {92GDCLASS(EditorPropertyArray, EditorProperty);9394struct Slot {95Ref<EditorPropertyArrayObject> object;96HBoxContainer *container = nullptr;97int index = -1;98Variant::Type type = Variant::VARIANT_MAX;99bool as_id = false;100EditorProperty *prop = nullptr;101Button *reorder_button = nullptr;102Button *edit_button = nullptr;103Button *remove_button = nullptr;104105void set_index(int p_idx) {106String prop_name = "indices/" + itos(p_idx);107prop->set_object_and_property(object.ptr(), prop_name);108prop->set_label(itos(p_idx));109index = p_idx;110}111};112113EditorVariantTypePopupMenu *change_type = nullptr;114115bool preview_value = false;116int page_length = 20;117int page_index = 0;118int changing_type_index = EditorPropertyArrayObject::NOT_CHANGING_TYPE;119Button *edit = nullptr;120PanelContainer *container = nullptr;121VBoxContainer *property_vbox = nullptr;122EditorSpinSlider *size_slider = nullptr;123Button *button_add_item = nullptr;124EditorPaginator *paginator = nullptr;125Variant::Type array_type;126Variant::Type subtype;127PropertyHint subtype_hint;128String subtype_hint_string;129LocalVector<Slot> slots;130131Slot reorder_slot;132int reorder_to_index = -1;133float reorder_mouse_y_delta = 0.0f;134void initialize_array(Variant &p_array);135136void _page_changed(int p_page);137138void _reorder_button_gui_input(const Ref<InputEvent> &p_event);139void _reorder_button_down(int p_index);140void _reorder_button_up();141void _create_new_property_slot();142143void _resource_selected(const String &p_path, Ref<Resource> p_resource);144145Node *get_base_node();146147protected:148Ref<EditorPropertyArrayObject> object;149150bool updating = false;151bool dropping = false;152153void _notification(int p_what);154155virtual void _add_element();156virtual void _length_changed(double p_page);157virtual void _edit_pressed();158virtual void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);159virtual void _change_type(Object *p_button, int p_slot_index);160virtual void _change_type_menu(int p_index);161162virtual void _object_id_selected(const StringName &p_property, ObjectID p_id);163virtual void _remove_pressed(int p_index);164165virtual void _button_draw();166virtual void _button_add_item_draw();167virtual bool _is_drop_valid(const Dictionary &p_drag_data) const;168virtual bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;169virtual void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);170171public:172void setup(Variant::Type p_array_type, const String &p_hint_string = "");173void set_preview_value(bool p_preview_value);174virtual void update_property() override;175virtual bool is_colored(ColorationMode p_mode) override;176EditorPropertyArray();177};178179class EditorPropertyDictionary : public EditorProperty {180GDCLASS(EditorPropertyDictionary, EditorProperty);181182struct Slot {183Ref<EditorPropertyDictionaryObject> object;184HBoxContainer *container = nullptr;185int index = -1;186Variant::Type type = Variant::VARIANT_MAX;187Variant::Type key_type = Variant::VARIANT_MAX;188bool as_id = false;189bool key_as_id = false;190EditorProperty *prop = nullptr;191EditorProperty *prop_key = nullptr;192Button *edit_button = nullptr;193Button *remove_button = nullptr;194String prop_name;195String key_name;196197void set_index(int p_idx) {198index = p_idx;199prop_name = object->get_property_name_for_index(p_idx);200key_name = object->get_key_name_for_index(p_idx);201update_prop_or_index();202}203204void set_prop(EditorProperty *p_prop) {205prop->add_sibling(p_prop);206prop->queue_free();207prop = p_prop;208update_prop_or_index();209}210211void set_key_prop(EditorProperty *p_prop) {212if (prop_key) {213prop_key->add_sibling(p_prop);214prop_key->queue_free();215prop_key = p_prop;216update_prop_or_index();217}218}219220void update_prop_or_index() {221prop->set_object_and_property(object.ptr(), prop_name);222if (prop_key) {223prop_key->set_object_and_property(object.ptr(), key_name);224} else {225prop->set_label(object->get_label_for_index(index));226}227}228};229230EditorVariantTypePopupMenu *change_type = nullptr;231bool updating = false;232233bool preview_value = false;234Ref<EditorPropertyDictionaryObject> object;235int page_length = 20;236int page_index = 0;237int changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE;238Button *edit = nullptr;239PanelContainer *container = nullptr;240VBoxContainer *property_vbox = nullptr;241PanelContainer *add_panel = nullptr;242Button *button_add_item = nullptr;243EditorPaginator *paginator = nullptr;244LocalVector<Slot> slots;245void _create_new_property_slot(int p_idx);246247void _page_changed(int p_page);248void _edit_pressed();249void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);250void _resource_selected(const String &p_path, Ref<Resource> p_resource);251void _change_type(Object *p_button, int p_slot_index);252void _change_type_menu(int p_index);253254void _add_key_value();255void _object_id_selected(const StringName &p_property, ObjectID p_id);256void _remove_pressed(int p_slot_index);257258Variant::Type key_subtype;259PropertyHint key_subtype_hint;260String key_subtype_hint_string;261Variant::Type value_subtype;262PropertyHint value_subtype_hint;263String value_subtype_hint_string;264void initialize_dictionary(Variant &p_dictionary);265266protected:267void _notification(int p_what);268269public:270void setup(PropertyHint p_hint, const String &p_hint_string = "");271void set_preview_value(bool p_preview_value);272virtual void update_property() override;273virtual bool is_colored(ColorationMode p_mode) override;274EditorPropertyDictionary();275};276277class EditorPropertyLocalizableString : public EditorProperty {278GDCLASS(EditorPropertyLocalizableString, EditorProperty);279280EditorLocaleDialog *locale_select = nullptr;281282bool updating;283284Ref<EditorPropertyDictionaryObject> object;285int page_length = 20;286int page_index = 0;287Button *edit = nullptr;288MarginContainer *container = nullptr;289VBoxContainer *property_vbox = nullptr;290EditorSpinSlider *size_slider = nullptr;291Button *button_add_item = nullptr;292EditorPaginator *paginator = nullptr;293294void _page_changed(int p_page);295void _edit_pressed();296void _remove_item(Object *p_button, int p_index);297void _property_changed(const String &p_property, const Variant &p_value, const String &p_name = "", bool p_changing = false);298299void _add_locale_popup();300void _add_locale(const String &p_locale);301void _object_id_selected(const StringName &p_property, ObjectID p_id);302303public:304virtual void update_property() override;305EditorPropertyLocalizableString();306};307308309