| 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 | #ifndef CAMERA_2D_H |
| 32 | #define CAMERA_2D_H |
| 33 | |
| 34 | #include "scene/2d/node_2d.h" |
| 35 | |
| 36 | class Camera2D : public Node2D { |
| 37 | GDCLASS(Camera2D, Node2D); |
| 38 | |
| 39 | public: |
| 40 | enum AnchorMode { |
| 41 | ANCHOR_MODE_FIXED_TOP_LEFT, |
| 42 | ANCHOR_MODE_DRAG_CENTER |
| 43 | }; |
| 44 | |
| 45 | enum Camera2DProcessCallback { |
| 46 | CAMERA2D_PROCESS_PHYSICS, |
| 47 | CAMERA2D_PROCESS_IDLE |
| 48 | }; |
| 49 | |
| 50 | protected: |
| 51 | Point2 camera_pos; |
| 52 | Point2 smoothed_camera_pos; |
| 53 | bool first = true; |
| 54 | bool just_exited_tree = false; |
| 55 | |
| 56 | ObjectID custom_viewport_id; // to check validity |
| 57 | Viewport *custom_viewport = nullptr; |
| 58 | Viewport *viewport = nullptr; |
| 59 | |
| 60 | StringName group_name; |
| 61 | StringName canvas_group_name; |
| 62 | RID canvas; |
| 63 | Vector2 offset; |
| 64 | Vector2 zoom = Vector2(1, 1); |
| 65 | Vector2 zoom_scale = Vector2(1, 1); |
| 66 | AnchorMode anchor_mode = ANCHOR_MODE_DRAG_CENTER; |
| 67 | bool ignore_rotation = true; |
| 68 | bool enabled = true; |
| 69 | real_t position_smoothing_speed = 5.0; |
| 70 | bool position_smoothing_enabled = false; |
| 71 | |
| 72 | real_t camera_angle = 0.0; |
| 73 | real_t rotation_smoothing_speed = 5.0; |
| 74 | bool rotation_smoothing_enabled = false; |
| 75 | |
| 76 | int limit[4]; |
| 77 | bool limit_smoothing_enabled = false; |
| 78 | |
| 79 | real_t drag_margin[4]; |
| 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 | void _make_current(Object *p_which); |
| 93 | void _reset_just_exited() { just_exited_tree = false; } |
| 94 | |
| 95 | void _set_old_smoothing(real_t p_enable); |
| 96 | |
| 97 | void _update_process_internal_for_smoothing(); |
| 98 | |
| 99 | bool screen_drawing_enabled = true; |
| 100 | bool limit_drawing_enabled = false; |
| 101 | bool margin_drawing_enabled = false; |
| 102 | |
| 103 | Camera2DProcessCallback process_callback = CAMERA2D_PROCESS_IDLE; |
| 104 | |
| 105 | Size2 _get_camera_screen_size() const; |
| 106 | |
| 107 | protected: |
| 108 | virtual Transform2D get_camera_transform(); |
| 109 | |
| 110 | void _notification(int p_what); |
| 111 | static void _bind_methods(); |
| 112 | void _validate_property(PropertyInfo &p_property) const; |
| 113 | |
| 114 | public: |
| 115 | void set_offset(const Vector2 &p_offset); |
| 116 | Vector2 get_offset() const; |
| 117 | |
| 118 | void set_anchor_mode(AnchorMode p_anchor_mode); |
| 119 | AnchorMode get_anchor_mode() const; |
| 120 | |
| 121 | void set_ignore_rotation(bool p_ignore); |
| 122 | bool is_ignoring_rotation() const; |
| 123 | |
| 124 | void set_limit(Side p_side, int p_limit); |
| 125 | int get_limit(Side p_side) const; |
| 126 | |
| 127 | void set_limit_smoothing_enabled(bool enable); |
| 128 | bool is_limit_smoothing_enabled() const; |
| 129 | |
| 130 | void set_drag_horizontal_enabled(bool p_enabled); |
| 131 | bool is_drag_horizontal_enabled() const; |
| 132 | |
| 133 | void set_drag_vertical_enabled(bool p_enabled); |
| 134 | bool is_drag_vertical_enabled() const; |
| 135 | |
| 136 | void set_drag_margin(Side p_side, real_t p_drag_margin); |
| 137 | real_t get_drag_margin(Side p_side) const; |
| 138 | |
| 139 | void set_drag_horizontal_offset(real_t p_offset); |
| 140 | real_t get_drag_horizontal_offset() const; |
| 141 | |
| 142 | void set_drag_vertical_offset(real_t p_offset); |
| 143 | real_t get_drag_vertical_offset() const; |
| 144 | |
| 145 | void set_position_smoothing_enabled(bool p_enabled); |
| 146 | bool is_position_smoothing_enabled() const; |
| 147 | |
| 148 | void set_position_smoothing_speed(real_t p_speed); |
| 149 | real_t get_position_smoothing_speed() const; |
| 150 | |
| 151 | void set_rotation_smoothing_speed(real_t p_speed); |
| 152 | real_t get_rotation_smoothing_speed() const; |
| 153 | |
| 154 | void set_rotation_smoothing_enabled(bool p_enabled); |
| 155 | bool is_rotation_smoothing_enabled() const; |
| 156 | |
| 157 | void set_process_callback(Camera2DProcessCallback p_mode); |
| 158 | Camera2DProcessCallback get_process_callback() const; |
| 159 | |
| 160 | void set_enabled(bool p_enabled); |
| 161 | bool is_enabled() const; |
| 162 | |
| 163 | void make_current(); |
| 164 | void clear_current(); |
| 165 | bool is_current() const; |
| 166 | |
| 167 | void set_zoom(const Vector2 &p_zoom); |
| 168 | Vector2 get_zoom() const; |
| 169 | |
| 170 | Point2 get_camera_screen_center() const; |
| 171 | |
| 172 | void set_custom_viewport(Node *p_viewport); |
| 173 | Node *get_custom_viewport() const; |
| 174 | |
| 175 | Vector2 get_camera_position() const; |
| 176 | void force_update_scroll(); |
| 177 | void reset_smoothing(); |
| 178 | void align(); |
| 179 | |
| 180 | void set_screen_drawing_enabled(bool enable); |
| 181 | bool is_screen_drawing_enabled() const; |
| 182 | |
| 183 | void set_limit_drawing_enabled(bool enable); |
| 184 | bool is_limit_drawing_enabled() const; |
| 185 | |
| 186 | void set_margin_drawing_enabled(bool enable); |
| 187 | bool is_margin_drawing_enabled() const; |
| 188 | |
| 189 | Camera2D(); |
| 190 | }; |
| 191 | |
| 192 | VARIANT_ENUM_CAST(Camera2D::AnchorMode); |
| 193 | VARIANT_ENUM_CAST(Camera2D::Camera2DProcessCallback); |
| 194 | |
| 195 | #endif // CAMERA_2D_H |
| 196 | |