PageRenderTime 4971ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/Troisieme_soutenance/eradiKor/eradiKor/Structures/Window.cs

http://xe8tmw7c.googlecode.com/
C# | 320 lines | 202 code | 40 blank | 78 comment | 19 complexity | fc5ee1498c55ea78b3359db11882e041 MD5 | raw file
Possible License(s): GPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Input;
  7. namespace eradiKor.Structures
  8. {
  9. #region enum GameParts
  10. /// <summary>
  11. /// Différentes parties visibles ŕ l’écran.
  12. /// </summary>
  13. public enum GameParts
  14. {
  15. Map,
  16. Tchat,
  17. TowerSelection,
  18. ProgressBar,
  19. Hud,
  20. OthersMaps,
  21. Zbimout,
  22. KorBox,
  23. Info
  24. }
  25. #endregion
  26. #region struct PartDatas
  27. public struct PartDatas
  28. {
  29. #region Attributs
  30. private Structures.GameParts part;
  31. private Rectangle position;
  32. private bool focus;
  33. #endregion
  34. #region Constructeur
  35. public PartDatas(Structures.GameParts partie, Rectangle pos)
  36. {
  37. this.part = partie;
  38. this.position = pos;
  39. this.focus = false;
  40. }
  41. #endregion
  42. #region Accesseurs
  43. /// <summary>
  44. /// Partie de l’écran visible par le joueur.
  45. /// </summary>
  46. public Structures.GameParts Part
  47. {
  48. get
  49. {
  50. return this.part;
  51. }
  52. }
  53. /// <summary>
  54. /// Position occupée par cette partie.
  55. /// </summary>
  56. public Rectangle Position
  57. {
  58. get
  59. {
  60. return this.position;
  61. }
  62. set
  63. {
  64. this.position = value;
  65. }
  66. }
  67. /// <summary>
  68. /// True si la souris est au dessus de cette partie.
  69. /// </summary>
  70. public bool Focus
  71. {
  72. get
  73. {
  74. return this.focus;
  75. }
  76. set
  77. {
  78. this.focus = value;
  79. }
  80. }
  81. #endregion
  82. }
  83. #endregion
  84. #region class Window
  85. public class Window
  86. {
  87. #region Attributs
  88. private int width, height;
  89. private int scaleCoef;
  90. private bool debug, heavyDebug, screenshotmode, blackWhite, hasFocus;
  91. private PartDatas mapPos, tchatPos, hudPos, infoPos, boitakorPos, statsPos;
  92. private Point lastMousePos, mouseVect;
  93. private General.Keymap keymap;
  94. #endregion
  95. #region Événements
  96. public delegate void WindowEventHandler(object sender, Events.WindowEventArgs e);
  97. public event WindowEventHandler OnResize;
  98. #endregion
  99. #region Constructeur
  100. /// <summary>
  101. /// Attribue les caractéristiques de la fenętre
  102. /// </summary>
  103. /// <param name="w">Largeur en pixels : Window.Clientbounds.Width</param>
  104. /// <param name="h">Hauteur en pixels : Window.Clientbounds.Height</param>
  105. /// <param name="fullscreen">Mode plein écran ou non : Window.Clientbounds</param>
  106. public Window(int w, int h)
  107. {
  108. this.width = 0;
  109. this.height = 0;
  110. this.scaleCoef = h / 40;
  111. this.debug = false;
  112. this.heavyDebug = false;
  113. this.blackWhite = true;
  114. this.lastMousePos = new Point(0, 0);
  115. this.mouseVect = new Point(0, 0);
  116. this.keymap = General.Keymap.US_int;
  117. this.OnResize += new WindowEventHandler(Window_OnResize);
  118. this.hasFocus = true;
  119. }
  120. #endregion
  121. #region Accesseur
  122. /// <summary>
  123. /// Largeur de la fenętre en pixel.
  124. /// </summary>
  125. public int Width { get { return this.width; } }
  126. /// <summary>
  127. /// Hauteur de la fenętre en pixel.
  128. /// </summary>
  129. public int Height { get { return this.height; } }
  130. /// <summary>
  131. /// Combien de pixel prend une case ŕ l’écran.
  132. /// </summary>
  133. public int ScaleCoef { get { return this.scaleCoef; } }
  134. /// <summary>
  135. /// Place allouée ŕ la map
  136. /// </summary>
  137. public Structures.PartDatas MapPos { get { return this.mapPos; } }
  138. /// <summary>
  139. /// Place allouée au tchat.
  140. /// </summary>
  141. public Structures.PartDatas TchatPos { get { return this.tchatPos; } }
  142. /// <summary>
  143. /// Position du score, vie, argent et vagues de Kors.
  144. /// </summary>
  145. public Structures.PartDatas InfoPos { get { return this.infoPos; } }
  146. public Structures.PartDatas HudPos { get { return this.hudPos; } }
  147. public Structures.PartDatas BoitakorPos { get { return this.boitakorPos; } }
  148. public Structures.PartDatas StatsPos { get { return this.statsPos; } }
  149. /// <summary>
  150. /// Le mode debug est-il activé ?
  151. /// </summary>
  152. public bool Debug
  153. {
  154. get { return this.debug; }
  155. set { this.debug = value; }
  156. }
  157. /// <summary>
  158. /// Le mode debug lourd est-il activé ?
  159. /// </summary>
  160. public bool HeavyDebug
  161. {
  162. get { return this.heavyDebug; }
  163. set { this.heavyDebug = value; }
  164. }
  165. /// <summary>
  166. /// Mode screenshot (ask 4you ...).
  167. /// </summary>
  168. public bool Screenshotmode
  169. {
  170. get { return this.screenshotmode; }
  171. set { this.screenshotmode = value; }
  172. }
  173. public bool BlackWhite
  174. {
  175. get { return this.blackWhite; }
  176. set { this.blackWhite = value; }
  177. }
  178. /// <summary>
  179. /// Derničre position connue de la souris.
  180. /// </summary>
  181. public Point MousePos { get { return this.lastMousePos; } }
  182. /// <summary>
  183. /// Vecteur direction de la souris.
  184. /// </summary>
  185. public Point MouseVect { get { return this.mouseVect; } }
  186. /// <summary>
  187. /// Layout du clavier.
  188. /// </summary>
  189. public General.Keymap Keymap
  190. {
  191. get { return this.keymap; }
  192. set { this.keymap = value; }
  193. }
  194. public bool HasFocus
  195. {
  196. get { return this.hasFocus; }
  197. set { this.hasFocus = value; }
  198. }
  199. #endregion
  200. #region Méthodes
  201. /// <summary>
  202. /// Indique si la fenętre a changé.
  203. /// </summary>
  204. /// <param name="win">Précédente fenętre</param>
  205. /// <returns>True si la fenętre a changé.</returns>
  206. public bool isEqual(ref Structures.Window win)
  207. {
  208. return ((this.width == win.Width) && (this.height == win.Height));
  209. }
  210. /// <summary>
  211. /// Met ŕ jour les informations de la fenętre.
  212. /// </summary>
  213. /// <param name="w">Largeur en pixel.</param>
  214. /// <param name="h">Hauteur en pixel.</param>
  215. public void update(int w, int h, Kore.Structures.Map map, MouseState mouse, Structures.Tchat tchat)
  216. {
  217. if (w != this.width || h != this.height)
  218. {
  219. Events.WindowEventArgs e = new Events.WindowEventArgs(w, h, map, mouse, tchat);
  220. OnResize(this, e);
  221. }
  222. if (tchat != null)
  223. this.tchatPos.Position = new Rectangle(this.width / 50, this.height * 5 / 6 - 20 * (tchat.DispLines - 5), this.width * 17 / 40 - 30, this.height / 6 + 20 * (tchat.DispLines - 5));
  224. //position de la souris
  225. Point mousePos = new Point(mouse.X, mouse.Y);
  226. this.mapPos.Focus = this.hasFocus && this.isIn(mousePos, mapPos.Position);
  227. this.tchatPos.Focus = this.hasFocus && this.isIn(mousePos, tchatPos.Position);
  228. this.mouseVect = new Point(mouse.X - this.lastMousePos.X, mouse.Y - this.lastMousePos.Y);
  229. this.lastMousePos = new Point(mouse.X, mouse.Y);
  230. }
  231. /// <summary>
  232. /// Savoir si un point est dans un rectangle.
  233. /// </summary>
  234. /// <param name="mouse">Position du point.</param>
  235. /// <param name="area">Rectangle.</param>
  236. /// <returns>True si le point est dans le rectangle.</returns>
  237. public bool isIn(Point mouse, Rectangle area)
  238. {
  239. return (mouse.X >= area.X && mouse.X <= (area.X + area.Width)
  240. && mouse.Y >= area.Y && mouse.Y <= area.Y + area.Height);
  241. }
  242. /// <summary>
  243. /// Recalcul la taille et la position de l'ensemble des parties de l'écran au prochain Update !
  244. /// </summary>
  245. public void recalc()
  246. {
  247. this.width = 0;
  248. this.height = 0;
  249. }
  250. #endregion
  251. #region Méthodes événementielles
  252. void Window_OnResize(object sender, eradiKor.Events.WindowEventArgs e)
  253. {
  254. //On sauve les nouvelles tailles
  255. this.width = e.Width;
  256. this.height = e.Height;
  257. //On repositionne les différentes parties de l'écran
  258. this.mapPos.Position = new Rectangle(this.width * 3 / 50, this.height * 17 / 100, this.width * 13 / 20, this.height * 3 / 5);
  259. int ww = this.width - (this.tchatPos.Position.X + this.tchatPos.Position.Width + 35);
  260. if (ww >= 500)
  261. this.boitakorPos.Position = new Rectangle(this.width - 500, this.height - 130, 500, 130);
  262. else
  263. this.boitakorPos.Position = new Rectangle(this.width - ww, this.height - 130 * ww / 500, ww, 130 * ww / 500);
  264. //if (this.width - this.width * 13 / 20 >= 300) = ŕ la ligne en dessous factorisée
  265. if (this.width * -0.6 >= 300)
  266. this.hudPos.Position = new Rectangle(this.width - 300, 5, 300, this.height - 5);
  267. else
  268. this.hudPos.Position = new Rectangle(this.width * 13 / 20, 5, this.width - this.width * 13 / 20, this.height - 5);
  269. this.statsPos = new Structures.PartDatas(Structures.GameParts.OthersMaps, new Rectangle(this.hudPos.Position.X + 25, this.hudPos.Position.Y + e.Height / 3, this.hudPos.Position.Width - 27, this.hudPos.Position.Height - this.boitakorPos.Position.Height - 210));
  270. this.infoPos.Position = new Rectangle(0, 0, this.width * 13 / 18, this.height * 1 / 15);
  271. //On met ŕ jour le coefficient de taille des éléments
  272. if (e.Carte != null)
  273. this.scaleCoef = Math.Min(this.mapPos.Position.Width / (e.Carte.MapSizeX - 2), this.mapPos.Position.Height / (e.Carte.MapSizeY - 2));
  274. if (this.scaleCoef < 1)
  275. this.scaleCoef = 1;
  276. }
  277. #endregion
  278. }
  279. #endregion
  280. }