CameraManager.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <OgreCamera.h>
  17. #include <OgreSingleton.h>
  18. #include "engine/Event.h"
  19. #include "engine/Manager.h"
  20. /**
  21. * The camera manager.
  22. */
  23. class CameraManager : public Manager, public Ogre::Singleton<CameraManager>{
  24. public:
  25. /**
  26. * Constructor.
  27. *
  28. * Initializes the camera with default parameters attached to the root scene
  29. * manager.
  30. */
  31. CameraManager();
  32. /**
  33. * Destructor.
  34. */
  35. virtual ~CameraManager();
  36. /**
  37. * Handles camera actions.
  38. *
  39. * Handles the scene manager camera actions based on events.
  40. *
  41. * @param[in] event Event that triggers the camera action.
  42. */
  43. void Input(const VGears::Event& event) override;
  44. /**
  45. * Handles camera actions.
  46. *
  47. * Handles the scene manager camera actions based on events.
  48. *
  49. * @param[in] event Event that triggers the camera action.
  50. * @param[in] time_since_last_frame For speed calculation.
  51. */
  52. void Input(const VGears::Event& event , Ogre::Real time_since_last_frame);
  53. /**
  54. * Trigered when the viewport is resized.
  55. *
  56. * Resets the aspect ratio.
  57. */
  58. void OnResize() override;
  59. /**
  60. * Updates debug information.
  61. */
  62. void UpdateDebug() override;
  63. /**
  64. * Clears all field information in the camera manager.
  65. */
  66. void ClearField() override;
  67. /**
  68. * Clears all battle information in the camera manager.
  69. */
  70. void ClearBattle() override;
  71. /**
  72. * Clears all world map information in the camera manager.
  73. */
  74. void ClearWorld() override;
  75. /**
  76. * Enables or disables the free camera.
  77. *
  78. * A free camera moves depending on player input, not according to a
  79. * script. For example, a camera following the PC is a free camera.
  80. *
  81. * @param[in] enable True to enable free camera, false to disable it.
  82. */
  83. void SetCameraFree(const bool enable);
  84. /**
  85. * Sets the camera properties.
  86. *
  87. * @param[in] position Camera coordinates.
  88. * @param[in] orientation Camera rotation, in quaternion format.
  89. * @param[in] fov Field of view, in radians.
  90. */
  91. void Set2DCamera(
  92. const Ogre::Vector3 position, const Ogre::Quaternion orientation,
  93. const Ogre::Radian fov
  94. );
  95. /**
  96. * Places the camera where the camera of a movie is, to draw the models over it. The field
  97. * camera is kept, and the scroll of the background doesn't move the camera until
  98. * {@see EndMovieCamera}.
  99. *
  100. * @param[in] position Camera coordinates.
  101. * @param[in] orientation Camera rotation.
  102. * @param[in] fov Vertical field of view.
  103. */
  104. void SetMovieCamera(
  105. const Ogre::Vector3& position, const Ogre::Quaternion& orientation, const Ogre::Radian& fov
  106. );
  107. /**
  108. * Puts the camera of the field back, with its scroll.
  109. */
  110. void EndMovieCamera();
  111. /**
  112. * Starts the battle camera
  113. *
  114. * Saves the position and orientation of the current camera to return once the battle is
  115. * over.
  116. *
  117. * @param[in] position Initial position of the battle camera.
  118. * @param[in] orientation Initial orientation of the battle camera. It indicates the point
  119. * the camera will look at.
  120. */
  121. void StartBattleCamera(const Ogre::Vector3 position, const Ogre::Vector3 orientation);
  122. /**
  123. * Ends the battle camera
  124. *
  125. * Returns the camera to the state it was when {@see StartBattleCamera} was called.
  126. */
  127. void EndBattleCamera();
  128. /**
  129. * Sets the camera scroll.
  130. *
  131. * Moves the camera to the desired position. If the camera is in free
  132. * mode, it sets the position, but it won't actually move the camera.
  133. *
  134. * @param[in] position Position to scroll the camera to.
  135. */
  136. void Set2DScroll(const Ogre::Vector2& position);
  137. /**
  138. * Retrieves the camera position.
  139. *
  140. * @return The camera current position, relative to absolute origin.
  141. */
  142. const Ogre::Vector2& Get2DScroll() const;
  143. /**
  144. * Calculates the position of a point in screen.
  145. *
  146. * The calculation is done using A map position and the camera scroll.
  147. *
  148. * @param[in] point Position of the map to be translated to screen
  149. * position.
  150. * @return Position of POINT in screen coordinates.
  151. */
  152. const Ogre::Vector3 ProjectPointToScreen(const Ogre::Vector3& point);
  153. /**
  154. * Sets the battle camera, like {@see SetBattleCameraUpright}.
  155. *
  156. * Unlike {@see StartBattleCamera}, it doesn't save the state of the field camera, so it
  157. * can be used to change the camera during a battle.
  158. *
  159. * @param[in] position Camera position.
  160. * @param[in] orientation Point the camera looks at.
  161. */
  162. void SetBattleCamera(const Ogre::Vector3 position, const Ogre::Vector3 orientation);
  163. /**
  164. * Sets the battle camera, always with the Z axis of the scene pointing up.
  165. *
  166. * It also moves the camera down to compensate for the part of the screen that the battle
  167. * interface covers.
  168. *
  169. * @param[in] position Camera position.
  170. * @param[in] look_at Point the camera looks at.
  171. */
  172. void SetBattleCameraUpright(const Ogre::Vector3 position, const Ogre::Vector3 look_at);
  173. /**
  174. * Projects a point of the scene into the coordinates of the UI.
  175. *
  176. * To be called from Lua scripts. UI units are the ones used by the UI widgets: 720 units
  177. * are the height of the screen, with the origin at the top left corner.
  178. *
  179. * @param[in] x X coordinate of the point.
  180. * @param[in] y Y coordinate of the point.
  181. * @param[in] z Z coordinate of the point.
  182. * @return X and Y in UI units, and the depth of the point (negative if it's behind the
  183. * camera).
  184. */
  185. std::tuple<float, float, float> ScriptProjectToUi(const float x, const float y, const float z);
  186. /**
  187. * Retrieves the camera.
  188. *
  189. * @return The current camera.
  190. */
  191. Ogre::Camera* GetCurrentCamera();
  192. /**
  193. * Retrieves the viewport.
  194. *
  195. * @return The camera viewport.
  196. */
  197. Ogre::Viewport* getViewport();
  198. /**
  199. * Sets the camera position and orientation.
  200. *
  201. * It's only meand to be used for 3D cameras (battle, world map...), and using it while on
  202. * a field can have unexpected results.
  203. *
  204. * @param[in] x X coordinate for the camera position.
  205. * @param[in] y Y coordinate for the camera position.
  206. * @param[in] z Z coordinate for the camera position.
  207. * @param[in] d_x X coordinate of the point the camera looks at.
  208. * @param[in] d_y Y coordinate of the point the camera looks at.
  209. * @param[in] d_z Z coordinate of the point the camera looks at.
  210. */
  211. void ScriptSetCamera(
  212. const int x, const int y, const int z, const int d_x, const int d_y, const int d_z
  213. );
  214. private:
  215. /**
  216. * Initializes the camera parameters.
  217. *
  218. * Must be called on construction.
  219. */
  220. void InitCommands();
  221. /**
  222. * Updates while the camera is in the field.
  223. */
  224. void UpdateField() override;
  225. /**
  226. * Updates while the camera is in battle.
  227. */
  228. void UpdateBattle() override;
  229. /**
  230. * Updates while the camera is in the world map.
  231. */
  232. void UpdateWorld() override;
  233. /**
  234. * The camera.
  235. */
  236. Ogre::Camera *camera_;
  237. /**
  238. * The viewport.
  239. */
  240. Ogre::Viewport *viewport_;
  241. /**
  242. * The initial position of the camera, saved when created.
  243. */
  244. Ogre::Vector3 position_initial_;
  245. /**
  246. * The initial orientation of the camera, saved when created.
  247. */
  248. Ogre::Quaternion orientation_initial_;
  249. /**
  250. * A backup of the field or world camera position for when the battle camera is activated.
  251. */
  252. Ogre::Vector3 position_backup_;
  253. /**
  254. * Field of view of the camera before a battle, restored when the battle ends.
  255. */
  256. Ogre::Radian fov_backup_;
  257. /**
  258. * A backup of the field or world camera orientation for when the battle camera is active.
  259. */
  260. Ogre::Quaternion orientation_backup_;
  261. /**
  262. * Flag to indicate a free camera.
  263. *
  264. * A free camera moves depending on player input, not according to a
  265. * script. For example, a camera following the PC is a free camera.
  266. */
  267. bool camera_free_;
  268. /**
  269. * Flag to indicate the free camera has rotated.
  270. */
  271. bool camera_free_rotate_;
  272. /**
  273. * Camera position.
  274. */
  275. Ogre::Vector3 d2_position_;
  276. /**
  277. * Camera orientation.
  278. */
  279. Ogre::Quaternion d2_orientation_;
  280. /**
  281. * The field of view.
  282. */
  283. Ogre::Radian d2_fov_;
  284. /**
  285. * The camera scroll.
  286. */
  287. Ogre::Vector2 d2_scroll_;
  288. /**
  289. * Whether the camera is the one of a movie.
  290. */
  291. bool movie_camera_ = false;
  292. };