Debian Codegraph @

scene/debugger/scene_debugger.h

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