node.h
1 /*************************************************************************/
2 /* node.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef NODE_H
30 #define NODE_H
31 
32 #include "object.h"
33 #include "path_db.h"
34 #include "map.h"
35 #include "object_type_db.h"
36 #include "script_language.h"
37 #include "scene/main/scene_main_loop.h"
38 
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 
49  enum PauseMode {
50 
51  PAUSE_MODE_INHERIT,
52  PAUSE_MODE_STOP,
53  PAUSE_MODE_PROCESS
54  };
55 
56 
57  struct Comparator {
58 
59  bool operator()(const Node* p_a, const Node* p_b) const { return p_b->is_greater_than(p_a); }
60  };
61 
62 private:
63 
64  struct GroupData {
65 
66  bool persistent;
67  GroupData() { persistent=false; }
68  };
69 
70  struct Data {
71 
72  String filename;
73  Ref<SceneState> instance_state;
74  Ref<SceneState> inherited_state;
75 
76  HashMap<NodePath,int> editable_instances;
77 
78  Node *parent;
79  Node *owner;
80  Vector<Node*> children; // list of children
81  int pos;
82  int depth;
83  int blocked; // safeguard that throws an error when attempting to modify the tree in a harmful way while being traversed.
84  StringName name;
85  SceneTree *tree;
86  bool inside_tree;
87 #ifdef TOOLS_ENABLED
88  NodePath import_path; //path used when imported, used by scene editors to keep tracking
89 #endif
90 
91  Viewport *viewport;
92 
93 
95  List<Node*>::Element *OW; // owned element
96  List<Node*> owned;
97 
98  PauseMode pause_mode;
99  Node *pause_owner;
100  // variables used to properly sort the node when processing, ignored otherwise
101  //should move all the stuff below to bits
102  bool fixed_process;
103  bool idle_process;
104 
105  bool input;
106  bool unhandled_input;
107  bool unhandled_key_input;
108 
109  bool parent_owned;
110  bool in_constructor;
111  bool use_placeholder;
112 
113 
114  } data;
115 
116 
117  void _print_tree(const Node *p_node);
118 
119  virtual bool _use_builtin_script() const { return true; }
120  Node *_get_node(const NodePath& p_path) const;
121  Node *_get_child_by_name(const StringName& p_name) const;
122 
123 
124 
125  void _validate_child_name(Node *p_name, bool p_force_human_readable=false);
126 
127  void _propagate_reverse_notification(int p_notification);
128  void _propagate_deferred_notification(int p_notification, bool p_reverse);
129  void _propagate_enter_tree();
130  void _propagate_ready();
131  void _propagate_exit_tree();
132  void _propagate_validate_owner();
133  void _print_stray_nodes();
134  void _propagate_pause_owner(Node*p_owner);
135  Array _get_node_and_resource(const NodePath& p_path);
136 
137  void _duplicate_signals(const Node* p_original,Node* p_copy) const;
138  void _duplicate_and_reown(Node* p_new_parent, const Map<Node*,Node*>& p_reown_map) const;
139  Node *_duplicate(bool p_use_instancing) const;
140 
141  Array _get_children() const;
142  Array _get_groups() const;
143 
144 friend class SceneTree;
145 
146  void _set_tree(SceneTree *p_tree);
147 protected:
148 
149  void _block() { data.blocked++; }
150  void _unblock() { data.blocked--; }
151 
152  void _notification(int p_notification);
153 
154  virtual void add_child_notify(Node *p_child);
155  virtual void remove_child_notify(Node *p_child);
156  virtual void move_child_notify(Node *p_child);
157  //void remove_and_delete_child(Node *p_child);
158 
159  void _propagate_replace_owner(Node *p_owner,Node* p_by_owner);
160 
161  static void _bind_methods();
162 
163 friend class SceneState;
164 
165  void _add_child_nocheck(Node* p_child,const StringName& p_name);
166  void _set_owner_nocheck(Node* p_owner);
167  void _set_name_nocheck(const StringName& p_name);
168 
169 public:
170 
171  enum {
172  // you can make your own, but don't use the same numbers as other notifications in other nodes
173  NOTIFICATION_ENTER_TREE=10,
174  NOTIFICATION_EXIT_TREE =11,
175  NOTIFICATION_MOVED_IN_PARENT =12,
176  NOTIFICATION_READY=13,
177  //NOTIFICATION_PARENT_DECONFIGURED =15, - it's confusing, it's going away
178  NOTIFICATION_PAUSED=14,
179  NOTIFICATION_UNPAUSED=15,
180  NOTIFICATION_FIXED_PROCESS = 16,
181  NOTIFICATION_PROCESS = 17,
182  NOTIFICATION_PARENTED=18,
183  NOTIFICATION_UNPARENTED=19,
184  NOTIFICATION_INSTANCED=20,
185  };
186 
187  /* NODE/TREE */
188 
189  StringName get_name() const;
190  void set_name(const String& p_name);
191 
192  void add_child(Node *p_child,bool p_legible_unique_name=false);
193  void remove_child(Node *p_child);
194 
195  int get_child_count() const;
196  Node *get_child(int p_index) const;
197  bool has_node(const NodePath& p_path) const;
198  Node *get_node(const NodePath& p_path) const;
199  Node* find_node(const String& p_mask,bool p_recursive=true,bool p_owned=true) const;
200  bool has_node_and_resource(const NodePath& p_path) const;
201  Node *get_node_and_resource(const NodePath& p_path,RES& r_res) const;
202 
203  Node *get_parent() const;
204  _FORCE_INLINE_ SceneTree *get_tree() const { ERR_FAIL_COND_V( !data.tree, NULL ); return data.tree; }
205 
206  _FORCE_INLINE_ bool is_inside_tree() const { return data.inside_tree; }
207 
208  bool is_a_parent_of(const Node *p_node) const;
209  bool is_greater_than(const Node *p_node) const;
210 
211  NodePath get_path() const;
212  NodePath get_path_to(const Node *p_node) const;
213 
214  void add_to_group(const StringName& p_identifier,bool p_persistent=false);
215  void remove_from_group(const StringName& p_identifier);
216  bool is_in_group(const StringName& p_identifier) const;
217 
218  struct GroupInfo {
219 
220  StringName name;
221  bool persistent;
222  };
223 
224  void get_groups(List<GroupInfo> *p_groups) const;
225 
226  void move_child(Node *p_child,int p_pos);
227  void raise();
228 
229  void set_owner(Node *p_owner);
230  Node *get_owner() const;
231  void get_owned_by(Node *p_by,List<Node*> *p_owned);
232 
233 
234  void remove_and_skip();
235  int get_index() const;
236 
237  void print_tree();
238 
239  void set_filename(const String& p_filename);
240  String get_filename() const;
241 
242  void set_editable_instance(Node* p_node,bool p_editable);
243  bool is_editable_instance(Node* p_node) const;
244  void set_editable_instances(const HashMap<NodePath,int>& p_editable_instances);
245  HashMap<NodePath,int> get_editable_instances() const;
246 
247 
248  /* NOTIFICATIONS */
249 
250  void propagate_notification(int p_notification);
251 
252  /* PROCESSING */
253  void set_fixed_process(bool p_process);
254  float get_fixed_process_delta_time() const;
255  bool is_fixed_processing() const;
256 
257  void set_process(bool p_process);
258  float get_process_delta_time() const;
259  bool is_processing() const;
260 
261 
262  void set_process_input(bool p_enable);
263  bool is_processing_input() const;
264 
265  void set_process_unhandled_input(bool p_enable);
266  bool is_processing_unhandled_input() const;
267 
268  void set_process_unhandled_key_input(bool p_enable);
269  bool is_processing_unhandled_key_input() const;
270 
271  int get_position_in_parent() const;
272 
273  Node *duplicate(bool p_use_instancing=false) const;
274  Node *duplicate_and_reown(const Map<Node*,Node*>& p_reown_map) const;
275 
276  //Node *clone_tree() const;
277 
278  // used by editors, to save what has changed only
279  void set_scene_instance_state(const Ref<SceneState>& p_state);
280  Ref<SceneState> get_scene_instance_state() const;
281 
282  void set_scene_inherited_state(const Ref<SceneState>& p_state);
283  Ref<SceneState> get_scene_inherited_state() const;
284 
285  void set_scene_instance_load_placeholder(bool p_enable);
286  bool get_scene_instance_load_placeholder() const;
287 
288  static Vector<Variant> make_binds(VARIANT_ARG_LIST);
289 
290  void replace_by(Node* p_node,bool p_keep_data=false);
291 
292  void set_pause_mode(PauseMode p_mode);
293  PauseMode get_pause_mode() const;
294  bool can_process() const;
295 
296  static void print_stray_nodes();
297 
298  String validate_child_name(const String& p_name) const;
299 
300  void queue_delete();
301 
302 //shitty hacks for speed
303  static void set_human_readable_collision_renaming(bool p_enabled);
304  static void init_node_hrcr();
305 
306  void force_parent_owned() { data.parent_owned=true; } //hack to avoid duplicate nodes
307 
308 #ifdef TOOLS_ENABLED
309  void set_import_path(const NodePath& p_import_path); //path used when imported, used by scene editors to keep tracking
310  NodePath get_import_path() const;
311 #endif
312 
313  void get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const;
314 
315  void clear_internal_tree_resource_paths();
316 
317  _FORCE_INLINE_ Viewport *get_viewport() const { return data.viewport; }
318 
319  /* CANVAS */
320 
321  Node();
322  ~Node();
323 };
324 
325 
327 
328 
329 #endif
Definition: array.h:38
Definition: viewport.h:75
void set_pause_mode(PauseMode p_mode)
Definition: node.cpp:371
Definition: path_db.h:41
Definition: node.h:42
Definition: set.h:44
Definition: string_db.h:48
Definition: packed_scene.h:36
Definition: list.h:44
Definition: scene_main_loop.h:51
Definition: node.h:57
Definition: map.h:41
Definition: ustring.h:64
Definition: object.h:317
Definition: node.h:218