scene_main_loop.h
1 /*************************************************************************/
2 /* scene_main_loop.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 SCENE_MAIN_LOOP_H
30 #define SCENE_MAIN_LOOP_H
31 
32 
33 #include "os/main_loop.h"
34 #include "scene/resources/world.h"
35 #include "scene/resources/world_2d.h"
36 #include "scene/main/scene_singleton.h"
37 #include "os/thread_safe.h"
38 #include "self_list.h"
44 class SceneTree;
45 class PackedScene;
46 class Node;
47 class Viewport;
48 class Material;
49 class Mesh;
50 
51 class SceneTree : public MainLoop {
52 
53  _THREAD_SAFE_CLASS_
54 
55  OBJ_TYPE( SceneTree, MainLoop );
56 public:
57 
58 
59  enum StretchMode {
60 
61  STRETCH_MODE_DISABLED,
62  STRETCH_MODE_2D,
63  STRETCH_MODE_VIEWPORT,
64  };
65 
66  enum StretchAspect {
67 
68  STRETCH_ASPECT_IGNORE,
69  STRETCH_ASPECT_KEEP,
70  STRETCH_ASPECT_KEEP_WIDTH,
71  STRETCH_ASPECT_KEEP_HEIGHT,
72  };
73 private:
74 
75 
76  struct Group {
77 
78  Vector<Node*> nodes;
79  uint64_t last_tree_version;
80  Group() { last_tree_version=0; };
81  };
82 
83  Viewport *root;
84 
85  uint64_t tree_version;
86  float fixed_process_time;
87  float idle_process_time;
88  bool accept_quit;
89  uint32_t last_id;
90 
91  bool editor_hint;
92  bool debug_collisions_hint;
93  bool debug_navigation_hint;
94  bool pause;
95  int root_lock;
96 
97  Map<StringName,Group> group_map;
98  bool _quit;
99  bool initialized;
100  bool input_handled;
101  Size2 last_screen_size;
102  StringName tree_changed_name;
103  StringName node_removed_name;
104 
105 
106  int64_t current_frame;
107  int node_count;
108 
109 #ifdef TOOLS_ENABLED
110  Node *edited_scene_root;
111 #endif
112  struct UGCall {
113 
114  StringName group;
115  StringName call;
116 
117  bool operator<(const UGCall& p_with) const { return group==p_with.group?call<p_with.call:group<p_with.group; }
118  };
119 
120  //safety for when a node is deleted while a group is being called
121  int call_lock;
122  Set<Node*> call_skip; //skip erased nodes
123 
124 
125  StretchMode stretch_mode;
126  StretchAspect stretch_aspect;
127  Size2i stretch_min;
128 
129  void _update_root_rect();
130 
131  List<ObjectID> delete_queue;
132 
133  Map<UGCall,Vector<Variant> > unique_group_calls;
134  bool ugc_locked;
135  void _flush_ugc();
136  void _flush_transform_notifications();
137 
138  void _update_group_order(Group& g);
139  void _update_listener();
140 
141  Array _get_nodes_in_group(const StringName& p_group);
142 
143  Node *current_scene;
144 
145  Color debug_collisions_color;
146  Color debug_collision_contact_color;
147  Color debug_navigation_color;
148  Color debug_navigation_disabled_color;
149  Ref<Mesh> debug_contact_mesh;
150  Ref<Material> navigation_material;
151  Ref<Material> navigation_disabled_material;
152  Ref<Material> collision_material;
153  int collision_debug_contacts;
154 
155  void _change_scene(Node* p_to);
156  //void _call_group(uint32_t p_call_flags,const StringName& p_group,const StringName& p_function,const Variant& p_arg1,const Variant& p_arg2);
157 
158  static SceneTree *singleton;
159 friend class Node;
160 
161  void tree_changed();
162  void node_removed(Node *p_node);
163 
164 
165  void add_to_group(const StringName& p_group, Node *p_node);
166  void remove_from_group(const StringName& p_group, Node *p_node);
167 
168  void _notify_group_pause(const StringName& p_group,int p_notification);
169  void _call_input_pause(const StringName& p_group,const StringName& p_method,const InputEvent& p_input);
170  Variant _call_group(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
171 
172 
173  static void _debugger_request_tree(void *self);
174  void _flush_delete_queue();
175 //optimization
176 friend class CanvasItem;
177 friend class Spatial;
178 friend class Viewport;
179 
180  SelfList<Node>::List xform_change_list;
181 
182 #ifdef DEBUG_ENABLED
183 
184  Map<int,NodePath> live_edit_node_path_cache;
185  Map<int,String> live_edit_resource_cache;
186 
187  NodePath live_edit_root;
188  String live_edit_scene;
189 
190  Map<String,Set<Node*> > live_scene_edit_cache;
191  Map<Node*,Map<ObjectID,Node*> > live_edit_remove_list;
192 
193  ScriptDebugger::LiveEditFuncs live_edit_funcs;
194 
195  void _live_edit_node_path_func(const NodePath &p_path,int p_id) ;
196  void _live_edit_res_path_func(const String &p_path,int p_id) ;
197 
198  void _live_edit_node_set_func(int p_id,const StringName& p_prop,const Variant& p_value) ;
199  void _live_edit_node_set_res_func(int p_id,const StringName& p_prop,const String& p_value) ;
200  void _live_edit_node_call_func(int p_id,const StringName& p_method,VARIANT_ARG_DECLARE) ;
201  void _live_edit_res_set_func(int p_id,const StringName& p_prop,const Variant& p_value) ;
202  void _live_edit_res_set_res_func(int p_id,const StringName& p_prop,const String& p_value) ;
203  void _live_edit_res_call_func(int p_id,const StringName& p_method,VARIANT_ARG_DECLARE) ;
204  void _live_edit_root_func(const NodePath& p_scene_path,const String& p_scene_from) ;
205 
206  void _live_edit_create_node_func(const NodePath& p_parent,const String& p_type,const String& p_name);
207  void _live_edit_instance_node_func(const NodePath& p_parent,const String& p_path,const String& p_name);
208  void _live_edit_remove_node_func(const NodePath& p_at);
209  void _live_edit_remove_and_keep_node_func(const NodePath& p_at,ObjectID p_keep_id);
210  void _live_edit_restore_node_func(ObjectID p_id,const NodePath& p_at,int p_at_pos);
211  void _live_edit_duplicate_node_func(const NodePath& p_at,const String& p_new_name);
212  void _live_edit_reparent_node_func(const NodePath& p_at,const NodePath& p_new_place,const String& p_new_name,int p_at_pos);
213 
214  static void _live_edit_node_path_funcs(void *self,const NodePath &p_path,int p_id) { reinterpret_cast<SceneTree*>(self)->_live_edit_node_path_func(p_path,p_id); }
215  static void _live_edit_res_path_funcs(void *self,const String &p_path,int p_id) { reinterpret_cast<SceneTree*>(self)->_live_edit_res_path_func(p_path,p_id); }
216 
217  static void _live_edit_node_set_funcs(void *self,int p_id,const StringName& p_prop,const Variant& p_value) { reinterpret_cast<SceneTree*>(self)->_live_edit_node_set_func(p_id,p_prop,p_value); }
218  static void _live_edit_node_set_res_funcs(void *self,int p_id,const StringName& p_prop,const String& p_value) { reinterpret_cast<SceneTree*>(self)->_live_edit_node_set_res_func(p_id,p_prop,p_value); }
219  static void _live_edit_node_call_funcs(void *self,int p_id,const StringName& p_method,VARIANT_ARG_DECLARE) { reinterpret_cast<SceneTree*>(self)->_live_edit_node_call_func(p_id,p_method,VARIANT_ARG_PASS); }
220  static void _live_edit_res_set_funcs(void *self,int p_id,const StringName& p_prop,const Variant& p_value) { reinterpret_cast<SceneTree*>(self)->_live_edit_res_set_func(p_id,p_prop,p_value); }
221  static void _live_edit_res_set_res_funcs(void *self,int p_id,const StringName& p_prop,const String& p_value) { reinterpret_cast<SceneTree*>(self)->_live_edit_res_set_res_func(p_id,p_prop,p_value); }
222  static void _live_edit_res_call_funcs(void *self,int p_id,const StringName& p_method,VARIANT_ARG_DECLARE) { reinterpret_cast<SceneTree*>(self)->_live_edit_res_call_func(p_id,p_method,VARIANT_ARG_PASS); }
223  static void _live_edit_root_funcs(void *self, const NodePath& p_scene_path,const String& p_scene_from) { reinterpret_cast<SceneTree*>(self)->_live_edit_root_func(p_scene_path,p_scene_from); }
224 
225  static void _live_edit_create_node_funcs(void* self,const NodePath& p_parent,const String& p_type,const String& p_name) { reinterpret_cast<SceneTree*>(self)->_live_edit_create_node_func(p_parent,p_type,p_name); }
226  static void _live_edit_instance_node_funcs(void* self,const NodePath& p_parent,const String& p_path,const String& p_name) { reinterpret_cast<SceneTree*>(self)->_live_edit_instance_node_func(p_parent,p_path,p_name); }
227  static void _live_edit_remove_node_funcs(void* self,const NodePath& p_at) { reinterpret_cast<SceneTree*>(self)->_live_edit_remove_node_func(p_at); }
228  static void _live_edit_remove_and_keep_node_funcs(void* self,const NodePath& p_at,ObjectID p_keep_id) { reinterpret_cast<SceneTree*>(self)->_live_edit_remove_and_keep_node_func(p_at,p_keep_id); }
229  static void _live_edit_restore_node_funcs(void* self,ObjectID p_id,const NodePath& p_at,int p_at_pos) { reinterpret_cast<SceneTree*>(self)->_live_edit_restore_node_func(p_id,p_at,p_at_pos); }
230  static void _live_edit_duplicate_node_funcs(void* self,const NodePath& p_at,const String& p_new_name) { reinterpret_cast<SceneTree*>(self)->_live_edit_duplicate_node_func(p_at,p_new_name); }
231  static void _live_edit_reparent_node_funcs(void* self,const NodePath& p_at,const NodePath& p_new_place,const String& p_new_name,int p_at_pos) { reinterpret_cast<SceneTree*>(self)->_live_edit_reparent_node_func(p_at,p_new_place,p_new_name,p_at_pos); }
232 
233 #endif
234 protected:
235 
236  void _notification(int p_notification);
237  static void _bind_methods();
238 
239 public:
240 
241  enum {
242  NOTIFICATION_TRANSFORM_CHANGED=29
243  };
244 
245  enum CallGroupFlags {
246  GROUP_CALL_DEFAULT=0,
247  GROUP_CALL_REVERSE=1,
248  GROUP_CALL_REALTIME=2,
249  GROUP_CALL_UNIQUE=4,
250  GROUP_CALL_MULIILEVEL=8,
251  };
252 
253  _FORCE_INLINE_ Viewport *get_root() const { return root; }
254 
255  uint32_t get_last_event_id() const;
256 
257  void call_group(uint32_t p_call_flags,const StringName& p_group,const StringName& p_function,VARIANT_ARG_LIST);
258  void notify_group(uint32_t p_call_flags,const StringName& p_group,int p_notification);
259  void set_group(uint32_t p_call_flags,const StringName& p_group,const String& p_name,const Variant& p_value);
260 
261 
262  virtual void input_text( const String& p_text );
263  virtual void input_event( const InputEvent& p_event );
264  virtual void init();
265 
266  virtual bool iteration(float p_time);
267  virtual bool idle(float p_time);
268 
269  virtual void finish();
270 
271  void set_auto_accept_quit(bool p_enable);
272 
273  void quit();
274 
275  void set_input_as_handled();
276  _FORCE_INLINE_ float get_fixed_process_time() const { return fixed_process_time; }
277  _FORCE_INLINE_ float get_idle_process_time() const { return idle_process_time; }
278 
279  void set_editor_hint(bool p_enabled);
280  bool is_editor_hint() const;
281 
282  bool is_node_being_edited(const Node* p_node) const;
283 
284  void set_pause(bool p_enabled);
285  bool is_paused() const;
286 
287  void set_camera(const RID& p_camera);
288  RID get_camera() const;
289 
290  void set_debug_collisions_hint(bool p_enabled);
291  bool is_debugging_collisions_hint() const;
292 
293  void set_debug_navigation_hint(bool p_enabled);
294  bool is_debugging_navigation_hint() const;
295 
296  void set_debug_collisions_color(const Color& p_color);
297  Color get_debug_collisions_color() const;
298 
299  void set_debug_collision_contact_color(const Color& p_color);
300  Color get_debug_collision_contact_color() const;
301 
302  void set_debug_navigation_color(const Color& p_color);
303  Color get_debug_navigation_color() const;
304 
305  void set_debug_navigation_disabled_color(const Color& p_color);
306  Color get_debug_navigation_disabled_color() const;
307 
308 
309  Ref<Material> get_debug_navigation_material();
310  Ref<Material> get_debug_navigation_disabled_material();
311  Ref<Material> get_debug_collision_material();
312  Ref<Mesh> get_debug_contact_mesh();
313 
314  int get_collision_debug_contact_count() { return collision_debug_contacts; }
315 
316  int64_t get_frame() const;
317 
318  int get_node_count() const;
319 
320  void queue_delete(Object *p_object);
321 
322  void get_nodes_in_group(const StringName& p_group,List<Node*> *p_list);
323  bool has_group(const StringName& p_identifier) const;
324 
325 
326  void set_screen_stretch(StretchMode p_mode,StretchAspect p_aspect,const Size2 p_minsize);
327 
328  //void change_scene(const String& p_path);
329  //Node *get_loaded_scene();
330 
331 #ifdef TOOLS_ENABLED
332  void set_edited_scene_root(Node *p_node);
333  Node *get_edited_scene_root() const;
334 #endif
335 
336  void set_current_scene(Node* p_scene);
337  Node* get_current_scene() const;
338  Error change_scene(const String& p_path);
339  Error change_scene_to(const Ref<PackedScene>& p_scene);
340  Error reload_current_scene();
341 
342  //used by Main::start, don't use otherwise
343  void add_current_scene(Node * p_current);
344 
345  static SceneTree* get_singleton() { return singleton; }
346 
347 
348  SceneTree();
349  ~SceneTree();
350 
351 };
352 
353 
354 VARIANT_ENUM_CAST( SceneTree::StretchMode );
355 VARIANT_ENUM_CAST( SceneTree::StretchAspect );
356 
357 
358 
359 #endif
Definition: mesh.h:40
Definition: array.h:38
Definition: self_list.h:35
Definition: main_loop.h:38
Definition: viewport.h:75
Definition: variant.h:74
Definition: path_db.h:41
Definition: node.h:42
Definition: math_2d.h:369
Definition: color.h:37
Definition: string_db.h:48
Definition: canvas_item.h:87
Definition: input_event.h:263
Definition: scene_main_loop.h:51
Definition: material.h:42
Definition: rid.h:47
Definition: packed_scene.h:187
Definition: math_2d.h:65
Definition: script_language.h:255
Definition: ustring.h:64
Definition: object.h:317
Definition: spatial.h:56
Definition: variant.h:379