test_camera_2d.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**************************************************************************/
  2. /* test_camera_2d.h */
  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. #ifndef TEST_CAMERA_2D_H
  31. #define TEST_CAMERA_2D_H
  32. #include "scene/2d/camera_2d.h"
  33. #include "scene/main/viewport.h"
  34. #include "scene/main/window.h"
  35. #include "tests/test_macros.h"
  36. namespace TestCamera2D {
  37. TEST_CASE("[SceneTree][Camera2D] Getters and setters") {
  38. Camera2D *test_camera = memnew(Camera2D);
  39. SUBCASE("AnchorMode") {
  40. test_camera->set_anchor_mode(Camera2D::AnchorMode::ANCHOR_MODE_FIXED_TOP_LEFT);
  41. CHECK(test_camera->get_anchor_mode() == Camera2D::AnchorMode::ANCHOR_MODE_FIXED_TOP_LEFT);
  42. test_camera->set_anchor_mode(Camera2D::AnchorMode::ANCHOR_MODE_DRAG_CENTER);
  43. CHECK(test_camera->get_anchor_mode() == Camera2D::AnchorMode::ANCHOR_MODE_DRAG_CENTER);
  44. }
  45. SUBCASE("ProcessCallback") {
  46. test_camera->set_process_callback(Camera2D::Camera2DProcessCallback::CAMERA2D_PROCESS_PHYSICS);
  47. CHECK(test_camera->get_process_callback() == Camera2D::Camera2DProcessCallback::CAMERA2D_PROCESS_PHYSICS);
  48. test_camera->set_process_callback(Camera2D::Camera2DProcessCallback::CAMERA2D_PROCESS_IDLE);
  49. CHECK(test_camera->get_process_callback() == Camera2D::Camera2DProcessCallback::CAMERA2D_PROCESS_IDLE);
  50. }
  51. SUBCASE("Drag") {
  52. constexpr float drag_left_margin = 0.8f;
  53. constexpr float drag_top_margin = 0.8f;
  54. constexpr float drag_right_margin = 0.8f;
  55. constexpr float drag_bottom_margin = 0.8f;
  56. constexpr float drag_horizontal_offset1 = 0.5f;
  57. constexpr float drag_horizontal_offset2 = -0.5f;
  58. constexpr float drag_vertical_offset1 = 0.5f;
  59. constexpr float drag_vertical_offset2 = -0.5f;
  60. test_camera->set_drag_margin(SIDE_LEFT, drag_left_margin);
  61. CHECK(test_camera->get_drag_margin(SIDE_LEFT) == drag_left_margin);
  62. test_camera->set_drag_margin(SIDE_TOP, drag_top_margin);
  63. CHECK(test_camera->get_drag_margin(SIDE_TOP) == drag_top_margin);
  64. test_camera->set_drag_margin(SIDE_RIGHT, drag_right_margin);
  65. CHECK(test_camera->get_drag_margin(SIDE_RIGHT) == drag_right_margin);
  66. test_camera->set_drag_margin(SIDE_BOTTOM, drag_bottom_margin);
  67. CHECK(test_camera->get_drag_margin(SIDE_BOTTOM) == drag_bottom_margin);
  68. test_camera->set_drag_horizontal_enabled(true);
  69. CHECK(test_camera->is_drag_horizontal_enabled());
  70. test_camera->set_drag_horizontal_enabled(false);
  71. CHECK_FALSE(test_camera->is_drag_horizontal_enabled());
  72. test_camera->set_drag_horizontal_offset(drag_horizontal_offset1);
  73. CHECK(test_camera->get_drag_horizontal_offset() == drag_horizontal_offset1);
  74. test_camera->set_drag_horizontal_offset(drag_horizontal_offset2);
  75. CHECK(test_camera->get_drag_horizontal_offset() == drag_horizontal_offset2);
  76. test_camera->set_drag_vertical_enabled(true);
  77. CHECK(test_camera->is_drag_vertical_enabled());
  78. test_camera->set_drag_vertical_enabled(false);
  79. CHECK_FALSE(test_camera->is_drag_vertical_enabled());
  80. test_camera->set_drag_vertical_offset(drag_vertical_offset1);
  81. CHECK(test_camera->get_drag_vertical_offset() == drag_vertical_offset1);
  82. test_camera->set_drag_vertical_offset(drag_vertical_offset2);
  83. CHECK(test_camera->get_drag_vertical_offset() == drag_vertical_offset2);
  84. }
  85. SUBCASE("Drawing") {
  86. test_camera->set_margin_drawing_enabled(true);
  87. CHECK(test_camera->is_margin_drawing_enabled());
  88. test_camera->set_margin_drawing_enabled(false);
  89. CHECK_FALSE(test_camera->is_margin_drawing_enabled());
  90. test_camera->set_limit_drawing_enabled(true);
  91. CHECK(test_camera->is_limit_drawing_enabled());
  92. test_camera->set_limit_drawing_enabled(false);
  93. CHECK_FALSE(test_camera->is_limit_drawing_enabled());
  94. test_camera->set_screen_drawing_enabled(true);
  95. CHECK(test_camera->is_screen_drawing_enabled());
  96. test_camera->set_screen_drawing_enabled(false);
  97. CHECK_FALSE(test_camera->is_screen_drawing_enabled());
  98. }
  99. SUBCASE("Enabled") {
  100. test_camera->set_enabled(true);
  101. CHECK(test_camera->is_enabled());
  102. test_camera->set_enabled(false);
  103. CHECK_FALSE(test_camera->is_enabled());
  104. }
  105. SUBCASE("Rotation") {
  106. constexpr float rotation_smoothing_speed = 20.0f;
  107. test_camera->set_ignore_rotation(true);
  108. CHECK(test_camera->is_ignoring_rotation());
  109. test_camera->set_ignore_rotation(false);
  110. CHECK_FALSE(test_camera->is_ignoring_rotation());
  111. test_camera->set_rotation_smoothing_enabled(true);
  112. CHECK(test_camera->is_rotation_smoothing_enabled());
  113. test_camera->set_rotation_smoothing_speed(rotation_smoothing_speed);
  114. CHECK(test_camera->get_rotation_smoothing_speed() == rotation_smoothing_speed);
  115. }
  116. SUBCASE("Zoom") {
  117. const Vector2 zoom = Vector2(4, 4);
  118. test_camera->set_zoom(zoom);
  119. CHECK(test_camera->get_zoom() == zoom);
  120. }
  121. SUBCASE("Offset") {
  122. const Vector2 offset = Vector2(100, 100);
  123. test_camera->set_offset(offset);
  124. CHECK(test_camera->get_offset() == offset);
  125. }
  126. SUBCASE("Limit") {
  127. constexpr int limit_left = 100;
  128. constexpr int limit_top = 100;
  129. constexpr int limit_right = 100;
  130. constexpr int limit_bottom = 100;
  131. test_camera->set_limit_smoothing_enabled(true);
  132. CHECK(test_camera->is_limit_smoothing_enabled());
  133. test_camera->set_limit_smoothing_enabled(false);
  134. CHECK_FALSE(test_camera->is_limit_smoothing_enabled());
  135. test_camera->set_limit(SIDE_LEFT, limit_left);
  136. CHECK(test_camera->get_limit(SIDE_LEFT) == limit_left);
  137. test_camera->set_limit(SIDE_TOP, limit_top);
  138. CHECK(test_camera->get_limit(SIDE_TOP) == limit_top);
  139. test_camera->set_limit(SIDE_RIGHT, limit_right);
  140. CHECK(test_camera->get_limit(SIDE_RIGHT) == limit_right);
  141. test_camera->set_limit(SIDE_BOTTOM, limit_bottom);
  142. CHECK(test_camera->get_limit(SIDE_BOTTOM) == limit_bottom);
  143. }
  144. SUBCASE("Position") {
  145. constexpr float smoothing_speed = 20.0f;
  146. test_camera->set_position_smoothing_enabled(true);
  147. CHECK(test_camera->is_position_smoothing_enabled());
  148. test_camera->set_position_smoothing_speed(smoothing_speed);
  149. CHECK(test_camera->get_position_smoothing_speed() == smoothing_speed);
  150. }
  151. memdelete(test_camera);
  152. }
  153. TEST_CASE("[SceneTree][Camera2D] Camera positioning") {
  154. SubViewport *mock_viewport = memnew(SubViewport);
  155. Camera2D *test_camera = memnew(Camera2D);
  156. mock_viewport->set_size(Vector2(400, 200));
  157. SceneTree::get_singleton()->get_root()->add_child(mock_viewport);
  158. mock_viewport->add_child(test_camera);
  159. SUBCASE("Anchor mode") {
  160. test_camera->set_anchor_mode(Camera2D::ANCHOR_MODE_DRAG_CENTER);
  161. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(0, 0)));
  162. CHECK(test_camera->get_camera_position().is_equal_approx(Vector2(0, 0)));
  163. test_camera->set_anchor_mode(Camera2D::ANCHOR_MODE_FIXED_TOP_LEFT);
  164. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(200, 100)));
  165. CHECK(test_camera->get_camera_position().is_equal_approx(Vector2(0, 0)));
  166. }
  167. SUBCASE("Offset") {
  168. test_camera->set_offset(Vector2(100, 100));
  169. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(100, 100)));
  170. CHECK(test_camera->get_camera_position().is_equal_approx(Vector2(0, 0)));
  171. test_camera->set_offset(Vector2(-100, 300));
  172. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(-100, 300)));
  173. CHECK(test_camera->get_camera_position().is_equal_approx(Vector2(0, 0)));
  174. test_camera->set_offset(Vector2(0, 0));
  175. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(0, 0)));
  176. CHECK(test_camera->get_camera_position().is_equal_approx(Vector2(0, 0)));
  177. }
  178. SUBCASE("Limits") {
  179. test_camera->set_limit(SIDE_LEFT, 100);
  180. test_camera->set_limit(SIDE_TOP, 50);
  181. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(300, 150)));
  182. CHECK(test_camera->get_camera_position().is_equal_approx(Vector2(0, 0)));
  183. test_camera->set_limit(SIDE_LEFT, 0);
  184. test_camera->set_limit(SIDE_TOP, 0);
  185. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(200, 100)));
  186. CHECK(test_camera->get_camera_position().is_equal_approx(Vector2(0, 0)));
  187. }
  188. SUBCASE("Drag") {
  189. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(0, 0)));
  190. // horizontal
  191. test_camera->set_drag_horizontal_enabled(true);
  192. test_camera->set_drag_margin(SIDE_RIGHT, 0.5);
  193. test_camera->set_position(Vector2(100, 100));
  194. test_camera->force_update_scroll();
  195. CHECK(test_camera->get_camera_position().is_equal_approx(Vector2(0, 100)));
  196. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(0, 100)));
  197. test_camera->set_position(Vector2(101, 101));
  198. test_camera->force_update_scroll();
  199. CHECK(test_camera->get_camera_position().is_equal_approx(Vector2(1, 101)));
  200. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(1, 101)));
  201. // test align
  202. test_camera->set_position(Vector2(0, 0));
  203. test_camera->align();
  204. CHECK(test_camera->get_camera_position().is_equal_approx(Vector2(0, 0)));
  205. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(0, 0)));
  206. // vertical
  207. test_camera->set_drag_vertical_enabled(true);
  208. test_camera->set_drag_horizontal_enabled(false);
  209. test_camera->set_drag_margin(SIDE_TOP, 0.3);
  210. test_camera->set_position(Vector2(200, -20));
  211. test_camera->force_update_scroll();
  212. CHECK(test_camera->get_camera_position().is_equal_approx(Vector2(200, 0)));
  213. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(200, 0)));
  214. test_camera->set_position(Vector2(250, -55));
  215. test_camera->force_update_scroll();
  216. CHECK(test_camera->get_camera_position().is_equal_approx(Vector2(250, -25)));
  217. CHECK(test_camera->get_camera_screen_center().is_equal_approx(Vector2(250, -25)));
  218. }
  219. memdelete(test_camera);
  220. memdelete(mock_viewport);
  221. }
  222. TEST_CASE("[SceneTree][Camera2D] Transforms") {
  223. SubViewport *mock_viewport = memnew(SubViewport);
  224. Camera2D *test_camera = memnew(Camera2D);
  225. mock_viewport->set_size(Vector2(400, 200));
  226. SceneTree::get_singleton()->get_root()->add_child(mock_viewport);
  227. mock_viewport->add_child(test_camera);
  228. SUBCASE("Default camera") {
  229. Transform2D xform = mock_viewport->get_canvas_transform();
  230. // x,y are basis vectors, origin = screen center
  231. Transform2D test_xform = Transform2D(Vector2(1, 0), Vector2(0, 1), Vector2(200, 100));
  232. CHECK(xform.is_equal_approx(test_xform));
  233. }
  234. SUBCASE("Zoom") {
  235. test_camera->set_zoom(Vector2(0.5, 2));
  236. Transform2D xform = mock_viewport->get_canvas_transform();
  237. Transform2D test_xform = Transform2D(Vector2(0.5, 0), Vector2(0, 2), Vector2(200, 100));
  238. CHECK(xform.is_equal_approx(test_xform));
  239. test_camera->set_zoom(Vector2(10, 10));
  240. xform = mock_viewport->get_canvas_transform();
  241. test_xform = Transform2D(Vector2(10, 0), Vector2(0, 10), Vector2(200, 100));
  242. CHECK(xform.is_equal_approx(test_xform));
  243. test_camera->set_zoom(Vector2(1, 1));
  244. xform = mock_viewport->get_canvas_transform();
  245. test_xform = Transform2D(Vector2(1, 0), Vector2(0, 1), Vector2(200, 100));
  246. CHECK(xform.is_equal_approx(test_xform));
  247. }
  248. SUBCASE("Rotation") {
  249. test_camera->set_rotation(Math_PI / 2);
  250. Transform2D xform = mock_viewport->get_canvas_transform();
  251. Transform2D test_xform = Transform2D(Vector2(1, 0), Vector2(0, 1), Vector2(200, 100));
  252. CHECK(xform.is_equal_approx(test_xform));
  253. test_camera->set_ignore_rotation(false);
  254. xform = mock_viewport->get_canvas_transform();
  255. test_xform = Transform2D(Vector2(0, -1), Vector2(1, 0), Vector2(200, 100));
  256. CHECK(xform.is_equal_approx(test_xform));
  257. test_camera->set_rotation(-1 * Math_PI);
  258. test_camera->force_update_scroll();
  259. xform = mock_viewport->get_canvas_transform();
  260. test_xform = Transform2D(Vector2(-1, 0), Vector2(0, -1), Vector2(200, 100));
  261. CHECK(xform.is_equal_approx(test_xform));
  262. test_camera->set_rotation(0);
  263. test_camera->force_update_scroll();
  264. xform = mock_viewport->get_canvas_transform();
  265. test_xform = Transform2D(Vector2(1, 0), Vector2(0, 1), Vector2(200, 100));
  266. CHECK(xform.is_equal_approx(test_xform));
  267. }
  268. memdelete(test_camera);
  269. memdelete(mock_viewport);
  270. }
  271. } // namespace TestCamera2D
  272. #endif // TEST_CAMERA_2D_H