DialogueScreen.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // DialogueScreen.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 Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Content;
  15. #endregion
  16. namespace RolePlaying
  17. {
  18. /// <summary>
  19. /// Display of conversation dialog between the player and the npc
  20. /// </summary>
  21. class DialogueScreen : GameScreen
  22. {
  23. #region Graphics Data
  24. private Texture2D backgroundTexture;
  25. private Vector2 backgroundPosition;
  26. private Texture2D fadeTexture;
  27. private Texture2D selectButtonTexture;
  28. private Vector2 selectPosition;
  29. private Vector2 selectButtonPosition;
  30. private Vector2 backPosition;
  31. private Texture2D backButtonTexture;
  32. private Vector2 backButtonPosition;
  33. private Texture2D scrollTexture;
  34. private Vector2 scrollPosition;
  35. private Texture2D lineTexture;
  36. private Vector2 topLinePosition;
  37. private Vector2 bottomLinePosition;
  38. private Vector2 titlePosition;
  39. private Vector2 dialogueStartPosition;
  40. #endregion
  41. #region Text Data
  42. /// <summary>
  43. /// The title text shown at the top of the screen.
  44. /// </summary>
  45. private string titleText;
  46. /// <summary>
  47. /// The title text shown at the top of the screen.
  48. /// </summary>
  49. public string TitleText
  50. {
  51. get { return titleText; }
  52. set { titleText = value; }
  53. }
  54. /// <summary>
  55. /// The dialogue shown in the main portion of this dialog.
  56. /// </summary>
  57. private string dialogueText;
  58. /// <summary>
  59. /// The dialogue shown in the main portion of this dialog, broken into lines.
  60. /// </summary>
  61. private List<string> dialogueList = new List<string>();
  62. /// <summary>
  63. /// The dialogue shown in the main portion of this dialog.
  64. /// </summary>
  65. public string DialogueText
  66. {
  67. get { return dialogueText; }
  68. set
  69. {
  70. // trim the new value
  71. string trimmedValue = value.Trim();
  72. // if it's a match for what we already have, then this is trivial
  73. if (dialogueText == trimmedValue)
  74. {
  75. return;
  76. }
  77. // assign the new value
  78. dialogueText = trimmedValue;
  79. // break the text into lines
  80. if (String.IsNullOrEmpty(dialogueText))
  81. {
  82. dialogueList.Clear();
  83. }
  84. else
  85. {
  86. dialogueList = Fonts.BreakTextIntoList(dialogueText,
  87. Fonts.DescriptionFont, maxWidth);
  88. }
  89. // set which lines ar edrawn
  90. startIndex = 0;
  91. endIndex = drawMaxLines;
  92. if (endIndex > dialogueList.Count)
  93. {
  94. dialogueStartPosition = new Vector2(271f,
  95. 375f - ((dialogueList.Count - startIndex) *
  96. (Fonts.DescriptionFont.LineSpacing) / 2));
  97. endIndex = dialogueList.Count;
  98. }
  99. else
  100. {
  101. dialogueStartPosition = new Vector2(271f, 225f);
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// The text shown next to the A button, if any.
  107. /// </summary>
  108. private string selectText = "Continue";
  109. /// <summary>
  110. /// The text shown next to the A button, if any.
  111. /// </summary>
  112. public string SelectText
  113. {
  114. get { return selectText; }
  115. set
  116. {
  117. if (selectText != value)
  118. {
  119. selectText = value;
  120. if (selectButtonTexture != null)
  121. {
  122. selectPosition.X = selectButtonPosition.X -
  123. Fonts.ButtonNamesFont.MeasureString(selectText).X - 10f;
  124. selectPosition.Y = selectButtonPosition.Y;
  125. }
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// The text shown next to the B button, if any.
  131. /// </summary>
  132. private string backText = "Back";
  133. /// <summary>
  134. /// The text shown next to the B button, if any.
  135. /// </summary>
  136. public string BackText
  137. {
  138. get { return backText; }
  139. set { backText = value; }
  140. }
  141. /// <summary>
  142. /// Maximum width of each line in pixels
  143. /// </summary>
  144. private const int maxWidth = 705;
  145. /// <summary>
  146. /// Starting index of the list to be displayed
  147. /// </summary>
  148. private int startIndex = 0;
  149. /// <summary>
  150. /// Ending index of the list to be displayed
  151. /// </summary>
  152. private int endIndex = drawMaxLines;
  153. /// <summary>
  154. /// Maximum number of lines to draw in the screen
  155. /// </summary>
  156. private const int drawMaxLines = 13;
  157. #endregion
  158. #region Initialization
  159. /// <summary>
  160. /// Construct a new DialogueScreen object.
  161. /// </summary>
  162. /// <param name="mapEntry"></param>
  163. public DialogueScreen()
  164. {
  165. this.IsPopup = true;
  166. }
  167. /// <summary>
  168. /// Load the graphics content
  169. /// </summary>
  170. /// <param name="batch">SpriteBatch object</param>
  171. /// <param name="screenWidth">Width of the screen</param>
  172. /// <param name="screenHeight">Height of the screen</param>
  173. public override void LoadContent()
  174. {
  175. ContentManager content = ScreenManager.Game.Content;
  176. fadeTexture =
  177. content.Load<Texture2D>(@"Textures\GameScreens\FadeScreen");
  178. backgroundTexture =
  179. content.Load<Texture2D>(@"Textures\GameScreens\PopupScreen");
  180. scrollTexture =
  181. content.Load<Texture2D>(@"Textures\GameScreens\ScrollButtons");
  182. selectButtonTexture = content.Load<Texture2D>(@"Textures\Buttons\AButton");
  183. backButtonTexture = content.Load<Texture2D>(@"Textures\Buttons\BButton");
  184. lineTexture =
  185. content.Load<Texture2D>(@"Textures\GameScreens\SeparationLine");
  186. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  187. backgroundPosition.X = (viewport.Width - backgroundTexture.Width) / 2;
  188. backgroundPosition.Y = (viewport.Height - backgroundTexture.Height) / 2;
  189. selectButtonPosition.X = viewport.Width / 2 + 260;
  190. selectButtonPosition.Y = backgroundPosition.Y + 530f;
  191. selectPosition.X = selectButtonPosition.X -
  192. Fonts.ButtonNamesFont.MeasureString(selectText).X - 10f;
  193. selectPosition.Y = selectButtonPosition.Y;
  194. backPosition.X = viewport.Width / 2 - 250f;
  195. backPosition.Y = backgroundPosition.Y + 530f;
  196. backButtonPosition.X = backPosition.X - backButtonTexture.Width - 10;
  197. backButtonPosition.Y = backPosition.Y;
  198. scrollPosition = backgroundPosition + new Vector2(820f, 200f);
  199. topLinePosition.X = (viewport.Width - lineTexture.Width) / 2 - 30f;
  200. topLinePosition.Y = 200f;
  201. bottomLinePosition.X = topLinePosition.X;
  202. bottomLinePosition.Y = 550f;
  203. titlePosition.X = (viewport.Width -
  204. Fonts.HeaderFont.MeasureString(titleText).X) / 2;
  205. titlePosition.Y = backgroundPosition.Y + 70f;
  206. }
  207. #endregion
  208. #region Updating
  209. /// <summary>
  210. /// Handles user input to the dialog.
  211. /// </summary>
  212. public override void HandleInput()
  213. {
  214. // Press Select or Bback
  215. if (InputManager.IsActionTriggered(InputManager.Action.Ok) ||
  216. InputManager.IsActionTriggered(InputManager.Action.Back))
  217. {
  218. ExitScreen();
  219. return;
  220. }
  221. // Scroll up
  222. if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
  223. {
  224. if (startIndex > 0)
  225. {
  226. startIndex--;
  227. endIndex--;
  228. }
  229. }
  230. // Scroll down
  231. else if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
  232. {
  233. if (startIndex < dialogueList.Count - drawMaxLines)
  234. {
  235. endIndex++;
  236. startIndex++;
  237. }
  238. }
  239. }
  240. #endregion
  241. #region Drawing
  242. /// <summary>
  243. /// draws the dialog.
  244. /// </summary>
  245. public override void Draw(GameTime gameTime)
  246. {
  247. Vector2 textPosition = dialogueStartPosition;
  248. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  249. spriteBatch.Begin();
  250. // draw the fading screen
  251. spriteBatch.Draw(fadeTexture, new Rectangle(0, 0, 1280, 720), Color.White);
  252. // draw popup background
  253. spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
  254. // draw the top line
  255. spriteBatch.Draw(lineTexture, topLinePosition, Color.White);
  256. // draw the bottom line
  257. spriteBatch.Draw(lineTexture, bottomLinePosition, Color.White);
  258. // draw scrollbar
  259. spriteBatch.Draw(scrollTexture, scrollPosition, Color.White);
  260. // draw title
  261. spriteBatch.DrawString(Fonts.HeaderFont, titleText, titlePosition,
  262. Fonts.CountColor);
  263. // draw the dialogue
  264. for (int i = startIndex; i < endIndex; i++)
  265. {
  266. spriteBatch.DrawString(Fonts.DescriptionFont, dialogueList[i],
  267. textPosition, Fonts.CountColor);
  268. textPosition.Y += Fonts.DescriptionFont.LineSpacing;
  269. }
  270. // draw the Back button and adjoining text
  271. if (!String.IsNullOrEmpty(backText))
  272. {
  273. spriteBatch.DrawString(Fonts.ButtonNamesFont, backText, backPosition,
  274. Color.White);
  275. spriteBatch.Draw(backButtonTexture, backButtonPosition, Color.White);
  276. }
  277. // draw the Select button and adjoining text
  278. if (!String.IsNullOrEmpty(selectText))
  279. {
  280. selectPosition.X = selectButtonPosition.X -
  281. Fonts.ButtonNamesFont.MeasureString(selectText).X - 10f;
  282. selectPosition.Y = selectButtonPosition.Y;
  283. spriteBatch.DrawString(Fonts.ButtonNamesFont, selectText, selectPosition,
  284. Color.White);
  285. spriteBatch.Draw(selectButtonTexture, selectButtonPosition, Color.White);
  286. }
  287. spriteBatch.End();
  288. }
  289. #endregion
  290. }
  291. }