camera_2d.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /**************************************************************************/
  2. /* camera_2d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "camera_2d.h"
  31. #include "core/config/project_settings.h"
  32. #include "scene/main/window.h"
  33. void Camera2D::_update_scroll() {
  34. if (!is_inside_tree()) {
  35. return;
  36. }
  37. if (Engine::get_singleton()->is_editor_hint()) {
  38. queue_redraw();
  39. // Only set viewport transform when not bound to the main viewport.
  40. if (get_viewport() == get_tree()->get_edited_scene_root()->get_viewport()) {
  41. return;
  42. }
  43. }
  44. if (!viewport) {
  45. return;
  46. }
  47. if (is_current()) {
  48. ERR_FAIL_COND(custom_viewport && !ObjectDB::get_instance(custom_viewport_id));
  49. Transform2D xform = get_camera_transform();
  50. viewport->set_canvas_transform(xform);
  51. Size2 screen_size = _get_camera_screen_size();
  52. Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5) : Point2());
  53. get_tree()->call_group(group_name, "_camera_moved", xform, screen_offset);
  54. };
  55. }
  56. void Camera2D::_update_process_callback() {
  57. if (Engine::get_singleton()->is_editor_hint()) {
  58. set_process_internal(false);
  59. set_physics_process_internal(false);
  60. } else if (process_callback == CAMERA2D_PROCESS_IDLE) {
  61. set_process_internal(true);
  62. set_physics_process_internal(false);
  63. } else {
  64. set_process_internal(false);
  65. set_physics_process_internal(true);
  66. }
  67. }
  68. void Camera2D::set_zoom(const Vector2 &p_zoom) {
  69. // Setting zoom to zero causes 'affine_invert' issues
  70. 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).");
  71. zoom = p_zoom;
  72. zoom_scale = Vector2(1, 1) / zoom;
  73. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  74. _update_scroll();
  75. smoothed_camera_pos = old_smoothed_camera_pos;
  76. };
  77. Vector2 Camera2D::get_zoom() const {
  78. return zoom;
  79. };
  80. Transform2D Camera2D::get_camera_transform() {
  81. if (!get_tree()) {
  82. return Transform2D();
  83. }
  84. ERR_FAIL_COND_V(custom_viewport && !ObjectDB::get_instance(custom_viewport_id), Transform2D());
  85. Size2 screen_size = _get_camera_screen_size();
  86. Point2 new_camera_pos = get_global_position();
  87. Point2 ret_camera_pos;
  88. if (!first) {
  89. if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
  90. if (drag_horizontal_enabled && !Engine::get_singleton()->is_editor_hint() && !drag_horizontal_offset_changed) {
  91. camera_pos.x = MIN(camera_pos.x, (new_camera_pos.x + screen_size.x * 0.5 * zoom_scale.x * drag_margin[SIDE_LEFT]));
  92. camera_pos.x = MAX(camera_pos.x, (new_camera_pos.x - screen_size.x * 0.5 * zoom_scale.x * drag_margin[SIDE_RIGHT]));
  93. } else {
  94. if (drag_horizontal_offset < 0) {
  95. camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_RIGHT] * drag_horizontal_offset;
  96. } else {
  97. camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_LEFT] * drag_horizontal_offset;
  98. }
  99. drag_horizontal_offset_changed = false;
  100. }
  101. if (drag_vertical_enabled && !Engine::get_singleton()->is_editor_hint() && !drag_vertical_offset_changed) {
  102. camera_pos.y = MIN(camera_pos.y, (new_camera_pos.y + screen_size.y * 0.5 * zoom_scale.y * drag_margin[SIDE_TOP]));
  103. camera_pos.y = MAX(camera_pos.y, (new_camera_pos.y - screen_size.y * 0.5 * zoom_scale.y * drag_margin[SIDE_BOTTOM]));
  104. } else {
  105. if (drag_vertical_offset < 0) {
  106. camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_BOTTOM] * drag_vertical_offset;
  107. } else {
  108. camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_TOP] * drag_vertical_offset;
  109. }
  110. drag_vertical_offset_changed = false;
  111. }
  112. } else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
  113. camera_pos = new_camera_pos;
  114. }
  115. Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom_scale) : Point2());
  116. Rect2 screen_rect(-screen_offset + camera_pos, screen_size * zoom_scale);
  117. if (limit_smoothing_enabled) {
  118. if (screen_rect.position.x < limit[SIDE_LEFT]) {
  119. camera_pos.x -= screen_rect.position.x - limit[SIDE_LEFT];
  120. }
  121. if (screen_rect.position.x + screen_rect.size.x > limit[SIDE_RIGHT]) {
  122. camera_pos.x -= screen_rect.position.x + screen_rect.size.x - limit[SIDE_RIGHT];
  123. }
  124. if (screen_rect.position.y + screen_rect.size.y > limit[SIDE_BOTTOM]) {
  125. camera_pos.y -= screen_rect.position.y + screen_rect.size.y - limit[SIDE_BOTTOM];
  126. }
  127. if (screen_rect.position.y < limit[SIDE_TOP]) {
  128. camera_pos.y -= screen_rect.position.y - limit[SIDE_TOP];
  129. }
  130. }
  131. if (position_smoothing_enabled && !Engine::get_singleton()->is_editor_hint()) {
  132. real_t c = position_smoothing_speed * (process_callback == CAMERA2D_PROCESS_PHYSICS ? get_physics_process_delta_time() : get_process_delta_time());
  133. smoothed_camera_pos = ((camera_pos - smoothed_camera_pos) * c) + smoothed_camera_pos;
  134. ret_camera_pos = smoothed_camera_pos;
  135. //camera_pos=camera_pos*(1.0-position_smoothing_speed)+new_camera_pos*position_smoothing_speed;
  136. } else {
  137. ret_camera_pos = smoothed_camera_pos = camera_pos;
  138. }
  139. } else {
  140. ret_camera_pos = smoothed_camera_pos = camera_pos = new_camera_pos;
  141. first = false;
  142. }
  143. Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom_scale) : Point2());
  144. if (!ignore_rotation) {
  145. if (rotation_smoothing_enabled && !Engine::get_singleton()->is_editor_hint()) {
  146. real_t step = rotation_smoothing_speed * (process_callback == CAMERA2D_PROCESS_PHYSICS ? get_physics_process_delta_time() : get_process_delta_time());
  147. camera_angle = Math::lerp_angle(camera_angle, get_global_rotation(), step);
  148. } else {
  149. camera_angle = get_global_rotation();
  150. }
  151. screen_offset = screen_offset.rotated(camera_angle);
  152. }
  153. Rect2 screen_rect(-screen_offset + ret_camera_pos, screen_size * zoom_scale);
  154. if (!position_smoothing_enabled || !limit_smoothing_enabled) {
  155. if (screen_rect.position.x < limit[SIDE_LEFT]) {
  156. screen_rect.position.x = limit[SIDE_LEFT];
  157. }
  158. if (screen_rect.position.x + screen_rect.size.x > limit[SIDE_RIGHT]) {
  159. screen_rect.position.x = limit[SIDE_RIGHT] - screen_rect.size.x;
  160. }
  161. if (screen_rect.position.y + screen_rect.size.y > limit[SIDE_BOTTOM]) {
  162. screen_rect.position.y = limit[SIDE_BOTTOM] - screen_rect.size.y;
  163. }
  164. if (screen_rect.position.y < limit[SIDE_TOP]) {
  165. screen_rect.position.y = limit[SIDE_TOP];
  166. }
  167. }
  168. if (offset != Vector2()) {
  169. screen_rect.position += offset;
  170. }
  171. camera_screen_center = screen_rect.get_center();
  172. Transform2D xform;
  173. xform.scale_basis(zoom_scale);
  174. if (!ignore_rotation) {
  175. xform.set_rotation(camera_angle);
  176. }
  177. xform.set_origin(screen_rect.position);
  178. return (xform).affine_inverse();
  179. }
  180. void Camera2D::_notification(int p_what) {
  181. switch (p_what) {
  182. case NOTIFICATION_INTERNAL_PROCESS:
  183. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  184. _update_scroll();
  185. } break;
  186. case NOTIFICATION_TRANSFORM_CHANGED: {
  187. if (!is_processing_internal() && !is_physics_processing_internal()) {
  188. _update_scroll();
  189. }
  190. } break;
  191. case NOTIFICATION_ENTER_TREE: {
  192. ERR_FAIL_COND(!is_inside_tree());
  193. if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
  194. viewport = custom_viewport;
  195. } else {
  196. viewport = get_viewport();
  197. }
  198. canvas = get_canvas();
  199. RID vp = viewport->get_viewport_rid();
  200. group_name = "__cameras_" + itos(vp.get_id());
  201. canvas_group_name = "__cameras_c" + itos(canvas.get_id());
  202. add_to_group(group_name);
  203. add_to_group(canvas_group_name);
  204. if (!Engine::get_singleton()->is_editor_hint() && enabled && !viewport->get_camera_2d()) {
  205. make_current();
  206. }
  207. _update_process_callback();
  208. first = true;
  209. _update_scroll();
  210. } break;
  211. case NOTIFICATION_EXIT_TREE: {
  212. remove_from_group(group_name);
  213. remove_from_group(canvas_group_name);
  214. if (is_current()) {
  215. clear_current();
  216. }
  217. viewport = nullptr;
  218. just_exited_tree = true;
  219. callable_mp(this, &Camera2D::_reset_just_exited).call_deferred();
  220. } break;
  221. #ifdef TOOLS_ENABLED
  222. case NOTIFICATION_DRAW: {
  223. if (!is_inside_tree() || !Engine::get_singleton()->is_editor_hint()) {
  224. break;
  225. }
  226. if (screen_drawing_enabled) {
  227. Color area_axis_color(1, 0.4, 1, 0.63);
  228. real_t area_axis_width = -1;
  229. if (is_current()) {
  230. area_axis_width = 3;
  231. }
  232. Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
  233. Size2 screen_size = _get_camera_screen_size();
  234. Vector2 screen_endpoints[4] = {
  235. inv_camera_transform.xform(Vector2(0, 0)),
  236. inv_camera_transform.xform(Vector2(screen_size.width, 0)),
  237. inv_camera_transform.xform(Vector2(screen_size.width, screen_size.height)),
  238. inv_camera_transform.xform(Vector2(0, screen_size.height))
  239. };
  240. Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space
  241. for (int i = 0; i < 4; i++) {
  242. draw_line(inv_transform.xform(screen_endpoints[i]), inv_transform.xform(screen_endpoints[(i + 1) % 4]), area_axis_color, area_axis_width);
  243. }
  244. }
  245. if (limit_drawing_enabled) {
  246. Color limit_drawing_color(1, 1, 0.25, 0.63);
  247. real_t limit_drawing_width = -1;
  248. if (is_current()) {
  249. limit_drawing_width = 3;
  250. }
  251. Vector2 camera_origin = get_global_position();
  252. Vector2 camera_scale = get_global_scale().abs();
  253. Vector2 limit_points[4] = {
  254. (Vector2(limit[SIDE_LEFT], limit[SIDE_TOP]) - camera_origin) / camera_scale,
  255. (Vector2(limit[SIDE_RIGHT], limit[SIDE_TOP]) - camera_origin) / camera_scale,
  256. (Vector2(limit[SIDE_RIGHT], limit[SIDE_BOTTOM]) - camera_origin) / camera_scale,
  257. (Vector2(limit[SIDE_LEFT], limit[SIDE_BOTTOM]) - camera_origin) / camera_scale
  258. };
  259. for (int i = 0; i < 4; i++) {
  260. draw_line(limit_points[i], limit_points[(i + 1) % 4], limit_drawing_color, limit_drawing_width);
  261. }
  262. }
  263. if (margin_drawing_enabled) {
  264. Color margin_drawing_color(0.25, 1, 1, 0.63);
  265. real_t margin_drawing_width = -1;
  266. if (is_current()) {
  267. margin_drawing_width = 3;
  268. }
  269. Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
  270. Size2 screen_size = _get_camera_screen_size();
  271. Vector2 margin_endpoints[4] = {
  272. inv_camera_transform.xform(Vector2((screen_size.width / 2) - ((screen_size.width / 2) * drag_margin[SIDE_LEFT]), (screen_size.height / 2) - ((screen_size.height / 2) * drag_margin[SIDE_TOP]))),
  273. inv_camera_transform.xform(Vector2((screen_size.width / 2) + ((screen_size.width / 2) * drag_margin[SIDE_RIGHT]), (screen_size.height / 2) - ((screen_size.height / 2) * drag_margin[SIDE_TOP]))),
  274. inv_camera_transform.xform(Vector2((screen_size.width / 2) + ((screen_size.width / 2) * drag_margin[SIDE_RIGHT]), (screen_size.height / 2) + ((screen_size.height / 2) * drag_margin[SIDE_BOTTOM]))),
  275. inv_camera_transform.xform(Vector2((screen_size.width / 2) - ((screen_size.width / 2) * drag_margin[SIDE_LEFT]), (screen_size.height / 2) + ((screen_size.height / 2) * drag_margin[SIDE_BOTTOM])))
  276. };
  277. Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space
  278. for (int i = 0; i < 4; i++) {
  279. draw_line(inv_transform.xform(margin_endpoints[i]), inv_transform.xform(margin_endpoints[(i + 1) % 4]), margin_drawing_color, margin_drawing_width);
  280. }
  281. }
  282. } break;
  283. #endif
  284. }
  285. }
  286. void Camera2D::set_offset(const Vector2 &p_offset) {
  287. offset = p_offset;
  288. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  289. _update_scroll();
  290. smoothed_camera_pos = old_smoothed_camera_pos;
  291. }
  292. Vector2 Camera2D::get_offset() const {
  293. return offset;
  294. }
  295. void Camera2D::set_anchor_mode(AnchorMode p_anchor_mode) {
  296. anchor_mode = p_anchor_mode;
  297. _update_scroll();
  298. }
  299. Camera2D::AnchorMode Camera2D::get_anchor_mode() const {
  300. return anchor_mode;
  301. }
  302. void Camera2D::set_ignore_rotation(bool p_ignore) {
  303. ignore_rotation = p_ignore;
  304. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  305. // Reset back to zero so it matches the camera rotation when ignore_rotation is enabled.
  306. if (ignore_rotation) {
  307. camera_angle = 0.0;
  308. }
  309. _update_scroll();
  310. smoothed_camera_pos = old_smoothed_camera_pos;
  311. }
  312. bool Camera2D::is_ignoring_rotation() const {
  313. return ignore_rotation;
  314. }
  315. void Camera2D::set_process_callback(Camera2DProcessCallback p_mode) {
  316. if (process_callback == p_mode) {
  317. return;
  318. }
  319. process_callback = p_mode;
  320. _update_process_callback();
  321. }
  322. void Camera2D::set_enabled(bool p_enabled) {
  323. enabled = p_enabled;
  324. if (enabled && is_inside_tree() && !viewport->get_camera_2d()) {
  325. make_current();
  326. } else if (!enabled && is_current()) {
  327. clear_current();
  328. }
  329. }
  330. bool Camera2D::is_enabled() const {
  331. return enabled;
  332. }
  333. Camera2D::Camera2DProcessCallback Camera2D::get_process_callback() const {
  334. return process_callback;
  335. }
  336. void Camera2D::_make_current(Object *p_which) {
  337. if (!viewport || (custom_viewport && !ObjectDB::get_instance(custom_viewport_id))) {
  338. return;
  339. }
  340. if (p_which == this) {
  341. if (is_inside_tree()) {
  342. viewport->_camera_2d_set(this);
  343. queue_redraw();
  344. }
  345. } else {
  346. if (is_inside_tree()) {
  347. if (viewport->get_camera_2d() == this) {
  348. viewport->_camera_2d_set(nullptr);
  349. }
  350. queue_redraw();
  351. }
  352. }
  353. }
  354. void Camera2D::_update_process_internal_for_smoothing() {
  355. bool is_not_in_scene_or_editor = !(is_inside_tree() && Engine::get_singleton()->is_editor_hint());
  356. bool is_any_smoothing_valid = position_smoothing_speed > 0 || rotation_smoothing_speed > 0;
  357. bool enable = is_any_smoothing_valid && is_not_in_scene_or_editor;
  358. set_process_internal(enable);
  359. }
  360. void Camera2D::make_current() {
  361. ERR_FAIL_COND(!enabled || !is_inside_tree());
  362. get_tree()->call_group(group_name, "_make_current", this);
  363. if (just_exited_tree) {
  364. // If camera exited the scene tree in the same frame, group call will skip it, so this needs to be called manually.
  365. _make_current(this);
  366. }
  367. _update_scroll();
  368. }
  369. void Camera2D::clear_current() {
  370. ERR_FAIL_COND(!is_current());
  371. if (viewport && !(custom_viewport && !ObjectDB::get_instance(custom_viewport_id)) && viewport->is_inside_tree()) {
  372. viewport->assign_next_enabled_camera_2d(group_name);
  373. }
  374. }
  375. bool Camera2D::is_current() const {
  376. if (viewport && !(custom_viewport && !ObjectDB::get_instance(custom_viewport_id))) {
  377. return viewport->get_camera_2d() == this;
  378. }
  379. return false;
  380. }
  381. void Camera2D::set_limit(Side p_side, int p_limit) {
  382. ERR_FAIL_INDEX((int)p_side, 4);
  383. limit[p_side] = p_limit;
  384. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  385. _update_scroll();
  386. smoothed_camera_pos = old_smoothed_camera_pos;
  387. }
  388. int Camera2D::get_limit(Side p_side) const {
  389. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  390. return limit[p_side];
  391. }
  392. void Camera2D::set_limit_smoothing_enabled(bool enable) {
  393. limit_smoothing_enabled = enable;
  394. _update_scroll();
  395. }
  396. bool Camera2D::is_limit_smoothing_enabled() const {
  397. return limit_smoothing_enabled;
  398. }
  399. void Camera2D::set_drag_margin(Side p_side, real_t p_drag_margin) {
  400. ERR_FAIL_INDEX((int)p_side, 4);
  401. drag_margin[p_side] = p_drag_margin;
  402. queue_redraw();
  403. }
  404. real_t Camera2D::get_drag_margin(Side p_side) const {
  405. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  406. return drag_margin[p_side];
  407. }
  408. Vector2 Camera2D::get_camera_position() const {
  409. return camera_pos;
  410. }
  411. void Camera2D::force_update_scroll() {
  412. _update_scroll();
  413. }
  414. void Camera2D::reset_smoothing() {
  415. _update_scroll();
  416. smoothed_camera_pos = camera_pos;
  417. }
  418. void Camera2D::align() {
  419. ERR_FAIL_COND(custom_viewport && !ObjectDB::get_instance(custom_viewport_id));
  420. Size2 screen_size = _get_camera_screen_size();
  421. Point2 current_camera_pos = get_global_position();
  422. if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
  423. if (drag_horizontal_offset < 0) {
  424. camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_RIGHT] * drag_horizontal_offset;
  425. } else {
  426. camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_LEFT] * drag_horizontal_offset;
  427. }
  428. if (drag_vertical_offset < 0) {
  429. camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_TOP] * drag_vertical_offset;
  430. } else {
  431. camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_BOTTOM] * drag_vertical_offset;
  432. }
  433. } else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
  434. camera_pos = current_camera_pos;
  435. }
  436. _update_scroll();
  437. }
  438. void Camera2D::set_position_smoothing_speed(real_t p_speed) {
  439. position_smoothing_speed = p_speed;
  440. _update_process_internal_for_smoothing();
  441. }
  442. real_t Camera2D::get_position_smoothing_speed() const {
  443. return position_smoothing_speed;
  444. }
  445. void Camera2D::set_rotation_smoothing_speed(real_t p_speed) {
  446. rotation_smoothing_speed = p_speed;
  447. _update_process_internal_for_smoothing();
  448. }
  449. real_t Camera2D::get_rotation_smoothing_speed() const {
  450. return rotation_smoothing_speed;
  451. }
  452. void Camera2D::set_rotation_smoothing_enabled(bool p_enabled) {
  453. rotation_smoothing_enabled = p_enabled;
  454. notify_property_list_changed();
  455. }
  456. bool Camera2D::is_rotation_smoothing_enabled() const {
  457. return rotation_smoothing_enabled;
  458. }
  459. Point2 Camera2D::get_camera_screen_center() const {
  460. return camera_screen_center;
  461. }
  462. Size2 Camera2D::_get_camera_screen_size() const {
  463. // special case if the camera2D is in the root viewport
  464. if (Engine::get_singleton()->is_editor_hint() && get_viewport()->get_parent_viewport() == get_tree()->get_root()) {
  465. return Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
  466. }
  467. return get_viewport_rect().size;
  468. }
  469. void Camera2D::set_drag_horizontal_enabled(bool p_enabled) {
  470. drag_horizontal_enabled = p_enabled;
  471. }
  472. bool Camera2D::is_drag_horizontal_enabled() const {
  473. return drag_horizontal_enabled;
  474. }
  475. void Camera2D::set_drag_vertical_enabled(bool p_enabled) {
  476. drag_vertical_enabled = p_enabled;
  477. }
  478. bool Camera2D::is_drag_vertical_enabled() const {
  479. return drag_vertical_enabled;
  480. }
  481. void Camera2D::set_drag_vertical_offset(real_t p_offset) {
  482. drag_vertical_offset = p_offset;
  483. drag_vertical_offset_changed = true;
  484. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  485. _update_scroll();
  486. smoothed_camera_pos = old_smoothed_camera_pos;
  487. }
  488. real_t Camera2D::get_drag_vertical_offset() const {
  489. return drag_vertical_offset;
  490. }
  491. void Camera2D::set_drag_horizontal_offset(real_t p_offset) {
  492. drag_horizontal_offset = p_offset;
  493. drag_horizontal_offset_changed = true;
  494. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  495. _update_scroll();
  496. smoothed_camera_pos = old_smoothed_camera_pos;
  497. }
  498. real_t Camera2D::get_drag_horizontal_offset() const {
  499. return drag_horizontal_offset;
  500. }
  501. void Camera2D::_set_old_smoothing(real_t p_enable) {
  502. //compatibility
  503. if (p_enable > 0) {
  504. position_smoothing_enabled = true;
  505. set_position_smoothing_speed(p_enable);
  506. }
  507. }
  508. void Camera2D::set_position_smoothing_enabled(bool p_enabled) {
  509. position_smoothing_enabled = p_enabled;
  510. notify_property_list_changed();
  511. }
  512. bool Camera2D::is_position_smoothing_enabled() const {
  513. return position_smoothing_enabled;
  514. }
  515. void Camera2D::set_custom_viewport(Node *p_viewport) {
  516. ERR_FAIL_NULL(p_viewport);
  517. if (is_inside_tree()) {
  518. remove_from_group(group_name);
  519. remove_from_group(canvas_group_name);
  520. }
  521. custom_viewport = Object::cast_to<Viewport>(p_viewport);
  522. if (custom_viewport) {
  523. custom_viewport_id = custom_viewport->get_instance_id();
  524. } else {
  525. custom_viewport_id = ObjectID();
  526. }
  527. if (is_inside_tree()) {
  528. if (custom_viewport) {
  529. viewport = custom_viewport;
  530. } else {
  531. viewport = get_viewport();
  532. }
  533. RID vp = viewport->get_viewport_rid();
  534. group_name = "__cameras_" + itos(vp.get_id());
  535. canvas_group_name = "__cameras_c" + itos(canvas.get_id());
  536. add_to_group(group_name);
  537. add_to_group(canvas_group_name);
  538. }
  539. }
  540. Node *Camera2D::get_custom_viewport() const {
  541. return custom_viewport;
  542. }
  543. void Camera2D::set_screen_drawing_enabled(bool enable) {
  544. screen_drawing_enabled = enable;
  545. #ifdef TOOLS_ENABLED
  546. queue_redraw();
  547. #endif
  548. }
  549. bool Camera2D::is_screen_drawing_enabled() const {
  550. return screen_drawing_enabled;
  551. }
  552. void Camera2D::set_limit_drawing_enabled(bool enable) {
  553. limit_drawing_enabled = enable;
  554. #ifdef TOOLS_ENABLED
  555. queue_redraw();
  556. #endif
  557. }
  558. bool Camera2D::is_limit_drawing_enabled() const {
  559. return limit_drawing_enabled;
  560. }
  561. void Camera2D::set_margin_drawing_enabled(bool enable) {
  562. margin_drawing_enabled = enable;
  563. #ifdef TOOLS_ENABLED
  564. queue_redraw();
  565. #endif
  566. }
  567. bool Camera2D::is_margin_drawing_enabled() const {
  568. return margin_drawing_enabled;
  569. }
  570. void Camera2D::_validate_property(PropertyInfo &p_property) const {
  571. if (!position_smoothing_enabled && p_property.name == "position_smoothing_speed") {
  572. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  573. }
  574. if (!rotation_smoothing_enabled && p_property.name == "rotation_smoothing_speed") {
  575. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  576. }
  577. }
  578. void Camera2D::_bind_methods() {
  579. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Camera2D::set_offset);
  580. ClassDB::bind_method(D_METHOD("get_offset"), &Camera2D::get_offset);
  581. ClassDB::bind_method(D_METHOD("set_anchor_mode", "anchor_mode"), &Camera2D::set_anchor_mode);
  582. ClassDB::bind_method(D_METHOD("get_anchor_mode"), &Camera2D::get_anchor_mode);
  583. ClassDB::bind_method(D_METHOD("set_ignore_rotation", "ignore"), &Camera2D::set_ignore_rotation);
  584. ClassDB::bind_method(D_METHOD("is_ignoring_rotation"), &Camera2D::is_ignoring_rotation);
  585. ClassDB::bind_method(D_METHOD("_update_scroll"), &Camera2D::_update_scroll);
  586. ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &Camera2D::set_process_callback);
  587. ClassDB::bind_method(D_METHOD("get_process_callback"), &Camera2D::get_process_callback);
  588. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &Camera2D::set_enabled);
  589. ClassDB::bind_method(D_METHOD("is_enabled"), &Camera2D::is_enabled);
  590. ClassDB::bind_method(D_METHOD("make_current"), &Camera2D::make_current);
  591. ClassDB::bind_method(D_METHOD("is_current"), &Camera2D::is_current);
  592. ClassDB::bind_method(D_METHOD("_make_current"), &Camera2D::_make_current);
  593. ClassDB::bind_method(D_METHOD("set_limit", "margin", "limit"), &Camera2D::set_limit);
  594. ClassDB::bind_method(D_METHOD("get_limit", "margin"), &Camera2D::get_limit);
  595. ClassDB::bind_method(D_METHOD("set_limit_smoothing_enabled", "limit_smoothing_enabled"), &Camera2D::set_limit_smoothing_enabled);
  596. ClassDB::bind_method(D_METHOD("is_limit_smoothing_enabled"), &Camera2D::is_limit_smoothing_enabled);
  597. ClassDB::bind_method(D_METHOD("set_drag_vertical_enabled", "enabled"), &Camera2D::set_drag_vertical_enabled);
  598. ClassDB::bind_method(D_METHOD("is_drag_vertical_enabled"), &Camera2D::is_drag_vertical_enabled);
  599. ClassDB::bind_method(D_METHOD("set_drag_horizontal_enabled", "enabled"), &Camera2D::set_drag_horizontal_enabled);
  600. ClassDB::bind_method(D_METHOD("is_drag_horizontal_enabled"), &Camera2D::is_drag_horizontal_enabled);
  601. ClassDB::bind_method(D_METHOD("set_drag_vertical_offset", "offset"), &Camera2D::set_drag_vertical_offset);
  602. ClassDB::bind_method(D_METHOD("get_drag_vertical_offset"), &Camera2D::get_drag_vertical_offset);
  603. ClassDB::bind_method(D_METHOD("set_drag_horizontal_offset", "offset"), &Camera2D::set_drag_horizontal_offset);
  604. ClassDB::bind_method(D_METHOD("get_drag_horizontal_offset"), &Camera2D::get_drag_horizontal_offset);
  605. ClassDB::bind_method(D_METHOD("set_drag_margin", "margin", "drag_margin"), &Camera2D::set_drag_margin);
  606. ClassDB::bind_method(D_METHOD("get_drag_margin", "margin"), &Camera2D::get_drag_margin);
  607. ClassDB::bind_method(D_METHOD("get_target_position"), &Camera2D::get_camera_position);
  608. ClassDB::bind_method(D_METHOD("get_screen_center_position"), &Camera2D::get_camera_screen_center);
  609. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &Camera2D::set_zoom);
  610. ClassDB::bind_method(D_METHOD("get_zoom"), &Camera2D::get_zoom);
  611. ClassDB::bind_method(D_METHOD("set_custom_viewport", "viewport"), &Camera2D::set_custom_viewport);
  612. ClassDB::bind_method(D_METHOD("get_custom_viewport"), &Camera2D::get_custom_viewport);
  613. ClassDB::bind_method(D_METHOD("set_position_smoothing_speed", "position_smoothing_speed"), &Camera2D::set_position_smoothing_speed);
  614. ClassDB::bind_method(D_METHOD("get_position_smoothing_speed"), &Camera2D::get_position_smoothing_speed);
  615. ClassDB::bind_method(D_METHOD("set_position_smoothing_enabled", "position_smoothing_speed"), &Camera2D::set_position_smoothing_enabled);
  616. ClassDB::bind_method(D_METHOD("is_position_smoothing_enabled"), &Camera2D::is_position_smoothing_enabled);
  617. ClassDB::bind_method(D_METHOD("set_rotation_smoothing_enabled", "enabled"), &Camera2D::set_rotation_smoothing_enabled);
  618. ClassDB::bind_method(D_METHOD("is_rotation_smoothing_enabled"), &Camera2D::is_rotation_smoothing_enabled);
  619. ClassDB::bind_method(D_METHOD("set_rotation_smoothing_speed", "speed"), &Camera2D::set_rotation_smoothing_speed);
  620. ClassDB::bind_method(D_METHOD("get_rotation_smoothing_speed"), &Camera2D::get_rotation_smoothing_speed);
  621. ClassDB::bind_method(D_METHOD("force_update_scroll"), &Camera2D::force_update_scroll);
  622. ClassDB::bind_method(D_METHOD("reset_smoothing"), &Camera2D::reset_smoothing);
  623. ClassDB::bind_method(D_METHOD("align"), &Camera2D::align);
  624. ClassDB::bind_method(D_METHOD("_set_old_smoothing", "follow_smoothing"), &Camera2D::_set_old_smoothing);
  625. ClassDB::bind_method(D_METHOD("set_screen_drawing_enabled", "screen_drawing_enabled"), &Camera2D::set_screen_drawing_enabled);
  626. ClassDB::bind_method(D_METHOD("is_screen_drawing_enabled"), &Camera2D::is_screen_drawing_enabled);
  627. ClassDB::bind_method(D_METHOD("set_limit_drawing_enabled", "limit_drawing_enabled"), &Camera2D::set_limit_drawing_enabled);
  628. ClassDB::bind_method(D_METHOD("is_limit_drawing_enabled"), &Camera2D::is_limit_drawing_enabled);
  629. ClassDB::bind_method(D_METHOD("set_margin_drawing_enabled", "margin_drawing_enabled"), &Camera2D::set_margin_drawing_enabled);
  630. ClassDB::bind_method(D_METHOD("is_margin_drawing_enabled"), &Camera2D::is_margin_drawing_enabled);
  631. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset");
  632. ADD_PROPERTY(PropertyInfo(Variant::INT, "anchor_mode", PROPERTY_HINT_ENUM, "Fixed TopLeft,Drag Center"), "set_anchor_mode", "get_anchor_mode");
  633. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_rotation"), "set_ignore_rotation", "is_ignoring_rotation");
  634. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  635. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "zoom", PROPERTY_HINT_LINK), "set_zoom", "get_zoom");
  636. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport", PROPERTY_USAGE_NONE), "set_custom_viewport", "get_custom_viewport");
  637. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_callback", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_process_callback", "get_process_callback");
  638. ADD_GROUP("Limit", "limit_");
  639. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_left", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_LEFT);
  640. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_top", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_TOP);
  641. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_right", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_RIGHT);
  642. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_bottom", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_BOTTOM);
  643. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "limit_smoothed"), "set_limit_smoothing_enabled", "is_limit_smoothing_enabled");
  644. ADD_GROUP("Position Smoothing", "position_smoothing_");
  645. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "position_smoothing_enabled"), "set_position_smoothing_enabled", "is_position_smoothing_enabled");
  646. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "position_smoothing_speed", PROPERTY_HINT_NONE, "suffix:px/s"), "set_position_smoothing_speed", "get_position_smoothing_speed");
  647. ADD_GROUP("Rotation Smoothing", "rotation_smoothing_");
  648. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotation_smoothing_enabled"), "set_rotation_smoothing_enabled", "is_rotation_smoothing_enabled");
  649. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation_smoothing_speed"), "set_rotation_smoothing_speed", "get_rotation_smoothing_speed");
  650. ADD_GROUP("Drag", "drag_");
  651. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_horizontal_enabled"), "set_drag_horizontal_enabled", "is_drag_horizontal_enabled");
  652. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_vertical_enabled"), "set_drag_vertical_enabled", "is_drag_vertical_enabled");
  653. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "drag_horizontal_offset", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_drag_horizontal_offset", "get_drag_horizontal_offset");
  654. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "drag_vertical_offset", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_drag_vertical_offset", "get_drag_vertical_offset");
  655. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_left_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_LEFT);
  656. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_top_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_TOP);
  657. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_right_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_RIGHT);
  658. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_bottom_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_BOTTOM);
  659. ADD_GROUP("Editor", "editor_");
  660. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_screen"), "set_screen_drawing_enabled", "is_screen_drawing_enabled");
  661. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_limits"), "set_limit_drawing_enabled", "is_limit_drawing_enabled");
  662. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_drag_margin"), "set_margin_drawing_enabled", "is_margin_drawing_enabled");
  663. BIND_ENUM_CONSTANT(ANCHOR_MODE_FIXED_TOP_LEFT);
  664. BIND_ENUM_CONSTANT(ANCHOR_MODE_DRAG_CENTER);
  665. BIND_ENUM_CONSTANT(CAMERA2D_PROCESS_PHYSICS);
  666. BIND_ENUM_CONSTANT(CAMERA2D_PROCESS_IDLE);
  667. }
  668. Camera2D::Camera2D() {
  669. limit[SIDE_LEFT] = -10000000;
  670. limit[SIDE_TOP] = -10000000;
  671. limit[SIDE_RIGHT] = 10000000;
  672. limit[SIDE_BOTTOM] = 10000000;
  673. drag_margin[SIDE_LEFT] = 0.2;
  674. drag_margin[SIDE_TOP] = 0.2;
  675. drag_margin[SIDE_RIGHT] = 0.2;
  676. drag_margin[SIDE_BOTTOM] = 0.2;
  677. set_notify_transform(true);
  678. set_hide_clip_children(true);
  679. }