scene_tree.h 14 KB

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