scene_debugger.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #ifndef SCENE_DEBUGGER_H
  31. #define SCENE_DEBUGGER_H
  32. #include "core/input/shortcut.h"
  33. #include "core/object/ref_counted.h"
  34. #include "core/string/ustring.h"
  35. #include "core/templates/pair.h"
  36. #include "core/variant/array.h"
  37. #include "scene/gui/view_panner.h"
  38. #include "scene/resources/mesh.h"
  39. class PopupMenu;
  40. class Script;
  41. class Node;
  42. class SceneDebugger {
  43. private:
  44. inline static SceneDebugger *singleton = nullptr;
  45. SceneDebugger();
  46. public:
  47. static void initialize();
  48. static void deinitialize();
  49. ~SceneDebugger();
  50. #ifdef DEBUG_ENABLED
  51. private:
  52. static void _handle_input(const Ref<InputEvent> &p_event, const Ref<Shortcut> &p_shortcut);
  53. static void _save_node(ObjectID id, const String &p_path);
  54. static void _set_node_owner_recursive(Node *p_node, Node *p_owner);
  55. static void _set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value);
  56. static void _send_object_id(ObjectID p_id, int p_max_size = 1 << 20);
  57. static void _next_frame();
  58. public:
  59. static Error parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured);
  60. static void add_to_cache(const String &p_filename, Node *p_node);
  61. static void remove_from_cache(const String &p_filename, Node *p_node);
  62. static void reload_cached_files(const PackedStringArray &p_files);
  63. #endif
  64. };
  65. #ifdef DEBUG_ENABLED
  66. class SceneDebuggerObject {
  67. private:
  68. void _parse_script_properties(Script *p_script, ScriptInstance *p_instance);
  69. public:
  70. typedef Pair<PropertyInfo, Variant> SceneDebuggerProperty;
  71. ObjectID id;
  72. String class_name;
  73. List<SceneDebuggerProperty> properties;
  74. SceneDebuggerObject(ObjectID p_id);
  75. SceneDebuggerObject() {}
  76. void serialize(Array &r_arr, int p_max_size = 1 << 20);
  77. void deserialize(const Array &p_arr);
  78. };
  79. class SceneDebuggerTree {
  80. public:
  81. struct RemoteNode {
  82. int child_count = 0;
  83. String name;
  84. String type_name;
  85. ObjectID id;
  86. String scene_file_path;
  87. uint8_t view_flags = 0;
  88. enum ViewFlags {
  89. VIEW_HAS_VISIBLE_METHOD = 1 << 1,
  90. VIEW_VISIBLE = 1 << 2,
  91. VIEW_VISIBLE_IN_TREE = 1 << 3,
  92. };
  93. 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) {
  94. child_count = p_child;
  95. name = p_name;
  96. type_name = p_type;
  97. id = p_id;
  98. scene_file_path = p_scene_file_path;
  99. view_flags = p_view_flags;
  100. }
  101. RemoteNode() {}
  102. };
  103. List<RemoteNode> nodes;
  104. void serialize(Array &r_arr);
  105. void deserialize(const Array &p_arr);
  106. SceneDebuggerTree(Node *p_root);
  107. SceneDebuggerTree() {}
  108. };
  109. class LiveEditor {
  110. private:
  111. friend class SceneDebugger;
  112. HashMap<int, NodePath> live_edit_node_path_cache;
  113. HashMap<int, String> live_edit_resource_cache;
  114. NodePath live_edit_root;
  115. String live_edit_scene;
  116. HashMap<String, HashSet<Node *>> live_scene_edit_cache;
  117. HashMap<Node *, HashMap<ObjectID, Node *>> live_edit_remove_list;
  118. void _send_tree();
  119. void _node_path_func(const NodePath &p_path, int p_id);
  120. void _res_path_func(const String &p_path, int p_id);
  121. void _node_set_func(int p_id, const StringName &p_prop, const Variant &p_value);
  122. void _node_set_res_func(int p_id, const StringName &p_prop, const String &p_value);
  123. void _node_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount);
  124. void _res_set_func(int p_id, const StringName &p_prop, const Variant &p_value);
  125. void _res_set_res_func(int p_id, const StringName &p_prop, const String &p_value);
  126. void _res_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount);
  127. void _root_func(const NodePath &p_scene_path, const String &p_scene_from);
  128. void _create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name);
  129. void _instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name);
  130. void _remove_node_func(const NodePath &p_at);
  131. void _remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id);
  132. void _restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos);
  133. void _duplicate_node_func(const NodePath &p_at, const String &p_new_name);
  134. void _reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos);
  135. LiveEditor() {
  136. singleton = this;
  137. live_edit_root = NodePath("/root");
  138. }
  139. inline static LiveEditor *singleton = nullptr;
  140. public:
  141. static LiveEditor *get_singleton();
  142. };
  143. class RuntimeNodeSelect : public Object {
  144. GDCLASS(RuntimeNodeSelect, Object);
  145. public:
  146. enum NodeType {
  147. NODE_TYPE_NONE,
  148. NODE_TYPE_2D,
  149. NODE_TYPE_3D,
  150. NODE_TYPE_MAX
  151. };
  152. enum SelectMode {
  153. SELECT_MODE_SINGLE,
  154. SELECT_MODE_LIST,
  155. SELECT_MODE_MAX
  156. };
  157. private:
  158. friend class SceneDebugger;
  159. struct SelectResult {
  160. Node *item = nullptr;
  161. real_t order = 0;
  162. _FORCE_INLINE_ bool operator<(const SelectResult &p_rr) const { return p_rr.order < order; }
  163. };
  164. bool has_selection = false;
  165. Node *selected_node = nullptr;
  166. PopupMenu *selection_list = nullptr;
  167. bool selection_visible = true;
  168. bool selection_update_queued = false;
  169. bool warped_panning = false;
  170. bool camera_override = false;
  171. // Values taken from EditorZoomWidget.
  172. const float VIEW_2D_MIN_ZOOM = 1.0 / 128;
  173. const float VIEW_2D_MAX_ZOOM = 128;
  174. Ref<ViewPanner> panner;
  175. Vector2 view_2d_offset;
  176. real_t view_2d_zoom = 1.0;
  177. RID sbox_2d_canvas;
  178. RID sbox_2d_ci;
  179. Transform2D sbox_2d_xform;
  180. Rect2 sbox_2d_rect;
  181. #ifndef _3D_DISABLED
  182. struct Cursor {
  183. Vector3 pos;
  184. real_t x_rot, y_rot, distance, fov_scale;
  185. Vector3 eye_pos; // Used in freelook mode.
  186. Cursor() {
  187. // These rotations place the camera in +X +Y +Z, aka south east, facing north west.
  188. x_rot = 0.5;
  189. y_rot = -0.5;
  190. distance = 4;
  191. fov_scale = 1.0;
  192. }
  193. };
  194. Cursor cursor;
  195. // Values taken from Node3DEditor.
  196. const float VIEW_3D_MIN_ZOOM = 0.01;
  197. #ifdef REAL_T_IS_DOUBLE
  198. const double VIEW_3D_MAX_ZOOM = 1'000'000'000'000;
  199. #else
  200. const float VIEW_3D_MAX_ZOOM = 10'000;
  201. #endif
  202. const float CAMERA_ZNEAR = 0.05;
  203. const float CAMERA_ZFAR = 4'000;
  204. const float CAMERA_BASE_FOV = 75;
  205. const float CAMERA_MIN_FOV_SCALE = 0.1;
  206. const float CAMERA_MAX_FOV_SCALE = 2.5;
  207. const float FREELOOK_BASE_SPEED = 4;
  208. const float RADS_PER_PIXEL = 0.004;
  209. bool camera_first_override = true;
  210. bool camera_freelook = false;
  211. Vector2 previous_mouse_position;
  212. Ref<ArrayMesh> sbox_3d_mesh;
  213. Ref<ArrayMesh> sbox_3d_mesh_xray;
  214. RID sbox_3d_instance;
  215. RID sbox_3d_instance_ofs;
  216. RID sbox_3d_instance_xray;
  217. RID sbox_3d_instance_xray_ofs;
  218. Transform3D sbox_3d_xform;
  219. AABB sbox_3d_bounds;
  220. #endif
  221. Point2 selection_position = Point2(INFINITY, INFINITY);
  222. bool list_shortcut_pressed = false;
  223. NodeType node_select_type = NODE_TYPE_2D;
  224. SelectMode node_select_mode = SELECT_MODE_SINGLE;
  225. void _setup(const Dictionary &p_settings);
  226. void _node_set_type(NodeType p_type);
  227. void _select_set_mode(SelectMode p_mode);
  228. void _set_camera_override_enabled(bool p_enabled);
  229. void _root_window_input(const Ref<InputEvent> &p_event);
  230. void _items_popup_index_pressed(int p_index, PopupMenu *p_popup);
  231. void _update_input_state();
  232. void _process_frame();
  233. void _physics_frame();
  234. void _click_point();
  235. void _select_node(Node *p_node);
  236. void _queue_selection_update();
  237. void _update_selection();
  238. void _clear_selection();
  239. void _set_selection_visible(bool p_visible);
  240. 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());
  241. void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  242. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  243. void _reset_camera_2d();
  244. void _update_view_2d();
  245. #ifndef _3D_DISABLED
  246. void _find_3d_items_at_pos(const Point2 &p_pos, Vector<SelectResult> &r_items);
  247. bool _handle_3d_input(const Ref<InputEvent> &p_event);
  248. void _set_camera_freelook_enabled(bool p_enabled);
  249. void _cursor_scale_distance(real_t p_scale);
  250. void _cursor_look(Ref<InputEventWithModifiers> p_event);
  251. void _cursor_pan(Ref<InputEventWithModifiers> p_event);
  252. void _cursor_orbit(Ref<InputEventWithModifiers> p_event);
  253. Transform3D _get_cursor_transform();
  254. void _reset_camera_3d();
  255. #endif
  256. RuntimeNodeSelect() { singleton = this; }
  257. inline static RuntimeNodeSelect *singleton = nullptr;
  258. public:
  259. static RuntimeNodeSelect *get_singleton();
  260. ~RuntimeNodeSelect();
  261. };
  262. #endif
  263. #endif // SCENE_DEBUGGER_H