scene_debugger.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /**************************************************************************/
  2. /* scene_debugger.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. #pragma once
  31. #include "core/input/shortcut.h"
  32. #include "core/object/ref_counted.h"
  33. #include "core/string/ustring.h"
  34. #include "core/templates/pair.h"
  35. #include "core/variant/array.h"
  36. #include "scene/gui/view_panner.h"
  37. #ifndef _3D_DISABLED
  38. #include "scene/resources/mesh.h"
  39. #endif // _3D_DISABLED
  40. class CanvasItem;
  41. class LiveEditor;
  42. class PopupMenu;
  43. class RuntimeNodeSelect;
  44. class Script;
  45. class SceneTree;
  46. #ifndef _3D_DISABLED
  47. class Node3D;
  48. #endif // _3D_DISABLED
  49. class SceneDebugger {
  50. private:
  51. inline static SceneDebugger *singleton = nullptr;
  52. SceneDebugger();
  53. public:
  54. static void initialize();
  55. static void deinitialize();
  56. ~SceneDebugger();
  57. #ifdef DEBUG_ENABLED
  58. private:
  59. static void _handle_input(const Ref<InputEvent> &p_event, const Ref<Shortcut> &p_shortcut);
  60. static void _handle_embed_input(const Ref<InputEvent> &p_event, const Dictionary &p_settings);
  61. static void _save_node(ObjectID id, const String &p_path);
  62. static void _set_node_owner_recursive(Node *p_node, Node *p_owner);
  63. static void _set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value, const String &p_field = "");
  64. static void _send_object_ids(const Vector<ObjectID> &p_ids, bool p_update_selection);
  65. static void _next_frame();
  66. /// Message handler function for parse_message.
  67. typedef Error (*ParseMessageFunc)(const Array &p_args);
  68. static HashMap<String, ParseMessageFunc> message_handlers;
  69. static void _init_message_handlers();
  70. static Error _msg_setup_scene(const Array &p_args);
  71. static Error _msg_setup_embedded_shortcuts(const Array &p_args);
  72. static Error _msg_request_scene_tree(const Array &p_args);
  73. static Error _msg_save_node(const Array &p_args);
  74. static Error _msg_inspect_objects(const Array &p_args);
  75. #ifndef DISABLE_DEPRECATED
  76. static Error _msg_inspect_object(const Array &p_args);
  77. #endif // DISABLE_DEPRECATED
  78. static Error _msg_clear_selection(const Array &p_args);
  79. static Error _msg_suspend_changed(const Array &p_args);
  80. static Error _msg_next_frame(const Array &p_args);
  81. static Error _msg_speed_changed(const Array &p_args);
  82. static Error _msg_debug_mute_audio(const Array &p_args);
  83. static Error _msg_override_cameras(const Array &p_args);
  84. static Error _msg_set_object_property(const Array &p_args);
  85. static Error _msg_set_object_property_field(const Array &p_args);
  86. static Error _msg_reload_cached_files(const Array &p_args);
  87. static Error _msg_live_set_root(const Array &p_args);
  88. static Error _msg_live_node_path(const Array &p_args);
  89. static Error _msg_live_res_path(const Array &p_args);
  90. static Error _msg_live_node_prop_res(const Array &p_args);
  91. static Error _msg_live_node_prop(const Array &p_args);
  92. static Error _msg_live_res_prop_res(const Array &p_args);
  93. static Error _msg_live_res_prop(const Array &p_args);
  94. static Error _msg_live_node_call(const Array &p_args);
  95. static Error _msg_live_res_call(const Array &p_args);
  96. static Error _msg_live_create_node(const Array &p_args);
  97. static Error _msg_live_instantiate_node(const Array &p_args);
  98. static Error _msg_live_remove_node(const Array &p_args);
  99. static Error _msg_live_remove_and_keep_node(const Array &p_args);
  100. static Error _msg_live_restore_node(const Array &p_args);
  101. static Error _msg_live_duplicate_node(const Array &p_args);
  102. static Error _msg_live_reparent_node(const Array &p_args);
  103. static Error _msg_runtime_node_select_setup(const Array &p_args);
  104. static Error _msg_runtime_node_select_set_type(const Array &p_args);
  105. static Error _msg_runtime_node_select_set_mode(const Array &p_args);
  106. static Error _msg_runtime_node_select_set_visible(const Array &p_args);
  107. static Error _msg_runtime_node_select_set_avoid_locked(const Array &p_args);
  108. static Error _msg_runtime_node_select_set_prefer_group(const Array &p_args);
  109. static Error _msg_rq_screenshot(const Array &p_args);
  110. static Error _msg_runtime_node_select_reset_camera_2d(const Array &p_args);
  111. static Error _msg_transform_camera_2d(const Array &p_args);
  112. #ifndef _3D_DISABLED
  113. static Error _msg_runtime_node_select_reset_camera_3d(const Array &p_args);
  114. static Error _msg_transform_camera_3d(const Array &p_args);
  115. #endif // _3D_DISABLED
  116. public:
  117. static Error parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured);
  118. static void add_to_cache(const String &p_filename, Node *p_node);
  119. static void remove_from_cache(const String &p_filename, Node *p_node);
  120. static void reload_cached_files(const PackedStringArray &p_files);
  121. #endif
  122. };
  123. #ifdef DEBUG_ENABLED
  124. class SceneDebuggerObject {
  125. private:
  126. void _parse_script_properties(Script *p_script, ScriptInstance *p_instance);
  127. public:
  128. typedef Pair<PropertyInfo, Variant> SceneDebuggerProperty;
  129. ObjectID id;
  130. String class_name;
  131. List<SceneDebuggerProperty> properties;
  132. SceneDebuggerObject(ObjectID p_id);
  133. SceneDebuggerObject(Object *p_obj);
  134. SceneDebuggerObject() {}
  135. void serialize(Array &r_arr, int p_max_size = 1 << 20);
  136. void deserialize(const Array &p_arr);
  137. void deserialize(uint64_t p_id, const String &p_class_name, const Array &p_props);
  138. };
  139. class SceneDebuggerTree {
  140. public:
  141. struct RemoteNode {
  142. int child_count = 0;
  143. String name;
  144. String type_name;
  145. ObjectID id;
  146. String scene_file_path;
  147. uint8_t view_flags = 0;
  148. enum ViewFlags {
  149. VIEW_HAS_VISIBLE_METHOD = 1 << 1,
  150. VIEW_VISIBLE = 1 << 2,
  151. VIEW_VISIBLE_IN_TREE = 1 << 3,
  152. };
  153. RemoteNode(int p_child, const String &p_name, const String &p_type, ObjectID p_id, const String p_scene_file_path, int p_view_flags) {
  154. child_count = p_child;
  155. name = p_name;
  156. type_name = p_type;
  157. id = p_id;
  158. scene_file_path = p_scene_file_path;
  159. view_flags = p_view_flags;
  160. }
  161. RemoteNode() {}
  162. };
  163. List<RemoteNode> nodes;
  164. void serialize(Array &r_arr);
  165. void deserialize(const Array &p_arr);
  166. SceneDebuggerTree(Node *p_root);
  167. SceneDebuggerTree() {}
  168. };
  169. class LiveEditor {
  170. private:
  171. friend class SceneDebugger;
  172. HashMap<int, NodePath> live_edit_node_path_cache;
  173. HashMap<int, String> live_edit_resource_cache;
  174. NodePath live_edit_root;
  175. String live_edit_scene;
  176. HashMap<String, HashSet<Node *>> live_scene_edit_cache;
  177. HashMap<Node *, HashMap<ObjectID, Node *>> live_edit_remove_list;
  178. void _send_tree();
  179. void _node_path_func(const NodePath &p_path, int p_id);
  180. void _res_path_func(const String &p_path, int p_id);
  181. void _node_set_func(int p_id, const StringName &p_prop, const Variant &p_value);
  182. void _node_set_res_func(int p_id, const StringName &p_prop, const String &p_value);
  183. void _node_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount);
  184. void _res_set_func(int p_id, const StringName &p_prop, const Variant &p_value);
  185. void _res_set_res_func(int p_id, const StringName &p_prop, const String &p_value);
  186. void _res_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount);
  187. void _root_func(const NodePath &p_scene_path, const String &p_scene_from);
  188. void _create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name);
  189. void _instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name);
  190. void _remove_node_func(const NodePath &p_at);
  191. void _remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id);
  192. void _restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos);
  193. void _duplicate_node_func(const NodePath &p_at, const String &p_new_name);
  194. void _reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos);
  195. LiveEditor() {
  196. singleton = this;
  197. live_edit_root = NodePath("/root");
  198. }
  199. inline static LiveEditor *singleton = nullptr;
  200. public:
  201. static LiveEditor *get_singleton();
  202. };
  203. class RuntimeNodeSelect : public Object {
  204. GDCLASS(RuntimeNodeSelect, Object);
  205. public:
  206. enum NodeType {
  207. NODE_TYPE_NONE,
  208. NODE_TYPE_2D,
  209. NODE_TYPE_3D,
  210. NODE_TYPE_MAX,
  211. };
  212. enum SelectMode {
  213. SELECT_MODE_SINGLE,
  214. SELECT_MODE_LIST,
  215. SELECT_MODE_MAX,
  216. };
  217. private:
  218. friend class SceneDebugger;
  219. NodeType node_select_type = NODE_TYPE_2D;
  220. SelectMode node_select_mode = SELECT_MODE_SINGLE;
  221. struct SelectResult {
  222. Node *item = nullptr;
  223. real_t order = 0;
  224. _FORCE_INLINE_ bool operator<(const SelectResult &p_rr) const { return p_rr.order < order; }
  225. };
  226. const int SELECTION_MIN_AREA = 8 * 8;
  227. enum SelectionDragState {
  228. SELECTION_DRAG_NONE,
  229. SELECTION_DRAG_MOVE,
  230. SELECTION_DRAG_END,
  231. };
  232. SelectionDragState selection_drag_state = SELECTION_DRAG_NONE;
  233. bool has_selection = false;
  234. int max_selection = 1;
  235. Point2 selection_position = Point2(Math::INF, Math::INF);
  236. Rect2 selection_drag_area;
  237. PopupMenu *selection_list = nullptr;
  238. Color selection_area_fill;
  239. Color selection_area_outline;
  240. bool selection_visible = true;
  241. bool selection_update_queued = false;
  242. bool avoid_locked_nodes = false;
  243. bool prefer_group_selection = false;
  244. bool multi_shortcut_pressed = false;
  245. bool list_shortcut_pressed = false;
  246. RID draw_canvas;
  247. RID sel_drag_ci;
  248. bool camera_override = false;
  249. bool camera_first_override = true;
  250. // Values taken from EditorZoomWidget.
  251. const float VIEW_2D_MIN_ZOOM = 1.0 / 128;
  252. const float VIEW_2D_MAX_ZOOM = 128;
  253. Ref<ViewPanner> panner;
  254. Vector2 view_2d_offset;
  255. real_t view_2d_zoom = 1.0;
  256. bool warped_panning = false;
  257. LocalVector<ObjectID> selected_ci_nodes;
  258. real_t sel_2d_grab_dist = 0;
  259. int sel_2d_scale = 1;
  260. RID sbox_2d_ci;
  261. #ifndef _3D_DISABLED
  262. struct Cursor {
  263. Vector3 pos;
  264. real_t x_rot, y_rot, distance, fov_scale;
  265. Vector3 eye_pos; // Used in freelook mode.
  266. Cursor() {
  267. // These rotations place the camera in +X +Y +Z, aka south east, facing north west.
  268. x_rot = 0.5;
  269. y_rot = -0.5;
  270. distance = 4;
  271. fov_scale = 1.0;
  272. }
  273. };
  274. Cursor cursor;
  275. // Values taken from Node3DEditor.
  276. const float VIEW_3D_MIN_ZOOM = 0.01;
  277. #ifdef REAL_T_IS_DOUBLE
  278. const double VIEW_3D_MAX_ZOOM = 1'000'000'000'000;
  279. #else
  280. const float VIEW_3D_MAX_ZOOM = 10'000;
  281. #endif // REAL_T_IS_DOUBLE
  282. const float CAMERA_MIN_FOV_SCALE = 0.1;
  283. const float CAMERA_MAX_FOV_SCALE = 2.5;
  284. bool camera_freelook = false;
  285. real_t camera_fov = 0;
  286. real_t camera_znear = 0;
  287. real_t camera_zfar = 0;
  288. bool invert_x_axis = false;
  289. bool invert_y_axis = false;
  290. bool warped_mouse_panning_3d = false;
  291. real_t freelook_base_speed = 0;
  292. real_t freelook_sensitivity = 0;
  293. real_t orbit_sensitivity = 0;
  294. real_t translation_sensitivity = 0;
  295. Vector2 previous_mouse_position;
  296. struct SelectionBox3D : public RefCounted {
  297. RID instance;
  298. RID instance_ofs;
  299. RID instance_xray;
  300. RID instance_xray_ofs;
  301. Transform3D transform;
  302. AABB bounds;
  303. ~SelectionBox3D() {
  304. if (instance.is_valid()) {
  305. RS::get_singleton()->free_rid(instance);
  306. RS::get_singleton()->free_rid(instance_ofs);
  307. RS::get_singleton()->free_rid(instance_xray);
  308. RS::get_singleton()->free_rid(instance_xray_ofs);
  309. }
  310. }
  311. };
  312. HashMap<ObjectID, Ref<SelectionBox3D>> selected_3d_nodes;
  313. Color sbox_3d_color;
  314. Ref<ArrayMesh> sbox_3d_mesh;
  315. Ref<ArrayMesh> sbox_3d_mesh_xray;
  316. RID sbox_3d;
  317. RID sbox_3d_ofs;
  318. RID sbox_3d_xray;
  319. RID sbox_3d_xray_ofs;
  320. #endif // _3D_DISABLED
  321. void _setup(const Dictionary &p_settings);
  322. void _node_set_type(NodeType p_type);
  323. void _select_set_mode(SelectMode p_mode);
  324. void _set_camera_override_enabled(bool p_enabled);
  325. void _root_window_input(const Ref<InputEvent> &p_event);
  326. void _items_popup_index_pressed(int p_index, PopupMenu *p_popup);
  327. void _update_input_state();
  328. void _process_frame();
  329. void _physics_frame();
  330. void _send_ids(const Vector<Node *> &p_picked_nodes, bool p_invert_new_selections = true);
  331. void _set_selected_nodes(const Vector<Node *> &p_nodes);
  332. void _queue_selection_update();
  333. void _update_selection();
  334. void _clear_selection();
  335. void _update_selection_drag(const Point2 &p_end_pos = Point2());
  336. void _set_selection_visible(bool p_visible);
  337. void _set_avoid_locked(bool p_enabled);
  338. void _set_prefer_group(bool p_enabled);
  339. void _open_selection_list(const Vector<SelectResult> &p_items, const Point2 &p_pos);
  340. void _close_selection_list();
  341. void _find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, Vector<SelectResult> &r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
  342. void _find_canvas_items_at_rect(const Rect2 &p_rect, Node *p_node, Vector<SelectResult> &r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
  343. void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  344. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  345. void _reset_camera_2d();
  346. void _update_view_2d();
  347. #ifndef _3D_DISABLED
  348. void _find_3d_items_at_pos(const Point2 &p_pos, Vector<SelectResult> &r_items);
  349. void _find_3d_items_at_rect(const Rect2 &p_rect, Vector<SelectResult> &r_items);
  350. Vector3 _get_screen_to_space(const Vector3 &p_vector3);
  351. bool _handle_3d_input(const Ref<InputEvent> &p_event);
  352. void _set_camera_freelook_enabled(bool p_enabled);
  353. void _cursor_scale_distance(real_t p_scale);
  354. void _scale_freelook_speed(real_t p_scale);
  355. void _cursor_look(Ref<InputEventWithModifiers> p_event);
  356. void _cursor_pan(Ref<InputEventWithModifiers> p_event);
  357. void _cursor_orbit(Ref<InputEventWithModifiers> p_event);
  358. Point2 _get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_event, Rect2 p_border) const;
  359. Transform3D _get_cursor_transform();
  360. void _reset_camera_3d();
  361. #endif // _3D_DISABLED
  362. RuntimeNodeSelect() { singleton = this; }
  363. inline static RuntimeNodeSelect *singleton = nullptr;
  364. public:
  365. static RuntimeNodeSelect *get_singleton();
  366. ~RuntimeNodeSelect();
  367. };
  368. #endif // DEBUG_ENABLED