1 /*************************************************************************/
2 /*  camera_2d.h                                                          */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
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 CAMERA_2D_H
31 #define CAMERA_2D_H
32 
33 #include "scene/2d/node_2d.h"
34 #include "scene/main/viewport.h"
35 
36 class Camera2D : public Node2D {
37 
38 	OBJ_TYPE(Camera2D, Node2D);
39 
40 public:
41 	enum AnchorMode {
42 		ANCHOR_MODE_FIXED_TOP_LEFT,
43 		ANCHOR_MODE_DRAG_CENTER
44 	};
45 
46 protected:
47 	Point2 camera_pos;
48 	Point2 smoothed_camera_pos;
49 	bool first;
50 
51 	ObjectID custom_viewport_id; // to check validity
52 	Viewport *custom_viewport;
53 	Viewport *viewport;
54 
55 	StringName group_name;
56 	StringName canvas_group_name;
57 	RID canvas;
58 	Vector2 offset;
59 	Vector2 zoom;
60 	AnchorMode anchor_mode;
61 	bool rotating;
62 	bool current;
63 	float smoothing;
64 	bool smoothing_enabled;
65 	int limit[4];
66 	float drag_margin[4];
67 
68 	bool h_drag_enabled;
69 	bool v_drag_enabled;
70 	float h_ofs;
71 	float v_ofs;
72 
73 	Point2 camera_screen_center;
74 	void _update_scroll();
75 
76 	void _make_current(Object *p_which);
77 	void _set_current(bool p_current);
78 
79 	void _set_old_smoothing(float p_enable);
80 
81 protected:
82 	virtual Matrix32 get_camera_transform();
83 	void _notification(int p_what);
84 	static void _bind_methods();
85 
86 public:
87 	void set_offset(const Vector2 &p_offset);
88 	Vector2 get_offset() const;
89 
90 	void set_anchor_mode(AnchorMode p_anchor_mode);
91 	AnchorMode get_anchor_mode() const;
92 
93 	void set_rotating(bool p_rotating);
94 	bool is_rotating() const;
95 
96 	void set_limit(Margin p_margin, int p_limit);
97 	int get_limit(Margin p_margin) const;
98 
99 	void set_h_drag_enabled(bool p_enabled);
100 	bool is_h_drag_enabled() const;
101 
102 	void set_v_drag_enabled(bool p_enabled);
103 	bool is_v_drag_enabled() const;
104 
105 	void set_drag_margin(Margin p_margin, float p_drag_margin);
106 	float get_drag_margin(Margin p_margin) const;
107 
108 	void set_v_offset(float p_offset);
109 	float get_v_offset() const;
110 
111 	void set_h_offset(float p_offset);
112 	float get_h_offset() const;
113 
114 	void set_enable_follow_smoothing(bool p_enabled);
115 	bool is_follow_smoothing_enabled() const;
116 
117 	void set_follow_smoothing(float p_speed);
118 	float get_follow_smoothing() const;
119 
120 	void make_current();
121 	void clear_current();
122 	bool is_current() const;
123 
124 	void set_zoom(const Vector2 &p_zoom);
125 	Vector2 get_zoom() const;
126 
127 	Point2 get_camera_screen_center() const;
128 
129 	void set_custom_viewport(Node *p_viewport);
130 	Node *get_custom_viewport() const;
131 
132 	Vector2 get_camera_pos() const;
133 	void force_update_scroll();
134 	void reset_smoothing();
135 	void align();
136 
137 	Camera2D();
138 };
139 
140 VARIANT_ENUM_CAST(Camera2D::AnchorMode);
141 
142 #endif // CAMERA_2D_H
143