camera_2d.cpp 27 KB

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