scene_tree.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 "io/networked_multiplayer_peer.h"
  33. #include "os/main_loop.h"
  34. #include "os/thread_safe.h"
  35. #include "scene/resources/mesh.h"
  36. #include "scene/resources/world.h"
  37. #include "scene/resources/world_2d.h"
  38. #include "self_list.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);
  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. enum NetworkCommands {
  147. NETWORK_COMMAND_REMOTE_CALL,
  148. NETWORK_COMMAND_REMOTE_SET,
  149. NETWORK_COMMAND_SIMPLIFY_PATH,
  150. NETWORK_COMMAND_CONFIRM_PATH,
  151. };
  152. Ref<NetworkedMultiplayerPeer> network_peer;
  153. Set<int> connected_peers;
  154. void _network_peer_connected(int p_id);
  155. void _network_peer_disconnected(int p_id);
  156. void _connected_to_server();
  157. void _connection_failed();
  158. void _server_disconnected();
  159. int rpc_sender_id;
  160. //path sent caches
  161. struct PathSentCache {
  162. Map<int, bool> confirmed_peers;
  163. int id;
  164. };
  165. HashMap<NodePath, PathSentCache> path_send_cache;
  166. int last_send_cache_id;
  167. //path get caches
  168. struct PathGetCache {
  169. struct NodeInfo {
  170. NodePath path;
  171. ObjectID instance;
  172. };
  173. Map<int, NodeInfo> nodes;
  174. };
  175. Map<int, PathGetCache> path_get_cache;
  176. Vector<uint8_t> packet_cache;
  177. void _network_process_packet(int p_from, const uint8_t *p_packet, int p_packet_len);
  178. void _network_poll();
  179. static SceneTree *singleton;
  180. friend class Node;
  181. void _rpc(Node *p_from, int p_to, bool p_unreliable, bool p_set, const StringName &p_name, const Variant **p_arg, int p_argcount);
  182. void tree_changed();
  183. void node_added(Node *p_node);
  184. void node_removed(Node *p_node);
  185. Group *add_to_group(const StringName &p_group, Node *p_node);
  186. void remove_from_group(const StringName &p_group, Node *p_node);
  187. void _notify_group_pause(const StringName &p_group, int p_notification);
  188. void _call_input_pause(const StringName &p_group, const StringName &p_method, const Ref<InputEvent> &p_input);
  189. Variant _call_group_flags(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  190. Variant _call_group(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  191. static void _debugger_request_tree(void *self);
  192. void _flush_delete_queue();
  193. //optimization
  194. friend class CanvasItem;
  195. friend class Spatial;
  196. friend class Viewport;
  197. SelfList<Node>::List xform_change_list;
  198. #ifdef DEBUG_ENABLED
  199. Map<int, NodePath> live_edit_node_path_cache;
  200. Map<int, String> live_edit_resource_cache;
  201. NodePath live_edit_root;
  202. String live_edit_scene;
  203. Map<String, Set<Node *> > live_scene_edit_cache;
  204. Map<Node *, Map<ObjectID, Node *> > live_edit_remove_list;
  205. ScriptDebugger::LiveEditFuncs live_edit_funcs;
  206. void _live_edit_node_path_func(const NodePath &p_path, int p_id);
  207. void _live_edit_res_path_func(const String &p_path, int p_id);
  208. void _live_edit_node_set_func(int p_id, const StringName &p_prop, const Variant &p_value);
  209. void _live_edit_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value);
  210. void _live_edit_node_call_func(int p_id, const StringName &p_method, VARIANT_ARG_DECLARE);
  211. void _live_edit_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value);
  212. void _live_edit_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value);
  213. void _live_edit_res_call_func(int p_id, const StringName &p_method, VARIANT_ARG_DECLARE);
  214. void _live_edit_root_func(const NodePath &p_scene_path, const String &p_scene_from);
  215. void _live_edit_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name);
  216. void _live_edit_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name);
  217. void _live_edit_remove_node_func(const NodePath &p_at);
  218. void _live_edit_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id);
  219. void _live_edit_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos);
  220. void _live_edit_duplicate_node_func(const NodePath &p_at, const String &p_new_name);
  221. void _live_edit_reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos);
  222. 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); }
  223. 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); }
  224. 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); }
  225. 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); }
  226. 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); }
  227. 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); }
  228. 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); }
  229. 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); }
  230. 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); }
  231. 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); }
  232. 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); }
  233. static void _live_edit_remove_node_funcs(void *self, const NodePath &p_at) { reinterpret_cast<SceneTree *>(self)->_live_edit_remove_node_func(p_at); }
  234. 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); }
  235. 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); }
  236. 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); }
  237. 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); }
  238. #endif
  239. enum {
  240. MAX_IDLE_CALLBACKS = 256
  241. };
  242. static IdleCallback idle_callbacks[MAX_IDLE_CALLBACKS];
  243. static int idle_callback_count;
  244. void _call_idle_callbacks();
  245. protected:
  246. void _notification(int p_notification);
  247. static void _bind_methods();
  248. public:
  249. enum {
  250. NOTIFICATION_TRANSFORM_CHANGED = 29
  251. };
  252. enum GroupCallFlags {
  253. GROUP_CALL_DEFAULT = 0,
  254. GROUP_CALL_REVERSE = 1,
  255. GROUP_CALL_REALTIME = 2,
  256. GROUP_CALL_UNIQUE = 4,
  257. GROUP_CALL_MULTILEVEL = 8,
  258. };
  259. _FORCE_INLINE_ Viewport *get_root() const { return root; }
  260. void call_group_flags(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, VARIANT_ARG_LIST);
  261. void notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification);
  262. void set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value);
  263. void call_group(const StringName &p_group, const StringName &p_function, VARIANT_ARG_LIST);
  264. void notify_group(const StringName &p_group, int p_notification);
  265. void set_group(const StringName &p_group, const String &p_name, const Variant &p_value);
  266. void flush_transform_notifications();
  267. virtual void input_text(const String &p_text);
  268. virtual void input_event(const Ref<InputEvent> &p_event);
  269. virtual void init();
  270. virtual bool iteration(float p_time);
  271. virtual bool idle(float p_time);
  272. virtual void finish();
  273. void set_auto_accept_quit(bool p_enable);
  274. void set_quit_on_go_back(bool p_enable);
  275. void quit();
  276. void set_input_as_handled();
  277. bool is_input_handled();
  278. _FORCE_INLINE_ float get_physics_process_time() const { return physics_process_time; }
  279. _FORCE_INLINE_ float get_idle_process_time() const { return idle_process_time; }
  280. #ifdef TOOLS_ENABLED
  281. bool is_node_being_edited(const Node *p_node) const;
  282. #else
  283. bool is_node_being_edited(const Node *p_node) const { return false; }
  284. #endif
  285. void set_pause(bool p_enabled);
  286. bool is_paused() const;
  287. void set_camera(const RID &p_camera);
  288. RID get_camera() const;
  289. #ifdef DEBUG_ENABLED
  290. void set_debug_collisions_hint(bool p_enabled);
  291. bool is_debugging_collisions_hint() const;
  292. void set_debug_navigation_hint(bool p_enabled);
  293. bool is_debugging_navigation_hint() const;
  294. #else
  295. void set_debug_collisions_hint(bool p_enabled) {}
  296. bool is_debugging_collisions_hint() const { return false; }
  297. void set_debug_navigation_hint(bool p_enabled) {}
  298. bool is_debugging_navigation_hint() const { return false; }
  299. #endif
  300. void set_debug_collisions_color(const Color &p_color);
  301. Color get_debug_collisions_color() const;
  302. void set_debug_collision_contact_color(const Color &p_color);
  303. Color get_debug_collision_contact_color() const;
  304. void set_debug_navigation_color(const Color &p_color);
  305. Color get_debug_navigation_color() const;
  306. void set_debug_navigation_disabled_color(const Color &p_color);
  307. Color get_debug_navigation_disabled_color() const;
  308. Ref<Material> get_debug_navigation_material();
  309. Ref<Material> get_debug_navigation_disabled_material();
  310. Ref<Material> get_debug_collision_material();
  311. Ref<ArrayMesh> get_debug_contact_mesh();
  312. int get_collision_debug_contact_count() { return collision_debug_contacts; }
  313. int64_t get_frame() const;
  314. int64_t get_event_count() const;
  315. int get_node_count() const;
  316. void queue_delete(Object *p_object);
  317. void get_nodes_in_group(const StringName &p_group, List<Node *> *p_list);
  318. bool has_group(const StringName &p_identifier) const;
  319. void set_screen_stretch(StretchMode p_mode, StretchAspect p_aspect, const Size2 p_minsize, real_t p_shrink = 1);
  320. void set_use_font_oversampling(bool p_oversampling);
  321. bool is_using_font_oversampling() const;
  322. //void change_scene(const String& p_path);
  323. //Node *get_loaded_scene();
  324. void set_edited_scene_root(Node *p_node);
  325. Node *get_edited_scene_root() const;
  326. void set_current_scene(Node *p_scene);
  327. Node *get_current_scene() const;
  328. Error change_scene(const String &p_path);
  329. Error change_scene_to(const Ref<PackedScene> &p_scene);
  330. Error reload_current_scene();
  331. Ref<SceneTreeTimer> create_timer(float p_delay_sec, bool p_process_pause = true);
  332. //used by Main::start, don't use otherwise
  333. void add_current_scene(Node *p_current);
  334. static SceneTree *get_singleton() { return singleton; }
  335. void drop_files(const Vector<String> &p_files, int p_from_screen = 0);
  336. //network API
  337. void set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_network_peer);
  338. Ref<NetworkedMultiplayerPeer> get_network_peer() const;
  339. bool is_network_server() const;
  340. bool has_network_peer() const;
  341. int get_network_unique_id() const;
  342. Vector<int> get_network_connected_peers() const;
  343. int get_rpc_sender_id() const;
  344. void set_refuse_new_network_connections(bool p_refuse);
  345. bool is_refusing_new_network_connections() const;
  346. static void add_idle_callback(IdleCallback p_callback);
  347. SceneTree();
  348. ~SceneTree();
  349. };
  350. VARIANT_ENUM_CAST(SceneTree::StretchMode);
  351. VARIANT_ENUM_CAST(SceneTree::StretchAspect);
  352. VARIANT_ENUM_CAST(SceneTree::GroupCallFlags);
  353. #endif