Viewer.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Viewer.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. using System.Text;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Content;
  16. using Microsoft.Xna.Framework.Graphics;
  17. using Microsoft.Xna.Framework.Storage;
  18. using RobotGameData.Camera;
  19. #endregion
  20. namespace RobotGameData.Render
  21. {
  22. #region Enum
  23. public enum ViewerWidth
  24. {
  25. Invalid = 0,
  26. Width480 = 853,
  27. Width720 = 1280,
  28. Width1080 = 1920,
  29. }
  30. public enum ViewerHeight
  31. {
  32. Invalid = 0,
  33. Height480 = 480,
  34. Height720 = 720,
  35. Height1080 = 1080,
  36. }
  37. #endregion
  38. #region ViewCamera
  39. /// <summary>
  40. /// a view class with a viewport and a camera.
  41. /// With the split-screen mode, there can be more than one viewport and one camera.
  42. /// </summary>
  43. public class ViewCamera
  44. {
  45. #region Fields
  46. int count = 0;
  47. List<Viewport> viewport = new List<Viewport>();
  48. List<CameraBase> camera = new List<CameraBase>();
  49. #endregion
  50. #region Properties
  51. public int Count
  52. {
  53. get { return count; }
  54. }
  55. public CameraBase FirstCamera
  56. {
  57. get { return camera[0]; }
  58. }
  59. #endregion
  60. #region Constructors
  61. public ViewCamera()
  62. {
  63. count = 0;
  64. viewport.Clear();
  65. camera.Clear();
  66. }
  67. public ViewCamera(CameraBase camera, Rectangle? viewportRectangle)
  68. {
  69. Add(camera, viewportRectangle);
  70. }
  71. #endregion
  72. /// <summary>
  73. /// adds a camera and viewport.
  74. /// </summary>
  75. /// <param name="camera"></param>
  76. /// <param name="viewportRectangle"></param>
  77. public void Add(CameraBase camera, Rectangle? viewportRectangle)
  78. {
  79. this.camera.Add(camera);
  80. Viewport newViewport = FrameworkCore.Game.GraphicsDevice.Viewport;
  81. if (viewportRectangle == null)
  82. {
  83. newViewport.X = 0;
  84. newViewport.Y = 0;
  85. newViewport.Width = FrameworkCore.Game.GraphicsDevice.Viewport.Width;
  86. newViewport.Height = FrameworkCore.Game.GraphicsDevice.Viewport.Height;
  87. }
  88. else
  89. {
  90. newViewport.X = viewportRectangle.Value.X;
  91. newViewport.Y = viewportRectangle.Value.Y;
  92. newViewport.Width = viewportRectangle.Value.Width;
  93. newViewport.Height = viewportRectangle.Value.Height;
  94. }
  95. this.viewport.Add(newViewport);
  96. count++;
  97. }
  98. /// <summary>
  99. /// removes the camera by index.
  100. /// </summary>
  101. /// <param name="index"></param>
  102. public void Remove(int index)
  103. {
  104. viewport.RemoveAt(index);
  105. camera.RemoveAt(index);
  106. }
  107. /// <summary>
  108. /// resize the camera and viewport by index.
  109. /// </summary>
  110. /// <param name="index">an index of camera</param>
  111. /// <param name="x">screen x-position</param>
  112. /// <param name="y">screen y-position</param>
  113. /// <param name="width">screen width</param>
  114. /// <param name="height">screen height</param>
  115. public void Resize(int index, int x, int y, int width, int height)
  116. {
  117. CameraBase camera = GetCamera(index);
  118. camera.Resize(width, height);
  119. Viewport newViewport = GetViewport(index);
  120. newViewport.X = x;
  121. newViewport.Y = y;
  122. newViewport.Width = width;
  123. newViewport.Height = height;
  124. this.viewport[index] = newViewport;
  125. }
  126. public CameraBase GetCamera(int index)
  127. {
  128. return this.camera[index];
  129. }
  130. public Viewport GetViewport(int index)
  131. {
  132. return this.viewport[index];
  133. }
  134. }
  135. #endregion
  136. /// <summary>
  137. /// This contains and manages RenderContext class and a 3D camera.
  138. /// Viewer may have several cameras.
  139. /// </summary>
  140. public class Viewer
  141. {
  142. #region Fields
  143. RenderContext renderContext = null;
  144. Dictionary<string, ViewCamera> viewCameraList =
  145. new Dictionary<string, ViewCamera>();
  146. ViewCamera currentViewCamera = null;
  147. ViewCamera defaultViewCamera = null;
  148. Viewport defaultViewport;
  149. RenderFog basicFog = null;
  150. RenderLighting basicLighting = null;
  151. #endregion
  152. #region Properties
  153. public static Viewport CurrentViewport
  154. {
  155. get { return FrameworkCore.Game.GraphicsDevice.Viewport; }
  156. }
  157. public Viewport DefaultViewport
  158. {
  159. get { return defaultViewport; }
  160. set { defaultViewport = value; }
  161. }
  162. public int ViewWidth
  163. {
  164. get { return this.defaultViewport.Width; }
  165. }
  166. public int ViewHeight
  167. {
  168. get { return this.defaultViewport.Height; }
  169. }
  170. public int ViewPosX
  171. {
  172. get { return this.defaultViewport.X; }
  173. }
  174. public int ViewPosY
  175. {
  176. get { return this.defaultViewport.Y; }
  177. }
  178. public RenderContext RenderContext
  179. {
  180. get { return renderContext; }
  181. }
  182. public GameSceneNode Scene3DRoot
  183. {
  184. get { return RenderContext.Scene3DRoot; }
  185. }
  186. public Color ClearColor
  187. {
  188. get { return this.RenderContext.ClearColor; }
  189. set { this.RenderContext.ClearColor = value; }
  190. }
  191. public RenderFog BasicFog
  192. {
  193. get { return this.basicFog; }
  194. set { this.basicFog = value; }
  195. }
  196. public RenderLighting BasicLighting
  197. {
  198. get { return this.basicLighting; }
  199. set { this.basicLighting = value; }
  200. }
  201. #endregion
  202. /// <summary>
  203. /// Constructor.
  204. /// </summary>
  205. /// <param name="game">game</param>
  206. public Viewer(Game game)
  207. {
  208. renderContext = new RenderContext(game);
  209. }
  210. /// <summary>
  211. /// initialize members and creates default camera.
  212. /// </summary>
  213. public void Initialize()
  214. {
  215. // initialize the RenderContext
  216. renderContext.Initialize();
  217. this.defaultViewport = FrameworkCore.Game.GraphicsDevice.Viewport;
  218. // creates default camera
  219. {
  220. defaultViewCamera = new ViewCamera(new CameraBase(), null);
  221. // default camera setting
  222. defaultViewCamera.FirstCamera.SetView(Vector3.Zero, Vector3.Forward,
  223. Vector3.Up);
  224. defaultViewCamera.FirstCamera.SetPespective(MathHelper.PiOver4,
  225. (float)ViewWidth,
  226. (float)ViewHeight, 1.0f, 1000.0f);
  227. currentViewCamera = defaultViewCamera;
  228. }
  229. }
  230. /// <summary>
  231. /// resize the default camera.
  232. /// </summary>
  233. /// <param name="width">screen width</param>
  234. /// <param name="height">screen height</param>
  235. public void Resize(int width, int height)
  236. {
  237. this.defaultViewport.Width = width;
  238. this.defaultViewport.Height = height;
  239. }
  240. /// <summary>
  241. /// update cameras.
  242. /// </summary>
  243. public void Update(GameTime gameTime)
  244. {
  245. // Update the current camera
  246. ViewCamera viewCamera = CurrentCamera;
  247. for (int i = 0; i < viewCamera.Count; i++)
  248. {
  249. CameraBase camera = viewCamera.GetCamera(i);
  250. camera.Update(gameTime);
  251. }
  252. // Update scene models in the render context
  253. renderContext.Update(gameTime);
  254. }
  255. public void Dispose()
  256. {
  257. if (renderContext != null)
  258. {
  259. renderContext.Dispose();
  260. renderContext = null;
  261. }
  262. }
  263. /// <summary>
  264. /// draws the render context.
  265. /// </summary>
  266. /// <param name="gameTime"></param>
  267. public void Draw(GameTime gameTime)
  268. {
  269. renderContext.Draw(gameTime);
  270. }
  271. /// <summary>
  272. /// adds a camera.
  273. /// </summary>
  274. /// <param name="key">key name</param>
  275. /// <param name="viewCamera">view camera</param>
  276. public void AddCamera(string key, ViewCamera viewCamera)
  277. {
  278. // New camera add
  279. viewCamera.FirstCamera.Name = key;
  280. viewCameraList.Add(key, viewCamera);
  281. }
  282. /// <summary>
  283. /// removes the camera by index.
  284. /// </summary>
  285. /// <param name="key"></param>
  286. public void RemoveCamera(string key)
  287. {
  288. viewCameraList.Remove(key);
  289. }
  290. public void RemoveAllCamera()
  291. {
  292. viewCameraList.Clear();
  293. }
  294. public ViewCamera GetViewCamera(string key)
  295. {
  296. // Compare valid camera
  297. if (viewCameraList.ContainsKey(key) == false)
  298. return null;
  299. return viewCameraList[key];
  300. }
  301. /// <summary>
  302. /// sets to current camera.
  303. /// </summary>
  304. /// <param name="key">key name</param>
  305. public bool SetCurrentCamera(string key)
  306. {
  307. // Set a current camera
  308. currentViewCamera = GetViewCamera(key);
  309. if (currentViewCamera == null)
  310. return false;
  311. return true;
  312. }
  313. public ViewCamera CurrentCamera
  314. {
  315. get { return currentViewCamera; }
  316. }
  317. /// <summary>
  318. /// changes to current camera by key name.
  319. /// </summary>
  320. /// <param name="key"></param>
  321. public void ChangeCamera(string key)
  322. {
  323. SetCurrentCamera(key);
  324. }
  325. }
  326. }