1 /*************************************************************************/
2 /*  property_editor.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 PROPERTY_EDITOR_H
31 #define PROPERTY_EDITOR_H
32 
33 #include "editor/editor_file_dialog.h"
34 #include "scene/gui/button.h"
35 #include "scene/gui/check_box.h"
36 #include "scene/gui/check_button.h"
37 #include "scene/gui/color_picker.h"
38 #include "scene/gui/dialogs.h"
39 #include "scene/gui/label.h"
40 #include "scene/gui/menu_button.h"
41 #include "scene/gui/split_container.h"
42 #include "scene/gui/text_edit.h"
43 #include "scene/gui/texture_frame.h"
44 #include "scene/gui/tree.h"
45 #include "scene_tree_editor.h"
46 
47 /**
48 	@author Juan Linietsky <reduzio@gmail.com>
49 */
50 
51 class PropertyValueEvaluator;
52 
53 class CustomPropertyEditor : public Popup {
54 
55 	OBJ_TYPE(CustomPropertyEditor, Popup);
56 
57 	enum {
58 		MAX_VALUE_EDITORS = 12,
59 		MAX_ACTION_BUTTONS = 5,
60 		OBJ_MENU_LOAD = 0,
61 		OBJ_MENU_EDIT = 1,
62 		OBJ_MENU_CLEAR = 2,
63 		OBJ_MENU_MAKE_UNIQUE = 3,
64 		OBJ_MENU_COPY = 4,
65 		OBJ_MENU_PASTE = 5,
66 		OBJ_MENU_REIMPORT = 6,
67 		OBJ_MENU_NEW_SCRIPT = 7,
68 		OBJ_MENU_SHOW_IN_FILE_SYSTEM = 8,
69 		TYPE_BASE_ID = 100
70 
71 	};
72 
73 	enum {
74 		EASING_LINEAR,
75 		EASING_EASE_IN,
76 		EASING_EASE_OUT,
77 		EASING_ZERO,
78 		EASING_IN_OUT,
79 		EASING_OUT_IN
80 	};
81 
82 	PopupMenu *menu;
83 	SceneTreeDialog *scene_tree;
84 	EditorFileDialog *file;
85 	ConfirmationDialog *error;
86 	String name;
87 	Variant::Type type;
88 	Variant v;
89 	List<String> field_names;
90 	int hint;
91 	String hint_text;
92 	LineEdit *value_editor[MAX_VALUE_EDITORS];
93 	int focused_value_editor;
94 	Label *value_label[MAX_VALUE_EDITORS];
95 	HScrollBar *scroll[4];
96 	Button *action_buttons[MAX_ACTION_BUTTONS];
97 	MenuButton *type_button;
98 	Vector<String> inheritors_array;
99 	TextureFrame *texture_preview;
100 	ColorPicker *color_picker;
101 	TextEdit *text_edit;
102 	bool read_only;
103 	CheckBox *checks20[20];
104 	SpinBox *spinbox;
105 	HSlider *slider;
106 
107 	Control *easing_draw;
108 
109 	Object *owner;
110 
111 	bool updating;
112 
113 	PropertyValueEvaluator *evaluator;
114 
115 	void _text_edit_changed();
116 	void _file_selected(String p_file);
117 	void _scroll_modified(double p_value);
118 	void _modified(String p_string);
119 	void _range_modified(double p_value);
120 	void _focus_enter();
121 	void _focus_exit();
122 	void _action_pressed(int p_which);
123 	void _type_create_selected(int p_idx);
124 
125 	void _color_changed(const Color &p_color);
126 	void _draw_easing();
127 	void _menu_option(int p_which);
128 
129 	void _drag_easing(const InputEvent &p_ev);
130 
131 	void _node_path_selected(NodePath p_path);
132 	void show_value_editors(int p_amount);
133 	void config_value_editors(int p_amount, int p_columns, int p_label_w, const List<String> &p_strings);
134 	void config_action_buttons(const List<String> &p_strings);
135 
136 	void _emit_changed_whole_or_field();
137 
138 protected:
139 	void _notification(int p_what);
140 	static void _bind_methods();
141 
142 public:
143 	void hide_menu();
144 
145 	Variant get_variant() const;
146 	String get_name() const;
147 
set_read_only(bool p_read_only)148 	void set_read_only(bool p_read_only) { read_only = p_read_only; }
149 
set_value_evaluator(PropertyValueEvaluator * p_evaluator)150 	void set_value_evaluator(PropertyValueEvaluator *p_evaluator) { evaluator = p_evaluator; }
151 
152 	bool edit(Object *p_owner, const String &p_name, Variant::Type p_type, const Variant &p_variant, int p_hint, String p_hint_text);
153 
154 	CustomPropertyEditor();
155 };
156 
157 class PropertyEditor : public Control {
158 
159 	OBJ_TYPE(PropertyEditor, Control);
160 
161 	Tree *tree;
162 	Label *top_label;
163 	//Object *object;
164 	LineEdit *search_box;
165 
166 	PropertyValueEvaluator *evaluator;
167 
168 	Object *obj;
169 
170 	Array _prop_edited_name;
171 	StringName _prop_edited;
172 
173 	bool capitalize_paths;
174 	bool changing;
175 	bool update_tree_pending;
176 	bool autoclear;
177 	bool keying;
178 	bool read_only;
179 	bool show_categories;
180 	bool show_type_icons;
181 	float refresh_countdown;
182 	bool use_doc_hints;
183 	bool use_filter;
184 	bool subsection_selectable;
185 
186 	HashMap<String, String> pending;
187 	String selected_property;
188 
189 	Map<StringName, Map<StringName, String> > descr_cache;
190 	Map<StringName, String> class_descr_cache;
191 
192 	CustomPropertyEditor *custom_editor;
193 
194 	void _resource_edit_request();
195 	void _custom_editor_edited();
196 	void _custom_editor_edited_field(const String &p_field_name);
197 	void _custom_editor_request(bool p_arrow);
198 
199 	void _item_selected();
200 	void _item_edited();
201 	TreeItem *get_parent_node(String p_path, HashMap<String, TreeItem *> &item_paths, TreeItem *root);
202 
203 	void set_item_text(TreeItem *p_item, int p_type, const String &p_name, int p_hint = PROPERTY_HINT_NONE, const String &p_hint_text = "");
204 
205 	TreeItem *find_item(TreeItem *p_item, const String &p_name);
206 
207 	virtual void _changed_callback(Object *p_changed, const char *p_what);
208 	virtual void _changed_callbacks(Object *p_changed, const String &p_callback);
209 
210 	void _check_reload_status(const String &p_name, TreeItem *item);
211 
212 	void _edit_button(Object *p_item, int p_column, int p_button);
213 
214 	void _node_removed(Node *p_node);
215 
216 	friend class ProjectExportDialog;
217 	friend class ScriptEditorDebugger;
218 	void _edit_set(const String &p_name, const Variant &p_value, const String &p_changed_field = "");
219 	void _draw_flags(Object *ti, const Rect2 &p_rect);
220 
221 	bool _might_be_in_instance();
222 	bool _get_instanced_node_original_property(const StringName &p_prop, Variant &value);
223 	bool _is_property_different(const Variant &p_current, const Variant &p_orig, int p_usage = 0);
224 
225 	void _refresh_item(TreeItem *p_item);
226 	void _set_range_def(Object *p_item, String prop, float p_frame);
227 
228 	void _filter_changed(const String &p_text);
229 
230 	void _mark_drop_fields(TreeItem *p_at);
231 	void _clear_drop_fields(TreeItem *p_at);
232 
233 	bool _is_drop_valid(const Dictionary &p_drag_data, const Dictionary &p_item_data) const;
234 	Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
235 	bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
236 	void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
237 
238 	void _resource_preview_done(const String &p_path, const Ref<Texture> &p_preview, Variant p_ud);
239 	void _draw_transparency(Object *t, const Rect2 &p_rect);
240 
241 	ObjectID _get_curent_remote_object_id(const StringName &p_name);
242 
243 	UndoRedo *undo_redo;
244 
245 protected:
246 	void _notification(int p_what);
247 	static void _bind_methods();
248 
249 public:
set_undo_redo(UndoRedo * p_undo_redo)250 	void set_undo_redo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; }
251 
252 	String get_selected_path() const;
253 
254 	Tree *get_scene_tree();
255 	Label *get_top_label();
256 	void hide_top_label();
257 	void update_tree();
258 	void update_property(const String &p_prop);
259 
260 	void refresh();
261 
262 	void edit(Object *p_object);
263 
264 	void set_keying(bool p_active);
set_read_only(bool p_read_only)265 	void set_read_only(bool p_read_only) {
266 		read_only = p_read_only;
267 		custom_editor->set_read_only(p_read_only);
268 	}
269 
270 	bool is_capitalize_paths_enabled() const;
271 	void set_enable_capitalize_paths(bool p_capitalize);
272 	void set_autoclear(bool p_enable);
273 
274 	void set_show_categories(bool p_show);
set_use_doc_hints(bool p_enable)275 	void set_use_doc_hints(bool p_enable) { use_doc_hints = p_enable; }
276 
277 	void set_use_filter(bool p_use);
278 	void register_text_enter(Node *p_line_edit);
279 
280 	void set_subsection_selectable(bool p_selectable);
281 
282 	PropertyEditor();
283 	~PropertyEditor();
284 };
285 
286 class SectionedPropertyEditorFilter;
287 
288 class SectionedPropertyEditor : public HBoxContainer {
289 
290 	OBJ_TYPE(SectionedPropertyEditor, HBoxContainer);
291 
292 	ObjectID obj;
293 
294 	ItemList *sections;
295 	SectionedPropertyEditorFilter *filter;
296 	PropertyEditor *editor;
297 
298 	static void _bind_methods();
299 	void _section_selected(int p_which);
300 
301 public:
302 	PropertyEditor *get_property_editor();
303 	void edit(Object *p_object);
304 	String get_full_item_path(const String &p_item);
305 
306 	void set_current_section(const String &p_section);
307 	String get_current_section() const;
308 
309 	void update_category_list();
310 
311 	SectionedPropertyEditor();
312 	~SectionedPropertyEditor();
313 };
314 
315 class PropertyValueEvaluator : public ValueEvaluator {
316 	OBJ_TYPE(PropertyValueEvaluator, ValueEvaluator);
317 
318 	Object *obj;
319 	ScriptLanguage *script_language;
320 	String _build_script(const String &p_text);
321 
_default_eval(const String & p_text)322 	_FORCE_INLINE_ double _default_eval(const String &p_text) {
323 		return p_text.to_double();
324 	}
325 
326 public:
327 	void edit(Object *p_obj);
328 	double eval(const String &p_text);
329 
330 	PropertyValueEvaluator();
331 	~PropertyValueEvaluator();
332 };
333 
334 #endif
335