InventoryScreen.cs 13 KB

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