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