camera_2d.cpp 26 KB

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