camera_2d.cpp 25 KB

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