editor_properties_array_dict.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. void set_array(const Variant &p_array);
  46. Variant get_array();
  47. EditorPropertyArrayObject();
  48. };
  49. class EditorPropertyDictionaryObject : public RefCounted {
  50. GDCLASS(EditorPropertyDictionaryObject, RefCounted);
  51. Variant new_item_key;
  52. Variant new_item_value;
  53. Dictionary dict;
  54. protected:
  55. bool _set(const StringName &p_name, const Variant &p_value);
  56. bool _get(const StringName &p_name, Variant &r_ret) const;
  57. public:
  58. enum {
  59. NEW_KEY_INDEX = -2,
  60. NEW_VALUE_INDEX,
  61. };
  62. void set_dict(const Dictionary &p_dict);
  63. Dictionary get_dict();
  64. void set_new_item_key(const Variant &p_new_item);
  65. Variant get_new_item_key();
  66. void set_new_item_value(const Variant &p_new_item);
  67. Variant get_new_item_value();
  68. String get_label_for_index(int p_index);
  69. String get_property_name_for_index(int p_index);
  70. EditorPropertyDictionaryObject();
  71. };
  72. class EditorPropertyArray : public EditorProperty {
  73. GDCLASS(EditorPropertyArray, EditorProperty);
  74. struct Slot {
  75. Ref<EditorPropertyArrayObject> object;
  76. HBoxContainer *container = nullptr;
  77. int index = -1;
  78. Variant::Type type = Variant::VARIANT_MAX;
  79. bool as_id = false;
  80. EditorProperty *prop = nullptr;
  81. Button *reorder_button = nullptr;
  82. void set_index(int p_idx) {
  83. String prop_name = "indices/" + itos(p_idx);
  84. prop->set_object_and_property(object.ptr(), prop_name);
  85. prop->set_label(itos(p_idx));
  86. index = p_idx;
  87. }
  88. };
  89. PopupMenu *change_type = nullptr;
  90. int page_length = 20;
  91. int page_index = 0;
  92. int changing_type_index;
  93. Button *edit = nullptr;
  94. PanelContainer *container = nullptr;
  95. VBoxContainer *property_vbox = nullptr;
  96. EditorSpinSlider *size_slider = nullptr;
  97. Button *button_add_item = nullptr;
  98. EditorPaginator *paginator = nullptr;
  99. Variant::Type array_type;
  100. Variant::Type subtype;
  101. PropertyHint subtype_hint;
  102. String subtype_hint_string;
  103. LocalVector<Slot> slots;
  104. Slot reorder_slot;
  105. int reorder_to_index = -1;
  106. float reorder_mouse_y_delta = 0.0f;
  107. void initialize_array(Variant &p_array);
  108. void _page_changed(int p_page);
  109. void _reorder_button_gui_input(const Ref<InputEvent> &p_event);
  110. void _reorder_button_down(int p_index);
  111. void _reorder_button_up();
  112. void _create_new_property_slot();
  113. Node *get_base_node();
  114. protected:
  115. Ref<EditorPropertyArrayObject> object;
  116. bool updating = false;
  117. bool dropping = false;
  118. static void _bind_methods();
  119. void _notification(int p_what);
  120. virtual void _add_element();
  121. virtual void _length_changed(double p_page);
  122. virtual void _edit_pressed();
  123. virtual void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
  124. virtual void _change_type(Object *p_button, int p_slot_index);
  125. virtual void _change_type_menu(int p_index);
  126. virtual void _object_id_selected(const StringName &p_property, ObjectID p_id);
  127. virtual void _remove_pressed(int p_index);
  128. virtual void _button_draw();
  129. virtual bool _is_drop_valid(const Dictionary &p_drag_data) const;
  130. virtual bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  131. virtual void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  132. public:
  133. void setup(Variant::Type p_array_type, const String &p_hint_string = "");
  134. virtual void update_property() override;
  135. virtual bool is_colored(ColorationMode p_mode) override;
  136. EditorPropertyArray();
  137. };
  138. class EditorPropertyDictionary : public EditorProperty {
  139. GDCLASS(EditorPropertyDictionary, EditorProperty);
  140. struct Slot {
  141. Ref<EditorPropertyDictionaryObject> object;
  142. HBoxContainer *container = nullptr;
  143. int index = -1;
  144. Variant::Type type = Variant::VARIANT_MAX;
  145. bool as_id = false;
  146. EditorProperty *prop = nullptr;
  147. String prop_name;
  148. void set_index(int p_idx) {
  149. index = p_idx;
  150. prop_name = object->get_property_name_for_index(p_idx);
  151. update_prop_or_index();
  152. }
  153. void set_prop(EditorProperty *p_prop) {
  154. prop->add_sibling(p_prop);
  155. prop->queue_free();
  156. prop = p_prop;
  157. update_prop_or_index();
  158. }
  159. void update_prop_or_index() {
  160. prop->set_object_and_property(object.ptr(), prop_name);
  161. prop->set_label(object->get_label_for_index(index));
  162. }
  163. };
  164. PopupMenu *change_type = nullptr;
  165. bool updating = false;
  166. Ref<EditorPropertyDictionaryObject> object;
  167. int page_length = 20;
  168. int page_index = 0;
  169. int changing_type_index;
  170. Button *edit = nullptr;
  171. PanelContainer *container = nullptr;
  172. VBoxContainer *property_vbox = nullptr;
  173. PanelContainer *add_panel = nullptr;
  174. EditorSpinSlider *size_sliderv = nullptr;
  175. Button *button_add_item = nullptr;
  176. EditorPaginator *paginator = nullptr;
  177. PropertyHint property_hint;
  178. LocalVector<Slot> slots;
  179. void _create_new_property_slot(int p_idx);
  180. void _page_changed(int p_page);
  181. void _edit_pressed();
  182. void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
  183. void _change_type(Object *p_button, int p_slot_index);
  184. void _change_type_menu(int p_index);
  185. void _add_key_value();
  186. void _object_id_selected(const StringName &p_property, ObjectID p_id);
  187. protected:
  188. static void _bind_methods();
  189. void _notification(int p_what);
  190. public:
  191. void setup(PropertyHint p_hint);
  192. virtual void update_property() override;
  193. virtual bool is_colored(ColorationMode p_mode) override;
  194. EditorPropertyDictionary();
  195. };
  196. class EditorPropertyLocalizableString : public EditorProperty {
  197. GDCLASS(EditorPropertyLocalizableString, EditorProperty);
  198. EditorLocaleDialog *locale_select = nullptr;
  199. bool updating;
  200. Ref<EditorPropertyDictionaryObject> object;
  201. int page_length = 20;
  202. int page_index = 0;
  203. Button *edit = nullptr;
  204. MarginContainer *container = nullptr;
  205. VBoxContainer *property_vbox = nullptr;
  206. EditorSpinSlider *size_slider = nullptr;
  207. Button *button_add_item = nullptr;
  208. EditorPaginator *paginator = nullptr;
  209. void _page_changed(int p_page);
  210. void _edit_pressed();
  211. void _remove_item(Object *p_button, int p_index);
  212. void _property_changed(const String &p_property, const Variant &p_value, const String &p_name = "", bool p_changing = false);
  213. void _add_locale_popup();
  214. void _add_locale(const String &p_locale);
  215. void _object_id_selected(const StringName &p_property, ObjectID p_id);
  216. protected:
  217. static void _bind_methods();
  218. void _notification(int p_what);
  219. public:
  220. virtual void update_property() override;
  221. EditorPropertyLocalizableString();
  222. };
  223. #endif // EDITOR_PROPERTIES_ARRAY_DICT_H