scene_tree.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*************************************************************************/
  2. /* scene_tree.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 SCENE_MAIN_LOOP_H
  31. #define SCENE_MAIN_LOOP_H
  32. #include "core/io/multiplayer_api.h"
  33. #include "core/os/main_loop.h"
  34. #include "core/os/thread_safe.h"
  35. #include "core/self_list.h"
  36. #include "scene/resources/mesh.h"
  37. #include "scene/resources/world.h"
  38. #include "scene/resources/world_2d.h"
  39. /**
  40. @author Juan Linietsky <reduzio@gmail.com>
  41. */
  42. class SceneTree;
  43. class PackedScene;
  44. class Node;
  45. class Viewport;
  46. class Material;
  47. class Mesh;
  48. class SceneTreeTimer : public Reference {
  49. GDCLASS(SceneTreeTimer, Reference);
  50. float time_left;
  51. bool process_pause;
  52. protected:
  53. static void _bind_methods();
  54. public:
  55. void set_time_left(float p_time);
  56. float get_time_left() const;
  57. void set_pause_mode_process(bool p_pause_mode_process);
  58. bool is_pause_mode_process();
  59. SceneTreeTimer();
  60. };
  61. class SceneTree : public MainLoop {
  62. _THREAD_SAFE_CLASS_
  63. GDCLASS(SceneTree, MainLoop);
  64. public:
  65. typedef void (*IdleCallback)();
  66. enum StretchMode {
  67. STRETCH_MODE_DISABLED,
  68. STRETCH_MODE_2D,
  69. STRETCH_MODE_VIEWPORT,
  70. };
  71. enum StretchAspect {
  72. STRETCH_ASPECT_IGNORE,
  73. STRETCH_ASPECT_KEEP,
  74. STRETCH_ASPECT_KEEP_WIDTH,
  75. STRETCH_ASPECT_KEEP_HEIGHT,
  76. STRETCH_ASPECT_EXPAND,
  77. };
  78. private:
  79. struct Group {
  80. Vector<Node *> nodes;
  81. //uint64_t last_tree_version;
  82. bool changed;
  83. Group() { changed = false; };
  84. };
  85. Viewport *root;
  86. uint64_t tree_version;
  87. float physics_process_time;
  88. float idle_process_time;
  89. bool accept_quit;
  90. bool quit_on_go_back;
  91. #ifdef DEBUG_ENABLED
  92. bool debug_collisions_hint;
  93. bool debug_navigation_hint;
  94. #endif
  95. bool pause;
  96. int root_lock;
  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_added_name;
  104. StringName node_removed_name;
  105. bool use_font_oversampling;
  106. int64_t current_frame;
  107. int64_t current_event;
  108. int node_count;
  109. #ifdef TOOLS_ENABLED
  110. Node *edited_scene_root;
  111. #endif
  112. struct UGCall {
  113. StringName group;
  114. StringName call;
  115. bool operator<(const UGCall &p_with) const { return group == p_with.group ? call < p_with.call : group < p_with.group; }
  116. };
  117. //safety for when a node is deleted while a group is being called
  118. int call_lock;
  119. Set<Node *> call_skip; //skip erased nodes
  120. StretchMode stretch_mode;
  121. StretchAspect stretch_aspect;
  122. Size2i stretch_min;
  123. real_t stretch_shrink;
  124. void _update_root_rect();
  125. List<ObjectID> delete_queue;
  126. Map<UGCall, Vector<Variant> > unique_group_calls;
  127. bool ugc_locked;
  128. void _flush_ugc();
  129. _FORCE_INLINE_ void _update_group_order(Group &g, bool p_use_priority = false);
  130. void _update_listener();
  131. Array _get_nodes_in_group(const StringName &p_group);
  132. Node *current_scene;
  133. Color debug_collisions_color;
  134. Color debug_collision_contact_color;
  135. Color debug_navigation_color;
  136. Color debug_navigation_disabled_color;
  137. Ref<ArrayMesh> debug_contact_mesh;
  138. Ref<Material> navigation_material;
  139. Ref<Material> navigation_disabled_material;
  140. Ref<Material> collision_material;
  141. int collision_debug_contacts;
  142. void _change_scene(Node *p_to);
  143. //void _call_group(uint32_t p_call_flags,const StringName& p_group,const StringName& p_function,const Variant& p_arg1,const Variant& p_arg2);
  144. List<Ref<SceneTreeTimer> > timers;
  145. ///network///
  146. Ref<MultiplayerAPI> multiplayer;
  147. bool multiplayer_poll;
  148. void _network_peer_connected(int p_id);
  149. void _network_peer_disconnected(int p_id);
  150. void _connected_to_server();
  151. void _connection_failed();
  152. void _server_disconnected();
  153. static SceneTree *singleton;
  154. friend class Node;
  155. void tree_changed();
  156. void node_added(Node *p_node);
  157. void node_removed(Node *p_node);
  158. Group *add_to_group(const StringName &p_group, Node *p_node);
  159. void remove_from_group(const StringName &p_group, Node *p_node);
  160. void make_group_changed(const StringName &p_group);
  161. void _notify_group_pause(const StringName &p_group, int p_notification);
  162. void _call_input_pause(const StringName &p_group, const StringName &p_method, const Ref<InputEvent> &p_input);
  163. Variant _call_group_flags(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  164. Variant _call_group(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  165. static void _debugger_request_tree(void *self);
  166. void _flush_delete_queue();
  167. //optimization
  168. friend class CanvasItem;
  169. friend class Spatial;
  170. friend class Viewport;
  171. SelfList<Node>::List xform_change_list;
  172. #ifdef DEBUG_ENABLED
  173. Map<int, NodePath> live_edit_node_path_cache;
  174. Map<int, String> live_edit_resource_cache;
  175. NodePath live_edit_root;
  176. String live_edit_scene;
  177. Map<String, Set<Node *> > live_scene_edit_cache;
  178. Map<Node *, Map<ObjectID, Node *> > live_edit_remove_list;
  179. ScriptDebugger::LiveEditFuncs live_edit_funcs;
  180. void _live_edit_node_path_func(const NodePath &p_path, int p_id);
  181. void _live_edit_res_path_func(const String &p_path, int p_id);
  182. void _live_edit_node_set_func(int p_id, const StringName &p_prop, const Variant &p_value);
  183. void _live_edit_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value);
  184. void _live_edit_node_call_func(int p_id, const StringName &p_method, VARIANT_ARG_DECLARE);
  185. void _live_edit_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value);
  186. void _live_edit_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value);
  187. void _live_edit_res_call_func(int p_id, const StringName &p_method, VARIANT_ARG_DECLARE);
  188. void _live_edit_root_func(const NodePath &p_scene_path, const String &p_scene_from);
  189. void _live_edit_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name);
  190. void _live_edit_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name);
  191. void _live_edit_remove_node_func(const NodePath &p_at);
  192. void _live_edit_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id);
  193. void _live_edit_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos);
  194. void _live_edit_duplicate_node_func(const NodePath &p_at, const String &p_new_name);
  195. void _live_edit_reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos);
  196. 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); }
  197. 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); }
  198. 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); }
  199. 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); }
  200. 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); }
  201. 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); }
  202. 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); }
  203. 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); }
  204. 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); }
  205. 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); }
  206. 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); }
  207. static void _live_edit_remove_node_funcs(void *self, const NodePath &p_at) { reinterpret_cast<SceneTree *>(self)->_live_edit_remove_node_func(p_at); }
  208. 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); }
  209. 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); }
  210. 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); }
  211. 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); }
  212. #endif
  213. enum {
  214. MAX_IDLE_CALLBACKS = 256
  215. };
  216. static IdleCallback idle_callbacks[MAX_IDLE_CALLBACKS];
  217. static int idle_callback_count;
  218. void _call_idle_callbacks();
  219. protected:
  220. void _notification(int p_notification);
  221. static void _bind_methods();
  222. public:
  223. enum {
  224. NOTIFICATION_TRANSFORM_CHANGED = 29
  225. };
  226. enum GroupCallFlags {
  227. GROUP_CALL_DEFAULT = 0,
  228. GROUP_CALL_REVERSE = 1,
  229. GROUP_CALL_REALTIME = 2,
  230. GROUP_CALL_UNIQUE = 4,
  231. GROUP_CALL_MULTILEVEL = 8,
  232. };
  233. _FORCE_INLINE_ Viewport *get_root() const { return root; }
  234. void call_group_flags(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, VARIANT_ARG_LIST);
  235. void notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification);
  236. void set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value);
  237. void call_group(const StringName &p_group, const StringName &p_function, VARIANT_ARG_LIST);
  238. void notify_group(const StringName &p_group, int p_notification);
  239. void set_group(const StringName &p_group, const String &p_name, const Variant &p_value);
  240. void flush_transform_notifications();
  241. virtual void input_text(const String &p_text);
  242. virtual void input_event(const Ref<InputEvent> &p_event);
  243. virtual void init();
  244. virtual bool iteration(float p_time);
  245. virtual bool idle(float p_time);
  246. virtual void finish();
  247. void set_auto_accept_quit(bool p_enable);
  248. void set_quit_on_go_back(bool p_enable);
  249. void quit();
  250. void set_input_as_handled();
  251. bool is_input_handled();
  252. _FORCE_INLINE_ float get_physics_process_time() const { return physics_process_time; }
  253. _FORCE_INLINE_ float get_idle_process_time() const { return idle_process_time; }
  254. #ifdef TOOLS_ENABLED
  255. bool is_node_being_edited(const Node *p_node) const;
  256. #else
  257. bool is_node_being_edited(const Node *p_node) const { return false; }
  258. #endif
  259. void set_pause(bool p_enabled);
  260. bool is_paused() const;
  261. void set_camera(const RID &p_camera);
  262. RID get_camera() const;
  263. #ifdef DEBUG_ENABLED
  264. void set_debug_collisions_hint(bool p_enabled);
  265. bool is_debugging_collisions_hint() const;
  266. void set_debug_navigation_hint(bool p_enabled);
  267. bool is_debugging_navigation_hint() const;
  268. #else
  269. void set_debug_collisions_hint(bool p_enabled) {}
  270. bool is_debugging_collisions_hint() const { return false; }
  271. void set_debug_navigation_hint(bool p_enabled) {}
  272. bool is_debugging_navigation_hint() const { return false; }
  273. #endif
  274. void set_debug_collisions_color(const Color &p_color);
  275. Color get_debug_collisions_color() const;
  276. void set_debug_collision_contact_color(const Color &p_color);
  277. Color get_debug_collision_contact_color() const;
  278. void set_debug_navigation_color(const Color &p_color);
  279. Color get_debug_navigation_color() const;
  280. void set_debug_navigation_disabled_color(const Color &p_color);
  281. Color get_debug_navigation_disabled_color() const;
  282. Ref<Material> get_debug_navigation_material();
  283. Ref<Material> get_debug_navigation_disabled_material();
  284. Ref<Material> get_debug_collision_material();
  285. Ref<ArrayMesh> get_debug_contact_mesh();
  286. int get_collision_debug_contact_count() { return collision_debug_contacts; }
  287. int64_t get_frame() const;
  288. int64_t get_event_count() const;
  289. int get_node_count() const;
  290. void queue_delete(Object *p_object);
  291. void get_nodes_in_group(const StringName &p_group, List<Node *> *p_list);
  292. bool has_group(const StringName &p_identifier) const;
  293. void set_screen_stretch(StretchMode p_mode, StretchAspect p_aspect, const Size2 p_minsize, real_t p_shrink = 1);
  294. void set_use_font_oversampling(bool p_oversampling);
  295. bool is_using_font_oversampling() const;
  296. //void change_scene(const String& p_path);
  297. //Node *get_loaded_scene();
  298. void set_edited_scene_root(Node *p_node);
  299. Node *get_edited_scene_root() const;
  300. void set_current_scene(Node *p_scene);
  301. Node *get_current_scene() const;
  302. Error change_scene(const String &p_path);
  303. Error change_scene_to(const Ref<PackedScene> &p_scene);
  304. Error reload_current_scene();
  305. Ref<SceneTreeTimer> create_timer(float p_delay_sec, bool p_process_pause = true);
  306. //used by Main::start, don't use otherwise
  307. void add_current_scene(Node *p_current);
  308. static SceneTree *get_singleton() { return singleton; }
  309. void drop_files(const Vector<String> &p_files, int p_from_screen = 0);
  310. //network API
  311. Ref<MultiplayerAPI> get_multiplayer() const;
  312. void set_multiplayer_poll_enabled(bool p_enabled);
  313. bool is_multiplayer_poll_enabled() const;
  314. void set_multiplayer(Ref<MultiplayerAPI> p_multiplayer);
  315. void set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_network_peer);
  316. Ref<NetworkedMultiplayerPeer> get_network_peer() const;
  317. bool is_network_server() const;
  318. bool has_network_peer() const;
  319. int get_network_unique_id() const;
  320. Vector<int> get_network_connected_peers() const;
  321. int get_rpc_sender_id() const;
  322. void set_refuse_new_network_connections(bool p_refuse);
  323. bool is_refusing_new_network_connections() const;
  324. static void add_idle_callback(IdleCallback p_callback);
  325. SceneTree();
  326. ~SceneTree();
  327. };
  328. VARIANT_ENUM_CAST(SceneTree::StretchMode);
  329. VARIANT_ENUM_CAST(SceneTree::StretchAspect);
  330. VARIANT_ENUM_CAST(SceneTree::GroupCallFlags);
  331. #endif