editor_properties_array_dict.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. void _notification(int p_what);
  124. virtual void _add_element();
  125. virtual void _length_changed(double p_page);
  126. virtual void _edit_pressed();
  127. virtual void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
  128. virtual void _change_type(Object *p_button, int p_slot_index);
  129. virtual void _change_type_menu(int p_index);
  130. virtual void _object_id_selected(const StringName &p_property, ObjectID p_id);
  131. virtual void _remove_pressed(int p_index);
  132. virtual void _button_draw();
  133. virtual bool _is_drop_valid(const Dictionary &p_drag_data) const;
  134. virtual bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  135. virtual void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  136. public:
  137. void setup(Variant::Type p_array_type, const String &p_hint_string = "");
  138. virtual void update_property() override;
  139. virtual bool is_colored(ColorationMode p_mode) override;
  140. EditorPropertyArray();
  141. };
  142. class EditorPropertyDictionary : public EditorProperty {
  143. GDCLASS(EditorPropertyDictionary, EditorProperty);
  144. struct Slot {
  145. Ref<EditorPropertyDictionaryObject> object;
  146. HBoxContainer *container = nullptr;
  147. int index = -1;
  148. Variant::Type type = Variant::VARIANT_MAX;
  149. bool as_id = false;
  150. EditorProperty *prop = nullptr;
  151. String prop_name;
  152. void set_index(int p_idx) {
  153. index = p_idx;
  154. prop_name = object->get_property_name_for_index(p_idx);
  155. update_prop_or_index();
  156. }
  157. void set_prop(EditorProperty *p_prop) {
  158. prop->add_sibling(p_prop);
  159. prop->queue_free();
  160. prop = p_prop;
  161. update_prop_or_index();
  162. }
  163. void update_prop_or_index() {
  164. prop->set_object_and_property(object.ptr(), prop_name);
  165. prop->set_label(object->get_label_for_index(index));
  166. }
  167. };
  168. PopupMenu *change_type = nullptr;
  169. bool updating = false;
  170. Ref<EditorPropertyDictionaryObject> object;
  171. int page_length = 20;
  172. int page_index = 0;
  173. int changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE;
  174. Button *edit = nullptr;
  175. PanelContainer *container = nullptr;
  176. VBoxContainer *property_vbox = nullptr;
  177. PanelContainer *add_panel = nullptr;
  178. EditorSpinSlider *size_sliderv = nullptr;
  179. Button *button_add_item = nullptr;
  180. EditorPaginator *paginator = nullptr;
  181. LocalVector<Slot> slots;
  182. void _create_new_property_slot(int p_idx);
  183. void _page_changed(int p_page);
  184. void _edit_pressed();
  185. void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
  186. void _change_type(Object *p_button, int p_slot_index);
  187. void _change_type_menu(int p_index);
  188. void _add_key_value();
  189. void _object_id_selected(const StringName &p_property, ObjectID p_id);
  190. void _remove_pressed(int p_slot_index);
  191. Variant::Type key_subtype;
  192. PropertyHint key_subtype_hint;
  193. String key_subtype_hint_string;
  194. Variant::Type value_subtype;
  195. PropertyHint value_subtype_hint;
  196. String value_subtype_hint_string;
  197. void initialize_dictionary(Variant &p_dictionary);
  198. protected:
  199. void _notification(int p_what);
  200. public:
  201. void setup(PropertyHint p_hint, const String &p_hint_string = "");
  202. virtual void update_property() override;
  203. virtual bool is_colored(ColorationMode p_mode) override;
  204. EditorPropertyDictionary();
  205. };
  206. class EditorPropertyLocalizableString : public EditorProperty {
  207. GDCLASS(EditorPropertyLocalizableString, EditorProperty);
  208. EditorLocaleDialog *locale_select = nullptr;
  209. bool updating;
  210. Ref<EditorPropertyDictionaryObject> object;
  211. int page_length = 20;
  212. int page_index = 0;
  213. Button *edit = nullptr;
  214. MarginContainer *container = nullptr;
  215. VBoxContainer *property_vbox = nullptr;
  216. EditorSpinSlider *size_slider = nullptr;
  217. Button *button_add_item = nullptr;
  218. EditorPaginator *paginator = nullptr;
  219. void _page_changed(int p_page);
  220. void _edit_pressed();
  221. void _remove_item(Object *p_button, int p_index);
  222. void _property_changed(const String &p_property, const Variant &p_value, const String &p_name = "", bool p_changing = false);
  223. void _add_locale_popup();
  224. void _add_locale(const String &p_locale);
  225. void _object_id_selected(const StringName &p_property, ObjectID p_id);
  226. protected:
  227. void _notification(int p_what);
  228. public:
  229. virtual void update_property() override;
  230. EditorPropertyLocalizableString();
  231. };
  232. #endif // EDITOR_PROPERTIES_ARRAY_DICT_H