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-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #ifndef ITEM_LIST_EDITOR_PLUGIN_H
32 #define ITEM_LIST_EDITOR_PLUGIN_H
33 
34 #include "canvas_item_editor_plugin.h"
35 #include "editor/editor_inspector.h"
36 #include "editor/editor_node.h"
37 #include "editor/editor_plugin.h"
38 #include "scene/gui/menu_button.h"
39 #include "scene/gui/option_button.h"
40 #include "scene/gui/popup_menu.h"
41 
42 class ItemListPlugin : public Object {
43 
44 	GDCLASS(ItemListPlugin, Object);
45 
46 protected:
47 	bool _set(const StringName &p_name, const Variant &p_value);
48 	bool _get(const StringName &p_name, Variant &r_ret) const;
49 	void _get_property_list(List<PropertyInfo> *p_list) const;
50 
51 public:
52 	enum Flags {
53 
54 		FLAG_ICON = 1,
55 		FLAG_CHECKABLE = 2,
56 		FLAG_ID = 4,
57 		FLAG_ENABLE = 8,
58 		FLAG_SEPARATOR = 16
59 	};
60 
61 	virtual void set_object(Object *p_object) = 0;
62 	virtual bool handles(Object *p_object) const = 0;
63 
64 	virtual int get_flags() const = 0;
65 
set_item_text(int p_idx,const String & p_text)66 	virtual void set_item_text(int p_idx, const String &p_text) {}
get_item_text(int p_idx)67 	virtual String get_item_text(int p_idx) const { return ""; };
68 
set_item_icon(int p_idx,const Ref<Texture> & p_tex)69 	virtual void set_item_icon(int p_idx, const Ref<Texture> &p_tex) {}
get_item_icon(int p_idx)70 	virtual Ref<Texture> get_item_icon(int p_idx) const { return Ref<Texture>(); };
71 
set_item_checkable(int p_idx,bool p_check)72 	virtual void set_item_checkable(int p_idx, bool p_check) {}
set_item_radio_checkable(int p_idx,bool p_check)73 	virtual void set_item_radio_checkable(int p_idx, bool p_check) {}
is_item_checkable(int p_idx)74 	virtual bool is_item_checkable(int p_idx) const { return false; };
is_item_radio_checkable(int p_idx)75 	virtual bool is_item_radio_checkable(int p_idx) const { return false; };
76 
set_item_checked(int p_idx,bool p_checked)77 	virtual void set_item_checked(int p_idx, bool p_checked) {}
is_item_checked(int p_idx)78 	virtual bool is_item_checked(int p_idx) const { return false; };
79 
set_item_enabled(int p_idx,int p_enabled)80 	virtual void set_item_enabled(int p_idx, int p_enabled) {}
is_item_enabled(int p_idx)81 	virtual bool is_item_enabled(int p_idx) const { return false; };
82 
set_item_id(int p_idx,int p_id)83 	virtual void set_item_id(int p_idx, int p_id) {}
get_item_id(int p_idx)84 	virtual int get_item_id(int p_idx) const { return -1; };
85 
set_item_separator(int p_idx,bool p_separator)86 	virtual void set_item_separator(int p_idx, bool p_separator) {}
is_item_separator(int p_idx)87 	virtual bool is_item_separator(int p_idx) const { return false; };
88 
89 	virtual void add_item() = 0;
90 	virtual int get_item_count() const = 0;
91 	virtual void erase(int p_idx) = 0;
92 
ItemListPlugin()93 	ItemListPlugin() {}
94 };
95 
96 ///////////////////////////////////////////////////////////////
97 
98 class ItemListOptionButtonPlugin : public ItemListPlugin {
99 
100 	GDCLASS(ItemListOptionButtonPlugin, ItemListPlugin);
101 
102 	OptionButton *ob;
103 
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 
set_item_text(int p_idx,const String & p_text)109 	virtual void set_item_text(int p_idx, const String &p_text) { ob->set_item_text(p_idx, p_text); }
get_item_text(int p_idx)110 	virtual String get_item_text(int p_idx) const { return ob->get_item_text(p_idx); }
111 
set_item_icon(int p_idx,const Ref<Texture> & p_tex)112 	virtual void set_item_icon(int p_idx, const Ref<Texture> &p_tex) { ob->set_item_icon(p_idx, p_tex); }
get_item_icon(int p_idx)113 	virtual Ref<Texture> get_item_icon(int p_idx) const { return ob->get_item_icon(p_idx); }
114 
set_item_enabled(int p_idx,int p_enabled)115 	virtual void set_item_enabled(int p_idx, int p_enabled) { ob->set_item_disabled(p_idx, !p_enabled); }
is_item_enabled(int p_idx)116 	virtual bool is_item_enabled(int p_idx) const { return !ob->is_item_disabled(p_idx); }
117 
set_item_id(int p_idx,int p_id)118 	virtual void set_item_id(int p_idx, int p_id) { ob->set_item_id(p_idx, p_id); }
get_item_id(int p_idx)119 	virtual int get_item_id(int p_idx) const { return ob->get_item_id(p_idx); }
120 
121 	virtual void add_item();
122 	virtual int get_item_count() const;
123 	virtual void erase(int p_idx);
124 
125 	ItemListOptionButtonPlugin();
126 };
127 
128 class ItemListPopupMenuPlugin : public ItemListPlugin {
129 
130 	GDCLASS(ItemListPopupMenuPlugin, ItemListPlugin);
131 
132 	PopupMenu *pp;
133 
134 public:
135 	virtual void set_object(Object *p_object);
136 	virtual bool handles(Object *p_object) const;
137 	virtual int get_flags() const;
138 
set_item_text(int p_idx,const String & p_text)139 	virtual void set_item_text(int p_idx, const String &p_text) { pp->set_item_text(p_idx, p_text); }
get_item_text(int p_idx)140 	virtual String get_item_text(int p_idx) const { return pp->get_item_text(p_idx); }
141 
set_item_icon(int p_idx,const Ref<Texture> & p_tex)142 	virtual void set_item_icon(int p_idx, const Ref<Texture> &p_tex) { pp->set_item_icon(p_idx, p_tex); }
get_item_icon(int p_idx)143 	virtual Ref<Texture> get_item_icon(int p_idx) const { return pp->get_item_icon(p_idx); }
144 
set_item_checkable(int p_idx,bool p_check)145 	virtual void set_item_checkable(int p_idx, bool p_check) { pp->set_item_as_checkable(p_idx, p_check); }
set_item_radio_checkable(int p_idx,bool p_check)146 	virtual void set_item_radio_checkable(int p_idx, bool p_check) { pp->set_item_as_radio_checkable(p_idx, p_check); }
is_item_checkable(int p_idx)147 	virtual bool is_item_checkable(int p_idx) const { return pp->is_item_checkable(p_idx); }
is_item_radio_checkable(int p_idx)148 	virtual bool is_item_radio_checkable(int p_idx) const { return pp->is_item_radio_checkable(p_idx); }
149 
set_item_checked(int p_idx,bool p_checked)150 	virtual void set_item_checked(int p_idx, bool p_checked) { pp->set_item_checked(p_idx, p_checked); }
is_item_checked(int p_idx)151 	virtual bool is_item_checked(int p_idx) const { return pp->is_item_checked(p_idx); }
152 
set_item_enabled(int p_idx,int p_enabled)153 	virtual void set_item_enabled(int p_idx, int p_enabled) { pp->set_item_disabled(p_idx, !p_enabled); }
is_item_enabled(int p_idx)154 	virtual bool is_item_enabled(int p_idx) const { return !pp->is_item_disabled(p_idx); }
155 
set_item_id(int p_idx,int p_id)156 	virtual void set_item_id(int p_idx, int p_id) { pp->set_item_id(p_idx, p_id); }
get_item_id(int p_idx)157 	virtual int get_item_id(int p_idx) const { return pp->get_item_id(p_idx); }
158 
set_item_separator(int p_idx,bool p_separator)159 	virtual void set_item_separator(int p_idx, bool p_separator) { pp->set_item_as_separator(p_idx, p_separator); }
is_item_separator(int p_idx)160 	virtual bool is_item_separator(int p_idx) const { return pp->is_item_separator(p_idx); }
161 
162 	virtual void add_item();
163 	virtual int get_item_count() const;
164 	virtual void erase(int p_idx);
165 
166 	ItemListPopupMenuPlugin();
167 };
168 
169 ///////////////////////////////////////////////////////////////
170 
171 class ItemListItemListPlugin : public ItemListPlugin {
172 
173 	GDCLASS(ItemListItemListPlugin, ItemListPlugin);
174 
175 	ItemList *pp;
176 
177 public:
178 	virtual void set_object(Object *p_object);
179 	virtual bool handles(Object *p_object) const;
180 	virtual int get_flags() const;
181 
set_item_text(int p_idx,const String & p_text)182 	virtual void set_item_text(int p_idx, const String &p_text) { pp->set_item_text(p_idx, p_text); }
get_item_text(int p_idx)183 	virtual String get_item_text(int p_idx) const { return pp->get_item_text(p_idx); }
184 
set_item_icon(int p_idx,const Ref<Texture> & p_tex)185 	virtual void set_item_icon(int p_idx, const Ref<Texture> &p_tex) { pp->set_item_icon(p_idx, p_tex); }
get_item_icon(int p_idx)186 	virtual Ref<Texture> get_item_icon(int p_idx) const { return pp->get_item_icon(p_idx); }
187 
set_item_enabled(int p_idx,int p_enabled)188 	virtual void set_item_enabled(int p_idx, int p_enabled) { pp->set_item_disabled(p_idx, !p_enabled); }
is_item_enabled(int p_idx)189 	virtual bool is_item_enabled(int p_idx) const { return !pp->is_item_disabled(p_idx); }
190 
191 	virtual void add_item();
192 	virtual int get_item_count() const;
193 	virtual void erase(int p_idx);
194 
195 	ItemListItemListPlugin();
196 };
197 
198 ///////////////////////////////////////////////////////////////
199 
200 class ItemListEditor : public HBoxContainer {
201 
202 	GDCLASS(ItemListEditor, HBoxContainer);
203 
204 	Node *item_list;
205 
206 	ToolButton *toolbar_button;
207 
208 	AcceptDialog *dialog;
209 	EditorInspector *property_editor;
210 	Tree *tree;
211 	Button *add_button;
212 	Button *del_button;
213 
214 	int selected_idx;
215 
216 	Vector<ItemListPlugin *> item_plugins;
217 
218 	void _edit_items();
219 
220 	void _add_pressed();
221 	void _delete_pressed();
222 
223 	void _node_removed(Node *p_node);
224 
225 protected:
226 	void _notification(int p_notification);
227 	static void _bind_methods();
228 
229 public:
230 	void edit(Node *p_item_list);
231 	bool handles(Object *p_object) const;
add_plugin(ItemListPlugin * p_plugin)232 	void add_plugin(ItemListPlugin *p_plugin) { item_plugins.push_back(p_plugin); }
233 	ItemListEditor();
234 	~ItemListEditor();
235 };
236 
237 class ItemListEditorPlugin : public EditorPlugin {
238 
239 	GDCLASS(ItemListEditorPlugin, EditorPlugin);
240 
241 	ItemListEditor *item_list_editor;
242 	EditorNode *editor;
243 
244 public:
get_name()245 	virtual String get_name() const { return "ItemList"; }
has_main_screen()246 	bool has_main_screen() const { return false; }
247 	virtual void edit(Object *p_object);
248 	virtual bool handles(Object *p_object) const;
249 	virtual void make_visible(bool p_visible);
250 
251 	ItemListEditorPlugin(EditorNode *p_node);
252 	~ItemListEditorPlugin();
253 };
254 
255 #endif // ITEM_LIST_EDITOR_PLUGIN_H
256