1 /*************************************************************************/
2 /*  node.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 NODE_H
31 #define NODE_H
32 
33 #include "map.h"
34 #include "object.h"
35 #include "object_type_db.h"
36 #include "path_db.h"
37 #include "scene/main/scene_main_loop.h"
38 #include "script_language.h"
39 
40 class Viewport;
41 class SceneState;
42 class Node : public Object {
43 
44 	OBJ_TYPE(Node, Object);
45 	OBJ_CATEGORY("Nodes");
46 
47 public:
48 	enum PauseMode {
49 
50 		PAUSE_MODE_INHERIT,
51 		PAUSE_MODE_STOP,
52 		PAUSE_MODE_PROCESS
53 	};
54 
55 	enum DuplicateFlags {
56 
57 		DUPLICATE_SIGNALS = 1,
58 		DUPLICATE_GROUPS = 2,
59 		DUPLICATE_SCRIPTS = 4
60 	};
61 
62 	struct Comparator {
63 
operatorComparator64 		bool operator()(const Node *p_a, const Node *p_b) const { return p_b->is_greater_than(p_a); }
65 	};
66 
67 private:
68 	struct GroupData {
69 
70 		bool persistent;
71 		SceneTree::Group *group;
GroupDataGroupData72 		GroupData() { persistent = false; }
73 	};
74 
75 	struct Data {
76 
77 		String filename;
78 		Ref<SceneState> instance_state;
79 		Ref<SceneState> inherited_state;
80 
81 		HashMap<NodePath, int> editable_instances;
82 
83 		Node *parent;
84 		Node *owner;
85 		Vector<Node *> children; // list of children
86 		int pos;
87 		int depth;
88 		int blocked; // safeguard that throws an error when attempting to modify the tree in a harmful way while being traversed.
89 		StringName name;
90 		SceneTree *tree;
91 		bool inside_tree;
92 		bool ready_notified;
93 #ifdef TOOLS_ENABLED
94 		NodePath import_path; //path used when imported, used by scene editors to keep tracking
95 #endif
96 
97 		Viewport *viewport;
98 
99 		Map<StringName, GroupData> grouped;
100 		List<Node *>::Element *OW; // owned element
101 		List<Node *> owned;
102 
103 		PauseMode pause_mode;
104 		Node *pause_owner;
105 		// variables used to properly sort the node when processing, ignored otherwise
106 		//should move all the stuff below to bits
107 		bool fixed_process;
108 		bool idle_process;
109 
110 		bool input;
111 		bool unhandled_input;
112 		bool unhandled_key_input;
113 
114 		bool parent_owned;
115 		bool in_constructor;
116 		bool use_placeholder;
117 
118 		bool display_folded;
119 
120 	} data;
121 
122 	void _print_tree(const Node *p_node);
123 
_use_builtin_script()124 	virtual bool _use_builtin_script() const { return true; }
125 	Node *_get_node(const NodePath &p_path) const;
126 	Node *_get_child_by_name(const StringName &p_name) const;
127 
128 	void _replace_connections_target(Node *p_new_target);
129 
130 	void _validate_child_name(Node *p_name, bool p_force_human_readable = false);
131 
132 	void _propagate_reverse_notification(int p_notification);
133 	void _propagate_deferred_notification(int p_notification, bool p_reverse);
134 	void _propagate_enter_tree();
135 	void _propagate_ready();
136 	void _propagate_exit_tree();
137 	void _propagate_validate_owner();
138 	void _print_stray_nodes();
139 	void _propagate_pause_owner(Node *p_owner);
140 	Array _get_node_and_resource(const NodePath &p_path);
141 
142 	void _duplicate_signals(const Node *p_original, Node *p_copy) const;
143 	void _duplicate_and_reown(Node *p_new_parent, const Map<Node *, Node *> &p_reown_map) const;
144 	Node *_duplicate(bool p_use_instancing, int p_flags) const;
145 
146 	Array _get_children() const;
147 	Array _get_groups() const;
148 
149 	friend class SceneTree;
150 
151 	void _set_tree(SceneTree *p_tree);
152 
153 protected:
_block()154 	void _block() { data.blocked++; }
_unblock()155 	void _unblock() { data.blocked--; }
156 
157 	void _notification(int p_notification);
158 
159 	virtual void add_child_notify(Node *p_child);
160 	virtual void remove_child_notify(Node *p_child);
161 	virtual void move_child_notify(Node *p_child);
162 	//void remove_and_delete_child(Node *p_child);
163 
164 	void _propagate_replace_owner(Node *p_owner, Node *p_by_owner);
165 
166 	static void _bind_methods();
167 
168 	friend class SceneState;
169 
170 	void _add_child_nocheck(Node *p_child, const StringName &p_name);
171 	void _set_owner_nocheck(Node *p_owner);
172 	void _set_name_nocheck(const StringName &p_name);
173 
174 public:
175 	enum {
176 		// you can make your own, but don't use the same numbers as other notifications in other nodes
177 		NOTIFICATION_ENTER_TREE = 10,
178 		NOTIFICATION_EXIT_TREE = 11,
179 		NOTIFICATION_MOVED_IN_PARENT = 12,
180 		NOTIFICATION_READY = 13,
181 		//NOTIFICATION_PARENT_DECONFIGURED =15, - it's confusing, it's going away
182 		NOTIFICATION_PAUSED = 14,
183 		NOTIFICATION_UNPAUSED = 15,
184 		NOTIFICATION_FIXED_PROCESS = 16,
185 		NOTIFICATION_PROCESS = 17,
186 		NOTIFICATION_PARENTED = 18,
187 		NOTIFICATION_UNPARENTED = 19,
188 		NOTIFICATION_INSTANCED = 20,
189 		NOTIFICATION_DRAG_BEGIN = 21,
190 		NOTIFICATION_DRAG_END = 22,
191 	};
192 
193 	/* NODE/TREE */
194 
195 	StringName get_name() const;
196 	void set_name(const String &p_name);
197 
198 	void add_child(Node *p_child, bool p_legible_unique_name = false);
199 	void add_child_below_node(Node *p_node, Node *p_child, bool p_legible_unique_name = false);
200 	void remove_child(Node *p_child);
201 
202 	int get_child_count() const;
203 	Node *get_child(int p_index) const;
204 	bool has_node(const NodePath &p_path) const;
205 	Node *get_node(const NodePath &p_path) const;
206 	Node *find_node(const String &p_mask, bool p_recursive = true, bool p_owned = true) const;
207 	bool has_node_and_resource(const NodePath &p_path) const;
208 	Node *get_node_and_resource(const NodePath &p_path, RES &r_res) const;
209 
210 	Node *get_parent() const;
get_tree()211 	_FORCE_INLINE_ SceneTree *get_tree() const {
212 		ERR_FAIL_COND_V(!data.tree, NULL);
213 		return data.tree;
214 	}
215 
is_inside_tree()216 	_FORCE_INLINE_ bool is_inside_tree() const { return data.inside_tree; }
217 
218 	bool is_a_parent_of(const Node *p_node) const;
219 	bool is_greater_than(const Node *p_node) const;
220 
221 	NodePath get_path() const;
222 	NodePath get_path_to(const Node *p_node) const;
223 	Node *find_common_parent_with(const Node *p_node) const;
224 
225 	void add_to_group(const StringName &p_identifier, bool p_persistent = false);
226 	void remove_from_group(const StringName &p_identifier);
227 	bool is_in_group(const StringName &p_identifier) const;
228 
229 	struct GroupInfo {
230 
231 		StringName name;
232 		bool persistent;
233 	};
234 
235 	void get_groups(List<GroupInfo> *p_groups) const;
236 	bool has_persistent_groups() const;
237 
238 	void move_child(Node *p_child, int p_pos);
239 	void raise();
240 
241 	void set_owner(Node *p_owner);
242 	Node *get_owner() const;
243 	void get_owned_by(Node *p_by, List<Node *> *p_owned);
244 
245 	void remove_and_skip();
246 	int get_index() const;
247 
248 	void print_tree();
249 
250 	void set_filename(const String &p_filename);
251 	String get_filename() const;
252 
253 	void set_editable_instance(Node *p_node, bool p_editable);
254 	bool is_editable_instance(Node *p_node) const;
255 	void set_editable_instances(const HashMap<NodePath, int> &p_editable_instances);
256 	HashMap<NodePath, int> get_editable_instances() const;
257 
258 	/* NOTIFICATIONS */
259 
260 	void propagate_notification(int p_notification);
261 
262 	/* PROCESSING */
263 	void set_fixed_process(bool p_process);
264 	float get_fixed_process_delta_time() const;
265 	bool is_fixed_processing() const;
266 
267 	void set_process(bool p_process);
268 	float get_process_delta_time() const;
269 	bool is_processing() const;
270 
271 	void set_process_input(bool p_enable);
272 	bool is_processing_input() const;
273 
274 	void set_process_unhandled_input(bool p_enable);
275 	bool is_processing_unhandled_input() const;
276 
277 	void set_process_unhandled_key_input(bool p_enable);
278 	bool is_processing_unhandled_key_input() const;
279 
280 	int get_position_in_parent() const;
281 
282 	Node *duplicate(bool p_use_instancing = false, int p_flags = DUPLICATE_GROUPS | DUPLICATE_SIGNALS | DUPLICATE_SCRIPTS) const;
283 	Node *duplicate_and_reown(const Map<Node *, Node *> &p_reown_map) const;
284 
285 	//Node *clone_tree() const;
286 
287 	// used by editors, to save what has changed only
288 	void set_scene_instance_state(const Ref<SceneState> &p_state);
289 	Ref<SceneState> get_scene_instance_state() const;
290 
291 	void set_scene_inherited_state(const Ref<SceneState> &p_state);
292 	Ref<SceneState> get_scene_inherited_state() const;
293 
294 	void set_scene_instance_load_placeholder(bool p_enable);
295 	bool get_scene_instance_load_placeholder() const;
296 
297 	static Vector<Variant> make_binds(VARIANT_ARG_LIST);
298 
299 	void replace_by(Node *p_node, bool p_keep_data = false);
300 
301 	void set_pause_mode(PauseMode p_mode);
302 	PauseMode get_pause_mode() const;
303 	bool can_process() const;
304 
305 	static void print_stray_nodes();
306 
307 	String validate_child_name(const String &p_name) const;
308 
309 	void queue_delete();
310 
311 	//shitty hacks for speed
312 	static void set_human_readable_collision_renaming(bool p_enabled);
313 	static void init_node_hrcr();
314 
force_parent_owned()315 	void force_parent_owned() { data.parent_owned = true; } //hack to avoid duplicate nodes
316 
317 #ifdef TOOLS_ENABLED
318 	void set_import_path(const NodePath &p_import_path); //path used when imported, used by scene editors to keep tracking
319 	NodePath get_import_path() const;
320 #endif
321 
322 	bool is_owned_by_parent() const;
323 
324 	void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
325 
326 	void clear_internal_tree_resource_paths();
327 
get_viewport()328 	_FORCE_INLINE_ Viewport *get_viewport() const { return data.viewport; }
329 
330 	virtual String get_configuration_warning() const;
331 
332 	void update_configuration_warning();
333 
334 	void set_display_folded(bool p_folded);
335 	bool is_displayed_folded() const;
336 	/* CANVAS */
337 
338 	Node();
339 	~Node();
340 };
341 
342 typedef Set<Node *, Node::Comparator> NodeSet;
343 
344 #endif
345