camera_2d.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. if (Engine::get_singleton()->is_in_physics_frame()) {
  266. _interpolation_data.xform_curr = get_camera_transform();
  267. }
  268. }
  269. } break;
  270. case NOTIFICATION_ENTER_TREE: {
  271. ERR_FAIL_COND(!is_inside_tree());
  272. if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
  273. viewport = custom_viewport;
  274. } else {
  275. viewport = get_viewport();
  276. }
  277. canvas = get_canvas();
  278. RID vp = viewport->get_viewport_rid();
  279. group_name = "__cameras_" + itos(vp.get_id());
  280. canvas_group_name = "__cameras_c" + itos(canvas.get_id());
  281. add_to_group(group_name);
  282. add_to_group(canvas_group_name);
  283. if (!_is_editing_in_editor() && enabled && !viewport->get_camera_2d()) {
  284. make_current();
  285. }
  286. _update_process_callback();
  287. first = true;
  288. _update_scroll();
  289. // Note that NOTIFICATION_RESET_PHYSICS_INTERPOLATION
  290. // is automatically called before this because Camera2D is inherited
  291. // from CanvasItem. However, the camera transform is not up to date
  292. // until this point, so we do an extra manual reset.
  293. if (is_physics_interpolated_and_enabled()) {
  294. _interpolation_data.xform_curr = get_camera_transform();
  295. _interpolation_data.xform_prev = _interpolation_data.xform_curr;
  296. }
  297. } break;
  298. case NOTIFICATION_EXIT_TREE: {
  299. remove_from_group(group_name);
  300. remove_from_group(canvas_group_name);
  301. if (is_current()) {
  302. clear_current();
  303. }
  304. viewport = nullptr;
  305. just_exited_tree = true;
  306. callable_mp(this, &Camera2D::_reset_just_exited).call_deferred();
  307. } break;
  308. #ifdef TOOLS_ENABLED
  309. case NOTIFICATION_DRAW: {
  310. if (!is_inside_tree() || !_is_editing_in_editor()) {
  311. break;
  312. }
  313. if (screen_drawing_enabled) {
  314. Color area_axis_color(1, 0.4, 1, 0.63);
  315. real_t area_axis_width = -1;
  316. if (is_current()) {
  317. area_axis_width = 3;
  318. }
  319. Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
  320. Size2 screen_size = _get_camera_screen_size();
  321. Vector2 screen_endpoints[4] = {
  322. inv_camera_transform.xform(Vector2(0, 0)),
  323. inv_camera_transform.xform(Vector2(screen_size.width, 0)),
  324. inv_camera_transform.xform(Vector2(screen_size.width, screen_size.height)),
  325. inv_camera_transform.xform(Vector2(0, screen_size.height))
  326. };
  327. Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space
  328. for (int i = 0; i < 4; i++) {
  329. draw_line(inv_transform.xform(screen_endpoints[i]), inv_transform.xform(screen_endpoints[(i + 1) % 4]), area_axis_color, area_axis_width);
  330. }
  331. }
  332. if (limit_drawing_enabled) {
  333. Color limit_drawing_color(1, 1, 0.25, 0.63);
  334. real_t limit_drawing_width = -1;
  335. if (is_current()) {
  336. limit_drawing_width = 3;
  337. }
  338. Vector2 camera_origin = get_global_position();
  339. Vector2 camera_scale = get_global_scale().abs();
  340. Vector2 limit_points[4] = {
  341. (Vector2(limit[SIDE_LEFT], limit[SIDE_TOP]) - camera_origin) / camera_scale,
  342. (Vector2(limit[SIDE_RIGHT], limit[SIDE_TOP]) - camera_origin) / camera_scale,
  343. (Vector2(limit[SIDE_RIGHT], limit[SIDE_BOTTOM]) - camera_origin) / camera_scale,
  344. (Vector2(limit[SIDE_LEFT], limit[SIDE_BOTTOM]) - camera_origin) / camera_scale
  345. };
  346. for (int i = 0; i < 4; i++) {
  347. draw_line(limit_points[i], limit_points[(i + 1) % 4], limit_drawing_color, limit_drawing_width);
  348. }
  349. }
  350. if (margin_drawing_enabled) {
  351. Color margin_drawing_color(0.25, 1, 1, 0.63);
  352. real_t margin_drawing_width = -1;
  353. if (is_current()) {
  354. margin_drawing_width = 3;
  355. }
  356. Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
  357. Size2 screen_size = _get_camera_screen_size();
  358. Vector2 margin_endpoints[4] = {
  359. 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]))),
  360. 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]))),
  361. 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]))),
  362. 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])))
  363. };
  364. Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space
  365. for (int i = 0; i < 4; i++) {
  366. draw_line(inv_transform.xform(margin_endpoints[i]), inv_transform.xform(margin_endpoints[(i + 1) % 4]), margin_drawing_color, margin_drawing_width);
  367. }
  368. }
  369. } break;
  370. #endif
  371. }
  372. }
  373. void Camera2D::set_offset(const Vector2 &p_offset) {
  374. offset = p_offset;
  375. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  376. _update_scroll();
  377. smoothed_camera_pos = old_smoothed_camera_pos;
  378. }
  379. Vector2 Camera2D::get_offset() const {
  380. return offset;
  381. }
  382. void Camera2D::set_anchor_mode(AnchorMode p_anchor_mode) {
  383. anchor_mode = p_anchor_mode;
  384. _update_scroll();
  385. }
  386. Camera2D::AnchorMode Camera2D::get_anchor_mode() const {
  387. return anchor_mode;
  388. }
  389. void Camera2D::set_ignore_rotation(bool p_ignore) {
  390. ignore_rotation = p_ignore;
  391. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  392. // Reset back to zero so it matches the camera rotation when ignore_rotation is enabled.
  393. if (ignore_rotation) {
  394. camera_angle = 0.0;
  395. }
  396. _update_scroll();
  397. smoothed_camera_pos = old_smoothed_camera_pos;
  398. }
  399. bool Camera2D::is_ignoring_rotation() const {
  400. return ignore_rotation;
  401. }
  402. void Camera2D::set_process_callback(Camera2DProcessCallback p_mode) {
  403. if (process_callback == p_mode) {
  404. return;
  405. }
  406. process_callback = p_mode;
  407. _update_process_callback();
  408. }
  409. void Camera2D::set_enabled(bool p_enabled) {
  410. enabled = p_enabled;
  411. if (!is_inside_tree()) {
  412. return;
  413. }
  414. if (enabled && !viewport->get_camera_2d()) {
  415. make_current();
  416. } else if (!enabled && is_current()) {
  417. clear_current();
  418. }
  419. }
  420. bool Camera2D::is_enabled() const {
  421. return enabled;
  422. }
  423. Camera2D::Camera2DProcessCallback Camera2D::get_process_callback() const {
  424. return process_callback;
  425. }
  426. void Camera2D::_make_current(Object *p_which) {
  427. if (!is_inside_tree() || !viewport) {
  428. return;
  429. }
  430. if (custom_viewport && !ObjectDB::get_instance(custom_viewport_id)) {
  431. return;
  432. }
  433. queue_redraw();
  434. bool was_current = viewport->get_camera_2d() == this;
  435. bool is_current = p_which == this;
  436. if (is_current) {
  437. viewport->_camera_2d_set(this);
  438. } else if (was_current) {
  439. viewport->_camera_2d_set(nullptr);
  440. }
  441. if (is_current != was_current) {
  442. _update_process_callback();
  443. }
  444. }
  445. void Camera2D::_update_process_internal_for_smoothing() {
  446. bool is_not_in_scene_or_editor = !(is_inside_tree() && _is_editing_in_editor());
  447. bool is_any_smoothing_valid = position_smoothing_speed > 0 || rotation_smoothing_speed > 0;
  448. bool enable = is_any_smoothing_valid && is_not_in_scene_or_editor;
  449. set_process_internal(enable);
  450. }
  451. void Camera2D::make_current() {
  452. ERR_FAIL_COND(!enabled || !is_inside_tree());
  453. get_tree()->call_group(group_name, "_make_current", this);
  454. if (just_exited_tree) {
  455. // If camera exited the scene tree in the same frame, group call will skip it, so this needs to be called manually.
  456. _make_current(this);
  457. }
  458. _update_scroll();
  459. _update_process_callback();
  460. }
  461. void Camera2D::clear_current() {
  462. ERR_FAIL_COND(!is_current());
  463. if (!viewport || !viewport->is_inside_tree()) {
  464. return;
  465. }
  466. if (!custom_viewport || ObjectDB::get_instance(custom_viewport_id)) {
  467. viewport->assign_next_enabled_camera_2d(group_name);
  468. }
  469. _update_process_callback();
  470. }
  471. bool Camera2D::is_current() const {
  472. if (!viewport) {
  473. return false;
  474. }
  475. if (!custom_viewport || ObjectDB::get_instance(custom_viewport_id)) {
  476. return viewport->get_camera_2d() == this;
  477. }
  478. return false;
  479. }
  480. void Camera2D::set_limit(Side p_side, int p_limit) {
  481. ERR_FAIL_INDEX((int)p_side, 4);
  482. limit[p_side] = p_limit;
  483. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  484. _update_scroll();
  485. smoothed_camera_pos = old_smoothed_camera_pos;
  486. }
  487. int Camera2D::get_limit(Side p_side) const {
  488. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  489. return limit[p_side];
  490. }
  491. void Camera2D::set_limit_smoothing_enabled(bool enable) {
  492. limit_smoothing_enabled = enable;
  493. _update_scroll();
  494. }
  495. bool Camera2D::is_limit_smoothing_enabled() const {
  496. return limit_smoothing_enabled;
  497. }
  498. void Camera2D::set_drag_margin(Side p_side, real_t p_drag_margin) {
  499. ERR_FAIL_INDEX((int)p_side, 4);
  500. drag_margin[p_side] = p_drag_margin;
  501. queue_redraw();
  502. }
  503. real_t Camera2D::get_drag_margin(Side p_side) const {
  504. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  505. return drag_margin[p_side];
  506. }
  507. Vector2 Camera2D::get_camera_position() const {
  508. return camera_pos;
  509. }
  510. void Camera2D::force_update_scroll() {
  511. _update_scroll();
  512. }
  513. void Camera2D::reset_smoothing() {
  514. _update_scroll();
  515. smoothed_camera_pos = camera_pos;
  516. }
  517. void Camera2D::align() {
  518. ERR_FAIL_COND(custom_viewport && !ObjectDB::get_instance(custom_viewport_id));
  519. Size2 screen_size = _get_camera_screen_size();
  520. Point2 current_camera_pos = get_global_position();
  521. if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
  522. if (drag_horizontal_offset < 0) {
  523. camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_RIGHT] * drag_horizontal_offset;
  524. } else {
  525. camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_LEFT] * drag_horizontal_offset;
  526. }
  527. if (drag_vertical_offset < 0) {
  528. camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_TOP] * drag_vertical_offset;
  529. } else {
  530. camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_BOTTOM] * drag_vertical_offset;
  531. }
  532. } else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
  533. camera_pos = current_camera_pos;
  534. }
  535. _update_scroll();
  536. }
  537. void Camera2D::set_position_smoothing_speed(real_t p_speed) {
  538. position_smoothing_speed = MAX(0, p_speed);
  539. _update_process_internal_for_smoothing();
  540. }
  541. real_t Camera2D::get_position_smoothing_speed() const {
  542. return position_smoothing_speed;
  543. }
  544. void Camera2D::set_rotation_smoothing_speed(real_t p_speed) {
  545. rotation_smoothing_speed = MAX(0, p_speed);
  546. _update_process_internal_for_smoothing();
  547. }
  548. real_t Camera2D::get_rotation_smoothing_speed() const {
  549. return rotation_smoothing_speed;
  550. }
  551. void Camera2D::set_rotation_smoothing_enabled(bool p_enabled) {
  552. rotation_smoothing_enabled = p_enabled;
  553. notify_property_list_changed();
  554. }
  555. bool Camera2D::is_rotation_smoothing_enabled() const {
  556. return rotation_smoothing_enabled;
  557. }
  558. Point2 Camera2D::get_camera_screen_center() const {
  559. return camera_screen_center;
  560. }
  561. Size2 Camera2D::_get_camera_screen_size() const {
  562. if (_is_editing_in_editor()) {
  563. return Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
  564. }
  565. return get_viewport_rect().size;
  566. }
  567. void Camera2D::set_drag_horizontal_enabled(bool p_enabled) {
  568. drag_horizontal_enabled = p_enabled;
  569. }
  570. bool Camera2D::is_drag_horizontal_enabled() const {
  571. return drag_horizontal_enabled;
  572. }
  573. void Camera2D::set_drag_vertical_enabled(bool p_enabled) {
  574. drag_vertical_enabled = p_enabled;
  575. }
  576. bool Camera2D::is_drag_vertical_enabled() const {
  577. return drag_vertical_enabled;
  578. }
  579. void Camera2D::set_drag_vertical_offset(real_t p_offset) {
  580. drag_vertical_offset = p_offset;
  581. drag_vertical_offset_changed = true;
  582. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  583. _update_scroll();
  584. smoothed_camera_pos = old_smoothed_camera_pos;
  585. }
  586. real_t Camera2D::get_drag_vertical_offset() const {
  587. return drag_vertical_offset;
  588. }
  589. void Camera2D::set_drag_horizontal_offset(real_t p_offset) {
  590. drag_horizontal_offset = p_offset;
  591. drag_horizontal_offset_changed = true;
  592. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  593. _update_scroll();
  594. smoothed_camera_pos = old_smoothed_camera_pos;
  595. }
  596. real_t Camera2D::get_drag_horizontal_offset() const {
  597. return drag_horizontal_offset;
  598. }
  599. void Camera2D::_set_old_smoothing(real_t p_enable) {
  600. //compatibility
  601. if (p_enable > 0) {
  602. position_smoothing_enabled = true;
  603. set_position_smoothing_speed(p_enable);
  604. }
  605. }
  606. void Camera2D::set_position_smoothing_enabled(bool p_enabled) {
  607. position_smoothing_enabled = p_enabled;
  608. notify_property_list_changed();
  609. }
  610. bool Camera2D::is_position_smoothing_enabled() const {
  611. return position_smoothing_enabled;
  612. }
  613. void Camera2D::set_custom_viewport(Node *p_viewport) {
  614. ERR_FAIL_NULL(p_viewport);
  615. if (is_inside_tree()) {
  616. remove_from_group(group_name);
  617. remove_from_group(canvas_group_name);
  618. }
  619. custom_viewport = Object::cast_to<Viewport>(p_viewport);
  620. if (custom_viewport) {
  621. custom_viewport_id = custom_viewport->get_instance_id();
  622. } else {
  623. custom_viewport_id = ObjectID();
  624. }
  625. if (is_inside_tree()) {
  626. if (custom_viewport) {
  627. viewport = custom_viewport;
  628. } else {
  629. viewport = get_viewport();
  630. }
  631. RID vp = viewport->get_viewport_rid();
  632. group_name = "__cameras_" + itos(vp.get_id());
  633. canvas_group_name = "__cameras_c" + itos(canvas.get_id());
  634. add_to_group(group_name);
  635. add_to_group(canvas_group_name);
  636. }
  637. }
  638. Node *Camera2D::get_custom_viewport() const {
  639. return custom_viewport;
  640. }
  641. void Camera2D::set_screen_drawing_enabled(bool enable) {
  642. screen_drawing_enabled = enable;
  643. #ifdef TOOLS_ENABLED
  644. queue_redraw();
  645. #endif
  646. }
  647. bool Camera2D::is_screen_drawing_enabled() const {
  648. return screen_drawing_enabled;
  649. }
  650. void Camera2D::set_limit_drawing_enabled(bool enable) {
  651. limit_drawing_enabled = enable;
  652. #ifdef TOOLS_ENABLED
  653. queue_redraw();
  654. #endif
  655. }
  656. bool Camera2D::is_limit_drawing_enabled() const {
  657. return limit_drawing_enabled;
  658. }
  659. void Camera2D::set_margin_drawing_enabled(bool enable) {
  660. margin_drawing_enabled = enable;
  661. #ifdef TOOLS_ENABLED
  662. queue_redraw();
  663. #endif
  664. }
  665. bool Camera2D::is_margin_drawing_enabled() const {
  666. return margin_drawing_enabled;
  667. }
  668. void Camera2D::_validate_property(PropertyInfo &p_property) const {
  669. if (!position_smoothing_enabled && p_property.name == "position_smoothing_speed") {
  670. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  671. }
  672. if (!rotation_smoothing_enabled && p_property.name == "rotation_smoothing_speed") {
  673. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  674. }
  675. }
  676. void Camera2D::_bind_methods() {
  677. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Camera2D::set_offset);
  678. ClassDB::bind_method(D_METHOD("get_offset"), &Camera2D::get_offset);
  679. ClassDB::bind_method(D_METHOD("set_anchor_mode", "anchor_mode"), &Camera2D::set_anchor_mode);
  680. ClassDB::bind_method(D_METHOD("get_anchor_mode"), &Camera2D::get_anchor_mode);
  681. ClassDB::bind_method(D_METHOD("set_ignore_rotation", "ignore"), &Camera2D::set_ignore_rotation);
  682. ClassDB::bind_method(D_METHOD("is_ignoring_rotation"), &Camera2D::is_ignoring_rotation);
  683. ClassDB::bind_method(D_METHOD("_update_scroll"), &Camera2D::_update_scroll);
  684. ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &Camera2D::set_process_callback);
  685. ClassDB::bind_method(D_METHOD("get_process_callback"), &Camera2D::get_process_callback);
  686. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &Camera2D::set_enabled);
  687. ClassDB::bind_method(D_METHOD("is_enabled"), &Camera2D::is_enabled);
  688. ClassDB::bind_method(D_METHOD("make_current"), &Camera2D::make_current);
  689. ClassDB::bind_method(D_METHOD("is_current"), &Camera2D::is_current);
  690. ClassDB::bind_method(D_METHOD("_make_current"), &Camera2D::_make_current);
  691. ClassDB::bind_method(D_METHOD("set_limit", "margin", "limit"), &Camera2D::set_limit);
  692. ClassDB::bind_method(D_METHOD("get_limit", "margin"), &Camera2D::get_limit);
  693. ClassDB::bind_method(D_METHOD("set_limit_smoothing_enabled", "limit_smoothing_enabled"), &Camera2D::set_limit_smoothing_enabled);
  694. ClassDB::bind_method(D_METHOD("is_limit_smoothing_enabled"), &Camera2D::is_limit_smoothing_enabled);
  695. ClassDB::bind_method(D_METHOD("set_drag_vertical_enabled", "enabled"), &Camera2D::set_drag_vertical_enabled);
  696. ClassDB::bind_method(D_METHOD("is_drag_vertical_enabled"), &Camera2D::is_drag_vertical_enabled);
  697. ClassDB::bind_method(D_METHOD("set_drag_horizontal_enabled", "enabled"), &Camera2D::set_drag_horizontal_enabled);
  698. ClassDB::bind_method(D_METHOD("is_drag_horizontal_enabled"), &Camera2D::is_drag_horizontal_enabled);
  699. ClassDB::bind_method(D_METHOD("set_drag_vertical_offset", "offset"), &Camera2D::set_drag_vertical_offset);
  700. ClassDB::bind_method(D_METHOD("get_drag_vertical_offset"), &Camera2D::get_drag_vertical_offset);
  701. ClassDB::bind_method(D_METHOD("set_drag_horizontal_offset", "offset"), &Camera2D::set_drag_horizontal_offset);
  702. ClassDB::bind_method(D_METHOD("get_drag_horizontal_offset"), &Camera2D::get_drag_horizontal_offset);
  703. ClassDB::bind_method(D_METHOD("set_drag_margin", "margin", "drag_margin"), &Camera2D::set_drag_margin);
  704. ClassDB::bind_method(D_METHOD("get_drag_margin", "margin"), &Camera2D::get_drag_margin);
  705. ClassDB::bind_method(D_METHOD("get_target_position"), &Camera2D::get_camera_position);
  706. ClassDB::bind_method(D_METHOD("get_screen_center_position"), &Camera2D::get_camera_screen_center);
  707. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &Camera2D::set_zoom);
  708. ClassDB::bind_method(D_METHOD("get_zoom"), &Camera2D::get_zoom);
  709. ClassDB::bind_method(D_METHOD("set_custom_viewport", "viewport"), &Camera2D::set_custom_viewport);
  710. ClassDB::bind_method(D_METHOD("get_custom_viewport"), &Camera2D::get_custom_viewport);
  711. ClassDB::bind_method(D_METHOD("set_position_smoothing_speed", "position_smoothing_speed"), &Camera2D::set_position_smoothing_speed);
  712. ClassDB::bind_method(D_METHOD("get_position_smoothing_speed"), &Camera2D::get_position_smoothing_speed);
  713. ClassDB::bind_method(D_METHOD("set_position_smoothing_enabled", "position_smoothing_speed"), &Camera2D::set_position_smoothing_enabled);
  714. ClassDB::bind_method(D_METHOD("is_position_smoothing_enabled"), &Camera2D::is_position_smoothing_enabled);
  715. ClassDB::bind_method(D_METHOD("set_rotation_smoothing_enabled", "enabled"), &Camera2D::set_rotation_smoothing_enabled);
  716. ClassDB::bind_method(D_METHOD("is_rotation_smoothing_enabled"), &Camera2D::is_rotation_smoothing_enabled);
  717. ClassDB::bind_method(D_METHOD("set_rotation_smoothing_speed", "speed"), &Camera2D::set_rotation_smoothing_speed);
  718. ClassDB::bind_method(D_METHOD("get_rotation_smoothing_speed"), &Camera2D::get_rotation_smoothing_speed);
  719. ClassDB::bind_method(D_METHOD("force_update_scroll"), &Camera2D::force_update_scroll);
  720. ClassDB::bind_method(D_METHOD("reset_smoothing"), &Camera2D::reset_smoothing);
  721. ClassDB::bind_method(D_METHOD("align"), &Camera2D::align);
  722. ClassDB::bind_method(D_METHOD("_set_old_smoothing", "follow_smoothing"), &Camera2D::_set_old_smoothing);
  723. ClassDB::bind_method(D_METHOD("set_screen_drawing_enabled", "screen_drawing_enabled"), &Camera2D::set_screen_drawing_enabled);
  724. ClassDB::bind_method(D_METHOD("is_screen_drawing_enabled"), &Camera2D::is_screen_drawing_enabled);
  725. ClassDB::bind_method(D_METHOD("set_limit_drawing_enabled", "limit_drawing_enabled"), &Camera2D::set_limit_drawing_enabled);
  726. ClassDB::bind_method(D_METHOD("is_limit_drawing_enabled"), &Camera2D::is_limit_drawing_enabled);
  727. ClassDB::bind_method(D_METHOD("set_margin_drawing_enabled", "margin_drawing_enabled"), &Camera2D::set_margin_drawing_enabled);
  728. ClassDB::bind_method(D_METHOD("is_margin_drawing_enabled"), &Camera2D::is_margin_drawing_enabled);
  729. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset");
  730. ADD_PROPERTY(PropertyInfo(Variant::INT, "anchor_mode", PROPERTY_HINT_ENUM, "Fixed Top Left,Drag Center"), "set_anchor_mode", "get_anchor_mode");
  731. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_rotation"), "set_ignore_rotation", "is_ignoring_rotation");
  732. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  733. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "zoom", PROPERTY_HINT_LINK), "set_zoom", "get_zoom");
  734. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport", PROPERTY_USAGE_NONE), "set_custom_viewport", "get_custom_viewport");
  735. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_callback", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_process_callback", "get_process_callback");
  736. ADD_GROUP("Limit", "limit_");
  737. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_left", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_LEFT);
  738. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_top", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_TOP);
  739. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_right", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_RIGHT);
  740. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_bottom", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_BOTTOM);
  741. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "limit_smoothed"), "set_limit_smoothing_enabled", "is_limit_smoothing_enabled");
  742. ADD_GROUP("Position Smoothing", "position_smoothing_");
  743. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "position_smoothing_enabled"), "set_position_smoothing_enabled", "is_position_smoothing_enabled");
  744. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "position_smoothing_speed", PROPERTY_HINT_NONE, "suffix:px/s"), "set_position_smoothing_speed", "get_position_smoothing_speed");
  745. ADD_GROUP("Rotation Smoothing", "rotation_smoothing_");
  746. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotation_smoothing_enabled"), "set_rotation_smoothing_enabled", "is_rotation_smoothing_enabled");
  747. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation_smoothing_speed"), "set_rotation_smoothing_speed", "get_rotation_smoothing_speed");
  748. ADD_GROUP("Drag", "drag_");
  749. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_horizontal_enabled"), "set_drag_horizontal_enabled", "is_drag_horizontal_enabled");
  750. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_vertical_enabled"), "set_drag_vertical_enabled", "is_drag_vertical_enabled");
  751. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "drag_horizontal_offset", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_drag_horizontal_offset", "get_drag_horizontal_offset");
  752. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "drag_vertical_offset", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_drag_vertical_offset", "get_drag_vertical_offset");
  753. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_left_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_LEFT);
  754. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_top_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_TOP);
  755. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_right_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_RIGHT);
  756. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_bottom_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_BOTTOM);
  757. ADD_GROUP("Editor", "editor_");
  758. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_screen"), "set_screen_drawing_enabled", "is_screen_drawing_enabled");
  759. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_limits"), "set_limit_drawing_enabled", "is_limit_drawing_enabled");
  760. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_drag_margin"), "set_margin_drawing_enabled", "is_margin_drawing_enabled");
  761. BIND_ENUM_CONSTANT(ANCHOR_MODE_FIXED_TOP_LEFT);
  762. BIND_ENUM_CONSTANT(ANCHOR_MODE_DRAG_CENTER);
  763. BIND_ENUM_CONSTANT(CAMERA2D_PROCESS_PHYSICS);
  764. BIND_ENUM_CONSTANT(CAMERA2D_PROCESS_IDLE);
  765. }
  766. Camera2D::Camera2D() {
  767. limit[SIDE_LEFT] = -10000000;
  768. limit[SIDE_TOP] = -10000000;
  769. limit[SIDE_RIGHT] = 10000000;
  770. limit[SIDE_BOTTOM] = 10000000;
  771. drag_margin[SIDE_LEFT] = 0.2;
  772. drag_margin[SIDE_TOP] = 0.2;
  773. drag_margin[SIDE_RIGHT] = 0.2;
  774. drag_margin[SIDE_BOTTOM] = 0.2;
  775. set_notify_transform(true);
  776. set_hide_clip_children(true);
  777. }