item_list_editor_plugin.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*************************************************************************/
  2. /* item_list_editor_plugin.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 ITEM_LIST_EDITOR_PLUGIN_H
  31. #define ITEM_LIST_EDITOR_PLUGIN_H
  32. #include "canvas_item_editor_plugin.h"
  33. #include "editor/editor_inspector.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_plugin.h"
  36. #include "scene/gui/menu_button.h"
  37. #include "scene/gui/option_button.h"
  38. #include "scene/gui/popup_menu.h"
  39. /**
  40. @author Juan Linietsky <reduzio@gmail.com>
  41. */
  42. class ItemListPlugin : public Object {
  43. GDCLASS(ItemListPlugin, Object);
  44. protected:
  45. bool _set(const StringName &p_name, const Variant &p_value);
  46. bool _get(const StringName &p_name, Variant &r_ret) const;
  47. void _get_property_list(List<PropertyInfo> *p_list) const;
  48. public:
  49. enum Flags {
  50. FLAG_ICON = 1,
  51. FLAG_CHECKABLE = 2,
  52. FLAG_ID = 4,
  53. FLAG_ENABLE = 8,
  54. FLAG_SEPARATOR = 16
  55. };
  56. virtual void set_object(Object *p_object) = 0;
  57. virtual bool handles(Object *p_object) const = 0;
  58. virtual int get_flags() const = 0;
  59. virtual void set_item_text(int p_idx, const String &p_text) {}
  60. virtual String get_item_text(int p_idx) const { return ""; };
  61. virtual void set_item_icon(int p_idx, const Ref<Texture> &p_tex) {}
  62. virtual Ref<Texture> get_item_icon(int p_idx) const { return Ref<Texture>(); };
  63. virtual void set_item_checkable(int p_idx, bool p_check) {}
  64. virtual void set_item_radio_checkable(int p_idx, bool p_check) {}
  65. virtual bool is_item_checkable(int p_idx) const { return false; };
  66. virtual bool is_item_radio_checkable(int p_idx) const { return false; };
  67. virtual void set_item_checked(int p_idx, bool p_checked) {}
  68. virtual bool is_item_checked(int p_idx) const { return false; };
  69. virtual void set_item_enabled(int p_idx, int p_enabled) {}
  70. virtual bool is_item_enabled(int p_idx) const { return false; };
  71. virtual void set_item_id(int p_idx, int p_id) {}
  72. virtual int get_item_id(int p_idx) const { return -1; };
  73. virtual void set_item_separator(int p_idx, bool p_separator) {}
  74. virtual bool is_item_separator(int p_idx) const { return false; };
  75. virtual void add_item() = 0;
  76. virtual int get_item_count() const = 0;
  77. virtual void erase(int p_idx) = 0;
  78. ItemListPlugin() {}
  79. };
  80. ///////////////////////////////////////////////////////////////
  81. class ItemListOptionButtonPlugin : public ItemListPlugin {
  82. GDCLASS(ItemListOptionButtonPlugin, ItemListPlugin);
  83. OptionButton *ob;
  84. public:
  85. virtual void set_object(Object *p_object);
  86. virtual bool handles(Object *p_object) const;
  87. virtual int get_flags() const;
  88. virtual void set_item_text(int p_idx, const String &p_text) { ob->set_item_text(p_idx, p_text); }
  89. virtual String get_item_text(int p_idx) const { return ob->get_item_text(p_idx); }
  90. virtual void set_item_icon(int p_idx, const Ref<Texture> &p_tex) { ob->set_item_icon(p_idx, p_tex); }
  91. virtual Ref<Texture> get_item_icon(int p_idx) const { return ob->get_item_icon(p_idx); }
  92. virtual void set_item_enabled(int p_idx, int p_enabled) { ob->set_item_disabled(p_idx, !p_enabled); }
  93. virtual bool is_item_enabled(int p_idx) const { return !ob->is_item_disabled(p_idx); }
  94. virtual void set_item_id(int p_idx, int p_id) { ob->set_item_id(p_idx, p_id); }
  95. virtual int get_item_id(int p_idx) const { return ob->get_item_id(p_idx); }
  96. virtual void add_item();
  97. virtual int get_item_count() const;
  98. virtual void erase(int p_idx);
  99. ItemListOptionButtonPlugin();
  100. };
  101. class ItemListPopupMenuPlugin : public ItemListPlugin {
  102. GDCLASS(ItemListPopupMenuPlugin, ItemListPlugin);
  103. PopupMenu *pp;
  104. public:
  105. virtual void set_object(Object *p_object);
  106. virtual bool handles(Object *p_object) const;
  107. virtual int get_flags() const;
  108. virtual void set_item_text(int p_idx, const String &p_text) { pp->set_item_text(p_idx, p_text); }
  109. virtual String get_item_text(int p_idx) const { return pp->get_item_text(p_idx); }
  110. virtual void set_item_icon(int p_idx, const Ref<Texture> &p_tex) { pp->set_item_icon(p_idx, p_tex); }
  111. virtual Ref<Texture> get_item_icon(int p_idx) const { return pp->get_item_icon(p_idx); }
  112. virtual void set_item_checkable(int p_idx, bool p_check) { pp->set_item_as_checkable(p_idx, p_check); }
  113. virtual void set_item_radio_checkable(int p_idx, bool p_check) { pp->set_item_as_radio_checkable(p_idx, p_check); }
  114. virtual bool is_item_checkable(int p_idx) const { return pp->is_item_checkable(p_idx); }
  115. virtual bool is_item_radio_checkable(int p_idx) const { return pp->is_item_radio_checkable(p_idx); }
  116. virtual void set_item_checked(int p_idx, bool p_checked) { pp->set_item_checked(p_idx, p_checked); }
  117. virtual bool is_item_checked(int p_idx) const { return pp->is_item_checked(p_idx); }
  118. virtual void set_item_enabled(int p_idx, int p_enabled) { pp->set_item_disabled(p_idx, !p_enabled); }
  119. virtual bool is_item_enabled(int p_idx) const { return !pp->is_item_disabled(p_idx); }
  120. virtual void set_item_id(int p_idx, int p_id) { pp->set_item_id(p_idx, p_idx); }
  121. virtual int get_item_id(int p_idx) const { return pp->get_item_id(p_idx); }
  122. virtual void set_item_separator(int p_idx, bool p_separator) { pp->set_item_as_separator(p_idx, p_separator); }
  123. virtual bool is_item_separator(int p_idx) const { return pp->is_item_separator(p_idx); }
  124. virtual void add_item();
  125. virtual int get_item_count() const;
  126. virtual void erase(int p_idx);
  127. ItemListPopupMenuPlugin();
  128. };
  129. ///////////////////////////////////////////////////////////////
  130. class ItemListItemListPlugin : public ItemListPlugin {
  131. GDCLASS(ItemListItemListPlugin, ItemListPlugin);
  132. ItemList *pp;
  133. public:
  134. virtual void set_object(Object *p_object);
  135. virtual bool handles(Object *p_object) const;
  136. virtual int get_flags() const;
  137. virtual void set_item_text(int p_idx, const String &p_text) { pp->set_item_text(p_idx, p_text); }
  138. virtual String get_item_text(int p_idx) const { return pp->get_item_text(p_idx); }
  139. virtual void set_item_icon(int p_idx, const Ref<Texture> &p_tex) { pp->set_item_icon(p_idx, p_tex); }
  140. virtual Ref<Texture> get_item_icon(int p_idx) const { return pp->get_item_icon(p_idx); }
  141. virtual void set_item_enabled(int p_idx, int p_enabled) { pp->set_item_disabled(p_idx, !p_enabled); }
  142. virtual bool is_item_enabled(int p_idx) const { return !pp->is_item_disabled(p_idx); }
  143. virtual void add_item();
  144. virtual int get_item_count() const;
  145. virtual void erase(int p_idx);
  146. ItemListItemListPlugin();
  147. };
  148. ///////////////////////////////////////////////////////////////
  149. class ItemListEditor : public HBoxContainer {
  150. GDCLASS(ItemListEditor, HBoxContainer);
  151. Node *item_list;
  152. ToolButton *toolbar_button;
  153. AcceptDialog *dialog;
  154. EditorInspector *property_editor;
  155. Tree *tree;
  156. Button *add_button;
  157. Button *del_button;
  158. int selected_idx;
  159. Vector<ItemListPlugin *> item_plugins;
  160. void _edit_items();
  161. void _add_pressed();
  162. void _delete_pressed();
  163. void _node_removed(Node *p_node);
  164. protected:
  165. void _notification(int p_notification);
  166. static void _bind_methods();
  167. public:
  168. void edit(Node *p_item_list);
  169. bool handles(Object *p_object) const;
  170. void add_plugin(ItemListPlugin *p_plugin) { item_plugins.push_back(p_plugin); }
  171. ItemListEditor();
  172. ~ItemListEditor();
  173. };
  174. class ItemListEditorPlugin : public EditorPlugin {
  175. GDCLASS(ItemListEditorPlugin, EditorPlugin);
  176. ItemListEditor *item_list_editor;
  177. EditorNode *editor;
  178. public:
  179. virtual String get_name() const { return "ItemList"; }
  180. bool has_main_screen() const { return false; }
  181. virtual void edit(Object *p_object);
  182. virtual bool handles(Object *p_object) const;
  183. virtual void make_visible(bool p_visible);
  184. ItemListEditorPlugin(EditorNode *p_node);
  185. ~ItemListEditorPlugin();
  186. };
  187. #endif // ITEM_LIST_EDITOR_PLUGIN_H