camera_2d.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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 "core/input/input.h"
  33. #include "scene/main/viewport.h"
  34. #ifdef TOOLS_ENABLED
  35. Dictionary Camera2D::_edit_get_state() const {
  36. Dictionary state = Node2D::_edit_get_state();
  37. state["limit_rect"] = get_limit_rect();
  38. return state;
  39. }
  40. void Camera2D::_edit_set_state(const Dictionary &p_state) {
  41. if (p_state.has("limit_rect")) {
  42. _set_limit_rect(p_state["limit_rect"]);
  43. }
  44. Node2D::_edit_set_state(p_state);
  45. }
  46. void Camera2D::_edit_set_position(const Point2 &p_position) {
  47. if (_is_dragging_limit_rect()) {
  48. Rect2 rect = get_limit_rect();
  49. rect.position = p_position;
  50. _set_limit_rect(rect);
  51. } else {
  52. Node2D::_edit_set_position(p_position);
  53. }
  54. }
  55. Point2 Camera2D::_edit_get_position() const {
  56. return _is_dragging_limit_rect() ? get_limit_rect().position : Node2D::_edit_get_position();
  57. }
  58. void Camera2D::_edit_set_rect(const Rect2 &p_rect) {
  59. ERR_FAIL_COND(limit_enabled && !_edit_use_rect());
  60. Rect2 rect = p_rect;
  61. Vector2 scl = get_global_scale().abs();
  62. rect.size *= scl;
  63. rect.position = (rect.position + get_global_position()) * scl;
  64. _set_limit_rect(rect);
  65. }
  66. #endif // TOOLS_ENABLED
  67. #ifdef DEBUG_ENABLED
  68. Rect2 Camera2D::_edit_get_rect() const {
  69. Rect2 rect = get_limit_rect();
  70. Vector2 scl = get_global_scale().abs();
  71. rect.size /= scl;
  72. rect.position = (rect.position - get_global_position()) / scl;
  73. return rect;
  74. }
  75. bool Camera2D::_edit_use_rect() const {
  76. return limit_enabled;
  77. }
  78. #endif // DEBUG_ENABLED
  79. void Camera2D::_update_scroll() {
  80. if (!is_inside_tree() || !viewport) {
  81. return;
  82. }
  83. if (is_part_of_edited_scene()) {
  84. queue_redraw();
  85. return;
  86. }
  87. if (is_current()) {
  88. ERR_FAIL_COND(custom_viewport && !ObjectDB::get_instance(custom_viewport_id));
  89. Size2 screen_size = _get_camera_screen_size();
  90. Transform2D xform;
  91. if (is_physics_interpolated_and_enabled()) {
  92. xform = _interpolation_data.xform_prev.interpolate_with(_interpolation_data.xform_curr, Engine::get_singleton()->get_physics_interpolation_fraction());
  93. camera_screen_center = xform.affine_inverse().xform(0.5 * screen_size);
  94. } else {
  95. xform = get_camera_transform();
  96. }
  97. viewport->set_canvas_transform(xform);
  98. Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5) : Point2());
  99. Point2 adj_screen_pos = camera_screen_center - (screen_size * 0.5);
  100. // TODO: Remove xform and screen_offset when ParallaxBackground/ParallaxLayer is removed.
  101. get_tree()->call_group(group_name, SNAME("_camera_moved"), xform, screen_offset, adj_screen_pos);
  102. }
  103. }
  104. #ifdef TOOLS_ENABLED
  105. bool Camera2D::_is_dragging_limit_rect() const {
  106. return _edit_use_rect() && Input::get_singleton()->is_key_pressed(Key::CTRL);
  107. }
  108. void Camera2D::_project_settings_changed() {
  109. if (screen_drawing_enabled) {
  110. queue_redraw();
  111. }
  112. }
  113. #endif
  114. void Camera2D::_update_process_callback() {
  115. if (is_physics_interpolated_and_enabled()) {
  116. set_process_internal(is_current());
  117. set_physics_process_internal(is_current());
  118. #ifdef TOOLS_ENABLED
  119. if (process_callback == CAMERA2D_PROCESS_IDLE) {
  120. WARN_PRINT_ONCE("Camera2D overridden to physics process mode due to use of physics interpolation.");
  121. }
  122. #endif
  123. } else if (is_part_of_edited_scene()) {
  124. set_process_internal(false);
  125. set_physics_process_internal(false);
  126. } else {
  127. if (process_callback == CAMERA2D_PROCESS_IDLE) {
  128. set_process_internal(true);
  129. set_physics_process_internal(false);
  130. } else {
  131. set_process_internal(false);
  132. set_physics_process_internal(true);
  133. }
  134. }
  135. }
  136. void Camera2D::set_zoom(const Vector2 &p_zoom) {
  137. // Setting zoom to zero causes 'affine_invert' issues.
  138. 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).");
  139. zoom = p_zoom;
  140. zoom_scale = Vector2(1, 1) / zoom;
  141. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  142. _update_scroll();
  143. smoothed_camera_pos = old_smoothed_camera_pos;
  144. }
  145. Vector2 Camera2D::get_zoom() const {
  146. return zoom;
  147. }
  148. Transform2D Camera2D::get_camera_transform() {
  149. if (!get_tree()) {
  150. return Transform2D();
  151. }
  152. ERR_FAIL_COND_V(custom_viewport && !ObjectDB::get_instance(custom_viewport_id), Transform2D());
  153. Size2 screen_size = _get_camera_screen_size();
  154. Point2 new_camera_pos = get_global_position();
  155. Point2 ret_camera_pos;
  156. if (!first) {
  157. if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
  158. if (drag_horizontal_enabled && !is_part_of_edited_scene() && !drag_horizontal_offset_changed) {
  159. camera_pos.x = MIN(camera_pos.x, (new_camera_pos.x + screen_size.x * 0.5 * zoom_scale.x * drag_margin[SIDE_LEFT]));
  160. camera_pos.x = MAX(camera_pos.x, (new_camera_pos.x - screen_size.x * 0.5 * zoom_scale.x * drag_margin[SIDE_RIGHT]));
  161. } else {
  162. if (drag_horizontal_offset < 0) {
  163. camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_RIGHT] * drag_horizontal_offset;
  164. } else {
  165. camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_LEFT] * drag_horizontal_offset;
  166. }
  167. drag_horizontal_offset_changed = false;
  168. }
  169. if (drag_vertical_enabled && !is_part_of_edited_scene() && !drag_vertical_offset_changed) {
  170. camera_pos.y = MIN(camera_pos.y, (new_camera_pos.y + screen_size.y * 0.5 * zoom_scale.y * drag_margin[SIDE_TOP]));
  171. camera_pos.y = MAX(camera_pos.y, (new_camera_pos.y - screen_size.y * 0.5 * zoom_scale.y * drag_margin[SIDE_BOTTOM]));
  172. } else {
  173. if (drag_vertical_offset < 0) {
  174. camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_BOTTOM] * drag_vertical_offset;
  175. } else {
  176. camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_TOP] * drag_vertical_offset;
  177. }
  178. drag_vertical_offset_changed = false;
  179. }
  180. } else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
  181. camera_pos = new_camera_pos;
  182. }
  183. Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom_scale) : Point2());
  184. Rect2 screen_rect(-screen_offset + camera_pos, screen_size * zoom_scale);
  185. if (limit_enabled && limit_smoothing_enabled) {
  186. // Apply horizontal limiting.
  187. if (screen_rect.size.x > limit[SIDE_RIGHT] - limit[SIDE_LEFT]) {
  188. // Split the limit difference horizontally.
  189. camera_pos.x -= screen_rect.position.x + (screen_rect.size.x - limit[SIDE_RIGHT] - limit[SIDE_LEFT]) / 2;
  190. } else if (screen_rect.position.x < limit[SIDE_LEFT]) {
  191. // Only apply left limit.
  192. camera_pos.x -= screen_rect.position.x - limit[SIDE_LEFT];
  193. } else if (screen_rect.position.x + screen_rect.size.x > limit[SIDE_RIGHT]) {
  194. // Only apply the right limit.
  195. camera_pos.x -= screen_rect.position.x + screen_rect.size.x - limit[SIDE_RIGHT];
  196. }
  197. // Apply vertical limiting.
  198. if (screen_rect.size.y > limit[SIDE_BOTTOM] - limit[SIDE_TOP]) {
  199. // Split the limit difference vertically.
  200. camera_pos.y -= screen_rect.position.y + (screen_rect.size.y - limit[SIDE_BOTTOM] - limit[SIDE_TOP]) / 2;
  201. } else if (screen_rect.position.y < limit[SIDE_TOP]) {
  202. // Only apply the top limit.
  203. camera_pos.y -= screen_rect.position.y - limit[SIDE_TOP];
  204. } else if (screen_rect.position.y + screen_rect.size.y > limit[SIDE_BOTTOM]) {
  205. // Only apply the bottom limit.
  206. camera_pos.y -= screen_rect.position.y + screen_rect.size.y - limit[SIDE_BOTTOM];
  207. }
  208. }
  209. // FIXME: There is a bug here, introduced before physics interpolation.
  210. // Smoothing occurs rather confusingly during the call to get_camera_transform().
  211. // It may be called MULTIPLE TIMES on certain frames,
  212. // therefore smoothing is not currently applied only once per frame / tick,
  213. // which will result in some haphazard results.
  214. if (position_smoothing_enabled && !is_part_of_edited_scene()) {
  215. bool physics_process = (process_callback == CAMERA2D_PROCESS_PHYSICS) || is_physics_interpolated_and_enabled();
  216. real_t delta = physics_process ? get_physics_process_delta_time() : get_process_delta_time();
  217. real_t c = position_smoothing_speed * delta;
  218. smoothed_camera_pos = ((camera_pos - smoothed_camera_pos) * c) + smoothed_camera_pos;
  219. ret_camera_pos = smoothed_camera_pos;
  220. //camera_pos=camera_pos*(1.0-position_smoothing_speed)+new_camera_pos*position_smoothing_speed;
  221. } else {
  222. ret_camera_pos = smoothed_camera_pos = camera_pos;
  223. }
  224. } else {
  225. ret_camera_pos = smoothed_camera_pos = camera_pos = new_camera_pos;
  226. first = false;
  227. }
  228. Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom_scale) : Point2());
  229. if (!ignore_rotation) {
  230. if (rotation_smoothing_enabled && !is_part_of_edited_scene()) {
  231. real_t step = rotation_smoothing_speed * (process_callback == CAMERA2D_PROCESS_PHYSICS ? get_physics_process_delta_time() : get_process_delta_time());
  232. camera_angle = Math::lerp_angle(camera_angle, get_global_rotation(), step);
  233. } else {
  234. camera_angle = get_global_rotation();
  235. }
  236. screen_offset = screen_offset.rotated(camera_angle);
  237. }
  238. Rect2 screen_rect(-screen_offset + ret_camera_pos, screen_size * zoom_scale);
  239. if (limit_enabled && (!position_smoothing_enabled || !limit_smoothing_enabled)) {
  240. Point2 bottom_right_corner = Point2(screen_rect.position + 2.0 * (camera_pos - screen_rect.position));
  241. // Apply horizontal limiting.
  242. if (bottom_right_corner.x - screen_rect.position.x > limit[SIDE_RIGHT] - limit[SIDE_LEFT]) {
  243. // Split the difference horizontally (center it).
  244. screen_rect.position.x = (limit[SIDE_LEFT] + limit[SIDE_RIGHT] - (bottom_right_corner.x - screen_rect.position.x)) / 2;
  245. } else if (screen_rect.position.x < limit[SIDE_LEFT]) {
  246. // Only apply left limit.
  247. screen_rect.position.x = limit[SIDE_LEFT];
  248. } else if (bottom_right_corner.x > limit[SIDE_RIGHT]) {
  249. // Only apply right limit.
  250. screen_rect.position.x = limit[SIDE_RIGHT] - (bottom_right_corner.x - screen_rect.position.x);
  251. }
  252. // Apply vertical limiting.
  253. if (bottom_right_corner.y - screen_rect.position.y > limit[SIDE_BOTTOM] - limit[SIDE_TOP]) {
  254. // Split the limit difference vertically.
  255. screen_rect.position.y = (limit[SIDE_TOP] + limit[SIDE_BOTTOM] - (bottom_right_corner.y - screen_rect.position.y)) / 2;
  256. } else if (screen_rect.position.y < limit[SIDE_TOP]) {
  257. // Only apply the top limit.
  258. screen_rect.position.y = limit[SIDE_TOP];
  259. } else if (bottom_right_corner.y > limit[SIDE_BOTTOM]) {
  260. // Only apply the bottom limit.
  261. screen_rect.position.y = limit[SIDE_BOTTOM] - (bottom_right_corner.y - screen_rect.position.y);
  262. }
  263. }
  264. if (offset != Vector2()) {
  265. screen_rect.position += offset;
  266. }
  267. Transform2D xform;
  268. xform.scale_basis(zoom_scale);
  269. if (!ignore_rotation) {
  270. xform.set_rotation(camera_angle);
  271. }
  272. xform.set_origin(screen_rect.position);
  273. camera_screen_center = xform.xform(0.5 * screen_size);
  274. return xform.affine_inverse();
  275. }
  276. void Camera2D::_ensure_update_interpolation_data() {
  277. // The "curr -> previous" update can either occur
  278. // on NOTIFICATION_INTERNAL_PHYSICS_PROCESS, OR
  279. // on NOTIFICATION_TRANSFORM_CHANGED,
  280. // if NOTIFICATION_TRANSFORM_CHANGED takes place earlier than
  281. // NOTIFICATION_INTERNAL_PHYSICS_PROCESS on a tick.
  282. // This is to ensure that the data keeps flowing, but the new data
  283. // doesn't overwrite before prev has been set.
  284. // Keep the data flowing.
  285. uint64_t tick = Engine::get_singleton()->get_physics_frames();
  286. if (_interpolation_data.last_update_physics_tick != tick) {
  287. _interpolation_data.xform_prev = _interpolation_data.xform_curr;
  288. _interpolation_data.last_update_physics_tick = tick;
  289. }
  290. }
  291. void Camera2D::_notification(int p_what) {
  292. switch (p_what) {
  293. #ifdef TOOLS_ENABLED
  294. case NOTIFICATION_READY: {
  295. if (is_part_of_edited_scene()) {
  296. ProjectSettings::get_singleton()->connect(SNAME("settings_changed"), callable_mp(this, &Camera2D::_project_settings_changed));
  297. }
  298. } break;
  299. #endif
  300. case NOTIFICATION_INTERNAL_PROCESS: {
  301. _update_scroll();
  302. } break;
  303. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  304. if (is_physics_interpolated_and_enabled()) {
  305. _ensure_update_interpolation_data();
  306. _interpolation_data.xform_curr = get_camera_transform();
  307. } else {
  308. _update_scroll();
  309. }
  310. } break;
  311. case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
  312. // Force the limits etc. to update.
  313. _interpolation_data.xform_curr = get_camera_transform();
  314. _interpolation_data.xform_prev = _interpolation_data.xform_curr;
  315. _update_process_callback();
  316. } break;
  317. case NOTIFICATION_SUSPENDED:
  318. case NOTIFICATION_PAUSED: {
  319. if (is_physics_interpolated_and_enabled()) {
  320. _update_scroll();
  321. }
  322. } break;
  323. case NOTIFICATION_TRANSFORM_CHANGED: {
  324. if ((!position_smoothing_enabled && !is_physics_interpolated_and_enabled()) || is_part_of_edited_scene()) {
  325. _update_scroll();
  326. }
  327. if (is_physics_interpolated_and_enabled()) {
  328. _ensure_update_interpolation_data();
  329. if (Engine::get_singleton()->is_in_physics_frame()) {
  330. _interpolation_data.xform_curr = get_camera_transform();
  331. }
  332. }
  333. } break;
  334. case NOTIFICATION_ENTER_TREE: {
  335. ERR_FAIL_COND(!is_inside_tree());
  336. if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
  337. viewport = custom_viewport;
  338. } else {
  339. viewport = get_viewport();
  340. }
  341. canvas = get_canvas();
  342. RID vp = viewport->get_viewport_rid();
  343. group_name = "__cameras_" + itos(vp.get_id());
  344. canvas_group_name = "__cameras_c" + itos(canvas.get_id());
  345. add_to_group(group_name);
  346. add_to_group(canvas_group_name);
  347. if (!is_part_of_edited_scene() && enabled && !viewport->get_camera_2d()) {
  348. make_current();
  349. }
  350. _update_process_callback();
  351. first = true;
  352. _update_scroll();
  353. // Note that NOTIFICATION_RESET_PHYSICS_INTERPOLATION
  354. // is automatically called before this because Camera2D is inherited
  355. // from CanvasItem. However, the camera transform is not up to date
  356. // until this point, so we do an extra manual reset.
  357. if (is_physics_interpolated_and_enabled()) {
  358. _interpolation_data.xform_curr = get_camera_transform();
  359. _interpolation_data.xform_prev = _interpolation_data.xform_curr;
  360. }
  361. } break;
  362. case NOTIFICATION_EXIT_TREE: {
  363. remove_from_group(group_name);
  364. remove_from_group(canvas_group_name);
  365. if (is_current()) {
  366. clear_current();
  367. }
  368. viewport = nullptr;
  369. just_exited_tree = true;
  370. callable_mp(this, &Camera2D::_reset_just_exited).call_deferred();
  371. } break;
  372. #ifdef TOOLS_ENABLED
  373. case NOTIFICATION_DRAW: {
  374. if (!is_inside_tree() || !is_part_of_edited_scene()) {
  375. break;
  376. }
  377. if (screen_drawing_enabled) {
  378. Color area_axis_color(1, 0.4, 1, 0.63);
  379. real_t area_axis_width = -1;
  380. if (is_current()) {
  381. area_axis_width = 3;
  382. }
  383. Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
  384. Size2 screen_size = _get_camera_screen_size();
  385. Vector2 screen_endpoints[4] = {
  386. inv_camera_transform.xform(Vector2(0, 0)),
  387. inv_camera_transform.xform(Vector2(screen_size.width, 0)),
  388. inv_camera_transform.xform(Vector2(screen_size.width, screen_size.height)),
  389. inv_camera_transform.xform(Vector2(0, screen_size.height))
  390. };
  391. Transform2D inv_transform = get_global_transform().affine_inverse(); // Undo global space.
  392. for (int i = 0; i < 4; i++) {
  393. draw_line(inv_transform.xform(screen_endpoints[i]), inv_transform.xform(screen_endpoints[(i + 1) % 4]), area_axis_color, area_axis_width);
  394. }
  395. }
  396. if (limit_enabled && limit_drawing_enabled) {
  397. real_t limit_drawing_width = -1;
  398. if (is_current()) {
  399. limit_drawing_width = 3;
  400. }
  401. Transform2D inv_transform = get_global_transform().affine_inverse();
  402. Vector2 limit_points[4] = {
  403. inv_transform.xform(Vector2(limit[SIDE_LEFT], limit[SIDE_TOP])),
  404. inv_transform.xform(Vector2(limit[SIDE_RIGHT], limit[SIDE_TOP])),
  405. inv_transform.xform(Vector2(limit[SIDE_RIGHT], limit[SIDE_BOTTOM])),
  406. inv_transform.xform(Vector2(limit[SIDE_LEFT], limit[SIDE_BOTTOM]))
  407. };
  408. for (int i = 0; i < 4; i++) {
  409. draw_line(limit_points[i], limit_points[(i + 1) % 4], Color(1, 1, 0.25, 0.63), limit_drawing_width);
  410. }
  411. }
  412. if (margin_drawing_enabled) {
  413. Color margin_drawing_color(0.25, 1, 1, 0.63);
  414. real_t margin_drawing_width = -1;
  415. if (is_current()) {
  416. margin_drawing_width = 3;
  417. }
  418. Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
  419. Size2 screen_size = _get_camera_screen_size();
  420. Vector2 margin_endpoints[4] = {
  421. 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]))),
  422. 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]))),
  423. 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]))),
  424. 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])))
  425. };
  426. Transform2D inv_transform = get_global_transform().affine_inverse(); // Undo global space.
  427. for (int i = 0; i < 4; i++) {
  428. draw_line(inv_transform.xform(margin_endpoints[i]), inv_transform.xform(margin_endpoints[(i + 1) % 4]), margin_drawing_color, margin_drawing_width);
  429. }
  430. }
  431. } break;
  432. #endif
  433. }
  434. }
  435. void Camera2D::set_offset(const Vector2 &p_offset) {
  436. if (offset == p_offset) {
  437. return;
  438. }
  439. offset = p_offset;
  440. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  441. _update_scroll();
  442. smoothed_camera_pos = old_smoothed_camera_pos;
  443. }
  444. Vector2 Camera2D::get_offset() const {
  445. return offset;
  446. }
  447. void Camera2D::set_anchor_mode(AnchorMode p_anchor_mode) {
  448. if (anchor_mode == p_anchor_mode) {
  449. return;
  450. }
  451. anchor_mode = p_anchor_mode;
  452. _update_scroll();
  453. }
  454. Camera2D::AnchorMode Camera2D::get_anchor_mode() const {
  455. return anchor_mode;
  456. }
  457. void Camera2D::set_ignore_rotation(bool p_ignore) {
  458. if (ignore_rotation == p_ignore) {
  459. return;
  460. }
  461. ignore_rotation = p_ignore;
  462. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  463. // Reset back to zero so it matches the camera rotation when ignore_rotation is enabled.
  464. if (ignore_rotation) {
  465. camera_angle = 0.0;
  466. }
  467. _update_scroll();
  468. smoothed_camera_pos = old_smoothed_camera_pos;
  469. }
  470. bool Camera2D::is_ignoring_rotation() const {
  471. return ignore_rotation;
  472. }
  473. void Camera2D::set_limit_enabled(bool p_limit_enabled) {
  474. if (limit_enabled == p_limit_enabled) {
  475. return;
  476. }
  477. limit_enabled = p_limit_enabled;
  478. _update_scroll();
  479. #ifdef TOOLS_ENABLED
  480. emit_signal("_camera_limit_enabled_updated"); // Used for Camera2DEditorPlugin.
  481. #endif
  482. }
  483. bool Camera2D::is_limit_enabled() const {
  484. return limit_enabled;
  485. }
  486. void Camera2D::set_process_callback(Camera2DProcessCallback p_mode) {
  487. if (process_callback == p_mode) {
  488. return;
  489. }
  490. process_callback = p_mode;
  491. _update_process_callback();
  492. }
  493. void Camera2D::set_enabled(bool p_enabled) {
  494. if (enabled == p_enabled) {
  495. return;
  496. }
  497. enabled = p_enabled;
  498. if (!is_inside_tree()) {
  499. return;
  500. }
  501. if (enabled && !viewport->get_camera_2d()) {
  502. make_current();
  503. } else if (!enabled && is_current()) {
  504. clear_current();
  505. }
  506. }
  507. bool Camera2D::is_enabled() const {
  508. return enabled;
  509. }
  510. Camera2D::Camera2DProcessCallback Camera2D::get_process_callback() const {
  511. return process_callback;
  512. }
  513. void Camera2D::_make_current(Object *p_which) {
  514. if (!is_inside_tree() || !viewport) {
  515. return;
  516. }
  517. if (custom_viewport && !ObjectDB::get_instance(custom_viewport_id)) {
  518. return;
  519. }
  520. queue_redraw();
  521. bool was_current = viewport->get_camera_2d() == this;
  522. bool is_current = p_which == this;
  523. if (is_current) {
  524. viewport->_camera_2d_set(this);
  525. } else if (was_current) {
  526. viewport->_camera_2d_set(nullptr);
  527. }
  528. if (is_current != was_current) {
  529. _update_process_callback();
  530. }
  531. }
  532. void Camera2D::_set_limit_rect(const Rect2 &p_limit_rect) {
  533. Point2 limit_rect_end = p_limit_rect.get_end();
  534. set_limit(SIDE_LEFT, p_limit_rect.position.x);
  535. set_limit(SIDE_TOP, p_limit_rect.position.y);
  536. set_limit(SIDE_RIGHT, limit_rect_end.x);
  537. set_limit(SIDE_BOTTOM, limit_rect_end.y);
  538. }
  539. Rect2 Camera2D::get_limit_rect() const {
  540. return Rect2(limit[SIDE_LEFT], limit[SIDE_TOP], limit[SIDE_RIGHT] - limit[SIDE_LEFT], limit[SIDE_BOTTOM] - limit[SIDE_TOP]);
  541. }
  542. void Camera2D::make_current() {
  543. ERR_FAIL_COND(!enabled || !is_inside_tree());
  544. get_tree()->call_group(group_name, "_make_current", this);
  545. if (just_exited_tree) {
  546. // If camera exited the scene tree in the same frame, group call will skip it, so this needs to be called manually.
  547. _make_current(this);
  548. }
  549. _update_scroll();
  550. _update_process_callback();
  551. }
  552. void Camera2D::clear_current() {
  553. ERR_FAIL_COND(!is_current());
  554. if (!viewport || !viewport->is_inside_tree()) {
  555. return;
  556. }
  557. if (!custom_viewport || ObjectDB::get_instance(custom_viewport_id)) {
  558. viewport->assign_next_enabled_camera_2d(group_name);
  559. }
  560. _update_process_callback();
  561. }
  562. bool Camera2D::is_current() const {
  563. if (!viewport) {
  564. return false;
  565. }
  566. if (!custom_viewport || ObjectDB::get_instance(custom_viewport_id)) {
  567. return viewport->get_camera_2d() == this;
  568. }
  569. return false;
  570. }
  571. void Camera2D::set_limit(Side p_side, int p_limit) {
  572. ERR_FAIL_INDEX((int)p_side, 4);
  573. if (limit[p_side] == p_limit) {
  574. return;
  575. }
  576. limit[p_side] = p_limit;
  577. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  578. _update_scroll();
  579. smoothed_camera_pos = old_smoothed_camera_pos;
  580. }
  581. int Camera2D::get_limit(Side p_side) const {
  582. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  583. return limit[p_side];
  584. }
  585. void Camera2D::set_limit_smoothing_enabled(bool p_enabled) {
  586. if (limit_smoothing_enabled == p_enabled) {
  587. return;
  588. }
  589. limit_smoothing_enabled = p_enabled;
  590. _update_scroll();
  591. }
  592. bool Camera2D::is_limit_smoothing_enabled() const {
  593. return limit_smoothing_enabled;
  594. }
  595. void Camera2D::set_drag_margin(Side p_side, real_t p_drag_margin) {
  596. ERR_FAIL_INDEX((int)p_side, 4);
  597. if (drag_margin[p_side] == p_drag_margin) {
  598. return;
  599. }
  600. drag_margin[p_side] = p_drag_margin;
  601. queue_redraw();
  602. }
  603. real_t Camera2D::get_drag_margin(Side p_side) const {
  604. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  605. return drag_margin[p_side];
  606. }
  607. Vector2 Camera2D::get_camera_position() const {
  608. return camera_pos;
  609. }
  610. void Camera2D::force_update_scroll() {
  611. _update_scroll();
  612. }
  613. void Camera2D::reset_smoothing() {
  614. _update_scroll();
  615. smoothed_camera_pos = camera_pos;
  616. }
  617. void Camera2D::align() {
  618. ERR_FAIL_COND(custom_viewport && !ObjectDB::get_instance(custom_viewport_id));
  619. Size2 screen_size = _get_camera_screen_size();
  620. Point2 current_camera_pos = get_global_position();
  621. if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
  622. if (drag_horizontal_offset < 0) {
  623. camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_RIGHT] * drag_horizontal_offset;
  624. } else {
  625. camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[SIDE_LEFT] * drag_horizontal_offset;
  626. }
  627. if (drag_vertical_offset < 0) {
  628. camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_TOP] * drag_vertical_offset;
  629. } else {
  630. camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[SIDE_BOTTOM] * drag_vertical_offset;
  631. }
  632. } else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
  633. camera_pos = current_camera_pos;
  634. }
  635. _update_scroll();
  636. }
  637. void Camera2D::set_position_smoothing_speed(real_t p_speed) {
  638. if (position_smoothing_speed == p_speed) {
  639. return;
  640. }
  641. position_smoothing_speed = MAX(0, p_speed);
  642. _update_process_callback();
  643. }
  644. real_t Camera2D::get_position_smoothing_speed() const {
  645. return position_smoothing_speed;
  646. }
  647. void Camera2D::set_rotation_smoothing_speed(real_t p_speed) {
  648. if (rotation_smoothing_speed == p_speed) {
  649. return;
  650. }
  651. rotation_smoothing_speed = MAX(0, p_speed);
  652. _update_process_callback();
  653. }
  654. real_t Camera2D::get_rotation_smoothing_speed() const {
  655. return rotation_smoothing_speed;
  656. }
  657. void Camera2D::set_rotation_smoothing_enabled(bool p_enabled) {
  658. if (rotation_smoothing_enabled == p_enabled) {
  659. return;
  660. }
  661. rotation_smoothing_enabled = p_enabled;
  662. }
  663. bool Camera2D::is_rotation_smoothing_enabled() const {
  664. return rotation_smoothing_enabled;
  665. }
  666. Point2 Camera2D::get_camera_screen_center() const {
  667. return camera_screen_center;
  668. }
  669. Size2 Camera2D::_get_camera_screen_size() const {
  670. if (is_part_of_edited_scene()) {
  671. return Size2(GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_width"), GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_height"));
  672. }
  673. return get_viewport_rect().size;
  674. }
  675. void Camera2D::set_drag_horizontal_enabled(bool p_enabled) {
  676. drag_horizontal_enabled = p_enabled;
  677. }
  678. bool Camera2D::is_drag_horizontal_enabled() const {
  679. return drag_horizontal_enabled;
  680. }
  681. void Camera2D::set_drag_vertical_enabled(bool p_enabled) {
  682. drag_vertical_enabled = p_enabled;
  683. }
  684. bool Camera2D::is_drag_vertical_enabled() const {
  685. return drag_vertical_enabled;
  686. }
  687. void Camera2D::set_drag_vertical_offset(real_t p_offset) {
  688. if (drag_vertical_offset == p_offset) {
  689. return;
  690. }
  691. drag_vertical_offset = p_offset;
  692. drag_vertical_offset_changed = true;
  693. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  694. _update_scroll();
  695. smoothed_camera_pos = old_smoothed_camera_pos;
  696. }
  697. real_t Camera2D::get_drag_vertical_offset() const {
  698. return drag_vertical_offset;
  699. }
  700. void Camera2D::set_drag_horizontal_offset(real_t p_offset) {
  701. if (drag_horizontal_offset == p_offset) {
  702. return;
  703. }
  704. drag_horizontal_offset = p_offset;
  705. drag_horizontal_offset_changed = true;
  706. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  707. _update_scroll();
  708. smoothed_camera_pos = old_smoothed_camera_pos;
  709. }
  710. real_t Camera2D::get_drag_horizontal_offset() const {
  711. return drag_horizontal_offset;
  712. }
  713. void Camera2D::set_position_smoothing_enabled(bool p_enabled) {
  714. if (position_smoothing_enabled == p_enabled) {
  715. return;
  716. }
  717. position_smoothing_enabled = p_enabled;
  718. }
  719. bool Camera2D::is_position_smoothing_enabled() const {
  720. return position_smoothing_enabled;
  721. }
  722. void Camera2D::set_custom_viewport(Node *p_viewport) {
  723. ERR_FAIL_NULL(p_viewport);
  724. if (custom_viewport == p_viewport) {
  725. return;
  726. }
  727. if (is_inside_tree()) {
  728. remove_from_group(group_name);
  729. remove_from_group(canvas_group_name);
  730. }
  731. custom_viewport = Object::cast_to<Viewport>(p_viewport);
  732. if (custom_viewport) {
  733. custom_viewport_id = custom_viewport->get_instance_id();
  734. } else {
  735. custom_viewport_id = ObjectID();
  736. }
  737. if (is_inside_tree()) {
  738. if (custom_viewport) {
  739. viewport = custom_viewport;
  740. } else {
  741. viewport = get_viewport();
  742. }
  743. RID vp = viewport->get_viewport_rid();
  744. group_name = "__cameras_" + itos(vp.get_id());
  745. canvas_group_name = "__cameras_c" + itos(canvas.get_id());
  746. add_to_group(group_name);
  747. add_to_group(canvas_group_name);
  748. }
  749. }
  750. Node *Camera2D::get_custom_viewport() const {
  751. return custom_viewport;
  752. }
  753. void Camera2D::set_screen_drawing_enabled(bool p_enabled) {
  754. screen_drawing_enabled = p_enabled;
  755. #ifdef TOOLS_ENABLED
  756. queue_redraw();
  757. #endif
  758. }
  759. bool Camera2D::is_screen_drawing_enabled() const {
  760. return screen_drawing_enabled;
  761. }
  762. void Camera2D::set_limit_drawing_enabled(bool p_enabled) {
  763. limit_drawing_enabled = p_enabled;
  764. #ifdef TOOLS_ENABLED
  765. queue_redraw();
  766. #endif
  767. }
  768. bool Camera2D::is_limit_drawing_enabled() const {
  769. return limit_drawing_enabled;
  770. }
  771. void Camera2D::set_margin_drawing_enabled(bool p_enabled) {
  772. margin_drawing_enabled = p_enabled;
  773. #ifdef TOOLS_ENABLED
  774. queue_redraw();
  775. #endif
  776. }
  777. bool Camera2D::is_margin_drawing_enabled() const {
  778. return margin_drawing_enabled;
  779. }
  780. void Camera2D::_bind_methods() {
  781. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Camera2D::set_offset);
  782. ClassDB::bind_method(D_METHOD("get_offset"), &Camera2D::get_offset);
  783. ClassDB::bind_method(D_METHOD("set_anchor_mode", "anchor_mode"), &Camera2D::set_anchor_mode);
  784. ClassDB::bind_method(D_METHOD("get_anchor_mode"), &Camera2D::get_anchor_mode);
  785. ClassDB::bind_method(D_METHOD("set_ignore_rotation", "ignore"), &Camera2D::set_ignore_rotation);
  786. ClassDB::bind_method(D_METHOD("is_ignoring_rotation"), &Camera2D::is_ignoring_rotation);
  787. ClassDB::bind_method(D_METHOD("_update_scroll"), &Camera2D::_update_scroll);
  788. ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &Camera2D::set_process_callback);
  789. ClassDB::bind_method(D_METHOD("get_process_callback"), &Camera2D::get_process_callback);
  790. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &Camera2D::set_enabled);
  791. ClassDB::bind_method(D_METHOD("is_enabled"), &Camera2D::is_enabled);
  792. ClassDB::bind_method(D_METHOD("make_current"), &Camera2D::make_current);
  793. ClassDB::bind_method(D_METHOD("is_current"), &Camera2D::is_current);
  794. ClassDB::bind_method(D_METHOD("_make_current"), &Camera2D::_make_current);
  795. ClassDB::bind_method(D_METHOD("set_limit_enabled", "limit_enabled"), &Camera2D::set_limit_enabled);
  796. ClassDB::bind_method(D_METHOD("is_limit_enabled"), &Camera2D::is_limit_enabled);
  797. ClassDB::bind_method(D_METHOD("set_limit", "margin", "limit"), &Camera2D::set_limit);
  798. ClassDB::bind_method(D_METHOD("get_limit", "margin"), &Camera2D::get_limit);
  799. ClassDB::bind_method(D_METHOD("set_limit_smoothing_enabled", "limit_smoothing_enabled"), &Camera2D::set_limit_smoothing_enabled);
  800. ClassDB::bind_method(D_METHOD("is_limit_smoothing_enabled"), &Camera2D::is_limit_smoothing_enabled);
  801. ClassDB::bind_method(D_METHOD("set_drag_vertical_enabled", "enabled"), &Camera2D::set_drag_vertical_enabled);
  802. ClassDB::bind_method(D_METHOD("is_drag_vertical_enabled"), &Camera2D::is_drag_vertical_enabled);
  803. ClassDB::bind_method(D_METHOD("set_drag_horizontal_enabled", "enabled"), &Camera2D::set_drag_horizontal_enabled);
  804. ClassDB::bind_method(D_METHOD("is_drag_horizontal_enabled"), &Camera2D::is_drag_horizontal_enabled);
  805. ClassDB::bind_method(D_METHOD("set_drag_vertical_offset", "offset"), &Camera2D::set_drag_vertical_offset);
  806. ClassDB::bind_method(D_METHOD("get_drag_vertical_offset"), &Camera2D::get_drag_vertical_offset);
  807. ClassDB::bind_method(D_METHOD("set_drag_horizontal_offset", "offset"), &Camera2D::set_drag_horizontal_offset);
  808. ClassDB::bind_method(D_METHOD("get_drag_horizontal_offset"), &Camera2D::get_drag_horizontal_offset);
  809. ClassDB::bind_method(D_METHOD("set_drag_margin", "margin", "drag_margin"), &Camera2D::set_drag_margin);
  810. ClassDB::bind_method(D_METHOD("get_drag_margin", "margin"), &Camera2D::get_drag_margin);
  811. ClassDB::bind_method(D_METHOD("get_target_position"), &Camera2D::get_camera_position);
  812. ClassDB::bind_method(D_METHOD("get_screen_center_position"), &Camera2D::get_camera_screen_center);
  813. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &Camera2D::set_zoom);
  814. ClassDB::bind_method(D_METHOD("get_zoom"), &Camera2D::get_zoom);
  815. ClassDB::bind_method(D_METHOD("set_custom_viewport", "viewport"), &Camera2D::set_custom_viewport);
  816. ClassDB::bind_method(D_METHOD("get_custom_viewport"), &Camera2D::get_custom_viewport);
  817. ClassDB::bind_method(D_METHOD("set_position_smoothing_speed", "position_smoothing_speed"), &Camera2D::set_position_smoothing_speed);
  818. ClassDB::bind_method(D_METHOD("get_position_smoothing_speed"), &Camera2D::get_position_smoothing_speed);
  819. ClassDB::bind_method(D_METHOD("set_position_smoothing_enabled", "position_smoothing_speed"), &Camera2D::set_position_smoothing_enabled);
  820. ClassDB::bind_method(D_METHOD("is_position_smoothing_enabled"), &Camera2D::is_position_smoothing_enabled);
  821. ClassDB::bind_method(D_METHOD("set_rotation_smoothing_enabled", "enabled"), &Camera2D::set_rotation_smoothing_enabled);
  822. ClassDB::bind_method(D_METHOD("is_rotation_smoothing_enabled"), &Camera2D::is_rotation_smoothing_enabled);
  823. ClassDB::bind_method(D_METHOD("set_rotation_smoothing_speed", "speed"), &Camera2D::set_rotation_smoothing_speed);
  824. ClassDB::bind_method(D_METHOD("get_rotation_smoothing_speed"), &Camera2D::get_rotation_smoothing_speed);
  825. ClassDB::bind_method(D_METHOD("force_update_scroll"), &Camera2D::force_update_scroll);
  826. ClassDB::bind_method(D_METHOD("reset_smoothing"), &Camera2D::reset_smoothing);
  827. ClassDB::bind_method(D_METHOD("align"), &Camera2D::align);
  828. ClassDB::bind_method(D_METHOD("set_screen_drawing_enabled", "screen_drawing_enabled"), &Camera2D::set_screen_drawing_enabled);
  829. ClassDB::bind_method(D_METHOD("is_screen_drawing_enabled"), &Camera2D::is_screen_drawing_enabled);
  830. ClassDB::bind_method(D_METHOD("set_limit_drawing_enabled", "limit_drawing_enabled"), &Camera2D::set_limit_drawing_enabled);
  831. ClassDB::bind_method(D_METHOD("is_limit_drawing_enabled"), &Camera2D::is_limit_drawing_enabled);
  832. ClassDB::bind_method(D_METHOD("set_margin_drawing_enabled", "margin_drawing_enabled"), &Camera2D::set_margin_drawing_enabled);
  833. ClassDB::bind_method(D_METHOD("is_margin_drawing_enabled"), &Camera2D::is_margin_drawing_enabled);
  834. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset");
  835. ADD_PROPERTY(PropertyInfo(Variant::INT, "anchor_mode", PROPERTY_HINT_ENUM, "Fixed Top Left,Drag Center"), "set_anchor_mode", "get_anchor_mode");
  836. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_rotation"), "set_ignore_rotation", "is_ignoring_rotation");
  837. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  838. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "zoom", PROPERTY_HINT_LINK), "set_zoom", "get_zoom");
  839. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport", PROPERTY_USAGE_NONE), "set_custom_viewport", "get_custom_viewport");
  840. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_callback", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_process_callback", "get_process_callback");
  841. ADD_GROUP("Limit", "limit_");
  842. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "limit_enabled", PROPERTY_HINT_GROUP_ENABLE, "feature"), "set_limit_enabled", "is_limit_enabled");
  843. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_left", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_LEFT);
  844. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_top", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_TOP);
  845. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_right", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_RIGHT);
  846. ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_bottom", PROPERTY_HINT_NONE, "suffix:px"), "set_limit", "get_limit", SIDE_BOTTOM);
  847. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "limit_smoothed"), "set_limit_smoothing_enabled", "is_limit_smoothing_enabled");
  848. ADD_GROUP("Position Smoothing", "position_smoothing_");
  849. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "position_smoothing_enabled", PROPERTY_HINT_GROUP_ENABLE, "feature"), "set_position_smoothing_enabled", "is_position_smoothing_enabled");
  850. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "position_smoothing_speed", PROPERTY_HINT_NONE, "suffix:px/s"), "set_position_smoothing_speed", "get_position_smoothing_speed");
  851. ADD_GROUP("Rotation Smoothing", "rotation_smoothing_");
  852. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotation_smoothing_enabled", PROPERTY_HINT_GROUP_ENABLE, "feature"), "set_rotation_smoothing_enabled", "is_rotation_smoothing_enabled");
  853. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation_smoothing_speed"), "set_rotation_smoothing_speed", "get_rotation_smoothing_speed");
  854. ADD_GROUP("Drag", "drag_");
  855. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_horizontal_enabled"), "set_drag_horizontal_enabled", "is_drag_horizontal_enabled");
  856. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_vertical_enabled"), "set_drag_vertical_enabled", "is_drag_vertical_enabled");
  857. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "drag_horizontal_offset", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_drag_horizontal_offset", "get_drag_horizontal_offset");
  858. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "drag_vertical_offset", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_drag_vertical_offset", "get_drag_vertical_offset");
  859. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_left_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_LEFT);
  860. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_top_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_TOP);
  861. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_right_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_RIGHT);
  862. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_bottom_margin", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", SIDE_BOTTOM);
  863. ADD_GROUP("Editor", "editor_");
  864. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_screen"), "set_screen_drawing_enabled", "is_screen_drawing_enabled");
  865. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_limits"), "set_limit_drawing_enabled", "is_limit_drawing_enabled");
  866. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_drag_margin"), "set_margin_drawing_enabled", "is_margin_drawing_enabled");
  867. BIND_ENUM_CONSTANT(ANCHOR_MODE_FIXED_TOP_LEFT);
  868. BIND_ENUM_CONSTANT(ANCHOR_MODE_DRAG_CENTER);
  869. BIND_ENUM_CONSTANT(CAMERA2D_PROCESS_PHYSICS);
  870. BIND_ENUM_CONSTANT(CAMERA2D_PROCESS_IDLE);
  871. }
  872. Camera2D::Camera2D() {
  873. set_notify_transform(true);
  874. set_hide_clip_children(true);
  875. #ifdef TOOLS_ENABLED
  876. add_user_signal(MethodInfo("_camera_limit_enabled_updated")); // Camera2DEditorPlugin listens to this.
  877. #endif
  878. }