Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/camera_2d.h
9896 views
1
/**************************************************************************/
2
/* camera_2d.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 "scene/2d/node_2d.h"
34
35
class Camera2D : public Node2D {
36
GDCLASS(Camera2D, Node2D);
37
38
public:
39
enum AnchorMode {
40
ANCHOR_MODE_FIXED_TOP_LEFT,
41
ANCHOR_MODE_DRAG_CENTER
42
};
43
44
enum Camera2DProcessCallback {
45
CAMERA2D_PROCESS_PHYSICS,
46
CAMERA2D_PROCESS_IDLE
47
};
48
49
protected:
50
Point2 camera_pos;
51
Point2 smoothed_camera_pos;
52
bool first = true;
53
bool just_exited_tree = false;
54
55
ObjectID custom_viewport_id; // To check validity.
56
Viewport *custom_viewport = nullptr;
57
Viewport *viewport = nullptr;
58
59
StringName group_name;
60
StringName canvas_group_name;
61
RID canvas;
62
Vector2 offset;
63
Vector2 zoom = Vector2(1, 1);
64
Vector2 zoom_scale = Vector2(1, 1);
65
AnchorMode anchor_mode = ANCHOR_MODE_DRAG_CENTER;
66
bool ignore_rotation = true;
67
bool enabled = true;
68
real_t position_smoothing_speed = 5.0;
69
bool position_smoothing_enabled = false;
70
71
real_t camera_angle = 0.0;
72
real_t rotation_smoothing_speed = 5.0;
73
bool rotation_smoothing_enabled = false;
74
75
bool limit_enabled = true;
76
int limit[4] = { -10000000, -10000000, 10000000, 10000000 }; // Left, top, right, bottom.
77
bool limit_smoothing_enabled = false;
78
79
real_t drag_margin[4] = { 0.2, 0.2, 0.2, 0.2 };
80
bool drag_horizontal_enabled = false;
81
bool drag_vertical_enabled = false;
82
real_t drag_horizontal_offset = 0.0;
83
real_t drag_vertical_offset = 0.0;
84
bool drag_horizontal_offset_changed = false;
85
bool drag_vertical_offset_changed = false;
86
87
Point2 camera_screen_center;
88
bool _is_editing_in_editor() const;
89
void _update_process_callback();
90
void _update_scroll();
91
92
#ifdef TOOLS_ENABLED
93
void _project_settings_changed();
94
#endif
95
96
void _make_current(Object *p_which);
97
void _reset_just_exited() { just_exited_tree = false; }
98
99
void _update_process_internal_for_smoothing();
100
101
bool screen_drawing_enabled = true;
102
bool limit_drawing_enabled = false;
103
bool margin_drawing_enabled = false;
104
105
Camera2DProcessCallback process_callback = CAMERA2D_PROCESS_IDLE;
106
107
struct InterpolationData {
108
Transform2D xform_curr;
109
Transform2D xform_prev;
110
uint32_t last_update_physics_tick = UINT32_MAX; // Ensure tick 0 is detected as a change.
111
} _interpolation_data;
112
113
void _ensure_update_interpolation_data();
114
115
Size2 _get_camera_screen_size() const;
116
117
protected:
118
virtual Transform2D get_camera_transform();
119
120
void _notification(int p_what);
121
static void _bind_methods();
122
123
public:
124
void set_limit_rect(const Rect2i &p_limit_rect);
125
Rect2i get_limit_rect() const;
126
127
void set_offset(const Vector2 &p_offset);
128
Vector2 get_offset() const;
129
130
void set_anchor_mode(AnchorMode p_anchor_mode);
131
AnchorMode get_anchor_mode() const;
132
133
void set_ignore_rotation(bool p_ignore);
134
bool is_ignoring_rotation() const;
135
136
void set_limit_enabled(bool p_limit_enabled);
137
bool is_limit_enabled() const;
138
139
void set_limit(Side p_side, int p_limit);
140
int get_limit(Side p_side) const;
141
142
void set_limit_smoothing_enabled(bool p_enabled);
143
bool is_limit_smoothing_enabled() const;
144
145
void set_drag_horizontal_enabled(bool p_enabled);
146
bool is_drag_horizontal_enabled() const;
147
148
void set_drag_vertical_enabled(bool p_enabled);
149
bool is_drag_vertical_enabled() const;
150
151
void set_drag_margin(Side p_side, real_t p_drag_margin);
152
real_t get_drag_margin(Side p_side) const;
153
154
void set_drag_horizontal_offset(real_t p_offset);
155
real_t get_drag_horizontal_offset() const;
156
157
void set_drag_vertical_offset(real_t p_offset);
158
real_t get_drag_vertical_offset() const;
159
160
void set_position_smoothing_enabled(bool p_enabled);
161
bool is_position_smoothing_enabled() const;
162
163
void set_position_smoothing_speed(real_t p_speed);
164
real_t get_position_smoothing_speed() const;
165
166
void set_rotation_smoothing_speed(real_t p_speed);
167
real_t get_rotation_smoothing_speed() const;
168
169
void set_rotation_smoothing_enabled(bool p_enabled);
170
bool is_rotation_smoothing_enabled() const;
171
172
void set_process_callback(Camera2DProcessCallback p_mode);
173
Camera2DProcessCallback get_process_callback() const;
174
175
void set_enabled(bool p_enabled);
176
bool is_enabled() const;
177
178
void make_current();
179
void clear_current();
180
bool is_current() const;
181
182
void set_zoom(const Vector2 &p_zoom);
183
Vector2 get_zoom() const;
184
185
Point2 get_camera_screen_center() const;
186
real_t get_screen_rotation() const;
187
188
void set_custom_viewport(Node *p_viewport);
189
Node *get_custom_viewport() const;
190
191
Vector2 get_camera_position() const;
192
void force_update_scroll();
193
void reset_smoothing();
194
void align();
195
196
void set_screen_drawing_enabled(bool p_enabled);
197
bool is_screen_drawing_enabled() const;
198
199
void set_limit_drawing_enabled(bool p_enabled);
200
bool is_limit_drawing_enabled() const;
201
202
void set_margin_drawing_enabled(bool p_enabled);
203
bool is_margin_drawing_enabled() const;
204
205
Camera2D();
206
};
207
208
VARIANT_ENUM_CAST(Camera2D::AnchorMode);
209
VARIANT_ENUM_CAST(Camera2D::Camera2DProcessCallback);
210
211