scene_main_loop.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. #include "os/main_loop.h"
  32. #include "scene/resources/world.h"
  33. #include "scene/resources/world_2d.h"
  34. #include "scene/main/scene_singleton.h"
  35. #include "os/thread_safe.h"
  36. #include "self_list.h"
  37. /**
  38. @author Juan Linietsky <reduzio@gmail.com>
  39. */
  40. class SceneTree;
  41. class PackedScene;
  42. class Node;
  43. class Viewport;
  44. class Material;
  45. class Mesh;
  46. class SceneTree : public MainLoop {
  47. _THREAD_SAFE_CLASS_
  48. OBJ_TYPE( SceneTree, MainLoop );
  49. public:
  50. enum StretchMode {
  51. STRETCH_MODE_DISABLED,
  52. STRETCH_MODE_2D,
  53. STRETCH_MODE_VIEWPORT,
  54. };
  55. enum StretchAspect {
  56. STRETCH_ASPECT_IGNORE,
  57. STRETCH_ASPECT_KEEP,
  58. STRETCH_ASPECT_KEEP_WIDTH,
  59. STRETCH_ASPECT_KEEP_HEIGHT,
  60. };
  61. private:
  62. struct Group {
  63. Vector<Node*> nodes;
  64. uint64_t last_tree_version;
  65. Group() { last_tree_version=0; };
  66. };
  67. Viewport *root;
  68. uint64_t tree_version;
  69. float fixed_process_time;
  70. float idle_process_time;
  71. bool accept_quit;
  72. uint32_t last_id;
  73. bool editor_hint;
  74. bool debug_collisions_hint;
  75. bool debug_navigation_hint;
  76. bool pause;
  77. int root_lock;
  78. Map<StringName,Group> group_map;
  79. bool _quit;
  80. bool initialized;
  81. bool input_handled;
  82. Size2 last_screen_size;
  83. StringName tree_changed_name;
  84. StringName node_removed_name;
  85. int64_t current_frame;
  86. int node_count;
  87. #ifdef TOOLS_ENABLED
  88. Node *edited_scene_root;
  89. #endif
  90. struct UGCall {
  91. StringName group;
  92. StringName call;
  93. bool operator<(const UGCall& p_with) const { return group==p_with.group?call<p_with.call:group<p_with.group; }
  94. };
  95. //safety for when a node is deleted while a group is being called
  96. int call_lock;
  97. Set<Node*> call_skip; //skip erased nodes
  98. StretchMode stretch_mode;
  99. StretchAspect stretch_aspect;
  100. Size2i stretch_min;
  101. void _update_root_rect();
  102. List<ObjectID> delete_queue;
  103. Map<UGCall,Vector<Variant> > unique_group_calls;
  104. bool ugc_locked;
  105. void _flush_ugc();
  106. void _flush_transform_notifications();
  107. void _update_group_order(Group& g);
  108. void _update_listener();
  109. Array _get_nodes_in_group(const StringName& p_group);
  110. Node *current_scene;
  111. Color debug_collisions_color;
  112. Color debug_collision_contact_color;
  113. Color debug_navigation_color;
  114. Color debug_navigation_disabled_color;
  115. Ref<Mesh> debug_contact_mesh;
  116. Ref<Material> navigation_material;
  117. Ref<Material> navigation_disabled_material;
  118. Ref<Material> collision_material;
  119. int collision_debug_contacts;
  120. void _change_scene(Node* p_to);
  121. //void _call_group(uint32_t p_call_flags,const StringName& p_group,const StringName& p_function,const Variant& p_arg1,const Variant& p_arg2);
  122. static SceneTree *singleton;
  123. friend class Node;
  124. void tree_changed();
  125. void node_removed(Node *p_node);
  126. void add_to_group(const StringName& p_group, Node *p_node);
  127. void remove_from_group(const StringName& p_group, Node *p_node);
  128. void _notify_group_pause(const StringName& p_group,int p_notification);
  129. void _call_input_pause(const StringName& p_group,const StringName& p_method,const InputEvent& p_input);
  130. Variant _call_group(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
  131. static void _debugger_request_tree(void *self);
  132. void _flush_delete_queue();
  133. //optimization
  134. friend class CanvasItem;
  135. friend class Spatial;
  136. friend class Viewport;
  137. SelfList<Node>::List xform_change_list;
  138. #ifdef DEBUG_ENABLED
  139. Map<int,NodePath> live_edit_node_path_cache;
  140. Map<int,String> live_edit_resource_cache;
  141. NodePath live_edit_root;
  142. String live_edit_scene;
  143. Map<String,Set<Node*> > live_scene_edit_cache;
  144. Map<Node*,Map<ObjectID,Node*> > live_edit_remove_list;
  145. ScriptDebugger::LiveEditFuncs live_edit_funcs;
  146. void _live_edit_node_path_func(const NodePath &p_path,int p_id) ;
  147. void _live_edit_res_path_func(const String &p_path,int p_id) ;
  148. void _live_edit_node_set_func(int p_id,const StringName& p_prop,const Variant& p_value) ;
  149. void _live_edit_node_set_res_func(int p_id,const StringName& p_prop,const String& p_value) ;
  150. void _live_edit_node_call_func(int p_id,const StringName& p_method,VARIANT_ARG_DECLARE) ;
  151. void _live_edit_res_set_func(int p_id,const StringName& p_prop,const Variant& p_value) ;
  152. void _live_edit_res_set_res_func(int p_id,const StringName& p_prop,const String& p_value) ;
  153. void _live_edit_res_call_func(int p_id,const StringName& p_method,VARIANT_ARG_DECLARE) ;
  154. void _live_edit_root_func(const NodePath& p_scene_path,const String& p_scene_from) ;
  155. void _live_edit_create_node_func(const NodePath& p_parent,const String& p_type,const String& p_name);
  156. void _live_edit_instance_node_func(const NodePath& p_parent,const String& p_path,const String& p_name);
  157. void _live_edit_remove_node_func(const NodePath& p_at);
  158. void _live_edit_remove_and_keep_node_func(const NodePath& p_at,ObjectID p_keep_id);
  159. void _live_edit_restore_node_func(ObjectID p_id,const NodePath& p_at,int p_at_pos);
  160. void _live_edit_duplicate_node_func(const NodePath& p_at,const String& p_new_name);
  161. void _live_edit_reparent_node_func(const NodePath& p_at,const NodePath& p_new_place,const String& p_new_name,int p_at_pos);
  162. 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); }
  163. 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); }
  164. 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); }
  165. 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); }
  166. 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); }
  167. 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); }
  168. 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); }
  169. 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); }
  170. 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); }
  171. 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); }
  172. 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); }
  173. static void _live_edit_remove_node_funcs(void* self,const NodePath& p_at) { reinterpret_cast<SceneTree*>(self)->_live_edit_remove_node_func(p_at); }
  174. 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); }
  175. 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); }
  176. 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); }
  177. 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); }
  178. #endif
  179. protected:
  180. void _notification(int p_notification);
  181. static void _bind_methods();
  182. public:
  183. enum {
  184. NOTIFICATION_TRANSFORM_CHANGED=29
  185. };
  186. enum CallGroupFlags {
  187. GROUP_CALL_DEFAULT=0,
  188. GROUP_CALL_REVERSE=1,
  189. GROUP_CALL_REALTIME=2,
  190. GROUP_CALL_UNIQUE=4,
  191. GROUP_CALL_MULIILEVEL=8,
  192. };
  193. _FORCE_INLINE_ Viewport *get_root() const { return root; }
  194. uint32_t get_last_event_id() const;
  195. void call_group(uint32_t p_call_flags,const StringName& p_group,const StringName& p_function,VARIANT_ARG_LIST);
  196. void notify_group(uint32_t p_call_flags,const StringName& p_group,int p_notification);
  197. void set_group(uint32_t p_call_flags,const StringName& p_group,const String& p_name,const Variant& p_value);
  198. virtual void input_text( const String& p_text );
  199. virtual void input_event( const InputEvent& p_event );
  200. virtual void init();
  201. virtual bool iteration(float p_time);
  202. virtual bool idle(float p_time);
  203. virtual void finish();
  204. void set_auto_accept_quit(bool p_enable);
  205. void quit();
  206. void set_input_as_handled();
  207. _FORCE_INLINE_ float get_fixed_process_time() const { return fixed_process_time; }
  208. _FORCE_INLINE_ float get_idle_process_time() const { return idle_process_time; }
  209. void set_editor_hint(bool p_enabled);
  210. bool is_editor_hint() const;
  211. bool is_node_being_edited(const Node* p_node) const;
  212. void set_pause(bool p_enabled);
  213. bool is_paused() const;
  214. void set_camera(const RID& p_camera);
  215. RID get_camera() const;
  216. void set_debug_collisions_hint(bool p_enabled);
  217. bool is_debugging_collisions_hint() const;
  218. void set_debug_navigation_hint(bool p_enabled);
  219. bool is_debugging_navigation_hint() const;
  220. void set_debug_collisions_color(const Color& p_color);
  221. Color get_debug_collisions_color() const;
  222. void set_debug_collision_contact_color(const Color& p_color);
  223. Color get_debug_collision_contact_color() const;
  224. void set_debug_navigation_color(const Color& p_color);
  225. Color get_debug_navigation_color() const;
  226. void set_debug_navigation_disabled_color(const Color& p_color);
  227. Color get_debug_navigation_disabled_color() const;
  228. Ref<Material> get_debug_navigation_material();
  229. Ref<Material> get_debug_navigation_disabled_material();
  230. Ref<Material> get_debug_collision_material();
  231. Ref<Mesh> get_debug_contact_mesh();
  232. int get_collision_debug_contact_count() { return collision_debug_contacts; }
  233. int64_t get_frame() const;
  234. int get_node_count() const;
  235. void queue_delete(Object *p_object);
  236. void get_nodes_in_group(const StringName& p_group,List<Node*> *p_list);
  237. bool has_group(const StringName& p_identifier) const;
  238. void set_screen_stretch(StretchMode p_mode,StretchAspect p_aspect,const Size2 p_minsize);
  239. //void change_scene(const String& p_path);
  240. //Node *get_loaded_scene();
  241. #ifdef TOOLS_ENABLED
  242. void set_edited_scene_root(Node *p_node);
  243. Node *get_edited_scene_root() const;
  244. #endif
  245. void set_current_scene(Node* p_scene);
  246. Node* get_current_scene() const;
  247. Error change_scene(const String& p_path);
  248. Error change_scene_to(const Ref<PackedScene>& p_scene);
  249. Error reload_current_scene();
  250. //used by Main::start, don't use otherwise
  251. void add_current_scene(Node * p_current);
  252. static SceneTree* get_singleton() { return singleton; }
  253. SceneTree();
  254. ~SceneTree();
  255. };
  256. VARIANT_ENUM_CAST( SceneTree::StretchMode );
  257. VARIANT_ENUM_CAST( SceneTree::StretchAspect );
  258. #endif