Path: blob/master/editor/inspector/editor_properties_array_dict.h
9902 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;102103void set_index(int p_idx) {104String prop_name = "indices/" + itos(p_idx);105prop->set_object_and_property(object.ptr(), prop_name);106prop->set_label(itos(p_idx));107index = p_idx;108}109};110111EditorVariantTypePopupMenu *change_type = nullptr;112113bool preview_value = false;114int page_length = 20;115int page_index = 0;116int changing_type_index = EditorPropertyArrayObject::NOT_CHANGING_TYPE;117Button *edit = nullptr;118PanelContainer *container = nullptr;119VBoxContainer *property_vbox = nullptr;120EditorSpinSlider *size_slider = nullptr;121Button *button_add_item = nullptr;122EditorPaginator *paginator = nullptr;123Variant::Type array_type;124Variant::Type subtype;125PropertyHint subtype_hint;126String subtype_hint_string;127LocalVector<Slot> slots;128129Slot reorder_slot;130int reorder_to_index = -1;131float reorder_mouse_y_delta = 0.0f;132void initialize_array(Variant &p_array);133134void _page_changed(int p_page);135136void _reorder_button_gui_input(const Ref<InputEvent> &p_event);137void _reorder_button_down(int p_index);138void _reorder_button_up();139void _create_new_property_slot();140141Node *get_base_node();142143protected:144Ref<EditorPropertyArrayObject> object;145146bool updating = false;147bool dropping = false;148149void _notification(int p_what);150151virtual void _add_element();152virtual void _length_changed(double p_page);153virtual void _edit_pressed();154virtual void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);155virtual void _change_type(Object *p_button, int p_slot_index);156virtual void _change_type_menu(int p_index);157158virtual void _object_id_selected(const StringName &p_property, ObjectID p_id);159virtual void _remove_pressed(int p_index);160161virtual void _button_draw();162virtual void _button_add_item_draw();163virtual bool _is_drop_valid(const Dictionary &p_drag_data) const;164virtual bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;165virtual void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);166167public:168void setup(Variant::Type p_array_type, const String &p_hint_string = "");169void set_preview_value(bool p_preview_value);170virtual void update_property() override;171virtual bool is_colored(ColorationMode p_mode) override;172EditorPropertyArray();173};174175class EditorPropertyDictionary : public EditorProperty {176GDCLASS(EditorPropertyDictionary, EditorProperty);177178struct Slot {179Ref<EditorPropertyDictionaryObject> object;180HBoxContainer *container = nullptr;181int index = -1;182Variant::Type type = Variant::VARIANT_MAX;183Variant::Type key_type = Variant::VARIANT_MAX;184bool as_id = false;185bool key_as_id = false;186EditorProperty *prop = nullptr;187EditorProperty *prop_key = nullptr;188String prop_name;189String key_name;190191void set_index(int p_idx) {192index = p_idx;193prop_name = object->get_property_name_for_index(p_idx);194key_name = object->get_key_name_for_index(p_idx);195update_prop_or_index();196}197198void set_prop(EditorProperty *p_prop) {199prop->add_sibling(p_prop);200prop->queue_free();201prop = p_prop;202update_prop_or_index();203}204205void set_key_prop(EditorProperty *p_prop) {206if (prop_key) {207prop_key->add_sibling(p_prop);208prop_key->queue_free();209prop_key = p_prop;210update_prop_or_index();211}212}213214void update_prop_or_index() {215prop->set_object_and_property(object.ptr(), prop_name);216if (prop_key) {217prop_key->set_object_and_property(object.ptr(), key_name);218} else {219prop->set_label(object->get_label_for_index(index));220}221}222};223224EditorVariantTypePopupMenu *change_type = nullptr;225bool updating = false;226227bool preview_value = false;228Ref<EditorPropertyDictionaryObject> object;229int page_length = 20;230int page_index = 0;231int changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE;232Button *edit = nullptr;233PanelContainer *container = nullptr;234VBoxContainer *property_vbox = nullptr;235PanelContainer *add_panel = nullptr;236EditorSpinSlider *size_sliderv = nullptr;237Button *button_add_item = nullptr;238EditorPaginator *paginator = nullptr;239LocalVector<Slot> slots;240void _create_new_property_slot(int p_idx);241242void _page_changed(int p_page);243void _edit_pressed();244void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);245void _change_type(Object *p_button, int p_slot_index);246void _change_type_menu(int p_index);247248void _add_key_value();249void _object_id_selected(const StringName &p_property, ObjectID p_id);250void _remove_pressed(int p_slot_index);251252Variant::Type key_subtype;253PropertyHint key_subtype_hint;254String key_subtype_hint_string;255Variant::Type value_subtype;256PropertyHint value_subtype_hint;257String value_subtype_hint_string;258void initialize_dictionary(Variant &p_dictionary);259260protected:261void _notification(int p_what);262263public:264void setup(PropertyHint p_hint, const String &p_hint_string = "");265void set_preview_value(bool p_preview_value);266virtual void update_property() override;267virtual bool is_colored(ColorationMode p_mode) override;268EditorPropertyDictionary();269};270271class EditorPropertyLocalizableString : public EditorProperty {272GDCLASS(EditorPropertyLocalizableString, EditorProperty);273274EditorLocaleDialog *locale_select = nullptr;275276bool updating;277278Ref<EditorPropertyDictionaryObject> object;279int page_length = 20;280int page_index = 0;281Button *edit = nullptr;282MarginContainer *container = nullptr;283VBoxContainer *property_vbox = nullptr;284EditorSpinSlider *size_slider = nullptr;285Button *button_add_item = nullptr;286EditorPaginator *paginator = nullptr;287288void _page_changed(int p_page);289void _edit_pressed();290void _remove_item(Object *p_button, int p_index);291void _property_changed(const String &p_property, const Variant &p_value, const String &p_name = "", bool p_changing = false);292293void _add_locale_popup();294void _add_locale(const String &p_locale);295void _object_id_selected(const StringName &p_property, ObjectID p_id);296297public:298virtual void update_property() override;299EditorPropertyLocalizableString();300};301302303