camera_2d.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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/engine.h"
  32. #include "core/math/math_funcs.h"
  33. #include "scene/scene_string_names.h"
  34. #include "servers/visual_server.h"
  35. void Camera2D::_update_scroll() {
  36. if (!is_inside_tree()) {
  37. return;
  38. }
  39. if (Engine::get_singleton()->is_editor_hint()) {
  40. update();
  41. // Only set viewport transform when not bound to the main viewport.
  42. if (get_viewport() == get_tree()->get_edited_scene_root()->get_viewport()) {
  43. return;
  44. }
  45. }
  46. if (!viewport) {
  47. return;
  48. }
  49. if (current) {
  50. ERR_FAIL_COND(custom_viewport && !ObjectDB::get_instance(custom_viewport_id));
  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. } else {
  55. xform = get_camera_transform();
  56. }
  57. viewport->set_canvas_transform(xform);
  58. Size2 screen_size = viewport->get_visible_rect().size;
  59. Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5) : Point2());
  60. get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, group_name, "_camera_moved", xform, screen_offset);
  61. };
  62. }
  63. void Camera2D::_update_process_mode() {
  64. if (is_physics_interpolated_and_enabled()) {
  65. set_process_internal(is_current());
  66. set_physics_process_internal(is_current());
  67. #ifdef TOOLS_ENABLED
  68. if (process_mode == CAMERA2D_PROCESS_IDLE) {
  69. WARN_PRINT_ONCE("Camera2D overridden to physics process mode due to use of physics interpolation.");
  70. }
  71. #endif
  72. } else {
  73. // Smoothing can be enabled in the editor but will never be active.
  74. if (process_mode == CAMERA2D_PROCESS_IDLE) {
  75. set_process_internal(smoothing_active);
  76. set_physics_process_internal(false);
  77. } else {
  78. set_process_internal(false);
  79. set_physics_process_internal(smoothing_active);
  80. }
  81. }
  82. }
  83. void Camera2D::_setup_viewport() {
  84. // Disconnect signal on previous viewport if there's one.
  85. if (viewport && viewport->is_connected("size_changed", this, "_update_scroll")) {
  86. viewport->disconnect("size_changed", this, "_update_scroll");
  87. }
  88. if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
  89. viewport = custom_viewport;
  90. } else {
  91. viewport = get_viewport();
  92. }
  93. RID vp = viewport->get_viewport_rid();
  94. group_name = "__cameras_" + itos(vp.get_id());
  95. canvas_group_name = "__cameras_c" + itos(canvas.get_id());
  96. add_to_group(group_name);
  97. add_to_group(canvas_group_name);
  98. viewport->connect("size_changed", this, "_update_scroll");
  99. }
  100. void Camera2D::set_zoom(const Vector2 &p_zoom) {
  101. // Setting zoom to zero causes 'affine_invert' issues
  102. 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).");
  103. zoom = p_zoom;
  104. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  105. _update_scroll();
  106. smoothed_camera_pos = old_smoothed_camera_pos;
  107. };
  108. Vector2 Camera2D::get_zoom() const {
  109. return zoom;
  110. };
  111. Transform2D Camera2D::get_camera_transform() {
  112. if (!get_tree() || !viewport) {
  113. return Transform2D();
  114. }
  115. ERR_FAIL_COND_V(custom_viewport && !ObjectDB::get_instance(custom_viewport_id), Transform2D());
  116. Size2 screen_size = viewport->get_visible_rect().size;
  117. Point2 new_camera_pos = get_global_transform().get_origin();
  118. Point2 ret_camera_pos;
  119. if (!first) {
  120. if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
  121. if (h_drag_enabled && !Engine::get_singleton()->is_editor_hint() && !h_offset_changed) {
  122. camera_pos.x = MIN(camera_pos.x, (new_camera_pos.x + screen_size.x * 0.5 * zoom.x * drag_margin[MARGIN_LEFT]));
  123. camera_pos.x = MAX(camera_pos.x, (new_camera_pos.x - screen_size.x * 0.5 * zoom.x * drag_margin[MARGIN_RIGHT]));
  124. } else {
  125. if (h_ofs < 0) {
  126. camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_RIGHT] * h_ofs;
  127. } else {
  128. camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_LEFT] * h_ofs;
  129. }
  130. h_offset_changed = false;
  131. }
  132. if (v_drag_enabled && !Engine::get_singleton()->is_editor_hint() && !v_offset_changed) {
  133. camera_pos.y = MIN(camera_pos.y, (new_camera_pos.y + screen_size.y * 0.5 * zoom.y * drag_margin[MARGIN_TOP]));
  134. camera_pos.y = MAX(camera_pos.y, (new_camera_pos.y - screen_size.y * 0.5 * zoom.y * drag_margin[MARGIN_BOTTOM]));
  135. } else {
  136. if (v_ofs < 0) {
  137. camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_BOTTOM] * v_ofs;
  138. } else {
  139. camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_TOP] * v_ofs;
  140. }
  141. v_offset_changed = false;
  142. }
  143. } else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
  144. camera_pos = new_camera_pos;
  145. }
  146. Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom) : Point2());
  147. Rect2 screen_rect(-screen_offset + camera_pos, screen_size * zoom);
  148. if (limit_smoothing_enabled) {
  149. if (screen_rect.position.x < limit[MARGIN_LEFT]) {
  150. camera_pos.x -= screen_rect.position.x - limit[MARGIN_LEFT];
  151. }
  152. if (screen_rect.position.x + screen_rect.size.x > limit[MARGIN_RIGHT]) {
  153. camera_pos.x -= screen_rect.position.x + screen_rect.size.x - limit[MARGIN_RIGHT];
  154. }
  155. if (screen_rect.position.y + screen_rect.size.y > limit[MARGIN_BOTTOM]) {
  156. camera_pos.y -= screen_rect.position.y + screen_rect.size.y - limit[MARGIN_BOTTOM];
  157. }
  158. if (screen_rect.position.y < limit[MARGIN_TOP]) {
  159. camera_pos.y -= screen_rect.position.y - limit[MARGIN_TOP];
  160. }
  161. }
  162. // TODO ..
  163. // There is a bug here.
  164. // Smoothing occurs rather confusingly
  165. // during the call to get_camera_transform().
  166. // It may be called MULTIPLE TIMES on certain frames,
  167. // therefore smoothing is not currently applied only once per frame / tick,
  168. // which will result in some haphazard results.
  169. if (smoothing_active) {
  170. // Note that if we are using physics interpolation,
  171. // processing will always be physics based (it ignores the process mode set in the UI).
  172. bool physics_process = (process_mode == CAMERA2D_PROCESS_PHYSICS) || is_physics_interpolated_and_enabled();
  173. float delta = physics_process ? get_physics_process_delta_time() : get_process_delta_time();
  174. float c = smoothing * delta;
  175. smoothed_camera_pos = ((camera_pos - smoothed_camera_pos) * c) + smoothed_camera_pos;
  176. ret_camera_pos = smoothed_camera_pos;
  177. } else {
  178. ret_camera_pos = smoothed_camera_pos = camera_pos;
  179. }
  180. } else {
  181. ret_camera_pos = smoothed_camera_pos = camera_pos = new_camera_pos;
  182. first = false;
  183. }
  184. Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom) : Point2());
  185. float angle = get_global_transform().get_rotation();
  186. if (rotating) {
  187. screen_offset = screen_offset.rotated(angle);
  188. }
  189. Rect2 screen_rect(-screen_offset + ret_camera_pos, screen_size * zoom);
  190. if (!smoothing_enabled || !limit_smoothing_enabled) {
  191. if (screen_rect.position.x < limit[MARGIN_LEFT]) {
  192. screen_rect.position.x = limit[MARGIN_LEFT];
  193. }
  194. if (screen_rect.position.x + screen_rect.size.x > limit[MARGIN_RIGHT]) {
  195. screen_rect.position.x = limit[MARGIN_RIGHT] - screen_rect.size.x;
  196. }
  197. if (screen_rect.position.y + screen_rect.size.y > limit[MARGIN_BOTTOM]) {
  198. screen_rect.position.y = limit[MARGIN_BOTTOM] - screen_rect.size.y;
  199. }
  200. if (screen_rect.position.y < limit[MARGIN_TOP]) {
  201. screen_rect.position.y = limit[MARGIN_TOP];
  202. }
  203. }
  204. if (offset != Vector2()) {
  205. screen_rect.position += offset;
  206. }
  207. camera_screen_center = screen_rect.position + screen_rect.size * 0.5;
  208. Transform2D xform;
  209. xform.scale_basis(zoom);
  210. if (rotating) {
  211. xform.set_rotation(angle);
  212. }
  213. xform.set_origin(screen_rect.position);
  214. return (xform).affine_inverse();
  215. }
  216. void Camera2D::_ensure_update_interpolation_data() {
  217. // The curr -> previous update can either occur
  218. // on the INTERNAL_PHYSICS_PROCESS OR
  219. // on NOTIFICATION_TRANSFORM_CHANGED,
  220. // if NOTIFICATION_TRANSFORM_CHANGED takes place
  221. // earlier than INTERNAL_PHYSICS_PROCESS on a tick.
  222. // This is to ensure that the data keeps flowing, but the new data
  223. // doesn't overwrite before prev has been set.
  224. // Keep the data flowing.
  225. uint64_t tick = Engine::get_singleton()->get_physics_frames();
  226. if (_interpolation_data.last_update_physics_tick != tick) {
  227. _interpolation_data.xform_prev = _interpolation_data.xform_curr;
  228. _interpolation_data.last_update_physics_tick = tick;
  229. }
  230. }
  231. void Camera2D::_notification(int p_what) {
  232. switch (p_what) {
  233. case NOTIFICATION_INTERNAL_PROCESS: {
  234. _update_scroll();
  235. } break;
  236. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  237. if (is_physics_interpolated_and_enabled()) {
  238. _ensure_update_interpolation_data();
  239. _interpolation_data.xform_curr = get_camera_transform();
  240. } else {
  241. _update_scroll();
  242. }
  243. } break;
  244. case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
  245. // Force the limits etc to update.
  246. _interpolation_data.xform_curr = get_camera_transform();
  247. _interpolation_data.xform_prev = _interpolation_data.xform_curr;
  248. } break;
  249. case NOTIFICATION_PAUSED: {
  250. if (is_physics_interpolated_and_enabled()) {
  251. _update_scroll();
  252. }
  253. } break;
  254. case NOTIFICATION_TRANSFORM_CHANGED: {
  255. if (!smoothing_active && !is_physics_interpolated_and_enabled()) {
  256. _update_scroll();
  257. }
  258. if (is_physics_interpolated_and_enabled()) {
  259. _ensure_update_interpolation_data();
  260. if (Engine::get_singleton()->is_in_physics_frame()) {
  261. _interpolation_data.xform_curr = get_camera_transform();
  262. }
  263. }
  264. } break;
  265. case NOTIFICATION_ENTER_TREE: {
  266. ERR_FAIL_COND(!is_inside_tree());
  267. canvas = get_canvas();
  268. _setup_viewport();
  269. _update_process_mode();
  270. // if a camera enters the tree that is set to current,
  271. // it should take over as the current camera, and mark
  272. // all other cameras as non current
  273. first = true;
  274. _set_current(current);
  275. // Note that NOTIFICATION_RESET_PHYSICS_INTERPOLATION
  276. // is automatically called before this because Camera2D is inherited
  277. // from CanvasItem. However, the camera transform is not up to date
  278. // until this point, so we do an extra manual reset.
  279. if (is_physics_interpolated_and_enabled()) {
  280. _interpolation_data.xform_curr = get_camera_transform();
  281. _interpolation_data.xform_prev = _interpolation_data.xform_curr;
  282. }
  283. } break;
  284. case NOTIFICATION_EXIT_TREE: {
  285. const bool viewport_valid = !custom_viewport || ObjectDB::get_instance(custom_viewport_id);
  286. if (is_current()) {
  287. if (viewport && viewport_valid) {
  288. viewport->set_canvas_transform(Transform2D());
  289. }
  290. }
  291. if (viewport && viewport_valid) {
  292. viewport->disconnect("size_changed", this, "_update_scroll");
  293. }
  294. remove_from_group(group_name);
  295. remove_from_group(canvas_group_name);
  296. viewport = nullptr;
  297. } break;
  298. #ifdef TOOLS_ENABLED
  299. case NOTIFICATION_DRAW: {
  300. if (!is_inside_tree() || !Engine::get_singleton()->is_editor_hint()) {
  301. break;
  302. }
  303. if (screen_drawing_enabled) {
  304. Color area_axis_color(1, 0.4, 1, 0.63);
  305. float area_axis_width = 1;
  306. if (is_current()) {
  307. area_axis_width = 3;
  308. }
  309. Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
  310. Size2 screen_size = get_viewport_rect().size;
  311. Vector2 screen_endpoints[4] = {
  312. inv_camera_transform.xform(Vector2(0, 0)),
  313. inv_camera_transform.xform(Vector2(screen_size.width, 0)),
  314. inv_camera_transform.xform(Vector2(screen_size.width, screen_size.height)),
  315. inv_camera_transform.xform(Vector2(0, screen_size.height))
  316. };
  317. Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space
  318. for (int i = 0; i < 4; i++) {
  319. draw_line(inv_transform.xform(screen_endpoints[i]), inv_transform.xform(screen_endpoints[(i + 1) % 4]), area_axis_color, area_axis_width);
  320. }
  321. }
  322. if (limit_drawing_enabled) {
  323. Color limit_drawing_color(1, 1, 0.25, 0.63);
  324. float limit_drawing_width = 1;
  325. if (is_current()) {
  326. limit_drawing_width = 3;
  327. }
  328. Vector2 camera_origin = get_global_transform().get_origin();
  329. Vector2 camera_scale = get_global_transform().get_scale().abs();
  330. Vector2 limit_points[4] = {
  331. (Vector2(limit[MARGIN_LEFT], limit[MARGIN_TOP]) - camera_origin) / camera_scale,
  332. (Vector2(limit[MARGIN_RIGHT], limit[MARGIN_TOP]) - camera_origin) / camera_scale,
  333. (Vector2(limit[MARGIN_RIGHT], limit[MARGIN_BOTTOM]) - camera_origin) / camera_scale,
  334. (Vector2(limit[MARGIN_LEFT], limit[MARGIN_BOTTOM]) - camera_origin) / camera_scale
  335. };
  336. for (int i = 0; i < 4; i++) {
  337. draw_line(limit_points[i], limit_points[(i + 1) % 4], limit_drawing_color, limit_drawing_width);
  338. }
  339. }
  340. if (margin_drawing_enabled) {
  341. Color margin_drawing_color(0.25, 1, 1, 0.63);
  342. float margin_drawing_width = 1;
  343. if (is_current()) {
  344. margin_drawing_width = 3;
  345. }
  346. Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
  347. Size2 screen_size = get_viewport_rect().size;
  348. Vector2 margin_endpoints[4] = {
  349. inv_camera_transform.xform(Vector2((screen_size.width / 2) - ((screen_size.width / 2) * drag_margin[MARGIN_LEFT]), (screen_size.height / 2) - ((screen_size.height / 2) * drag_margin[MARGIN_TOP]))),
  350. inv_camera_transform.xform(Vector2((screen_size.width / 2) + ((screen_size.width / 2) * drag_margin[MARGIN_RIGHT]), (screen_size.height / 2) - ((screen_size.height / 2) * drag_margin[MARGIN_TOP]))),
  351. inv_camera_transform.xform(Vector2((screen_size.width / 2) + ((screen_size.width / 2) * drag_margin[MARGIN_RIGHT]), (screen_size.height / 2) + ((screen_size.height / 2) * drag_margin[MARGIN_BOTTOM]))),
  352. inv_camera_transform.xform(Vector2((screen_size.width / 2) - ((screen_size.width / 2) * drag_margin[MARGIN_LEFT]), (screen_size.height / 2) + ((screen_size.height / 2) * drag_margin[MARGIN_BOTTOM])))
  353. };
  354. Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space
  355. for (int i = 0; i < 4; i++) {
  356. draw_line(inv_transform.xform(margin_endpoints[i]), inv_transform.xform(margin_endpoints[(i + 1) % 4]), margin_drawing_color, margin_drawing_width);
  357. }
  358. }
  359. } break;
  360. #endif
  361. }
  362. }
  363. void Camera2D::set_offset(const Vector2 &p_offset) {
  364. offset = p_offset;
  365. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  366. _update_scroll();
  367. smoothed_camera_pos = old_smoothed_camera_pos;
  368. }
  369. Vector2 Camera2D::get_offset() const {
  370. return offset;
  371. }
  372. void Camera2D::set_anchor_mode(AnchorMode p_anchor_mode) {
  373. anchor_mode = p_anchor_mode;
  374. _update_scroll();
  375. }
  376. Camera2D::AnchorMode Camera2D::get_anchor_mode() const {
  377. return anchor_mode;
  378. }
  379. void Camera2D::set_rotating(bool p_rotating) {
  380. rotating = p_rotating;
  381. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  382. _update_scroll();
  383. smoothed_camera_pos = old_smoothed_camera_pos;
  384. }
  385. bool Camera2D::is_rotating() const {
  386. return rotating;
  387. }
  388. void Camera2D::set_process_mode(Camera2DProcessMode p_mode) {
  389. if (process_mode == p_mode) {
  390. return;
  391. }
  392. process_mode = p_mode;
  393. _update_process_mode();
  394. }
  395. Camera2D::Camera2DProcessMode Camera2D::get_process_mode() const {
  396. return process_mode;
  397. }
  398. void Camera2D::_make_current(Object *p_which) {
  399. bool new_current = false;
  400. if (p_which == this) {
  401. new_current = true;
  402. }
  403. if (new_current != current) {
  404. current = new_current;
  405. _update_process_mode();
  406. }
  407. }
  408. void Camera2D::_set_current(bool p_current) {
  409. if (p_current) {
  410. make_current();
  411. }
  412. current = p_current;
  413. _update_process_mode();
  414. update();
  415. }
  416. bool Camera2D::is_current() const {
  417. return current;
  418. }
  419. void Camera2D::make_current() {
  420. if (!is_inside_tree()) {
  421. current = true;
  422. } else {
  423. get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, group_name, "_make_current", this);
  424. }
  425. _update_scroll();
  426. _update_process_mode();
  427. }
  428. void Camera2D::clear_current() {
  429. current = false;
  430. if (is_inside_tree()) {
  431. get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, group_name, "_make_current", (Object *)nullptr);
  432. }
  433. _update_process_mode();
  434. }
  435. void Camera2D::set_limit(Margin p_margin, int p_limit) {
  436. ERR_FAIL_INDEX((int)p_margin, 4);
  437. limit[p_margin] = p_limit;
  438. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  439. _update_scroll();
  440. smoothed_camera_pos = old_smoothed_camera_pos;
  441. }
  442. int Camera2D::get_limit(Margin p_margin) const {
  443. ERR_FAIL_INDEX_V((int)p_margin, 4, 0);
  444. return limit[p_margin];
  445. }
  446. void Camera2D::set_limit_smoothing_enabled(bool enable) {
  447. limit_smoothing_enabled = enable;
  448. _update_scroll();
  449. }
  450. bool Camera2D::is_limit_smoothing_enabled() const {
  451. return limit_smoothing_enabled;
  452. }
  453. void Camera2D::set_drag_margin(Margin p_margin, float p_drag_margin) {
  454. ERR_FAIL_INDEX((int)p_margin, 4);
  455. drag_margin[p_margin] = p_drag_margin;
  456. update();
  457. }
  458. float Camera2D::get_drag_margin(Margin p_margin) const {
  459. ERR_FAIL_INDEX_V((int)p_margin, 4, 0);
  460. return drag_margin[p_margin];
  461. }
  462. Vector2 Camera2D::get_camera_position() const {
  463. return camera_pos;
  464. }
  465. void Camera2D::force_update_scroll() {
  466. _update_scroll();
  467. }
  468. void Camera2D::reset_smoothing() {
  469. _update_scroll();
  470. smoothed_camera_pos = camera_pos;
  471. }
  472. void Camera2D::align() {
  473. ERR_FAIL_COND(!is_inside_tree() || !viewport);
  474. ERR_FAIL_COND(custom_viewport && !ObjectDB::get_instance(custom_viewport_id));
  475. Size2 screen_size = viewport->get_visible_rect().size;
  476. Point2 current_camera_pos = get_global_transform().get_origin();
  477. if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
  478. if (h_ofs < 0) {
  479. camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_RIGHT] * h_ofs;
  480. } else {
  481. camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_LEFT] * h_ofs;
  482. }
  483. if (v_ofs < 0) {
  484. camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_TOP] * v_ofs;
  485. } else {
  486. camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_BOTTOM] * v_ofs;
  487. }
  488. } else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
  489. camera_pos = current_camera_pos;
  490. }
  491. _update_scroll();
  492. }
  493. void Camera2D::set_follow_smoothing(float p_speed) {
  494. smoothing = p_speed;
  495. }
  496. float Camera2D::get_follow_smoothing() const {
  497. return smoothing;
  498. }
  499. Point2 Camera2D::get_camera_screen_center() const {
  500. return camera_screen_center;
  501. }
  502. void Camera2D::set_h_drag_enabled(bool p_enabled) {
  503. h_drag_enabled = p_enabled;
  504. }
  505. bool Camera2D::is_h_drag_enabled() const {
  506. return h_drag_enabled;
  507. }
  508. void Camera2D::set_v_drag_enabled(bool p_enabled) {
  509. v_drag_enabled = p_enabled;
  510. }
  511. bool Camera2D::is_v_drag_enabled() const {
  512. return v_drag_enabled;
  513. }
  514. void Camera2D::set_v_offset(float p_offset) {
  515. v_ofs = p_offset;
  516. v_offset_changed = true;
  517. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  518. _update_scroll();
  519. smoothed_camera_pos = old_smoothed_camera_pos;
  520. }
  521. float Camera2D::get_v_offset() const {
  522. return v_ofs;
  523. }
  524. void Camera2D::set_h_offset(float p_offset) {
  525. h_ofs = p_offset;
  526. h_offset_changed = true;
  527. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  528. _update_scroll();
  529. smoothed_camera_pos = old_smoothed_camera_pos;
  530. }
  531. float Camera2D::get_h_offset() const {
  532. return h_ofs;
  533. }
  534. void Camera2D::set_enable_follow_smoothing(bool p_enabled) {
  535. // watch for situation where an pre-enabled camera is added to the tree
  536. // processing must be resumed and bypass this noop check
  537. // (this currently works but is a possible future bug)
  538. if (smoothing_enabled == p_enabled) {
  539. return;
  540. }
  541. // Separate the logic between enabled and active, because the smoothing
  542. // cannot be active in the editor. This can be done without a separate flag
  543. // but is bug prone so this approach is easier to follow.
  544. smoothing_enabled = p_enabled;
  545. smoothing_active = smoothing_enabled && !Engine::get_singleton()->is_editor_hint();
  546. // keep the processing up to date after each change
  547. _update_process_mode();
  548. }
  549. bool Camera2D::is_follow_smoothing_enabled() const {
  550. return smoothing_enabled;
  551. }
  552. void Camera2D::set_custom_viewport(Node *p_viewport) {
  553. ERR_FAIL_NULL(p_viewport);
  554. if (is_inside_tree()) {
  555. remove_from_group(group_name);
  556. remove_from_group(canvas_group_name);
  557. }
  558. if (custom_viewport && !ObjectDB::get_instance(custom_viewport_id)) {
  559. viewport = nullptr;
  560. }
  561. custom_viewport = Object::cast_to<Viewport>(p_viewport);
  562. if (custom_viewport) {
  563. custom_viewport_id = custom_viewport->get_instance_id();
  564. } else {
  565. custom_viewport_id = 0;
  566. }
  567. if (is_inside_tree()) {
  568. _setup_viewport();
  569. }
  570. }
  571. Node *Camera2D::get_custom_viewport() const {
  572. return custom_viewport;
  573. }
  574. void Camera2D::set_screen_drawing_enabled(bool enable) {
  575. screen_drawing_enabled = enable;
  576. #ifdef TOOLS_ENABLED
  577. update();
  578. #endif
  579. }
  580. bool Camera2D::is_screen_drawing_enabled() const {
  581. return screen_drawing_enabled;
  582. }
  583. void Camera2D::set_limit_drawing_enabled(bool enable) {
  584. limit_drawing_enabled = enable;
  585. #ifdef TOOLS_ENABLED
  586. update();
  587. #endif
  588. }
  589. bool Camera2D::is_limit_drawing_enabled() const {
  590. return limit_drawing_enabled;
  591. }
  592. void Camera2D::set_margin_drawing_enabled(bool enable) {
  593. margin_drawing_enabled = enable;
  594. #ifdef TOOLS_ENABLED
  595. update();
  596. #endif
  597. }
  598. bool Camera2D::is_margin_drawing_enabled() const {
  599. return margin_drawing_enabled;
  600. }
  601. void Camera2D::_bind_methods() {
  602. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Camera2D::set_offset);
  603. ClassDB::bind_method(D_METHOD("get_offset"), &Camera2D::get_offset);
  604. ClassDB::bind_method(D_METHOD("set_anchor_mode", "anchor_mode"), &Camera2D::set_anchor_mode);
  605. ClassDB::bind_method(D_METHOD("get_anchor_mode"), &Camera2D::get_anchor_mode);
  606. ClassDB::bind_method(D_METHOD("set_rotating", "rotating"), &Camera2D::set_rotating);
  607. ClassDB::bind_method(D_METHOD("is_rotating"), &Camera2D::is_rotating);
  608. ClassDB::bind_method(D_METHOD("make_current"), &Camera2D::make_current);
  609. ClassDB::bind_method(D_METHOD("clear_current"), &Camera2D::clear_current);
  610. ClassDB::bind_method(D_METHOD("_make_current"), &Camera2D::_make_current);
  611. ClassDB::bind_method(D_METHOD("_update_scroll"), &Camera2D::_update_scroll);
  612. ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &Camera2D::set_process_mode);
  613. ClassDB::bind_method(D_METHOD("get_process_mode"), &Camera2D::get_process_mode);
  614. ClassDB::bind_method(D_METHOD("_set_current", "current"), &Camera2D::_set_current);
  615. ClassDB::bind_method(D_METHOD("is_current"), &Camera2D::is_current);
  616. ClassDB::bind_method(D_METHOD("set_limit", "margin", "limit"), &Camera2D::set_limit);
  617. ClassDB::bind_method(D_METHOD("get_limit", "margin"), &Camera2D::get_limit);
  618. ClassDB::bind_method(D_METHOD("set_limit_smoothing_enabled", "limit_smoothing_enabled"), &Camera2D::set_limit_smoothing_enabled);
  619. ClassDB::bind_method(D_METHOD("is_limit_smoothing_enabled"), &Camera2D::is_limit_smoothing_enabled);
  620. ClassDB::bind_method(D_METHOD("set_v_drag_enabled", "enabled"), &Camera2D::set_v_drag_enabled);
  621. ClassDB::bind_method(D_METHOD("is_v_drag_enabled"), &Camera2D::is_v_drag_enabled);
  622. ClassDB::bind_method(D_METHOD("set_h_drag_enabled", "enabled"), &Camera2D::set_h_drag_enabled);
  623. ClassDB::bind_method(D_METHOD("is_h_drag_enabled"), &Camera2D::is_h_drag_enabled);
  624. ClassDB::bind_method(D_METHOD("set_v_offset", "ofs"), &Camera2D::set_v_offset);
  625. ClassDB::bind_method(D_METHOD("get_v_offset"), &Camera2D::get_v_offset);
  626. ClassDB::bind_method(D_METHOD("set_h_offset", "ofs"), &Camera2D::set_h_offset);
  627. ClassDB::bind_method(D_METHOD("get_h_offset"), &Camera2D::get_h_offset);
  628. ClassDB::bind_method(D_METHOD("set_drag_margin", "margin", "drag_margin"), &Camera2D::set_drag_margin);
  629. ClassDB::bind_method(D_METHOD("get_drag_margin", "margin"), &Camera2D::get_drag_margin);
  630. ClassDB::bind_method(D_METHOD("get_camera_position"), &Camera2D::get_camera_position);
  631. ClassDB::bind_method(D_METHOD("get_camera_screen_center"), &Camera2D::get_camera_screen_center);
  632. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &Camera2D::set_zoom);
  633. ClassDB::bind_method(D_METHOD("get_zoom"), &Camera2D::get_zoom);
  634. ClassDB::bind_method(D_METHOD("set_custom_viewport", "viewport"), &Camera2D::set_custom_viewport);
  635. ClassDB::bind_method(D_METHOD("get_custom_viewport"), &Camera2D::get_custom_viewport);
  636. ClassDB::bind_method(D_METHOD("set_follow_smoothing", "follow_smoothing"), &Camera2D::set_follow_smoothing);
  637. ClassDB::bind_method(D_METHOD("get_follow_smoothing"), &Camera2D::get_follow_smoothing);
  638. ClassDB::bind_method(D_METHOD("set_enable_follow_smoothing", "follow_smoothing"), &Camera2D::set_enable_follow_smoothing);
  639. ClassDB::bind_method(D_METHOD("is_follow_smoothing_enabled"), &Camera2D::is_follow_smoothing_enabled);
  640. ClassDB::bind_method(D_METHOD("force_update_scroll"), &Camera2D::force_update_scroll);
  641. ClassDB::bind_method(D_METHOD("reset_smoothing"), &Camera2D::reset_smoothing);
  642. ClassDB::bind_method(D_METHOD("align"), &Camera2D::align);
  643. ClassDB::bind_method(D_METHOD("set_screen_drawing_enabled", "screen_drawing_enabled"), &Camera2D::set_screen_drawing_enabled);
  644. ClassDB::bind_method(D_METHOD("is_screen_drawing_enabled"), &Camera2D::is_screen_drawing_enabled);
  645. ClassDB::bind_method(D_METHOD("set_limit_drawing_enabled", "limit_drawing_enabled"), &Camera2D::set_limit_drawing_enabled);
  646. ClassDB::bind_method(D_METHOD("is_limit_drawing_enabled"), &Camera2D::is_limit_drawing_enabled);
  647. ClassDB::bind_method(D_METHOD("set_margin_drawing_enabled", "margin_drawing_enabled"), &Camera2D::set_margin_drawing_enabled);
  648. ClassDB::bind_method(D_METHOD("is_margin_drawing_enabled"), &Camera2D::is_margin_drawing_enabled);
  649. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  650. ADD_PROPERTY(PropertyInfo(Variant::INT, "anchor_mode", PROPERTY_HINT_ENUM, "Fixed TopLeft,Drag Center"), "set_anchor_mode", "get_anchor_mode");
  651. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotating"), "set_rotating", "is_rotating");
  652. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), "_set_current", "is_current");
  653. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "zoom", PROPERTY_HINT_LINK), "set_zoom", "get_zoom");
  654. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport", 0), "set_custom_viewport", "get_custom_viewport");
  655. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_process_mode", "get_process_mode");
  656. ADD_GROUP("Limit", "limit_");
  657. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_left"), "set_limit", "get_limit", MARGIN_LEFT);
  658. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_top"), "set_limit", "get_limit", MARGIN_TOP);
  659. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_right"), "set_limit", "get_limit", MARGIN_RIGHT);
  660. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_bottom"), "set_limit", "get_limit", MARGIN_BOTTOM);
  661. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "limit_smoothed"), "set_limit_smoothing_enabled", "is_limit_smoothing_enabled");
  662. ADD_GROUP("Draw Margin", "draw_margin_");
  663. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_margin_h_enabled"), "set_h_drag_enabled", "is_h_drag_enabled");
  664. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_margin_v_enabled"), "set_v_drag_enabled", "is_v_drag_enabled");
  665. ADD_GROUP("Smoothing", "smoothing_");
  666. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smoothing_enabled"), "set_enable_follow_smoothing", "is_follow_smoothing_enabled");
  667. ADD_PROPERTY(PropertyInfo(Variant::REAL, "smoothing_speed"), "set_follow_smoothing", "get_follow_smoothing");
  668. ADD_GROUP("Offset", "offset_");
  669. ADD_PROPERTY(PropertyInfo(Variant::REAL, "offset_h", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_h_offset", "get_h_offset");
  670. ADD_PROPERTY(PropertyInfo(Variant::REAL, "offset_v", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_v_offset", "get_v_offset");
  671. ADD_GROUP("Drag Margin", "drag_margin_");
  672. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "drag_margin_left", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", MARGIN_LEFT);
  673. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "drag_margin_top", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", MARGIN_TOP);
  674. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "drag_margin_right", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", MARGIN_RIGHT);
  675. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "drag_margin_bottom", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", MARGIN_BOTTOM);
  676. ADD_GROUP("Editor", "editor_");
  677. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_screen"), "set_screen_drawing_enabled", "is_screen_drawing_enabled");
  678. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_limits"), "set_limit_drawing_enabled", "is_limit_drawing_enabled");
  679. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_drag_margin"), "set_margin_drawing_enabled", "is_margin_drawing_enabled");
  680. BIND_ENUM_CONSTANT(ANCHOR_MODE_FIXED_TOP_LEFT);
  681. BIND_ENUM_CONSTANT(ANCHOR_MODE_DRAG_CENTER);
  682. BIND_ENUM_CONSTANT(CAMERA2D_PROCESS_PHYSICS);
  683. BIND_ENUM_CONSTANT(CAMERA2D_PROCESS_IDLE);
  684. }
  685. Camera2D::Camera2D() {
  686. anchor_mode = ANCHOR_MODE_DRAG_CENTER;
  687. rotating = false;
  688. current = false;
  689. limit[MARGIN_LEFT] = -10000000;
  690. limit[MARGIN_TOP] = -10000000;
  691. limit[MARGIN_RIGHT] = 10000000;
  692. limit[MARGIN_BOTTOM] = 10000000;
  693. drag_margin[MARGIN_LEFT] = 0.2;
  694. drag_margin[MARGIN_TOP] = 0.2;
  695. drag_margin[MARGIN_RIGHT] = 0.2;
  696. drag_margin[MARGIN_BOTTOM] = 0.2;
  697. camera_pos = Vector2();
  698. first = true;
  699. smoothing_enabled = false;
  700. smoothing_active = false;
  701. limit_smoothing_enabled = false;
  702. viewport = nullptr;
  703. custom_viewport = nullptr;
  704. custom_viewport_id = 0;
  705. process_mode = CAMERA2D_PROCESS_IDLE;
  706. smoothing = 5.0;
  707. zoom = Vector2(1, 1);
  708. screen_drawing_enabled = true;
  709. limit_drawing_enabled = false;
  710. margin_drawing_enabled = false;
  711. h_drag_enabled = false;
  712. v_drag_enabled = false;
  713. h_ofs = 0;
  714. v_ofs = 0;
  715. h_offset_changed = false;
  716. v_offset_changed = false;
  717. set_notify_transform(true);
  718. }