editor_properties_array_dict.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**************************************************************************/
  2. /* editor_properties_array_dict.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef EDITOR_PROPERTIES_ARRAY_DICT_H
  31. #define EDITOR_PROPERTIES_ARRAY_DICT_H
  32. #include "editor/editor_inspector.h"
  33. #include "editor/editor_locale_dialog.h"
  34. #include "editor/filesystem_dock.h"
  35. class Button;
  36. class EditorSpinSlider;
  37. class MarginContainer;
  38. class EditorPropertyArrayObject : public RefCounted {
  39. GDCLASS(EditorPropertyArrayObject, RefCounted);
  40. Variant array;
  41. protected:
  42. bool _set(const StringName &p_name, const Variant &p_value);
  43. bool _get(const StringName &p_name, Variant &r_ret) const;
  44. public:
  45. enum {
  46. NOT_CHANGING_TYPE = -1,
  47. };
  48. void set_array(const Variant &p_array);
  49. Variant get_array();
  50. EditorPropertyArrayObject();
  51. };
  52. class EditorPropertyDictionaryObject : public RefCounted {
  53. GDCLASS(EditorPropertyDictionaryObject, RefCounted);
  54. Variant new_item_key;
  55. Variant new_item_value;
  56. Dictionary dict;
  57. protected:
  58. bool _set(const StringName &p_name, const Variant &p_value);
  59. bool _get(const StringName &p_name, Variant &r_ret) const;
  60. public:
  61. enum {
  62. NOT_CHANGING_TYPE = -3,
  63. NEW_KEY_INDEX,
  64. NEW_VALUE_INDEX,
  65. };
  66. bool get_by_property_name(const String &p_name, Variant &r_ret) const;
  67. void set_dict(const Dictionary &p_dict);
  68. Dictionary get_dict();
  69. void set_new_item_key(const Variant &p_new_item);
  70. Variant get_new_item_key();
  71. void set_new_item_value(const Variant &p_new_item);
  72. Variant get_new_item_value();
  73. String get_label_for_index(int p_index);
  74. String get_property_name_for_index(int p_index);
  75. EditorPropertyDictionaryObject();
  76. };
  77. class EditorPropertyArray : public EditorProperty {
  78. GDCLASS(EditorPropertyArray, EditorProperty);
  79. struct Slot {
  80. Ref<EditorPropertyArrayObject> object;
  81. HBoxContainer *container = nullptr;
  82. int index = -1;
  83. Variant::Type type = Variant::VARIANT_MAX;
  84. bool as_id = false;
  85. EditorProperty *prop = nullptr;
  86. Button *reorder_button = nullptr;
  87. void set_index(int p_idx) {
  88. String prop_name = "indices/" + itos(p_idx);
  89. prop->set_object_and_property(object.ptr(), prop_name);
  90. prop->set_label(itos(p_idx));
  91. index = p_idx;
  92. }
  93. };
  94. PopupMenu *change_type = nullptr;
  95. int page_length = 20;
  96. int page_index = 0;
  97. int changing_type_index = EditorPropertyArrayObject::NOT_CHANGING_TYPE;
  98. Button *edit = nullptr;
  99. PanelContainer *container = nullptr;
  100. VBoxContainer *property_vbox = nullptr;
  101. EditorSpinSlider *size_slider = nullptr;
  102. Button *button_add_item = nullptr;
  103. EditorPaginator *paginator = nullptr;
  104. Variant::Type array_type;
  105. Variant::Type subtype;
  106. PropertyHint subtype_hint;
  107. String subtype_hint_string;
  108. LocalVector<Slot> slots;
  109. Slot reorder_slot;
  110. int reorder_to_index = -1;
  111. float reorder_mouse_y_delta = 0.0f;
  112. void initialize_array(Variant &p_array);
  113. void _page_changed(int p_page);
  114. void _reorder_button_gui_input(const Ref<InputEvent> &p_event);
  115. void _reorder_button_down(int p_index);
  116. void _reorder_button_up();
  117. void _create_new_property_slot();
  118. Node *get_base_node();
  119. protected:
  120. Ref<EditorPropertyArrayObject> object;
  121. bool updating = false;
  122. bool dropping = false;
  123. static void _bind_methods();
  124. void _notification(int p_what);
  125. virtual void _add_element();
  126. virtual void _length_changed(double p_page);
  127. virtual void _edit_pressed();
  128. virtual void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
  129. virtual void _change_type(Object *p_button, int p_slot_index);
  130. virtual void _change_type_menu(int p_index);
  131. virtual void _object_id_selected(const StringName &p_property, ObjectID p_id);
  132. virtual void _remove_pressed(int p_index);
  133. virtual void _button_draw();
  134. virtual bool _is_drop_valid(const Dictionary &p_drag_data) const;
  135. virtual bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  136. virtual void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  137. public:
  138. void setup(Variant::Type p_array_type, const String &p_hint_string = "");
  139. virtual void update_property() override;
  140. virtual bool is_colored(ColorationMode p_mode) override;
  141. EditorPropertyArray();
  142. };
  143. class EditorPropertyDictionary : public EditorProperty {
  144. GDCLASS(EditorPropertyDictionary, EditorProperty);
  145. struct Slot {
  146. Ref<EditorPropertyDictionaryObject> object;
  147. HBoxContainer *container = nullptr;
  148. int index = -1;
  149. Variant::Type type = Variant::VARIANT_MAX;
  150. bool as_id = false;
  151. EditorProperty *prop = nullptr;
  152. String prop_name;
  153. void set_index(int p_idx) {
  154. index = p_idx;
  155. prop_name = object->get_property_name_for_index(p_idx);
  156. update_prop_or_index();
  157. }
  158. void set_prop(EditorProperty *p_prop) {
  159. prop->add_sibling(p_prop);
  160. prop->queue_free();
  161. prop = p_prop;
  162. update_prop_or_index();
  163. }
  164. void update_prop_or_index() {
  165. prop->set_object_and_property(object.ptr(), prop_name);
  166. prop->set_label(object->get_label_for_index(index));
  167. }
  168. };
  169. PopupMenu *change_type = nullptr;
  170. bool updating = false;
  171. Ref<EditorPropertyDictionaryObject> object;
  172. int page_length = 20;
  173. int page_index = 0;
  174. int changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE;
  175. Button *edit = nullptr;
  176. PanelContainer *container = nullptr;
  177. VBoxContainer *property_vbox = nullptr;
  178. PanelContainer *add_panel = nullptr;
  179. EditorSpinSlider *size_sliderv = nullptr;
  180. Button *button_add_item = nullptr;
  181. EditorPaginator *paginator = nullptr;
  182. PropertyHint property_hint;
  183. LocalVector<Slot> slots;
  184. void _create_new_property_slot(int p_idx);
  185. void _page_changed(int p_page);
  186. void _edit_pressed();
  187. void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
  188. void _change_type(Object *p_button, int p_slot_index);
  189. void _change_type_menu(int p_index);
  190. void _add_key_value();
  191. void _object_id_selected(const StringName &p_property, ObjectID p_id);
  192. protected:
  193. static void _bind_methods();
  194. void _notification(int p_what);
  195. public:
  196. void setup(PropertyHint p_hint);
  197. virtual void update_property() override;
  198. virtual bool is_colored(ColorationMode p_mode) override;
  199. EditorPropertyDictionary();
  200. };
  201. class EditorPropertyLocalizableString : public EditorProperty {
  202. GDCLASS(EditorPropertyLocalizableString, EditorProperty);
  203. EditorLocaleDialog *locale_select = nullptr;
  204. bool updating;
  205. Ref<EditorPropertyDictionaryObject> object;
  206. int page_length = 20;
  207. int page_index = 0;
  208. Button *edit = nullptr;
  209. MarginContainer *container = nullptr;
  210. VBoxContainer *property_vbox = nullptr;
  211. EditorSpinSlider *size_slider = nullptr;
  212. Button *button_add_item = nullptr;
  213. EditorPaginator *paginator = nullptr;
  214. void _page_changed(int p_page);
  215. void _edit_pressed();
  216. void _remove_item(Object *p_button, int p_index);
  217. void _property_changed(const String &p_property, const Variant &p_value, const String &p_name = "", bool p_changing = false);
  218. void _add_locale_popup();
  219. void _add_locale(const String &p_locale);
  220. void _object_id_selected(const StringName &p_property, ObjectID p_id);
  221. protected:
  222. static void _bind_methods();
  223. void _notification(int p_what);
  224. public:
  225. virtual void update_property() override;
  226. EditorPropertyLocalizableString();
  227. };
  228. #endif // EDITOR_PROPERTIES_ARRAY_DICT_H