Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/debugger/view_3d_controller.h
45993 views
1
/**************************************************************************/
2
/* view_3d_controller.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
#ifndef _3D_DISABLED
34
35
#include "core/object/ref_counted.h"
36
37
namespace View3DControllerConsts {
38
constexpr float DISTANCE_DEFAULT = 4;
39
40
constexpr float ZOOM_FREELOOK_MULTIPLIER = 1.08;
41
constexpr float ZOOM_FREELOOK_MIN = 0.01;
42
#ifdef REAL_T_IS_DOUBLE
43
constexpr double ZOOM_FREELOOK_MAX = 1'000'000'000'000;
44
#else
45
constexpr float ZOOM_FREELOOK_MAX = 10'000;
46
#endif
47
48
constexpr float CAMERA_MIN_FOV_SCALE = 0.1;
49
constexpr float CAMERA_MAX_FOV_SCALE = 2.5;
50
} //namespace View3DControllerConsts
51
52
class InputEvent;
53
class InputEventMouseMotion;
54
class InputEventWithModifiers;
55
class Shortcut;
56
57
class View3DController : public RefCounted {
58
GDCLASS(View3DController, RefCounted);
59
60
public:
61
enum NavigationMode {
62
NAV_MODE_NONE,
63
NAV_MODE_PAN,
64
NAV_MODE_ZOOM,
65
NAV_MODE_ORBIT,
66
NAV_MODE_LOOK,
67
NAV_MODE_MOVE,
68
};
69
70
enum NavigationScheme {
71
NAV_SCHEME_GODOT,
72
NAV_SCHEME_MAYA,
73
NAV_SCHEME_MODO,
74
NAV_SCHEME_CUSTOM,
75
NAV_SCHEME_TABLET,
76
};
77
78
enum NavigationMouseButton {
79
NAV_MOUSE_BUTTON_LEFT,
80
NAV_MOUSE_BUTTON_MIDDLE,
81
NAV_MOUSE_BUTTON_RIGHT,
82
NAV_MOUSE_BUTTON_4,
83
NAV_MOUSE_BUTTON_5,
84
};
85
86
enum ZoomStyle {
87
ZOOM_VERTICAL,
88
ZOOM_HORIZONTAL,
89
};
90
91
enum FreelookScheme {
92
FREELOOK_DEFAULT,
93
FREELOOK_PARTIALLY_AXIS_LOCKED,
94
FREELOOK_FULLY_AXIS_LOCKED,
95
};
96
97
enum ViewType {
98
VIEW_TYPE_USER,
99
VIEW_TYPE_TOP,
100
VIEW_TYPE_BOTTOM,
101
VIEW_TYPE_LEFT,
102
VIEW_TYPE_RIGHT,
103
VIEW_TYPE_FRONT,
104
VIEW_TYPE_REAR,
105
};
106
107
enum OrthogonalMode {
108
ORTHOGONAL_DISABLED,
109
ORTHOGONAL_ENABLED,
110
ORTHOGONAL_AUTO,
111
};
112
113
enum ShortcutName {
114
SHORTCUT_FOV_INCREASE,
115
SHORTCUT_FOV_DECREASE,
116
SHORTCUT_FOV_RESET,
117
118
SHORTCUT_PAN_MOD_1,
119
SHORTCUT_PAN_MOD_2,
120
121
SHORTCUT_ORBIT_MOD_1,
122
SHORTCUT_ORBIT_MOD_2,
123
SHORTCUT_ORBIT_SNAP_MOD_1,
124
SHORTCUT_ORBIT_SNAP_MOD_2,
125
126
SHORTCUT_ZOOM_MOD_1,
127
SHORTCUT_ZOOM_MOD_2,
128
129
SHORTCUT_FREELOOK_FORWARD,
130
SHORTCUT_FREELOOK_BACKWARDS,
131
SHORTCUT_FREELOOK_LEFT,
132
SHORTCUT_FREELOOK_RIGHT,
133
SHORTCUT_FREELOOK_UP,
134
SHORTCUT_FREELOOK_DOWN,
135
SHORTCUT_FREELOOK_SPEED_MOD,
136
SHORTCUT_FREELOOK_SLOW_MOD,
137
138
SHORTCUT_MAX,
139
};
140
141
struct Cursor {
142
Vector3 pos;
143
real_t x_rot;
144
real_t y_rot;
145
real_t distance;
146
real_t fov_scale;
147
real_t unsnapped_x_rot;
148
real_t unsnapped_y_rot;
149
Vector3 eye_pos; // Used for freelook.
150
// TODO: These variables are not related to cursor manipulation, and specific
151
// to Node3DEditorPlugin. So remove them in the future.
152
bool region_select;
153
Point2 region_begin;
154
Point2 region_end;
155
156
Cursor() {
157
// These rotations place the camera in +X +Y +Z, aka south east, facing north west.
158
x_rot = 0.5;
159
y_rot = -0.5;
160
unsnapped_x_rot = x_rot;
161
unsnapped_y_rot = y_rot;
162
distance = 4;
163
fov_scale = 1.0;
164
region_select = false;
165
}
166
};
167
168
// Viewport camera supports movement smoothing,
169
// so one cursor is the real cursor, while the other can be an interpolated version.
170
Cursor cursor; // Immediate cursor.
171
172
private:
173
Cursor cursor_interp; // That one may be interpolated (don't modify this one except for smoothing purposes).
174
Cursor previous_cursor; // Storing previous cursor state for canceling purposes.
175
bool navigating = false;
176
bool navigation_cancelled = false;
177
void cancel_navigation();
178
179
protected:
180
static void _bind_methods();
181
182
private:
183
HashMap<int, Ref<Shortcut>> inputs;
184
185
NavigationScheme navigation_scheme = NAV_SCHEME_GODOT;
186
ViewType view_type = VIEW_TYPE_USER;
187
188
NavigationMouseButton pan_mouse_button = NAV_MOUSE_BUTTON_MIDDLE;
189
190
NavigationMouseButton orbit_mouse_button = NAV_MOUSE_BUTTON_MIDDLE;
191
float orbit_sensitivity = 0;
192
float orbit_inertia = 0;
193
194
bool freelook = false;
195
FreelookScheme freelook_scheme = FREELOOK_DEFAULT;
196
float freelook_speed = 0;
197
float freelook_base_speed = 0;
198
float freelook_speed_zoom_link = 0;
199
float freelook_sensitivity = 0;
200
float freelook_inertia = 0;
201
bool freelook_invert_y_axis = false;
202
203
ZoomStyle zoom_style = ZOOM_VERTICAL;
204
NavigationMouseButton zoom_mouse_button = NAV_MOUSE_BUTTON_MIDDLE;
205
float zoom_inertia = 0;
206
int zoom_failed_attempts_count = 0;
207
208
float translation_sensitivity = 0;
209
float translation_inertia = 0;
210
211
float angle_snap_threshold = 0;
212
bool warped_mouse_panning = false;
213
214
bool emulate_3_button_mouse = false;
215
bool emulate_numpad = true;
216
217
OrthogonalMode orthogonal = ORTHOGONAL_DISABLED;
218
bool auto_orthogonal_allowed = false;
219
220
bool lock_rotation = false;
221
222
float znear = 0;
223
float zfar = 0;
224
225
bool invert_x_axis = false;
226
bool invert_y_axis = false;
227
228
Point2 previous_mouse_position;
229
230
Transform3D _to_camera_transform(const Cursor &p_cursor) const;
231
232
struct ShortcutCheck {
233
bool mod_pressed = false;
234
bool not_empty = true;
235
int input_count = 0;
236
NavigationMouseButton mouse_preference = NAV_MOUSE_BUTTON_LEFT;
237
NavigationMode result_nav_mode = NAV_MODE_NONE;
238
239
ShortcutCheck(bool p_mod_pressed, bool p_not_empty, int p_input_count, const NavigationMouseButton &p_mouse_preference, const NavigationMode &p_result_nav_mode) :
240
mod_pressed(p_mod_pressed), not_empty(p_not_empty), input_count(p_input_count), mouse_preference(p_mouse_preference), result_nav_mode(p_result_nav_mode) {
241
}
242
};
243
244
struct ShortcutCheckSetComparator {
245
_FORCE_INLINE_ bool operator()(const ShortcutCheck &A, const ShortcutCheck &B) const {
246
return A.input_count > B.input_count;
247
}
248
};
249
250
bool _is_shortcut_pressed(const ShortcutName p_name, const bool p_true_if_null = false);
251
bool _is_shortcut_empty(const ShortcutName p_name);
252
NavigationMode _get_nav_mode_from_shortcuts(NavigationMouseButton p_mouse_button, const Vector<ShortcutCheck> &p_shortcut_checks, bool p_not_empty);
253
254
public:
255
bool gui_input(const Ref<InputEvent> &p_event, const Rect2 &p_surface_rect);
256
bool is_navigating() const { return navigating; }
257
258
void cursor_pan(const Ref<InputEventWithModifiers> &p_event, const Vector2 &p_relative);
259
void cursor_look(const Ref<InputEventWithModifiers> &p_event, const Vector2 &p_relative);
260
void cursor_orbit(const Ref<InputEventWithModifiers> &p_event, const Vector2 &p_relative);
261
void cursor_zoom(const Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
262
263
void update_camera(const real_t p_delta = 0);
264
265
void update_freelook(const float p_delta);
266
void scale_freelook_speed(const float p_scale);
267
268
void scale_cursor_distance(const float p_scale);
269
270
inline Transform3D to_camera_transform() const { return _to_camera_transform(cursor); }
271
inline Transform3D interp_to_camera_transform() const { return _to_camera_transform(cursor_interp); }
272
273
void set_shortcut(const ShortcutName p_name, const Ref<Shortcut> &p_shortcut);
274
275
void set_navigation_scheme(const NavigationScheme p_scheme) { navigation_scheme = p_scheme; }
276
277
void set_view_type(const ViewType p_view);
278
ViewType get_view_type() const { return view_type; }
279
String get_view_type_name() const;
280
281
void set_pan_mouse_button(const NavigationMouseButton p_button) { pan_mouse_button = p_button; }
282
283
void set_orbit_sensitivity(const float p_sensitivity) { orbit_sensitivity = p_sensitivity; }
284
void set_orbit_inertia(const float p_inertia) { orbit_inertia = p_inertia; }
285
void set_orbit_mouse_button(const NavigationMouseButton p_button) { orbit_mouse_button = p_button; }
286
287
void set_freelook_enabled(const bool p_enabled);
288
bool is_freelook_enabled() const { return freelook; }
289
void set_freelook_scheme(FreelookScheme p_scheme) { freelook_scheme = p_scheme; }
290
FreelookScheme get_freelook_scheme() const { return freelook_scheme; }
291
void set_freelook_base_speed(const float p_speed);
292
float get_freelook_speed() const { return freelook_speed; }
293
void set_freelook_sensitivity(const float p_sensitivity) { freelook_sensitivity = p_sensitivity; }
294
void set_freelook_inertia(const float p_inertia) { freelook_inertia = p_inertia; }
295
void set_freelook_speed_zoom_link(const bool p_enabled) { freelook_speed_zoom_link = p_enabled; }
296
void set_freelook_invert_y_axis(const bool p_enabled) { freelook_invert_y_axis = p_enabled; }
297
298
void set_zoom_style(ZoomStyle p_style) { zoom_style = p_style; }
299
void set_zoom_inertia(const float p_inertia) { zoom_inertia = p_inertia; }
300
void set_zoom_mouse_button(const NavigationMouseButton p_button) { zoom_mouse_button = p_button; }
301
302
void set_translation_sensitivity(const float p_sensitivity) { translation_sensitivity = p_sensitivity; }
303
void set_translation_inertia(const float p_inertia) { translation_inertia = p_inertia; }
304
305
void set_angle_snap_threshold(const float p_threshold) { angle_snap_threshold = p_threshold; }
306
307
void set_emulate_3_button_mouse(const bool p_enabled) { emulate_3_button_mouse = p_enabled; }
308
void set_emulate_numpad(const bool p_enabled) { emulate_numpad = p_enabled; }
309
310
void set_orthogonal(const bool p_enabled) { orthogonal = p_enabled ? ORTHOGONAL_ENABLED : ORTHOGONAL_DISABLED; }
311
bool is_orthogonal() const { return orthogonal != ORTHOGONAL_DISABLED; }
312
OrthogonalMode get_orthogonal_mode() const { return orthogonal; }
313
void force_auto_orthogonal();
314
void set_auto_orthogonal_allowed(const bool p_enabled);
315
316
void set_lock_rotation(const bool p_locked) { lock_rotation = p_locked; }
317
bool is_locking_rotation() { return lock_rotation; }
318
319
void set_z_near(const float p_near) { znear = p_near; }
320
void set_z_far(const float p_far) { zfar = p_far; }
321
322
void set_invert_x_axis(const bool p_invert) { invert_x_axis = p_invert; }
323
void set_invert_y_axis(const bool p_invert) { invert_y_axis = p_invert; }
324
325
void set_warped_mouse_panning(const bool p_enabled) { warped_mouse_panning = p_enabled; }
326
Point2 get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_event, const Rect2 &p_surface_rect) const;
327
328
int get_zoom_failed_attempts_count() const { return zoom_failed_attempts_count; }
329
};
330
331
#endif // _3D_DISABLED
332
333