InventoryScreen.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // InventoryScreen.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 System.Collections.ObjectModel;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Content;
  16. using RolePlayingGameData;
  17. #endregion
  18. namespace RolePlaying
  19. {
  20. /// <summary>
  21. /// Displays the inventory of the party, either showing items or equipment.
  22. /// </summary>
  23. class InventoryScreen : ListScreen<ContentEntry<Gear>>
  24. {
  25. #region Columns
  26. protected string nameColumnText = "Name";
  27. private const int nameColumnInterval = 80;
  28. protected string powerColumnText = "Power (min, max)";
  29. private const int powerColumnInterval = 270;
  30. protected string quantityColumnText = "Qty";
  31. private const int quantityColumnInterval = 450;
  32. #endregion
  33. #region Data Access
  34. /// <summary>
  35. /// If true, the menu is only displaying items; otherwise, only equipment.
  36. /// </summary>
  37. protected bool isItems;
  38. /// <summary>
  39. /// Retrieve the list of gear shown in this menu.
  40. /// </summary>
  41. /// <returns></returns>
  42. public override ReadOnlyCollection<ContentEntry<Gear>> GetDataList()
  43. {
  44. List<ContentEntry<Gear>> dataList = new List<ContentEntry<Gear>>();
  45. ReadOnlyCollection<ContentEntry<Gear>> inventory = Session.Party.Inventory;
  46. // build a new list of only the desired gear
  47. foreach (ContentEntry<Gear> gearEntry in inventory)
  48. {
  49. if (isItems)
  50. {
  51. if (gearEntry.Content is Item)
  52. {
  53. dataList.Add(gearEntry);
  54. }
  55. }
  56. else
  57. {
  58. if (gearEntry.Content is Equipment)
  59. {
  60. dataList.Add(gearEntry);
  61. }
  62. }
  63. }
  64. // sort the list by name
  65. dataList.Sort(
  66. delegate(ContentEntry<Gear> gearEntry1, ContentEntry<Gear> gearEntry2)
  67. {
  68. // handle null values
  69. if ((gearEntry1 == null) || (gearEntry1.Content == null))
  70. {
  71. return ((gearEntry2 == null) || (gearEntry2.Content == null) ?
  72. 0 : 1);
  73. }
  74. else if ((gearEntry2 == null) || (gearEntry2.Content == null))
  75. {
  76. return -1;
  77. }
  78. // sort by name
  79. return gearEntry1.Content.Name.CompareTo(gearEntry2.Content.Name);
  80. });
  81. return dataList.AsReadOnly();
  82. }
  83. #endregion
  84. #region Initialization
  85. /// <summary>
  86. /// Constructs a new InventoryScreen object.
  87. /// </summary>
  88. public InventoryScreen(bool isItems)
  89. : base()
  90. {
  91. this.isItems = isItems;
  92. // configure the menu text
  93. titleText = "Inventory";
  94. selectButtonText = "Select";
  95. backButtonText = "Back";
  96. xButtonText = "Drop";
  97. yButtonText = String.Empty;
  98. ResetTriggerText();
  99. }
  100. #endregion
  101. #region Input Handling
  102. /// <summary>
  103. /// Delegate for item-selection events.
  104. /// </summary>
  105. public delegate void GearSelectedHandler(Gear gear);
  106. /// <summary>
  107. /// Responds when an item is selected by this menu.
  108. /// </summary>
  109. /// <remarks>
  110. /// Typically used by the calling menu, like the combat HUD menu,
  111. /// to respond to selection.
  112. /// </remarks>
  113. public event GearSelectedHandler GearSelected;
  114. /// <summary>
  115. /// Respond to the triggering of the Select action (and related key).
  116. /// </summary>
  117. protected override void SelectTriggered(ContentEntry<Gear> entry)
  118. {
  119. // check the parameter
  120. if ((entry == null) || (entry.Content == null))
  121. {
  122. return;
  123. }
  124. // if the event is valid, fire it and exit this screen
  125. if (GearSelected != null)
  126. {
  127. GearSelected(entry.Content);
  128. ExitScreen();
  129. return;
  130. }
  131. // otherwise, open the selection screen over this screen
  132. ScreenManager.AddScreen(new PlayerSelectionScreen(entry.Content));
  133. }
  134. /// <summary>
  135. /// Respond to the triggering of the X button (and related key).
  136. /// </summary>
  137. protected override void ButtonXPressed(ContentEntry<Gear> entry)
  138. {
  139. // check the parameter
  140. if ((entry == null) || (entry.Content == null))
  141. {
  142. return;
  143. }
  144. // check whether the gear could be dropped
  145. if (!entry.Content.IsDroppable)
  146. {
  147. return;
  148. }
  149. // add a message box confirming the drop
  150. MessageBoxScreen dropEquipmentConfirmationScreen =
  151. new MessageBoxScreen("Are you sure you want to drop the " +
  152. entry.Content.Name + "?");
  153. dropEquipmentConfirmationScreen.Accepted +=
  154. new EventHandler<EventArgs>(delegate(object sender, EventArgs args)
  155. {
  156. Session.Party.RemoveFromInventory(entry.Content, 1);
  157. });
  158. ScreenManager.AddScreen(dropEquipmentConfirmationScreen);
  159. }
  160. /// <summary>
  161. /// Switch to the screen to the "left" of this one in the UI.
  162. /// </summary>
  163. protected override void PageScreenLeft()
  164. {
  165. if (CombatEngine.IsActive)
  166. {
  167. return;
  168. }
  169. if (isItems)
  170. {
  171. ExitScreen();
  172. ScreenManager.AddScreen(new StatisticsScreen(Session.Party.Players[0]));
  173. }
  174. else
  175. {
  176. isItems = !isItems;
  177. ResetTriggerText();
  178. }
  179. }
  180. /// <summary>
  181. /// Switch to the screen to the "right" of this one in the UI.
  182. /// </summary>
  183. protected override void PageScreenRight()
  184. {
  185. if (CombatEngine.IsActive)
  186. {
  187. return;
  188. }
  189. if (isItems)
  190. {
  191. isItems = !isItems;
  192. ResetTriggerText();
  193. }
  194. else
  195. {
  196. ExitScreen();
  197. ScreenManager.AddScreen(new QuestLogScreen(null));
  198. }
  199. }
  200. /// <summary>
  201. /// Reset the trigger button text to the names of the
  202. /// previous and next UI screens.
  203. /// </summary>
  204. protected virtual void ResetTriggerText()
  205. {
  206. if (CombatEngine.IsActive)
  207. {
  208. leftTriggerText = rightTriggerText = String.Empty;
  209. }
  210. else
  211. {
  212. if (isItems)
  213. {
  214. leftTriggerText = "Statistics";
  215. rightTriggerText = "Equipment";
  216. }
  217. else
  218. {
  219. leftTriggerText = "Items";
  220. rightTriggerText = "Quests";
  221. }
  222. }
  223. }
  224. #endregion
  225. #region Drawing
  226. /// <summary>
  227. /// Draw the gear's content entry at the given position in the list.
  228. /// </summary>
  229. /// <param name="contentEntry">The content entry to draw.</param>
  230. /// <param name="position">The position to draw the entry at.</param>
  231. /// <param name="isSelected">If true, this item is selected.</param>
  232. protected override void DrawEntry(ContentEntry<Gear> entry, Vector2 position,
  233. bool isSelected)
  234. {
  235. // check the parameter
  236. if (entry == null)
  237. {
  238. throw new ArgumentNullException("entry");
  239. }
  240. Gear gear = entry.Content as Gear;
  241. if (gear == null)
  242. {
  243. return;
  244. }
  245. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  246. Vector2 drawPosition = position;
  247. // draw the icon
  248. spriteBatch.Draw(gear.IconTexture, drawPosition + iconOffset, Color.White);
  249. // draw the name
  250. Color color = isSelected ? Fonts.HighlightColor : Fonts.DisplayColor;
  251. drawPosition.Y += listLineSpacing / 4;
  252. drawPosition.X += nameColumnInterval;
  253. spriteBatch.DrawString(Fonts.GearInfoFont, gear.Name, drawPosition, color);
  254. // draw the power
  255. drawPosition.X += powerColumnInterval;
  256. string powerText = gear.GetPowerText();
  257. Vector2 powerTextSize = Fonts.GearInfoFont.MeasureString(powerText);
  258. Vector2 powerPosition = drawPosition;
  259. powerPosition.Y -= (float)Math.Ceiling((powerTextSize.Y - 30f) / 2);
  260. spriteBatch.DrawString(Fonts.GearInfoFont, powerText,
  261. powerPosition, color);
  262. // draw the quantity
  263. drawPosition.X += quantityColumnInterval;
  264. spriteBatch.DrawString(Fonts.GearInfoFont, entry.Count.ToString(),
  265. drawPosition, color);
  266. // turn on or off the select and drop buttons
  267. if (isSelected)
  268. {
  269. selectButtonText = "Select";
  270. xButtonText = entry.Content.IsDroppable ? "Drop" : String.Empty;
  271. }
  272. }
  273. /// <summary>
  274. /// Draw the description of the selected item.
  275. /// </summary>
  276. protected override void DrawSelectedDescription(ContentEntry<Gear> entry)
  277. {
  278. // check the parameter
  279. if (entry == null)
  280. {
  281. throw new ArgumentNullException("entry");
  282. }
  283. Gear gear = entry.Content as Gear;
  284. if (gear == null)
  285. {
  286. return;
  287. }
  288. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  289. Vector2 position = descriptionTextPosition;
  290. // draw the description
  291. // -- it's up to the content owner to fit the description
  292. string text = gear.Description;
  293. if (!String.IsNullOrEmpty(text))
  294. {
  295. spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
  296. Fonts.DescriptionColor);
  297. position.Y += Fonts.DescriptionFont.LineSpacing;
  298. }
  299. // draw additional information for equipment
  300. Equipment equipment = entry.Content as Equipment;
  301. if (equipment != null)
  302. {
  303. // draw the modifiers
  304. text = equipment.OwnerBuffStatistics.GetModifierString();
  305. if (!String.IsNullOrEmpty(text))
  306. {
  307. spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
  308. Fonts.DescriptionColor);
  309. position.Y += Fonts.DescriptionFont.LineSpacing;
  310. }
  311. }
  312. // draw the restrictions
  313. text = entry.Content.GetRestrictionsText();
  314. if (!String.IsNullOrEmpty(text))
  315. {
  316. spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
  317. Fonts.DescriptionColor);
  318. position.Y += Fonts.DescriptionFont.LineSpacing;
  319. }
  320. }
  321. /// <summary>
  322. /// Draw the column headers above the gear list.
  323. /// </summary>
  324. protected override void DrawColumnHeaders()
  325. {
  326. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  327. Vector2 position = listEntryStartPosition;
  328. position.X += nameColumnInterval;
  329. if (!String.IsNullOrEmpty(nameColumnText))
  330. {
  331. spriteBatch.DrawString(Fonts.CaptionFont, nameColumnText, position,
  332. Fonts.CaptionColor);
  333. }
  334. position.X += powerColumnInterval;
  335. if (!String.IsNullOrEmpty(powerColumnText))
  336. {
  337. spriteBatch.DrawString(Fonts.CaptionFont, powerColumnText, position,
  338. Fonts.CaptionColor);
  339. }
  340. position.X += quantityColumnInterval;
  341. if (!String.IsNullOrEmpty(quantityColumnText))
  342. {
  343. spriteBatch.DrawString(Fonts.CaptionFont, quantityColumnText, position,
  344. Fonts.CaptionColor);
  345. }
  346. }
  347. #endregion
  348. }
  349. }