CameraManager.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "Event.h"
  19. #include "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. * Starts the battle camera
  97. *
  98. * Saves the position and orientation of the current camera to return once the battle is
  99. * over.
  100. *
  101. * @param[in] position Initial position of the battle camera.
  102. * @param[in] orientation Initial orientation of the battle camera. It indicates the point
  103. * the camera will look at.
  104. */
  105. void StartBattleCamera(const Ogre::Vector3 position, const Ogre::Vector3 orientation);
  106. /**
  107. * Ends the battle camera
  108. *
  109. * Returns the camera to the state it was when {@see StartBattleCamera} was called.
  110. */
  111. void EndBattleCamera();
  112. /**
  113. * Sets the camera scroll.
  114. *
  115. * Moves the camera to the desired position. If the camera is in free
  116. * mode, it sets the position, but it won't actually move the camera.
  117. *
  118. * @param[in] position Position to scroll the camera to.
  119. */
  120. void Set2DScroll(const Ogre::Vector2& position);
  121. /**
  122. * Retrieves the camera position.
  123. *
  124. * @return The camera current position, relative to absolute origin.
  125. */
  126. const Ogre::Vector2& Get2DScroll() const;
  127. /**
  128. * Calculates the position of a point in screen.
  129. *
  130. * The calculation is done using A map position and the camera scroll.
  131. *
  132. * @param[in] point Position of the map to be translated to screen
  133. * position.
  134. * @return Position of POINT in screen coordinates.
  135. */
  136. const Ogre::Vector3 ProjectPointToScreen(const Ogre::Vector3& point);
  137. /**
  138. * Retrieves the camera.
  139. *
  140. * @return The current camera.
  141. */
  142. Ogre::Camera* GetCurrentCamera();
  143. /**
  144. * Retrieves the viewport.
  145. *
  146. * @return The camera viewport.
  147. */
  148. Ogre::Viewport* getViewport();
  149. /**
  150. * Enables or disables the camera wireframe.
  151. *
  152. * In wireframe mode, faces will not be rendered, only edges
  153. *
  154. * @param[in] enable True to enable wireframe mode, false to disable.
  155. */
  156. void EnableWireFrame(bool enable);
  157. /**
  158. * Sets the camera position and orientation.
  159. *
  160. * It's only meand to be used for 3D cameras (battle, world map...), and using it while on
  161. * a field can have unexpected results.
  162. *
  163. * @param[in] x X coordinate for the camera position.
  164. * @param[in] y Y coordinate for the camera position.
  165. * @param[in] z Z coordinate for the camera position.
  166. * @param[in] d_x X coordinate of the point the camera looks at.
  167. * @param[in] d_y Y coordinate of the point the camera looks at.
  168. * @param[in] d_y Z coordinate of the point the camera looks at.
  169. */
  170. void ScriptSetCamera(
  171. const int x, const int y, const int z, const int d_x, const int d_y, const int d_z
  172. );
  173. private:
  174. /**
  175. * Initializes the camera parameters.
  176. *
  177. * Must be called on construction.
  178. */
  179. void InitCommands();
  180. /**
  181. * Updates while the camera is in the field.
  182. */
  183. void UpdateField() override;
  184. /**
  185. * Updates while the camera is in battle.
  186. */
  187. void UpdateBattle() override;
  188. /**
  189. * Updates while the camera is in the world map.
  190. */
  191. void UpdateWorld() override;
  192. /**
  193. * The camera.
  194. */
  195. Ogre::Camera *camera_;
  196. /**
  197. * The viewport.
  198. */
  199. Ogre::Viewport *viewport_;
  200. /**
  201. * The initial position of the camera, saved when created.
  202. */
  203. Ogre::Vector3 position_initial_;
  204. /**
  205. * The initial orientation of the camera, saved when created.
  206. */
  207. Ogre::Quaternion orientation_initial_;
  208. /**
  209. * A backup of the field or world camera position for when the battle camera is activated.
  210. */
  211. Ogre::Vector3 position_backup_;
  212. /**
  213. * A backup of the field or world camera orientation for when the battle camera is active.
  214. */
  215. Ogre::Quaternion orientation_backup_;
  216. /**
  217. * Flag to indicate a free camera.
  218. *
  219. * A free camera moves depending on player input, not according to a
  220. * script. For example, a camera following the PC is a free camera.
  221. */
  222. bool camera_free_;
  223. /**
  224. * Flag to indicate the free camera has rotated.
  225. */
  226. bool camera_free_rotate_;
  227. /**
  228. * Camera position.
  229. */
  230. Ogre::Vector3 d2_position_;
  231. /**
  232. * Camera orientation.
  233. */
  234. Ogre::Quaternion d2_orientation_;
  235. /**
  236. * The field of view.
  237. */
  238. Ogre::Radian d2_fov_;
  239. /**
  240. * The camera scroll.
  241. */
  242. Ogre::Vector2 d2_scroll_;
  243. };