camera_2d.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /*************************************************************************/
  2. /* camera_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "camera_2d.h"
  30. #include "scene/scene_string_names.h"
  31. #include "servers/visual_server.h"
  32. #include "core/math/math_funcs.h"
  33. #include <editor/editor_node.h>
  34. void Camera2D::_update_scroll() {
  35. if (!is_inside_tree())
  36. return;
  37. if (get_tree()->is_editor_hint()) {
  38. update(); //will just be drawn
  39. return;
  40. }
  41. if (current) {
  42. ERR_FAIL_COND( custom_viewport && !ObjectDB::get_instance(custom_viewport_id) );
  43. Transform2D xform = get_camera_transform();
  44. if (viewport) {
  45. viewport->set_canvas_transform( xform );
  46. }
  47. get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME,group_name,"_camera_moved",xform);
  48. };
  49. }
  50. void Camera2D::set_zoom(const Vector2 &p_zoom) {
  51. zoom = p_zoom;
  52. Point2 old_smoothed_camera_pos = smoothed_camera_pos;
  53. _update_scroll();
  54. smoothed_camera_pos = old_smoothed_camera_pos;
  55. };
  56. Vector2 Camera2D::get_zoom() const {
  57. return zoom;
  58. };
  59. Transform2D Camera2D::get_camera_transform() {
  60. if (!get_tree())
  61. return Transform2D();
  62. ERR_FAIL_COND_V( custom_viewport && !ObjectDB::get_instance(custom_viewport_id), Transform2D() );
  63. Size2 screen_size = viewport->get_visible_rect().size;
  64. Point2 new_camera_pos = get_global_transform().get_origin();
  65. Point2 ret_camera_pos;
  66. if (!first) {
  67. if (anchor_mode==ANCHOR_MODE_DRAG_CENTER) {
  68. if (h_drag_enabled && !get_tree()->is_editor_hint()) {
  69. camera_pos.x = MIN( camera_pos.x, (new_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_RIGHT]));
  70. camera_pos.x = MAX( camera_pos.x, (new_camera_pos.x - screen_size.x * 0.5 * drag_margin[MARGIN_LEFT]));
  71. } else {
  72. if (h_ofs<0) {
  73. camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_RIGHT] * h_ofs;
  74. } else {
  75. camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_LEFT] * h_ofs;
  76. }
  77. }
  78. if (v_drag_enabled && !get_tree()->is_editor_hint()) {
  79. camera_pos.y = MIN( camera_pos.y, (new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_BOTTOM]));
  80. camera_pos.y = MAX( camera_pos.y, (new_camera_pos.y - screen_size.y * 0.5 * drag_margin[MARGIN_TOP]));
  81. } else {
  82. if (v_ofs<0) {
  83. camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_TOP] * v_ofs;
  84. } else {
  85. camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_BOTTOM] * v_ofs;
  86. }
  87. }
  88. } else if (anchor_mode==ANCHOR_MODE_FIXED_TOP_LEFT){
  89. camera_pos=new_camera_pos;
  90. }
  91. Point2 screen_offset = (anchor_mode==ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom) : Point2());
  92. Rect2 screen_rect(-screen_offset+camera_pos,screen_size*zoom);
  93. if (offset!=Vector2())
  94. screen_rect.pos+=offset;
  95. if (limit_smoothing_enabled) {
  96. if (screen_rect.pos.x < limit[MARGIN_LEFT])
  97. camera_pos.x -= screen_rect.pos.x - limit[MARGIN_LEFT];
  98. if (screen_rect.pos.x + screen_rect.size.x > limit[MARGIN_RIGHT])
  99. camera_pos.x -= screen_rect.pos.x + screen_rect.size.x - limit[MARGIN_RIGHT];
  100. if (screen_rect.pos.y + screen_rect.size.y > limit[MARGIN_BOTTOM])
  101. camera_pos.y -= screen_rect.pos.y + screen_rect.size.y - limit[MARGIN_BOTTOM];
  102. if (screen_rect.pos.y < limit[MARGIN_TOP])
  103. camera_pos.y -= screen_rect.pos.y - limit[MARGIN_TOP];
  104. }
  105. if (smoothing_enabled && !get_tree()->is_editor_hint()) {
  106. float c = smoothing*get_fixed_process_delta_time();
  107. smoothed_camera_pos = ((camera_pos-smoothed_camera_pos)*c)+smoothed_camera_pos;
  108. ret_camera_pos=smoothed_camera_pos;
  109. //camera_pos=camera_pos*(1.0-smoothing)+new_camera_pos*smoothing;
  110. } else {
  111. ret_camera_pos=smoothed_camera_pos=camera_pos;
  112. }
  113. } else {
  114. ret_camera_pos=smoothed_camera_pos=camera_pos=new_camera_pos;
  115. first=false;
  116. }
  117. Point2 screen_offset = (anchor_mode==ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom) : Point2());
  118. float angle = get_global_transform().get_rotation();
  119. if(rotating){
  120. screen_offset = screen_offset.rotated(angle);
  121. }
  122. Rect2 screen_rect(-screen_offset+ret_camera_pos,screen_size*zoom);
  123. if (screen_rect.pos.x < limit[MARGIN_LEFT])
  124. screen_rect.pos.x = limit[MARGIN_LEFT];
  125. if (screen_rect.pos.x + screen_rect.size.x > limit[MARGIN_RIGHT])
  126. screen_rect.pos.x = limit[MARGIN_RIGHT] - screen_rect.size.x;
  127. if (screen_rect.pos.y + screen_rect.size.y > limit[MARGIN_BOTTOM])
  128. screen_rect.pos.y = limit[MARGIN_BOTTOM] - screen_rect.size.y;
  129. if (screen_rect.pos.y < limit[MARGIN_TOP])
  130. screen_rect.pos.y =limit[MARGIN_TOP];
  131. if (offset!=Vector2()) {
  132. screen_rect.pos+=offset;
  133. if (screen_rect.pos.x + screen_rect.size.x > limit[MARGIN_RIGHT])
  134. screen_rect.pos.x = limit[MARGIN_RIGHT] - screen_rect.size.x;
  135. if (screen_rect.pos.y + screen_rect.size.y > limit[MARGIN_BOTTOM])
  136. screen_rect.pos.y = limit[MARGIN_BOTTOM] - screen_rect.size.y;
  137. if (screen_rect.pos.x < limit[MARGIN_LEFT])
  138. screen_rect.pos.x=limit[MARGIN_LEFT];
  139. if (screen_rect.pos.y < limit[MARGIN_TOP])
  140. screen_rect.pos.y =limit[MARGIN_TOP];
  141. }
  142. camera_screen_center=screen_rect.pos+screen_rect.size*0.5;
  143. Transform2D xform;
  144. if(rotating){
  145. xform.set_rotation(angle);
  146. }
  147. xform.scale_basis(zoom);
  148. xform.set_origin(screen_rect.pos/*.floor()*/);
  149. /*
  150. if (0) {
  151. xform = get_global_transform() * xform;
  152. } else {
  153. xform.elements[2]+=get_global_transform().get_origin();
  154. }
  155. */
  156. return (xform).affine_inverse();
  157. }
  158. void Camera2D::_notification(int p_what) {
  159. switch(p_what) {
  160. case NOTIFICATION_FIXED_PROCESS: {
  161. _update_scroll();
  162. } break;
  163. case NOTIFICATION_TRANSFORM_CHANGED: {
  164. if (!is_fixed_processing())
  165. _update_scroll();
  166. } break;
  167. case NOTIFICATION_ENTER_TREE: {
  168. if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
  169. viewport=custom_viewport;
  170. } else {
  171. viewport=get_viewport();
  172. }
  173. canvas = get_canvas();
  174. RID vp = viewport->get_viewport_rid();
  175. group_name = "__cameras_"+itos(vp.get_id());
  176. canvas_group_name ="__cameras_c"+itos(canvas.get_id());
  177. add_to_group(group_name);
  178. add_to_group(canvas_group_name);
  179. if(get_tree()->is_editor_hint()) {
  180. set_fixed_process(false);
  181. }
  182. _update_scroll();
  183. first=true;
  184. } break;
  185. case NOTIFICATION_EXIT_TREE: {
  186. if (is_current()) {
  187. if (viewport && !(custom_viewport && !ObjectDB::get_instance(custom_viewport_id))) {
  188. viewport->set_canvas_transform( Transform2D() );
  189. }
  190. }
  191. remove_from_group(group_name);
  192. remove_from_group(canvas_group_name);
  193. viewport=NULL;
  194. } break;
  195. case NOTIFICATION_DRAW: {
  196. if (!is_inside_tree() || !get_tree()->is_editor_hint())
  197. break;
  198. Color area_axis_color(0.5, 0.42, 0.87, 0.63);
  199. float area_axis_width = 1;
  200. if(current)
  201. area_axis_width = 3;
  202. Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
  203. Size2 screen_size = get_viewport_rect().size;
  204. Vector2 screen_endpoints[4]= {
  205. inv_camera_transform.xform(Vector2(0, 0)),
  206. inv_camera_transform.xform(Vector2(screen_size.width,0)),
  207. inv_camera_transform.xform(Vector2(screen_size.width, screen_size.height)),
  208. inv_camera_transform.xform(Vector2(0, screen_size.height))
  209. };
  210. Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space
  211. for(int i=0;i<4;i++) {
  212. draw_line(inv_transform.xform(screen_endpoints[i]), inv_transform.xform(screen_endpoints[(i+1)%4]), area_axis_color, area_axis_width);
  213. }
  214. } break;
  215. }
  216. }
  217. void Camera2D::set_offset(const Vector2& p_offset) {
  218. offset=p_offset;
  219. _update_scroll();
  220. }
  221. Vector2 Camera2D::get_offset() const{
  222. return offset;
  223. }
  224. void Camera2D::set_anchor_mode(AnchorMode p_anchor_mode){
  225. anchor_mode=p_anchor_mode;
  226. _update_scroll();
  227. }
  228. Camera2D::AnchorMode Camera2D::get_anchor_mode() const {
  229. return anchor_mode;
  230. }
  231. void Camera2D::set_rotating(bool p_rotating){
  232. rotating=p_rotating;
  233. _update_scroll();
  234. }
  235. bool Camera2D::is_rotating() const {
  236. return rotating;
  237. }
  238. void Camera2D::_make_current(Object *p_which) {
  239. if (p_which==this) {
  240. current=true;
  241. _update_scroll();
  242. } else {
  243. current=false;
  244. }
  245. }
  246. void Camera2D::_set_current(bool p_current) {
  247. if (p_current)
  248. make_current();
  249. current=p_current;
  250. }
  251. bool Camera2D::is_current() const {
  252. return current;
  253. }
  254. void Camera2D::make_current() {
  255. if (!is_inside_tree()) {
  256. current=true;
  257. } else {
  258. get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME,group_name,"_make_current",this);
  259. }
  260. }
  261. void Camera2D::clear_current() {
  262. current=false;
  263. if (is_inside_tree()) {
  264. get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME,group_name,"_make_current",(Object*)(NULL));
  265. }
  266. }
  267. void Camera2D::set_limit(Margin p_margin,int p_limit) {
  268. ERR_FAIL_INDEX(p_margin,4);
  269. limit[p_margin]=p_limit;
  270. }
  271. int Camera2D::get_limit(Margin p_margin) const{
  272. ERR_FAIL_INDEX_V(p_margin,4,0);
  273. return limit[p_margin];
  274. }
  275. void Camera2D::set_limit_smoothing_enabled(bool enable) {
  276. limit_smoothing_enabled = enable;
  277. _update_scroll();
  278. }
  279. bool Camera2D::is_limit_smoothing_enabled() const{
  280. return limit_smoothing_enabled;
  281. }
  282. void Camera2D::set_drag_margin(Margin p_margin,float p_drag_margin) {
  283. ERR_FAIL_INDEX(p_margin,4);
  284. drag_margin[p_margin]=p_drag_margin;
  285. }
  286. float Camera2D::get_drag_margin(Margin p_margin) const{
  287. ERR_FAIL_INDEX_V(p_margin,4,0);
  288. return drag_margin[p_margin];
  289. }
  290. Vector2 Camera2D::get_camera_pos() const {
  291. return camera_pos;
  292. }
  293. void Camera2D::force_update_scroll() {
  294. _update_scroll();
  295. }
  296. void Camera2D::reset_smoothing() {
  297. smoothed_camera_pos = camera_pos;
  298. _update_scroll();
  299. }
  300. void Camera2D::align() {
  301. ERR_FAIL_COND( custom_viewport && !ObjectDB::get_instance(custom_viewport_id) );
  302. Size2 screen_size = viewport->get_visible_rect().size;
  303. Point2 current_camera_pos = get_global_transform().get_origin();
  304. if (anchor_mode==ANCHOR_MODE_DRAG_CENTER) {
  305. if (h_ofs<0) {
  306. camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_RIGHT] * h_ofs;
  307. } else {
  308. camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_LEFT] * h_ofs;
  309. }
  310. if (v_ofs<0) {
  311. camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_TOP] * v_ofs;
  312. } else {
  313. camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_BOTTOM] * v_ofs;
  314. }
  315. } else if (anchor_mode==ANCHOR_MODE_FIXED_TOP_LEFT){
  316. camera_pos=current_camera_pos;
  317. }
  318. _update_scroll();
  319. }
  320. void Camera2D::set_follow_smoothing(float p_speed) {
  321. smoothing=p_speed;
  322. if (smoothing>0 && !(is_inside_tree() && get_tree()->is_editor_hint()))
  323. set_fixed_process(true);
  324. else
  325. set_fixed_process(false);
  326. }
  327. float Camera2D::get_follow_smoothing() const{
  328. return smoothing;
  329. }
  330. Point2 Camera2D::get_camera_screen_center() const {
  331. return camera_screen_center;
  332. }
  333. void Camera2D::set_h_drag_enabled(bool p_enabled) {
  334. h_drag_enabled=p_enabled;
  335. }
  336. bool Camera2D::is_h_drag_enabled() const{
  337. return h_drag_enabled;
  338. }
  339. void Camera2D::set_v_drag_enabled(bool p_enabled){
  340. v_drag_enabled=p_enabled;
  341. }
  342. bool Camera2D::is_v_drag_enabled() const{
  343. return v_drag_enabled;
  344. }
  345. void Camera2D::set_v_offset(float p_offset) {
  346. v_ofs=p_offset;
  347. }
  348. float Camera2D::get_v_offset() const{
  349. return v_ofs;
  350. }
  351. void Camera2D::set_h_offset(float p_offset){
  352. h_ofs=p_offset;
  353. }
  354. float Camera2D::get_h_offset() const{
  355. return h_ofs;
  356. }
  357. void Camera2D::_set_old_smoothing(float p_val) {
  358. //compatibility
  359. if (p_val>0) {
  360. smoothing_enabled=true;
  361. set_follow_smoothing(p_val);
  362. }
  363. }
  364. void Camera2D::set_enable_follow_smoothing(bool p_enabled) {
  365. smoothing_enabled=p_enabled;
  366. }
  367. bool Camera2D::is_follow_smoothing_enabled() const {
  368. return smoothing_enabled;
  369. }
  370. void Camera2D::set_custom_viewport(Node *p_viewport) {
  371. ERR_FAIL_NULL(p_viewport);
  372. if (is_inside_tree()) {
  373. remove_from_group(group_name);
  374. remove_from_group(canvas_group_name);
  375. }
  376. custom_viewport=p_viewport->cast_to<Viewport>();
  377. if (custom_viewport) {
  378. custom_viewport_id=custom_viewport->get_instance_ID();
  379. } else {
  380. custom_viewport_id=0;
  381. }
  382. if (is_inside_tree()) {
  383. if (custom_viewport)
  384. viewport=custom_viewport;
  385. else
  386. viewport=get_viewport();
  387. RID vp = viewport->get_viewport_rid();
  388. group_name = "__cameras_"+itos(vp.get_id());
  389. canvas_group_name ="__cameras_c"+itos(canvas.get_id());
  390. add_to_group(group_name);
  391. add_to_group(canvas_group_name);
  392. }
  393. }
  394. Node* Camera2D::get_custom_viewport() const {
  395. return custom_viewport;
  396. }
  397. void Camera2D::_bind_methods() {
  398. ClassDB::bind_method(_MD("set_offset","offset"),&Camera2D::set_offset);
  399. ClassDB::bind_method(_MD("get_offset"),&Camera2D::get_offset);
  400. ClassDB::bind_method(_MD("set_anchor_mode","anchor_mode"),&Camera2D::set_anchor_mode);
  401. ClassDB::bind_method(_MD("get_anchor_mode"),&Camera2D::get_anchor_mode);
  402. ClassDB::bind_method(_MD("set_rotating","rotating"),&Camera2D::set_rotating);
  403. ClassDB::bind_method(_MD("is_rotating"),&Camera2D::is_rotating);
  404. ClassDB::bind_method(_MD("make_current"),&Camera2D::make_current);
  405. ClassDB::bind_method(_MD("clear_current"),&Camera2D::clear_current);
  406. ClassDB::bind_method(_MD("_make_current"),&Camera2D::_make_current);
  407. ClassDB::bind_method(_MD("_update_scroll"),&Camera2D::_update_scroll);
  408. ClassDB::bind_method(_MD("_set_current","current"),&Camera2D::_set_current);
  409. ClassDB::bind_method(_MD("is_current"),&Camera2D::is_current);
  410. ClassDB::bind_method(_MD("set_limit","margin","limit"),&Camera2D::set_limit);
  411. ClassDB::bind_method(_MD("get_limit","margin"),&Camera2D::get_limit);
  412. ClassDB::bind_method(_MD("set_limit_smoothing_enabled","limit_smoothing_enabled"),&Camera2D::set_limit_smoothing_enabled);
  413. ClassDB::bind_method(_MD("is_limit_smoothing_enabled"),&Camera2D::is_limit_smoothing_enabled);
  414. ClassDB::bind_method(_MD("set_v_drag_enabled","enabled"),&Camera2D::set_v_drag_enabled);
  415. ClassDB::bind_method(_MD("is_v_drag_enabled"),&Camera2D::is_v_drag_enabled);
  416. ClassDB::bind_method(_MD("set_h_drag_enabled","enabled"),&Camera2D::set_h_drag_enabled);
  417. ClassDB::bind_method(_MD("is_h_drag_enabled"),&Camera2D::is_h_drag_enabled);
  418. ClassDB::bind_method(_MD("set_v_offset","ofs"),&Camera2D::set_v_offset);
  419. ClassDB::bind_method(_MD("get_v_offset"),&Camera2D::get_v_offset);
  420. ClassDB::bind_method(_MD("set_h_offset","ofs"),&Camera2D::set_h_offset);
  421. ClassDB::bind_method(_MD("get_h_offset"),&Camera2D::get_h_offset);
  422. ClassDB::bind_method(_MD("set_drag_margin","margin","drag_margin"),&Camera2D::set_drag_margin);
  423. ClassDB::bind_method(_MD("get_drag_margin","margin"),&Camera2D::get_drag_margin);
  424. ClassDB::bind_method(_MD("get_camera_pos"),&Camera2D::get_camera_pos);
  425. ClassDB::bind_method(_MD("get_camera_screen_center"),&Camera2D::get_camera_screen_center);
  426. ClassDB::bind_method(_MD("set_zoom","zoom"),&Camera2D::set_zoom);
  427. ClassDB::bind_method(_MD("get_zoom"),&Camera2D::get_zoom);
  428. ClassDB::bind_method(_MD("set_custom_viewport","viewport:Viewport"),&Camera2D::set_custom_viewport);
  429. ClassDB::bind_method(_MD("get_custom_viewport:Viewport"),&Camera2D::get_custom_viewport);
  430. ClassDB::bind_method(_MD("set_follow_smoothing","follow_smoothing"),&Camera2D::set_follow_smoothing);
  431. ClassDB::bind_method(_MD("get_follow_smoothing"),&Camera2D::get_follow_smoothing);
  432. ClassDB::bind_method(_MD("set_enable_follow_smoothing","follow_smoothing"),&Camera2D::set_enable_follow_smoothing);
  433. ClassDB::bind_method(_MD("is_follow_smoothing_enabled"),&Camera2D::is_follow_smoothing_enabled);
  434. ClassDB::bind_method(_MD("force_update_scroll"),&Camera2D::force_update_scroll);
  435. ClassDB::bind_method(_MD("reset_smoothing"),&Camera2D::reset_smoothing);
  436. ClassDB::bind_method(_MD("align"),&Camera2D::align);
  437. ClassDB::bind_method(_MD("_set_old_smoothing","follow_smoothing"),&Camera2D::_set_old_smoothing);
  438. ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"offset"),_SCS("set_offset"),_SCS("get_offset"));
  439. ADD_PROPERTY( PropertyInfo(Variant::INT,"anchor_mode",PROPERTY_HINT_ENUM,"Fixed TopLeft,Drag Center"),_SCS("set_anchor_mode"),_SCS("get_anchor_mode"));
  440. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"rotating"),_SCS("set_rotating"),_SCS("is_rotating"));
  441. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"current"),_SCS("_set_current"),_SCS("is_current"));
  442. ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"zoom"),_SCS("set_zoom"),_SCS("get_zoom") );
  443. ADD_GROUP("Limit","limit_");
  444. ADD_PROPERTYI( PropertyInfo(Variant::INT,"limit_left"),_SCS("set_limit"),_SCS("get_limit"),MARGIN_LEFT);
  445. ADD_PROPERTYI( PropertyInfo(Variant::INT,"limit_top"),_SCS("set_limit"),_SCS("get_limit"),MARGIN_TOP);
  446. ADD_PROPERTYI( PropertyInfo(Variant::INT,"limit_right"),_SCS("set_limit"),_SCS("get_limit"),MARGIN_RIGHT);
  447. ADD_PROPERTYI( PropertyInfo(Variant::INT,"limit_bottom"),_SCS("set_limit"),_SCS("get_limit"),MARGIN_BOTTOM);
  448. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"limit_smoothed"),_SCS("set_limit_smoothing_enabled"),_SCS("is_limit_smoothing_enabled") );
  449. ADD_GROUP("Draw Margin","draw_margin_");
  450. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"drag_margin_h_enabled"),_SCS("set_h_drag_enabled"),_SCS("is_h_drag_enabled") );
  451. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"drag_margin_v_enabled"),_SCS("set_v_drag_enabled"),_SCS("is_v_drag_enabled") );
  452. ADD_GROUP("Smoothing","smoothing_");
  453. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"smoothing_enabled"),_SCS("set_enable_follow_smoothing"),_SCS("is_follow_smoothing_enabled") );
  454. ADD_PROPERTY( PropertyInfo(Variant::REAL,"smoothing_speed"),_SCS("set_follow_smoothing"),_SCS("get_follow_smoothing") );
  455. ADD_GROUP("Drag Margin","drag_margin_");
  456. ADD_PROPERTYI( PropertyInfo(Variant::REAL,"drag_margin_left",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_drag_margin"),_SCS("get_drag_margin"),MARGIN_LEFT);
  457. ADD_PROPERTYI( PropertyInfo(Variant::REAL,"drag_margin_top",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_drag_margin"),_SCS("get_drag_margin"),MARGIN_TOP);
  458. ADD_PROPERTYI( PropertyInfo(Variant::REAL,"drag_margin_right",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_drag_margin"),_SCS("get_drag_margin"),MARGIN_RIGHT);
  459. ADD_PROPERTYI( PropertyInfo(Variant::REAL,"drag_margin_bottom",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_drag_margin"),_SCS("get_drag_margin"),MARGIN_BOTTOM);
  460. BIND_CONSTANT( ANCHOR_MODE_DRAG_CENTER );
  461. BIND_CONSTANT( ANCHOR_MODE_FIXED_TOP_LEFT );
  462. }
  463. Camera2D::Camera2D() {
  464. anchor_mode=ANCHOR_MODE_DRAG_CENTER;
  465. rotating=false;
  466. current=false;
  467. limit[MARGIN_LEFT]=-10000000;
  468. limit[MARGIN_TOP]=-10000000;
  469. limit[MARGIN_RIGHT]=10000000;
  470. limit[MARGIN_BOTTOM]=10000000;
  471. drag_margin[MARGIN_LEFT]=0.2;
  472. drag_margin[MARGIN_TOP]=0.2;
  473. drag_margin[MARGIN_RIGHT]=0.2;
  474. drag_margin[MARGIN_BOTTOM]=0.2;
  475. camera_pos=Vector2();
  476. first=true;
  477. smoothing_enabled=false;
  478. limit_smoothing_enabled=false;
  479. custom_viewport=NULL;
  480. custom_viewport_id=0;
  481. smoothing=5.0;
  482. zoom = Vector2(1, 1);
  483. h_drag_enabled=true;
  484. v_drag_enabled=true;
  485. h_ofs=0;
  486. v_ofs=0;
  487. set_notify_transform(true);
  488. }