DialogueScreen.cs 11 KB

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