scene_tree.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*************************************************************************/
  2. /* scene_tree.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. class PackedScene;
  40. class Node;
  41. class Viewport;
  42. class Material;
  43. class Mesh;
  44. class SceneTreeTimer : public Reference {
  45. GDCLASS(SceneTreeTimer, Reference);
  46. float time_left;
  47. bool process_pause;
  48. bool ignore_time_scale = false;
  49. protected:
  50. static void _bind_methods();
  51. public:
  52. void set_time_left(float p_time);
  53. float get_time_left() const;
  54. void set_pause_mode_process(bool p_pause_mode_process);
  55. bool is_pause_mode_process();
  56. void set_ignore_time_scale(bool p_ignore);
  57. bool is_ignore_time_scale();
  58. void release_connections();
  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. StringName node_renamed_name;
  106. bool use_font_oversampling;
  107. int64_t current_frame;
  108. int64_t current_event;
  109. int node_count;
  110. #ifdef TOOLS_ENABLED
  111. Node *edited_scene_root;
  112. #endif
  113. struct UGCall {
  114. StringName group;
  115. StringName call;
  116. bool operator<(const UGCall &p_with) const { return group == p_with.group ? call < p_with.call : group < p_with.group; }
  117. };
  118. //safety for when a node is deleted while a group is being called
  119. int call_lock;
  120. Set<Node *> call_skip; //skip erased nodes
  121. StretchMode stretch_mode;
  122. StretchAspect stretch_aspect;
  123. Size2i stretch_min;
  124. real_t stretch_scale;
  125. void _update_font_oversampling(float p_ratio);
  126. void _update_root_rect();
  127. List<ObjectID> delete_queue;
  128. Map<UGCall, Vector<Variant>> unique_group_calls;
  129. bool ugc_locked;
  130. void _flush_ugc();
  131. _FORCE_INLINE_ void _update_group_order(Group &g, bool p_use_priority = false);
  132. void _update_listener();
  133. Array _get_nodes_in_group(const StringName &p_group);
  134. Node *current_scene;
  135. Color debug_collisions_color;
  136. Color debug_collision_contact_color;
  137. Color debug_navigation_color;
  138. Color debug_navigation_disabled_color;
  139. Ref<ArrayMesh> debug_contact_mesh;
  140. Ref<Material> navigation_material;
  141. Ref<Material> navigation_disabled_material;
  142. Ref<Material> collision_material;
  143. int collision_debug_contacts;
  144. void _change_scene(Node *p_to);
  145. //void _call_group(uint32_t p_call_flags,const StringName& p_group,const StringName& p_function,const Variant& p_arg1,const Variant& p_arg2);
  146. List<Ref<SceneTreeTimer>> timers;
  147. ///network///
  148. Ref<MultiplayerAPI> multiplayer;
  149. bool multiplayer_poll;
  150. void _network_peer_connected(int p_id);
  151. void _network_peer_disconnected(int p_id);
  152. void _connected_to_server();
  153. void _connection_failed();
  154. void _server_disconnected();
  155. static SceneTree *singleton;
  156. friend class Node;
  157. void tree_changed();
  158. void node_added(Node *p_node);
  159. void node_removed(Node *p_node);
  160. void node_renamed(Node *p_node);
  161. Group *add_to_group(const StringName &p_group, Node *p_node);
  162. void remove_from_group(const StringName &p_group, Node *p_node);
  163. void make_group_changed(const StringName &p_group);
  164. void _notify_group_pause(const StringName &p_group, int p_notification);
  165. void _call_input_pause(const StringName &p_group, const StringName &p_method, const Ref<InputEvent> &p_input);
  166. Variant _call_group_flags(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  167. Variant _call_group(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  168. void _flush_delete_queue();
  169. //optimization
  170. friend class CanvasItem;
  171. friend class Spatial;
  172. friend class Viewport;
  173. SelfList<Node>::List xform_change_list;
  174. friend class ScriptDebuggerRemote;
  175. #ifdef DEBUG_ENABLED
  176. Map<int, NodePath> live_edit_node_path_cache;
  177. Map<int, String> live_edit_resource_cache;
  178. NodePath live_edit_root;
  179. String live_edit_scene;
  180. Map<String, Set<Node *>> live_scene_edit_cache;
  181. Map<Node *, Map<ObjectID, Node *>> live_edit_remove_list;
  182. void _debugger_request_tree();
  183. void _live_edit_node_path_func(const NodePath &p_path, int p_id);
  184. void _live_edit_res_path_func(const String &p_path, int p_id);
  185. void _live_edit_node_set_func(int p_id, const StringName &p_prop, const Variant &p_value);
  186. void _live_edit_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value);
  187. void _live_edit_node_call_func(int p_id, const StringName &p_method, VARIANT_ARG_DECLARE);
  188. void _live_edit_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value);
  189. void _live_edit_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value);
  190. void _live_edit_res_call_func(int p_id, const StringName &p_method, VARIANT_ARG_DECLARE);
  191. void _live_edit_root_func(const NodePath &p_scene_path, const String &p_scene_from);
  192. void _live_edit_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name);
  193. void _live_edit_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name);
  194. void _live_edit_remove_node_func(const NodePath &p_at);
  195. void _live_edit_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id);
  196. void _live_edit_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos);
  197. void _live_edit_duplicate_node_func(const NodePath &p_at, const String &p_new_name);
  198. void _live_edit_reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos);
  199. #endif
  200. enum {
  201. MAX_IDLE_CALLBACKS = 256
  202. };
  203. static IdleCallback idle_callbacks[MAX_IDLE_CALLBACKS];
  204. static int idle_callback_count;
  205. void _call_idle_callbacks();
  206. protected:
  207. void _notification(int p_notification);
  208. static void _bind_methods();
  209. public:
  210. enum {
  211. NOTIFICATION_TRANSFORM_CHANGED = 2000
  212. };
  213. enum GroupCallFlags {
  214. GROUP_CALL_DEFAULT = 0,
  215. GROUP_CALL_REVERSE = 1,
  216. GROUP_CALL_REALTIME = 2,
  217. GROUP_CALL_UNIQUE = 4,
  218. GROUP_CALL_MULTILEVEL = 8,
  219. };
  220. _FORCE_INLINE_ Viewport *get_root() const { return root; }
  221. void call_group_flags(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, VARIANT_ARG_LIST);
  222. void notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification);
  223. void set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value);
  224. void call_group(const StringName &p_group, const StringName &p_function, VARIANT_ARG_LIST);
  225. void notify_group(const StringName &p_group, int p_notification);
  226. void set_group(const StringName &p_group, const String &p_name, const Variant &p_value);
  227. void flush_transform_notifications();
  228. virtual void input_text(const String &p_text);
  229. virtual void input_event(const Ref<InputEvent> &p_event);
  230. virtual void init();
  231. virtual bool iteration(float p_time);
  232. virtual bool idle(float p_time);
  233. virtual void finish();
  234. void set_auto_accept_quit(bool p_enable);
  235. void set_quit_on_go_back(bool p_enable);
  236. void quit(int p_exit_code = -1);
  237. void set_input_as_handled();
  238. bool is_input_handled();
  239. _FORCE_INLINE_ float get_physics_process_time() const { return physics_process_time; }
  240. _FORCE_INLINE_ float get_idle_process_time() const { return idle_process_time; }
  241. #ifdef TOOLS_ENABLED
  242. bool is_node_being_edited(const Node *p_node) const;
  243. #else
  244. bool is_node_being_edited(const Node *p_node) const { return false; }
  245. #endif
  246. void set_pause(bool p_enabled);
  247. bool is_paused() const;
  248. void set_camera(const RID &p_camera);
  249. RID get_camera() const;
  250. #ifdef DEBUG_ENABLED
  251. void set_debug_collisions_hint(bool p_enabled);
  252. bool is_debugging_collisions_hint() const;
  253. void set_debug_navigation_hint(bool p_enabled);
  254. bool is_debugging_navigation_hint() const;
  255. #else
  256. void set_debug_collisions_hint(bool p_enabled) {}
  257. bool is_debugging_collisions_hint() const { return false; }
  258. void set_debug_navigation_hint(bool p_enabled) {}
  259. bool is_debugging_navigation_hint() const { return false; }
  260. #endif
  261. void set_debug_collisions_color(const Color &p_color);
  262. Color get_debug_collisions_color() const;
  263. void set_debug_collision_contact_color(const Color &p_color);
  264. Color get_debug_collision_contact_color() const;
  265. void set_debug_navigation_color(const Color &p_color);
  266. Color get_debug_navigation_color() const;
  267. void set_debug_navigation_disabled_color(const Color &p_color);
  268. Color get_debug_navigation_disabled_color() const;
  269. Ref<Material> get_debug_navigation_material();
  270. Ref<Material> get_debug_navigation_disabled_material();
  271. Ref<Material> get_debug_collision_material();
  272. Ref<ArrayMesh> get_debug_contact_mesh();
  273. int get_collision_debug_contact_count() { return collision_debug_contacts; }
  274. int64_t get_frame() const;
  275. int64_t get_event_count() const;
  276. int get_node_count() const;
  277. void queue_delete(Object *p_object);
  278. void get_nodes_in_group(const StringName &p_group, List<Node *> *p_list);
  279. bool has_group(const StringName &p_identifier) const;
  280. void set_screen_stretch(StretchMode p_mode, StretchAspect p_aspect, const Size2 &p_minsize, real_t p_scale = 1.0);
  281. void set_use_font_oversampling(bool p_oversampling);
  282. bool is_using_font_oversampling() const;
  283. //void change_scene(const String& p_path);
  284. //Node *get_loaded_scene();
  285. void set_edited_scene_root(Node *p_node);
  286. Node *get_edited_scene_root() const;
  287. void set_current_scene(Node *p_scene);
  288. Node *get_current_scene() const;
  289. Error change_scene(const String &p_path);
  290. Error change_scene_to(const Ref<PackedScene> &p_scene);
  291. Error reload_current_scene();
  292. Ref<SceneTreeTimer> create_timer(float p_delay_sec, bool p_process_pause = true);
  293. //used by Main::start, don't use otherwise
  294. void add_current_scene(Node *p_current);
  295. static SceneTree *get_singleton() { return singleton; }
  296. void drop_files(const Vector<String> &p_files, int p_from_screen = 0);
  297. void global_menu_action(const Variant &p_id, const Variant &p_meta);
  298. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  299. //network API
  300. Ref<MultiplayerAPI> get_multiplayer() const;
  301. void set_multiplayer_poll_enabled(bool p_enabled);
  302. bool is_multiplayer_poll_enabled() const;
  303. void set_multiplayer(Ref<MultiplayerAPI> p_multiplayer);
  304. void set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_network_peer);
  305. Ref<NetworkedMultiplayerPeer> get_network_peer() const;
  306. bool is_network_server() const;
  307. bool has_network_peer() const;
  308. int get_network_unique_id() const;
  309. Vector<int> get_network_connected_peers() const;
  310. int get_rpc_sender_id() const;
  311. void set_refuse_new_network_connections(bool p_refuse);
  312. bool is_refusing_new_network_connections() const;
  313. static void add_idle_callback(IdleCallback p_callback);
  314. SceneTree();
  315. ~SceneTree();
  316. };
  317. VARIANT_ENUM_CAST(SceneTree::StretchMode);
  318. VARIANT_ENUM_CAST(SceneTree::StretchAspect);
  319. VARIANT_ENUM_CAST(SceneTree::GroupCallFlags);
  320. #endif