DialogueScreen.cs 11 KB

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