PageRenderTime 5156ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/RecycleRobo/RecycleRobo/Setting/Setting.cs

https://bitbucket.org/huuloc/imaginecup_recyclerobo
C# | 249 lines | 205 code | 27 blank | 17 comment | 8 complexity | 98d3ad458fc094fe5de68c90b5103459 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using RecycleRobo.Scenes;
  12. namespace RecycleRobo
  13. {
  14. /// <summary>
  15. /// This is a game component that implements IUpdateable.
  16. /// </summary>
  17. public class Setting
  18. {
  19. GameManager game;
  20. Button resoButton, exitButton, resumeButton, volumeButton, aboutButton, retreatButton;
  21. Button checkButton, crossButton;
  22. Vector2 basePos;
  23. float buttonScale = 0.625f;
  24. float margin = 9f;
  25. Texture2D resoTextureFull, resoTextureNormal, exitTexture, resumeTexture, volumeTexture, aboutTexture;
  26. Vector2 resoPos, exitPos, resumePos, volumePos, aboutPos, volume2Pos;
  27. //Slider volume, volume2;
  28. FxSlider volume;
  29. SoudSlider volume2;
  30. public bool isPlaySene = false;
  31. public bool isResume = true;
  32. public bool isRetreat = false;
  33. HowToPlay howToPlay;
  34. public Setting(GameManager game, Vector2 basePos)
  35. {
  36. this.game = game;
  37. this.basePos = basePos;
  38. howToPlay = new HowToPlay(game);
  39. Initialize();
  40. }
  41. public void Initialize()
  42. {
  43. //resoTextureFull = game.Content.Load<Texture2D>("Setting/official/fullOnTile");
  44. //resoTextureNormal = game.Content.Load<Texture2D>("Setting/official/fullOffTile");
  45. //exitTexture = game.Content.Load<Texture2D>("Setting/official/exitTile");
  46. //Texture2D exitTextureHover = game.Content.Load<Texture2D>("Setting/official/exitHover");
  47. //resumeTexture = game.Content.Load<Texture2D>("Setting/official/resumeTile");
  48. //Texture2D resumeTextureHover = game.Content.Load<Texture2D>("Setting/official/resumeHover");
  49. //Texture2D retreatTexture = game.Content.Load<Texture2D>("Setting/official/retreatTile");
  50. //Texture2D retreatTextureHover = game.Content.Load<Texture2D>("Setting/official/retreatHover");
  51. //volumeTexture = game.Content.Load<Texture2D>("Setting/official/volumeTile");
  52. //aboutTexture = game.Content.Load<Texture2D>("Setting/official/aboutTile");
  53. resoTextureFull = Asset.resoTextureFull;
  54. resoTextureNormal = Asset.resoTextureNormal;
  55. exitTexture = Asset.exitTexture;
  56. Texture2D exitTextureHover = Asset.exitTextureHover;
  57. resumeTexture = Asset.resumeTexture;
  58. Texture2D resumeTextureHover = Asset.resumeTextureHover;
  59. Texture2D retreatTexture = Asset.retreatTexture;
  60. Texture2D retreatTextureHover = Asset.retreatTextureHover;
  61. volumeTexture = Asset.volumeTexture;
  62. aboutTexture = Asset.aboutTexture;
  63. volumePos = new Vector2(0, 0) + basePos;
  64. volume = new FxSlider(game, volumePos + new Vector2(-33, 33));
  65. volume2 = new SoudSlider(game, volumePos + new Vector2(-33, -3));
  66. resoPos = new Vector2(0, resoTextureFull.Height * buttonScale + margin + 2) + basePos;
  67. resumePos = new Vector2(resoTextureFull.Width * buttonScale + margin + 2, 0) + basePos;
  68. exitPos = new Vector2(resoTextureFull.Width * buttonScale + margin + 2, resoTextureFull.Height * buttonScale + margin) + basePos;
  69. aboutPos = new Vector2(aboutTexture.Width*buttonScale/2 - resoTextureFull.Width * buttonScale/2, 2 * (resoTextureFull.Height * buttonScale + margin)) + basePos;
  70. if (GlobalClass.isFullScreen)
  71. {
  72. resoButton = new Button(resoTextureFull, resoTextureNormal, resoTextureFull, buttonScale, 0f, resoPos);
  73. }
  74. else
  75. {
  76. resoButton = new Button(resoTextureNormal, resoTextureFull, resoTextureNormal, buttonScale, 0f, resoPos);
  77. }
  78. retreatButton = new Button(retreatTexture, retreatTextureHover, retreatTexture, buttonScale, 0f, exitPos);
  79. resumeButton = new Button(resumeTexture, resumeTextureHover, resumeTexture, buttonScale, 0f, resumePos);
  80. exitButton = new Button(exitTexture, exitTextureHover, exitTexture, buttonScale, 0f, exitPos);
  81. volumeButton = new Button(volumeTexture, volumeTexture, volumeTexture, buttonScale, 0f, volumePos);
  82. aboutButton = new Button(aboutTexture, aboutTexture, aboutTexture, buttonScale, 0f, aboutPos);
  83. resoButton.Clicked += new EventHandler(changeResolution);
  84. resumeButton.Clicked += new EventHandler(resumeButton_Clicked);
  85. exitButton.Clicked += new EventHandler(exitButton_Clicked);
  86. retreatButton.Clicked += new EventHandler(retreatButton_Clicked);
  87. resoButton.layer = 0.06f;
  88. exitButton.layer = 0.06f;
  89. retreatButton.layer = 0.06f;
  90. resumeButton.layer = 0.06f;
  91. aboutButton.layer = 0.06f;
  92. volumeButton.layer = 0.06f;
  93. InitConfirmButton();
  94. }
  95. public void retreatButton_Clicked(object o, EventArgs e)
  96. {
  97. isRetreat = true;
  98. isResume = true;
  99. //GlobalClass.isPause = true;
  100. }
  101. public void exitButton_Clicked(object o, EventArgs e)
  102. {
  103. game.Exit();
  104. }
  105. public void resumeButton_Clicked(object o, EventArgs e)
  106. {
  107. isResume = !isResume;
  108. GlobalClass.isSetting = false;
  109. }
  110. public void InitConfirmButton()
  111. {
  112. Texture2D checkTexture = Asset.checkTexture;
  113. Texture2D crossTexture = Asset.crossTexture;
  114. Texture2D checkTextureHover = Asset.checkTextureHover;
  115. Texture2D crossTextureHover = Asset.crossTextureHover;
  116. checkButton = new Button(checkTexture, checkTextureHover, checkTexture, 0.625f, 0, new Vector2(GlobalClass.ScreenWidth / 2 - 120, GlobalClass.ScreenHeight / 2));
  117. checkButton.Clicked += new EventHandler(checkButton_Clicked);
  118. checkButton.layer = 0.05f;
  119. crossButton = new Button(crossTexture, crossTextureHover, crossTexture,
  120. 0.625f, 0f, new Vector2(GlobalClass.ScreenWidth / 2 + 160, GlobalClass.ScreenHeight / 2));
  121. crossButton.layer = 0.05f;
  122. crossButton.Clicked += new EventHandler(crossButton_Clicked);
  123. }
  124. public void checkButton_Clicked(object o, EventArgs e)
  125. {
  126. MediaPlayer.Stop();
  127. MediaPlayer.Play(game.Content.Load<Song>("sounds\\main_sound"));
  128. MediaPlayer.IsRepeating = true;
  129. this.game.StartMap();
  130. GlobalClass.isSetting = false;
  131. }
  132. public void crossButton_Clicked(object o, EventArgs e)
  133. {
  134. isRetreat = false;
  135. GlobalClass.isSetting = false;
  136. }
  137. public void SaveData()
  138. {
  139. SettingData save = new SettingData(GlobalClass.volumeMusic, GlobalClass.volumeSoundFx, GlobalClass.isFullScreen);
  140. Data saveData = new Data();
  141. saveData.Save(save, "Setting\\Setting.xml");
  142. }
  143. private void changeResolution(object o, EventArgs e)
  144. {
  145. GlobalClass.isFullScreen = !GlobalClass.isFullScreen;
  146. if (GlobalClass.isFullScreen)
  147. {
  148. setFullScreen();
  149. Matrix transfrom = RotatedRectangle.GetMatrixTransfrom(new Vector2(howToPlay.clipRect.X, howToPlay.clipRect.Y), Vector2.Zero, 0f, 1f);
  150. howToPlay.clipRect = RotatedRectangle.CalculateBoundingRectangle(howToPlay.clipRect, GameManager.transform);
  151. resoButton = new Button(resoTextureFull, resoTextureNormal, resoTextureFull, buttonScale, 0f, resoPos);
  152. resoButton.layer = 0.06f;
  153. }
  154. else
  155. {
  156. setNormalScreen();
  157. //howToPlay.clipRect = RotatedRectangle.CalculateBoundingRectangle(howToPlay.clipRect, GameManager.transform);
  158. howToPlay.clipRect = howToPlay.originRect;
  159. resoButton = new Button(resoTextureNormal, resoTextureFull, resoTextureNormal, buttonScale, 0f, resoPos);
  160. resoButton.layer = 0.06f;
  161. }
  162. resoButton.Clicked += new EventHandler(changeResolution);
  163. }
  164. public static void setFullScreen()
  165. {
  166. Resolution.SetResolution((int)GlobalClass.fullWidthDisplay, (int)GlobalClass.fullHeightDisplay, true);
  167. GameManager.transform = Resolution.getTransformationMatrix();
  168. }
  169. public static void setNormalScreen()
  170. {
  171. Resolution.SetResolution(Convert.ToInt16(GlobalClass.fullWidthDisplay * 0.85f), Convert.ToInt16(GlobalClass.fullHeightDisplay * 0.85f), false);
  172. //HowToPlay.clipRect = RotatedRectangle.CalculateBoundingRectangle(HowToPlay.clipRect, GameManager.transform);
  173. GameManager.transform = Resolution.getTransformationMatrix();
  174. }
  175. public void Draw(SpriteBatch spriteBatch)
  176. {
  177. if (!isResume)
  178. {
  179. if (isPlaySene){
  180. retreatButton.Draw(spriteBatch);
  181. }
  182. else
  183. exitButton.Draw(spriteBatch);
  184. resumeButton.Draw(spriteBatch);
  185. resoButton.Draw(spriteBatch);
  186. volumeButton.Draw(spriteBatch);
  187. aboutButton.Draw(spriteBatch);
  188. volume.Draw(spriteBatch);
  189. volume2.Draw(spriteBatch);
  190. howToPlay.Draw(spriteBatch);
  191. }
  192. if (isRetreat)
  193. {
  194. spriteBatch.DrawString(Asset.mediumFont, "Are you sure? You want to retreat!", new Vector2(GlobalClass.ScreenWidth / 2 - 160, GlobalClass.ScreenHeight / 2 - 100), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.01f);
  195. checkButton.Draw(spriteBatch);
  196. crossButton.Draw(spriteBatch);
  197. }
  198. }
  199. public void Update(GameTime gameTime)
  200. {
  201. if (!isResume)
  202. {
  203. volume.Update(gameTime);
  204. volume2.Update(gameTime);
  205. if (isPlaySene){
  206. retreatButton.Update(gameTime);
  207. }
  208. else
  209. exitButton.Update(gameTime);
  210. resumeButton.Update(gameTime);
  211. resoButton.Update(gameTime);
  212. howToPlay.Update(gameTime);
  213. SaveData();
  214. }
  215. if (isRetreat)
  216. {
  217. checkButton.Update(gameTime);
  218. crossButton.Update(gameTime);
  219. }
  220. }
  221. }
  222. }