scene_tree.h 14 KB

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