camera_2d.cpp 28 KB

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