camera_2d.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /**************************************************************************/
  2. /* camera_2d.cpp */
  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. #include "camera_2d.h"
  31. #include "core/config/project_settings.h"
  32. #include "scene/main/viewport.h"
  33. bool Camera2D::_is_editing_in_editor() const {
  34. #ifdef TOOLS_ENABLED
  35. return is_part_of_edited_scene();
  36. #else
  37. return false;
  38. #endif // TOOLS_ENABLED
  39. }
  40. void Camera2D::_update_scroll() {
  41. if (!is_inside_tree() || !viewport) {
  42. return;
  43. }
  44. if (_is_editing_in_editor()) {
  45. queue_redraw();
  46. return;
  47. }
  48. if (is_current()) {
  49. ERR_FAIL_COND(custom_viewport && !ObjectDB::get_instance(custom_viewport_id));
  50. Size2 screen_size = _get_camera_screen_size();
  51. Transform2D xform;
  52. if (is_physics_interpolated_and_enabled()) {
  53. xform = _interpolation_data.xform_prev.interpolate_with(_interpolation_data.xform_curr, Engine::get_singleton()->get_physics_interpolation_fraction());
  54. camera_screen_center = xform.affine_inverse().xform(0.5 * screen_size);
  55. } else {
  56. xform = get_camera_transform();
  57. }
  58. viewport->set_canvas_transform(xform);
  59. Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5) : Point2());
  60. Point2 adj_screen_pos = camera_screen_center - (screen_size * 0.5);
  61. // TODO: Remove xform and screen_offset when ParallaxBackground/ParallaxLayer is removed.
  62. get_tree()->call_group(group_name, SNAME("_camera_moved"), xform, screen_offset, adj_screen_pos);
  63. }
  64. }
  65. #ifdef TOOLS_ENABLED
  66. void Camera2D::_project_settings_changed() {
  67. if (screen_drawing_enabled) {
  68. queue_redraw();
  69. }
  70. }
  71. #endif
  72. void Camera2D::_update_process_callback() {
  73. if (is_physics_interpolated_and_enabled()) {
  74. set_process_internal(is_current());
  75. set_physics_process_internal(is_current());
  76. #ifdef TOOLS_ENABLED
  77. if (process_callback == CAMERA2D_PROCESS_IDLE) {
  78. WARN_PRINT_ONCE("Camera2D overridden to physics process mode due to use of physics interpolation.");
  79. }
  80. #endif
  81. } else if (_is_editing_in_editor()) {
  82. set_process_internal(false);
  83. set_physics_process_internal(false);
  84. } else {
  85. if (process_callback == CAMERA2D_PROCESS_IDLE) {
  86. set_process_internal(true);
  87. set_physics_process_internal(false);
  88. } else {
  89. set_process_internal(false);
  90. set_physics_process_internal(true);
  91. }
  92. }
  93. }
  94. void Camera2D::set_zoom(const Vector2 &p_zoom) {
  95. // Setting zoom to zero causes 'affine_invert' issues
  96. ERR_FAIL_COND_MSG(Math::is_zero_approx(p_zoom.x) || Math::is_zero_approx(p_zoom.y), "Zoom level must be different from 0 (can be negative).");
  97. zoom = p_zoom;
  98. zoom_scale = Vector2(1, 1) / zoom;
  99. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  100. _update_scroll();
  101. smoothed_camera_pos = old_smoothed_camera_pos;
  102. }
  103. Vector2 Camera2D::get_zoom() const {
  104. return zoom;
  105. }
  106. Transform2D Camera2D::get_camera_transform() {
  107. if (!get_tree()) {
  108. return Transform2D();
  109. }
  110. ERR_FAIL_COND_V(custom_viewport && !ObjectDB::get_instance(custom_viewport_id), Transform2D());
  111. Size2 screen_size = _get_camera_screen_size();
  112. Point2 new_camera_pos = get_global_position();
  113. Point2 ret_camera_pos;
  114. if (!first) {
  115. if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
  116. if (drag_horizontal_enabled && !_is_editing_in_editor() && !drag_horizontal_offset_changed) {
  117. camera_pos.x = MIN(camera_pos.x, (new_camera_pos.x + screen_size.x * 0.5 * zoom_scale.x * drag_margin[SIDE_LEFT]));
  118. camera_pos.x = MAX(camera_pos.x, (new_camera_pos.x - screen_size.x * 0.5 * zoom_scale.x * drag_margin[SIDE_RIGHT]));
  119. } else {
  120. if (drag_horizontal_offset < 0) {
  121. camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_RIGHT] * drag_horizontal_offset;
  122. } else {
  123. camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_LEFT] * drag_horizontal_offset;
  124. }
  125. drag_horizontal_offset_changed = false;
  126. }
  127. if (drag_vertical_enabled && !_is_editing_in_editor() && !drag_vertical_offset_changed) {
  128. camera_pos.y = MIN(camera_pos.y, (new_camera_pos.y + screen_size.y * 0.5 * zoom_scale.y * drag_margin[SIDE_TOP]));
  129. camera_pos.y = MAX(camera_pos.y, (new_camera_pos.y - screen_size.y * 0.5 * zoom_scale.y * drag_margin[SIDE_BOTTOM]));
  130. } else {
  131. if (drag_vertical_offset < 0) {
  132. camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_BOTTOM] * drag_vertical_offset;
  133. } else {
  134. camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_TOP] * drag_vertical_offset;
  135. }
  136. drag_vertical_offset_changed = false;
  137. }
  138. } else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
  139. camera_pos = new_camera_pos;
  140. }
  141. Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom_scale) : Point2());
  142. Rect2 screen_rect(-screen_offset + camera_pos, screen_size * zoom_scale);
  143. if (limit_smoothing_enabled) {
  144. if (screen_rect.position.x < limit[SIDE_LEFT]) {
  145. camera_pos.x -= screen_rect.position.x - limit[SIDE_LEFT];
  146. }
  147. if (screen_rect.position.x + screen_rect.size.x > limit[SIDE_RIGHT]) {
  148. camera_pos.x -= screen_rect.position.x + screen_rect.size.x - limit[SIDE_RIGHT];
  149. }
  150. if (screen_rect.position.y + screen_rect.size.y > limit[SIDE_BOTTOM]) {
  151. camera_pos.y -= screen_rect.position.y + screen_rect.size.y - limit[SIDE_BOTTOM];
  152. }
  153. if (screen_rect.position.y < limit[SIDE_TOP]) {
  154. camera_pos.y -= screen_rect.position.y - limit[SIDE_TOP];
  155. }
  156. }
  157. // FIXME: There is a bug here, introduced before physics interpolation.
  158. // Smoothing occurs rather confusingly during the call to get_camera_transform().
  159. // It may be called MULTIPLE TIMES on certain frames,
  160. // therefore smoothing is not currently applied only once per frame / tick,
  161. // which will result in some haphazard results.
  162. if (position_smoothing_enabled && !_is_editing_in_editor()) {
  163. bool physics_process = (process_callback == CAMERA2D_PROCESS_PHYSICS) || is_physics_interpolated_and_enabled();
  164. real_t delta = physics_process ? get_physics_process_delta_time() : get_process_delta_time();
  165. real_t c = position_smoothing_speed * delta;
  166. smoothed_camera_pos = ((camera_pos - smoothed_camera_pos) * c) + smoothed_camera_pos;
  167. ret_camera_pos = smoothed_camera_pos;
  168. //camera_pos=camera_pos*(1.0-position_smoothing_speed)+new_camera_pos*position_smoothing_speed;
  169. } else {
  170. ret_camera_pos = smoothed_camera_pos = camera_pos;
  171. }
  172. } else {
  173. ret_camera_pos = smoothed_camera_pos = camera_pos = new_camera_pos;
  174. first = false;
  175. }
  176. Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom_scale) : Point2());
  177. if (!ignore_rotation) {
  178. if (rotation_smoothing_enabled && !_is_editing_in_editor()) {
  179. real_t step = rotation_smoothing_speed * (process_callback == CAMERA2D_PROCESS_PHYSICS ? get_physics_process_delta_time() : get_process_delta_time());
  180. camera_angle = Math::lerp_angle(camera_angle, get_global_rotation(), step);
  181. } else {
  182. camera_angle = get_global_rotation();
  183. }
  184. screen_offset = screen_offset.rotated(camera_angle);
  185. }
  186. Rect2 screen_rect(-screen_offset + ret_camera_pos, screen_size * zoom_scale);
  187. if (!position_smoothing_enabled || !limit_smoothing_enabled) {
  188. if (screen_rect.position.x < limit[SIDE_LEFT]) {
  189. screen_rect.position.x = limit[SIDE_LEFT];
  190. }
  191. if (screen_rect.position.x + screen_rect.size.x > limit[SIDE_RIGHT]) {
  192. screen_rect.position.x = limit[SIDE_RIGHT] - screen_rect.size.x;
  193. }
  194. if (screen_rect.position.y + screen_rect.size.y > limit[SIDE_BOTTOM]) {
  195. screen_rect.position.y = limit[SIDE_BOTTOM] - screen_rect.size.y;
  196. }
  197. if (screen_rect.position.y < limit[SIDE_TOP]) {
  198. screen_rect.position.y = limit[SIDE_TOP];
  199. }
  200. }
  201. if (offset != Vector2()) {
  202. screen_rect.position += offset;
  203. }
  204. Transform2D xform;
  205. xform.scale_basis(zoom_scale);
  206. if (!ignore_rotation) {
  207. xform.set_rotation(camera_angle);
  208. }
  209. xform.set_origin(screen_rect.position);
  210. camera_screen_center = xform.xform(0.5 * screen_size);
  211. return xform.affine_inverse();
  212. }
  213. void Camera2D::_ensure_update_interpolation_data() {
  214. // The "curr -> previous" update can either occur
  215. // on NOTIFICATION_INTERNAL_PHYSICS_PROCESS, OR
  216. // on NOTIFICATION_TRANSFORM_CHANGED,
  217. // if NOTIFICATION_TRANSFORM_CHANGED takes place earlier than
  218. // NOTIFICATION_INTERNAL_PHYSICS_PROCESS on a tick.
  219. // This is to ensure that the data keeps flowing, but the new data
  220. // doesn't overwrite before prev has been set.
  221. // Keep the data flowing.
  222. uint64_t tick = Engine::get_singleton()->get_physics_frames();
  223. if (_interpolation_data.last_update_physics_tick != tick) {
  224. _interpolation_data.xform_prev = _interpolation_data.xform_curr;
  225. _interpolation_data.last_update_physics_tick = tick;
  226. }
  227. }
  228. void Camera2D::_notification(int p_what) {
  229. switch (p_what) {
  230. #ifdef TOOLS_ENABLED
  231. case NOTIFICATION_READY: {
  232. if (is_part_of_edited_scene()) {
  233. ProjectSettings::get_singleton()->connect(SNAME("settings_changed"), callable_mp(this, &Camera2D::_project_settings_changed));
  234. }
  235. } break;
  236. #endif
  237. case NOTIFICATION_INTERNAL_PROCESS: {
  238. _update_scroll();
  239. } break;
  240. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  241. if (is_physics_interpolated_and_enabled()) {
  242. _ensure_update_interpolation_data();
  243. _interpolation_data.xform_curr = get_camera_transform();
  244. } else {
  245. _update_scroll();
  246. }
  247. } break;
  248. case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
  249. // Force the limits etc. to update.
  250. _interpolation_data.xform_curr = get_camera_transform();
  251. _interpolation_data.xform_prev = _interpolation_data.xform_curr;
  252. } break;
  253. case NOTIFICATION_SUSPENDED:
  254. case NOTIFICATION_PAUSED: {
  255. if (is_physics_interpolated_and_enabled()) {
  256. _update_scroll();
  257. }
  258. } break;
  259. case NOTIFICATION_TRANSFORM_CHANGED: {
  260. if ((!position_smoothing_enabled && !is_physics_interpolated_and_enabled()) || _is_editing_in_editor()) {
  261. _update_scroll();
  262. }
  263. if (is_physics_interpolated_and_enabled()) {
  264. _ensure_update_interpolation_data();
  265. _interpolation_data.xform_curr = get_camera_transform();
  266. }
  267. } break;
  268. case NOTIFICATION_ENTER_TREE: {
  269. ERR_FAIL_COND(!is_inside_tree());
  270. if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
  271. viewport = custom_viewport;
  272. } else {
  273. viewport = get_viewport();
  274. }
  275. canvas = get_canvas();
  276. RID vp = viewport->get_viewport_rid();
  277. group_name = "__cameras_" + itos(vp.get_id());
  278. canvas_group_name = "__cameras_c" + itos(canvas.get_id());
  279. add_to_group(group_name);
  280. add_to_group(canvas_group_name);
  281. if (!_is_editing_in_editor() && enabled && !viewport->get_camera_2d()) {
  282. make_current();
  283. }
  284. _update_process_callback();
  285. first = true;
  286. _update_scroll();
  287. // Note that NOTIFICATION_RESET_PHYSICS_INTERPOLATION
  288. // is automatically called before this because Camera2D is inherited
  289. // from CanvasItem. However, the camera transform is not up to date
  290. // until this point, so we do an extra manual reset.
  291. if (is_physics_interpolated_and_enabled()) {
  292. _interpolation_data.xform_curr = get_camera_transform();
  293. _interpolation_data.xform_prev = _interpolation_data.xform_curr;
  294. }
  295. } break;
  296. case NOTIFICATION_EXIT_TREE: {
  297. remove_from_group(group_name);
  298. remove_from_group(canvas_group_name);
  299. if (is_current()) {
  300. clear_current();
  301. }
  302. viewport = nullptr;
  303. just_exited_tree = true;
  304. callable_mp(this, &Camera2D::_reset_just_exited).call_deferred();
  305. } break;
  306. #ifdef TOOLS_ENABLED
  307. case NOTIFICATION_DRAW: {
  308. if (!is_inside_tree() || !_is_editing_in_editor()) {
  309. break;
  310. }
  311. if (screen_drawing_enabled) {
  312. Color area_axis_color(1, 0.4, 1, 0.63);
  313. real_t area_axis_width = -1;
  314. if (is_current()) {
  315. area_axis_width = 3;
  316. }
  317. Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
  318. Size2 screen_size = _get_camera_screen_size();
  319. Vector2 screen_endpoints[4] = {
  320. inv_camera_transform.xform(Vector2(0, 0)),
  321. inv_camera_transform.xform(Vector2(screen_size.width, 0)),
  322. inv_camera_transform.xform(Vector2(screen_size.width, screen_size.height)),
  323. inv_camera_transform.xform(Vector2(0, screen_size.height))
  324. };
  325. Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space
  326. for (int i = 0; i < 4; i++) {
  327. draw_line(inv_transform.xform(screen_endpoints[i]), inv_transform.xform(screen_endpoints[(i + 1) % 4]), area_axis_color, area_axis_width);
  328. }
  329. }
  330. if (limit_drawing_enabled) {
  331. Color limit_drawing_color(1, 1, 0.25, 0.63);
  332. real_t limit_drawing_width = -1;
  333. if (is_current()) {
  334. limit_drawing_width = 3;
  335. }
  336. Vector2 camera_origin = get_global_position();
  337. Vector2 camera_scale = get_global_scale().abs();
  338. Vector2 limit_points[4] = {
  339. (Vector2(limit[SIDE_LEFT], limit[SIDE_TOP]) - camera_origin) / camera_scale,
  340. (Vector2(limit[SIDE_RIGHT], limit[SIDE_TOP]) - camera_origin) / camera_scale,
  341. (Vector2(limit[SIDE_RIGHT], limit[SIDE_BOTTOM]) - camera_origin) / camera_scale,
  342. (Vector2(limit[SIDE_LEFT], limit[SIDE_BOTTOM]) - camera_origin) / camera_scale
  343. };
  344. for (int i = 0; i < 4; i++) {
  345. draw_line(limit_points[i], limit_points[(i + 1) % 4], limit_drawing_color, limit_drawing_width);
  346. }
  347. }
  348. if (margin_drawing_enabled) {
  349. Color margin_drawing_color(0.25, 1, 1, 0.63);
  350. real_t margin_drawing_width = -1;
  351. if (is_current()) {
  352. margin_drawing_width = 3;
  353. }
  354. Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
  355. Size2 screen_size = _get_camera_screen_size();
  356. Vector2 margin_endpoints[4] = {
  357. inv_camera_transform.xform(Vector2((screen_size.width / 2) - ((screen_size.width / 2) * drag_margin[SIDE_LEFT]), (screen_size.height / 2) - ((screen_size.height / 2) * drag_margin[SIDE_TOP]))),
  358. inv_camera_transform.xform(Vector2((screen_size.width / 2) + ((screen_size.width / 2) * drag_margin[SIDE_RIGHT]), (screen_size.height / 2) - ((screen_size.height / 2) * drag_margin[SIDE_TOP]))),
  359. inv_camera_transform.xform(Vector2((screen_size.width / 2) + ((screen_size.width / 2) * drag_margin[SIDE_RIGHT]), (screen_size.height / 2) + ((screen_size.height / 2) * drag_margin[SIDE_BOTTOM]))),
  360. inv_camera_transform.xform(Vector2((screen_size.width / 2) - ((screen_size.width / 2) * drag_margin[SIDE_LEFT]), (screen_size.height / 2) + ((screen_size.height / 2) * drag_margin[SIDE_BOTTOM])))
  361. };
  362. Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space
  363. for (int i = 0; i < 4; i++) {
  364. draw_line(inv_transform.xform(margin_endpoints[i]), inv_transform.xform(margin_endpoints[(i + 1) % 4]), margin_drawing_color, margin_drawing_width);
  365. }
  366. }
  367. } break;
  368. #endif
  369. }
  370. }
  371. void Camera2D::set_offset(const Vector2 &p_offset) {
  372. offset = p_offset;
  373. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  374. _update_scroll();
  375. smoothed_camera_pos = old_smoothed_camera_pos;
  376. }
  377. Vector2 Camera2D::get_offset() const {
  378. return offset;
  379. }
  380. void Camera2D::set_anchor_mode(AnchorMode p_anchor_mode) {
  381. anchor_mode = p_anchor_mode;
  382. _update_scroll();
  383. }
  384. Camera2D::AnchorMode Camera2D::get_anchor_mode() const {
  385. return anchor_mode;
  386. }
  387. void Camera2D::set_ignore_rotation(bool p_ignore) {
  388. ignore_rotation = p_ignore;
  389. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  390. // Reset back to zero so it matches the camera rotation when ignore_rotation is enabled.
  391. if (ignore_rotation) {
  392. camera_angle = 0.0;
  393. }
  394. _update_scroll();
  395. smoothed_camera_pos = old_smoothed_camera_pos;
  396. }
  397. bool Camera2D::is_ignoring_rotation() const {
  398. return ignore_rotation;
  399. }
  400. void Camera2D::set_process_callback(Camera2DProcessCallback p_mode) {
  401. if (process_callback == p_mode) {
  402. return;
  403. }
  404. process_callback = p_mode;
  405. _update_process_callback();
  406. }
  407. void Camera2D::set_enabled(bool p_enabled) {
  408. enabled = p_enabled;
  409. if (!is_inside_tree()) {
  410. return;
  411. }
  412. if (enabled && !viewport->get_camera_2d()) {
  413. make_current();
  414. } else if (!enabled && is_current()) {
  415. clear_current();
  416. }
  417. }
  418. bool Camera2D::is_enabled() const {
  419. return enabled;
  420. }
  421. Camera2D::Camera2DProcessCallback Camera2D::get_process_callback() const {
  422. return process_callback;
  423. }
  424. void Camera2D::_make_current(Object *p_which) {
  425. if (!is_inside_tree() || !viewport) {
  426. return;
  427. }
  428. if (custom_viewport && !ObjectDB::get_instance(custom_viewport_id)) {
  429. return;
  430. }
  431. queue_redraw();
  432. bool was_current = viewport->get_camera_2d() == this;
  433. bool is_current = p_which == this;
  434. if (is_current) {
  435. viewport->_camera_2d_set(this);
  436. } else if (was_current) {
  437. viewport->_camera_2d_set(nullptr);
  438. }
  439. if (is_current != was_current) {
  440. _update_process_callback();
  441. }
  442. }
  443. void Camera2D::_update_process_internal_for_smoothing() {
  444. bool is_not_in_scene_or_editor = !(is_inside_tree() && _is_editing_in_editor());
  445. bool is_any_smoothing_valid = position_smoothing_speed > 0 || rotation_smoothing_speed > 0;
  446. bool enable = is_any_smoothing_valid && is_not_in_scene_or_editor;
  447. set_process_internal(enable);
  448. }
  449. void Camera2D::make_current() {
  450. ERR_FAIL_COND(!enabled || !is_inside_tree());
  451. get_tree()->call_group(group_name, "_make_current", this);
  452. if (just_exited_tree) {
  453. // If camera exited the scene tree in the same frame, group call will skip it, so this needs to be called manually.
  454. _make_current(this);
  455. }
  456. _update_scroll();
  457. _update_process_callback();
  458. }
  459. void Camera2D::clear_current() {
  460. ERR_FAIL_COND(!is_current());
  461. if (!viewport || !viewport->is_inside_tree()) {
  462. return;
  463. }
  464. if (!custom_viewport || ObjectDB::get_instance(custom_viewport_id)) {
  465. viewport->assign_next_enabled_camera_2d(group_name);
  466. }
  467. _update_process_callback();
  468. }
  469. bool Camera2D::is_current() const {
  470. if (!viewport) {
  471. return false;
  472. }
  473. if (!custom_viewport || ObjectDB::get_instance(custom_viewport_id)) {
  474. return viewport->get_camera_2d() == this;
  475. }
  476. return false;
  477. }
  478. void Camera2D::set_limit(Side p_side, int p_limit) {
  479. ERR_FAIL_INDEX((int)p_side, 4);
  480. limit[p_side] = p_limit;
  481. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  482. _update_scroll();
  483. smoothed_camera_pos = old_smoothed_camera_pos;
  484. }
  485. int Camera2D::get_limit(Side p_side) const {
  486. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  487. return limit[p_side];
  488. }
  489. void Camera2D::set_limit_smoothing_enabled(bool enable) {
  490. limit_smoothing_enabled = enable;
  491. _update_scroll();
  492. }
  493. bool Camera2D::is_limit_smoothing_enabled() const {
  494. return limit_smoothing_enabled;
  495. }
  496. void Camera2D::set_drag_margin(Side p_side, real_t p_drag_margin) {
  497. ERR_FAIL_INDEX((int)p_side, 4);
  498. drag_margin[p_side] = p_drag_margin;
  499. queue_redraw();
  500. }
  501. real_t Camera2D::get_drag_margin(Side p_side) const {
  502. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  503. return drag_margin[p_side];
  504. }
  505. Vector2 Camera2D::get_camera_position() const {
  506. return camera_pos;
  507. }
  508. void Camera2D::force_update_scroll() {
  509. _update_scroll();
  510. }
  511. void Camera2D::reset_smoothing() {
  512. _update_scroll();
  513. smoothed_camera_pos = camera_pos;
  514. }
  515. void Camera2D::align() {
  516. ERR_FAIL_COND(custom_viewport && !ObjectDB::get_instance(custom_viewport_id));
  517. Size2 screen_size = _get_camera_screen_size();
  518. Point2 current_camera_pos = get_global_position();
  519. if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
  520. if (drag_horizontal_offset < 0) {
  521. camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_RIGHT] * drag_horizontal_offset;
  522. } else {
  523. camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_LEFT] * drag_horizontal_offset;
  524. }
  525. if (drag_vertical_offset < 0) {
  526. camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_TOP] * drag_vertical_offset;
  527. } else {
  528. camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_BOTTOM] * drag_vertical_offset;
  529. }
  530. } else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
  531. camera_pos = current_camera_pos;
  532. }
  533. _update_scroll();
  534. }
  535. void Camera2D::set_position_smoothing_speed(real_t p_speed) {
  536. position_smoothing_speed = MAX(0, p_speed);
  537. _update_process_internal_for_smoothing();
  538. }
  539. real_t Camera2D::get_position_smoothing_speed() const {
  540. return position_smoothing_speed;
  541. }
  542. void Camera2D::set_rotation_smoothing_speed(real_t p_speed) {
  543. rotation_smoothing_speed = MAX(0, p_speed);
  544. _update_process_internal_for_smoothing();
  545. }
  546. real_t Camera2D::get_rotation_smoothing_speed() const {
  547. return rotation_smoothing_speed;
  548. }
  549. void Camera2D::set_rotation_smoothing_enabled(bool p_enabled) {
  550. rotation_smoothing_enabled = p_enabled;
  551. notify_property_list_changed();
  552. }
  553. bool Camera2D::is_rotation_smoothing_enabled() const {
  554. return rotation_smoothing_enabled;
  555. }
  556. Point2 Camera2D::get_camera_screen_center() const {
  557. return camera_screen_center;
  558. }
  559. Size2 Camera2D::_get_camera_screen_size() const {
  560. if (_is_editing_in_editor()) {
  561. return Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
  562. }
  563. return get_viewport_rect().size;
  564. }
  565. void Camera2D::set_drag_horizontal_enabled(bool p_enabled) {
  566. drag_horizontal_enabled = p_enabled;
  567. }
  568. bool Camera2D::is_drag_horizontal_enabled() const {
  569. return drag_horizontal_enabled;
  570. }
  571. void Camera2D::set_drag_vertical_enabled(bool p_enabled) {
  572. drag_vertical_enabled = p_enabled;
  573. }
  574. bool Camera2D::is_drag_vertical_enabled() const {
  575. return drag_vertical_enabled;
  576. }
  577. void Camera2D::set_drag_vertical_offset(real_t p_offset) {
  578. drag_vertical_offset = p_offset;
  579. drag_vertical_offset_changed = true;
  580. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  581. _update_scroll();
  582. smoothed_camera_pos = old_smoothed_camera_pos;
  583. }
  584. real_t Camera2D::get_drag_vertical_offset() const {
  585. return drag_vertical_offset;
  586. }
  587. void Camera2D::set_drag_horizontal_offset(real_t p_offset) {
  588. drag_horizontal_offset = p_offset;
  589. drag_horizontal_offset_changed = true;
  590. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  591. _update_scroll();
  592. smoothed_camera_pos = old_smoothed_camera_pos;
  593. }
  594. real_t Camera2D::get_drag_horizontal_offset() const {
  595. return drag_horizontal_offset;
  596. }
  597. void Camera2D::_set_old_smoothing(real_t p_enable) {
  598. //compatibility
  599. if (p_enable > 0) {
  600. position_smoothing_enabled = true;
  601. set_position_smoothing_speed(p_enable);
  602. }
  603. }
  604. void Camera2D::set_position_smoothing_enabled(bool p_enabled) {
  605. position_smoothing_enabled = p_enabled;
  606. notify_property_list_changed();
  607. }
  608. bool Camera2D::is_position_smoothing_enabled() const {
  609. return position_smoothing_enabled;
  610. }
  611. void Camera2D::set_custom_viewport(Node *p_viewport) {
  612. ERR_FAIL_NULL(p_viewport);
  613. if (is_inside_tree()) {
  614. remove_from_group(group_name);
  615. remove_from_group(canvas_group_name);
  616. }
  617. custom_viewport = Object::cast_to<Viewport>(p_viewport);
  618. if (custom_viewport) {
  619. custom_viewport_id = custom_viewport->get_instance_id();
  620. } else {
  621. custom_viewport_id = ObjectID();
  622. }
  623. if (is_inside_tree()) {
  624. if (custom_viewport) {
  625. viewport = custom_viewport;
  626. } else {
  627. viewport = get_viewport();
  628. }
  629. RID vp = viewport->get_viewport_rid();
  630. group_name = "__cameras_" + itos(vp.get_id());
  631. canvas_group_name = "__cameras_c" + itos(canvas.get_id());
  632. add_to_group(group_name);
  633. add_to_group(canvas_group_name);
  634. }
  635. }
  636. Node *Camera2D::get_custom_viewport() const {
  637. return custom_viewport;
  638. }
  639. void Camera2D::set_screen_drawing_enabled(bool enable) {
  640. screen_drawing_enabled = enable;
  641. #ifdef TOOLS_ENABLED
  642. queue_redraw();
  643. #endif
  644. }
  645. bool Camera2D::is_screen_drawing_enabled() const {
  646. return screen_drawing_enabled;
  647. }
  648. void Camera2D::set_limit_drawing_enabled(bool enable) {
  649. limit_drawing_enabled = enable;
  650. #ifdef TOOLS_ENABLED
  651. queue_redraw();
  652. #endif
  653. }
  654. bool Camera2D::is_limit_drawing_enabled() const {
  655. return limit_drawing_enabled;
  656. }
  657. void Camera2D::set_margin_drawing_enabled(bool enable) {
  658. margin_drawing_enabled = enable;
  659. #ifdef TOOLS_ENABLED
  660. queue_redraw();
  661. #endif
  662. }
  663. bool Camera2D::is_margin_drawing_enabled() const {
  664. return margin_drawing_enabled;
  665. }
  666. void Camera2D::_validate_property(PropertyInfo &p_property) const {
  667. if (!position_smoothing_enabled && p_property.name == "position_smoothing_speed") {
  668. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  669. }
  670. if (!rotation_smoothing_enabled && p_property.name == "rotation_smoothing_speed") {
  671. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  672. }
  673. }
  674. void Camera2D::_bind_methods() {
  675. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Camera2D::set_offset);
  676. ClassDB::bind_method(D_METHOD("get_offset"), &Camera2D::get_offset);
  677. ClassDB::bind_method(D_METHOD("set_anchor_mode", "anchor_mode"), &Camera2D::set_anchor_mode);
  678. ClassDB::bind_method(D_METHOD("get_anchor_mode"), &Camera2D::get_anchor_mode);
  679. ClassDB::bind_method(D_METHOD("set_ignore_rotation", "ignore"), &Camera2D::set_ignore_rotation);
  680. ClassDB::bind_method(D_METHOD("is_ignoring_rotation"), &Camera2D::is_ignoring_rotation);
  681. ClassDB::bind_method(D_METHOD("_update_scroll"), &Camera2D::_update_scroll);
  682. ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &Camera2D::set_process_callback);
  683. ClassDB::bind_method(D_METHOD("get_process_callback"), &Camera2D::get_process_callback);
  684. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &Camera2D::set_enabled);
  685. ClassDB::bind_method(D_METHOD("is_enabled"), &Camera2D::is_enabled);
  686. ClassDB::bind_method(D_METHOD("make_current"), &Camera2D::make_current);
  687. ClassDB::bind_method(D_METHOD("is_current"), &Camera2D::is_current);
  688. ClassDB::bind_method(D_METHOD("_make_current"), &Camera2D::_make_current);
  689. ClassDB::bind_method(D_METHOD("set_limit", "margin", "limit"), &Camera2D::set_limit);
  690. ClassDB::bind_method(D_METHOD("get_limit", "margin"), &Camera2D::get_limit);
  691. ClassDB::bind_method(D_METHOD("set_limit_smoothing_enabled", "limit_smoothing_enabled"), &Camera2D::set_limit_smoothing_enabled);
  692. ClassDB::bind_method(D_METHOD("is_limit_smoothing_enabled"), &Camera2D::is_limit_smoothing_enabled);
  693. ClassDB::bind_method(D_METHOD("set_drag_vertical_enabled", "enabled"), &Camera2D::set_drag_vertical_enabled);
  694. ClassDB::bind_method(D_METHOD("is_drag_vertical_enabled"), &Camera2D::is_drag_vertical_enabled);
  695. ClassDB::bind_method(D_METHOD("set_drag_horizontal_enabled", "enabled"), &Camera2D::set_drag_horizontal_enabled);
  696. ClassDB::bind_method(D_METHOD("is_drag_horizontal_enabled"), &Camera2D::is_drag_horizontal_enabled);
  697. ClassDB::bind_method(D_METHOD("set_drag_vertical_offset", "offset"), &Camera2D::set_drag_vertical_offset);
  698. ClassDB::bind_method(D_METHOD("get_drag_vertical_offset"), &Camera2D::get_drag_vertical_offset);
  699. ClassDB::bind_method(D_METHOD("set_drag_horizontal_offset", "offset"), &Camera2D::set_drag_horizontal_offset);
  700. ClassDB::bind_method(D_METHOD("get_drag_horizontal_offset"), &Camera2D::get_drag_horizontal_offset);
  701. ClassDB::bind_method(D_METHOD("set_drag_margin", "margin", "drag_margin"), &Camera2D::set_drag_margin);
  702. ClassDB::bind_method(D_METHOD("get_drag_margin", "margin"), &Camera2D::get_drag_margin);
  703. ClassDB::bind_method(D_METHOD("get_target_position"), &Camera2D::get_camera_position);
  704. ClassDB::bind_method(D_METHOD("get_screen_center_position"), &Camera2D::get_camera_screen_center);
  705. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &Camera2D::set_zoom);
  706. ClassDB::bind_method(D_METHOD("get_zoom"), &Camera2D::get_zoom);
  707. ClassDB::bind_method(D_METHOD("set_custom_viewport", "viewport"), &Camera2D::set_custom_viewport);
  708. ClassDB::bind_method(D_METHOD("get_custom_viewport"), &Camera2D::get_custom_viewport);
  709. ClassDB::bind_method(D_METHOD("set_position_smoothing_speed", "position_smoothing_speed"), &Camera2D::set_position_smoothing_speed);
  710. ClassDB::bind_method(D_METHOD("get_position_smoothing_speed"), &Camera2D::get_position_smoothing_speed);
  711. ClassDB::bind_method(D_METHOD("set_position_smoothing_enabled", "position_smoothing_speed"), &Camera2D::set_position_smoothing_enabled);
  712. ClassDB::bind_method(D_METHOD("is_position_smoothing_enabled"), &Camera2D::is_position_smoothing_enabled);
  713. ClassDB::bind_method(D_METHOD("set_rotation_smoothing_enabled", "enabled"), &Camera2D::set_rotation_smoothing_enabled);
  714. ClassDB::bind_method(D_METHOD("is_rotation_smoothing_enabled"), &Camera2D::is_rotation_smoothing_enabled);
  715. ClassDB::bind_method(D_METHOD("set_rotation_smoothing_speed", "speed"), &Camera2D::set_rotation_smoothing_speed);
  716. ClassDB::bind_method(D_METHOD("get_rotation_smoothing_speed"), &Camera2D::get_rotation_smoothing_speed);
  717. ClassDB::bind_method(D_METHOD("force_update_scroll"), &Camera2D::force_update_scroll);
  718. ClassDB::bind_method(D_METHOD("reset_smoothing"), &Camera2D::reset_smoothing);
  719. ClassDB::bind_method(D_METHOD("align"), &Camera2D::align);
  720. ClassDB::bind_method(D_METHOD("_set_old_smoothing", "follow_smoothing"), &Camera2D::_set_old_smoothing);
  721. ClassDB::bind_method(D_METHOD("set_screen_drawing_enabled", "screen_drawing_enabled"), &Camera2D::set_screen_drawing_enabled);
  722. ClassDB::bind_method(D_METHOD("is_screen_drawing_enabled"), &Camera2D::is_screen_drawing_enabled);
  723. ClassDB::bind_method(D_METHOD("set_limit_drawing_enabled", "limit_drawing_enabled"), &Camera2D::set_limit_drawing_enabled);
  724. ClassDB::bind_method(D_METHOD("is_limit_drawing_enabled"), &Camera2D::is_limit_drawing_enabled);
  725. ClassDB::bind_method(D_METHOD("set_margin_drawing_enabled", "margin_drawing_enabled"), &Camera2D::set_margin_drawing_enabled);
  726. ClassDB::bind_method(D_METHOD("is_margin_drawing_enabled"), &Camera2D::is_margin_drawing_enabled);
  727. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset");
  728. ADD_PROPERTY(PropertyInfo(Variant::INT, "anchor_mode", PROPERTY_HINT_ENUM, "Fixed Top Left,Drag Center"), "set_anchor_mode", "get_anchor_mode");
  729. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_rotation"), "set_ignore_rotation", "is_ignoring_rotation");
  730. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  731. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "zoom", PROPERTY_HINT_LINK), "set_zoom", "get_zoom");
  732. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport", PROPERTY_USAGE_NONE), "set_custom_viewport", "get_custom_viewport");
  733. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_callback", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_process_callback", "get_process_callback");
  734. ADD_GROUP("Limit", "limit_");
  735. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_left", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_LEFT);
  736. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_top", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_TOP);
  737. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_right", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_RIGHT);
  738. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_bottom", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_BOTTOM);
  739. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "limit_smoothed"), "set_limit_smoothing_enabled", "is_limit_smoothing_enabled");
  740. ADD_GROUP("Position Smoothing", "position_smoothing_");
  741. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "position_smoothing_enabled"), "set_position_smoothing_enabled", "is_position_smoothing_enabled");
  742. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "position_smoothing_speed", PROPERTY_HINT_NONE, "suffix:px/s"), "set_position_smoothing_speed", "get_position_smoothing_speed");
  743. ADD_GROUP("Rotation Smoothing", "rotation_smoothing_");
  744. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotation_smoothing_enabled"), "set_rotation_smoothing_enabled", "is_rotation_smoothing_enabled");
  745. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation_smoothing_speed"), "set_rotation_smoothing_speed", "get_rotation_smoothing_speed");
  746. ADD_GROUP("Drag", "drag_");
  747. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_horizontal_enabled"), "set_drag_horizontal_enabled", "is_drag_horizontal_enabled");
  748. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_vertical_enabled"), "set_drag_vertical_enabled", "is_drag_vertical_enabled");
  749. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "drag_horizontal_offset", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_drag_horizontal_offset", "get_drag_horizontal_offset");
  750. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "drag_vertical_offset", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_drag_vertical_offset", "get_drag_vertical_offset");
  751. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_left_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_LEFT);
  752. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_top_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_TOP);
  753. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_right_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_RIGHT);
  754. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_bottom_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_BOTTOM);
  755. ADD_GROUP("Editor", "editor_");
  756. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_screen"), "set_screen_drawing_enabled", "is_screen_drawing_enabled");
  757. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_limits"), "set_limit_drawing_enabled", "is_limit_drawing_enabled");
  758. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_drag_margin"), "set_margin_drawing_enabled", "is_margin_drawing_enabled");
  759. BIND_ENUM_CONSTANT(ANCHOR_MODE_FIXED_TOP_LEFT);
  760. BIND_ENUM_CONSTANT(ANCHOR_MODE_DRAG_CENTER);
  761. BIND_ENUM_CONSTANT(CAMERA2D_PROCESS_PHYSICS);
  762. BIND_ENUM_CONSTANT(CAMERA2D_PROCESS_IDLE);
  763. }
  764. Camera2D::Camera2D() {
  765. limit[SIDE_LEFT] = -10000000;
  766. limit[SIDE_TOP] = -10000000;
  767. limit[SIDE_RIGHT] = 10000000;
  768. limit[SIDE_BOTTOM] = 10000000;
  769. drag_margin[SIDE_LEFT] = 0.2;
  770. drag_margin[SIDE_TOP] = 0.2;
  771. drag_margin[SIDE_RIGHT] = 0.2;
  772. drag_margin[SIDE_BOTTOM] = 0.2;
  773. set_notify_transform(true);
  774. set_hide_clip_children(true);
  775. }