item_list_editor_plugin.h 9.1 KB

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