InventoryScreen.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. leftTriggerText = "Statistics";
  204. rightTriggerText = "Equipment";
  205. }
  206. else
  207. {
  208. leftTriggerText = "Inventory";
  209. rightTriggerText = "Quests";
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// Draw the gear's content entry at the given position in the list.
  215. /// </summary>
  216. /// <param name="contentEntry">The content entry to draw.</param>
  217. /// <param name="position">The position to draw the entry at.</param>
  218. /// <param name="isSelected">If true, this item is selected.</param>
  219. protected override void DrawEntry(ContentEntry<Gear> entry, Vector2 position,
  220. bool isSelected)
  221. {
  222. // check the parameter
  223. if (entry == null)
  224. {
  225. throw new ArgumentNullException("entry");
  226. }
  227. Gear gear = entry.Content as Gear;
  228. if (gear == null)
  229. {
  230. return;
  231. }
  232. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  233. Vector2 drawPosition = position;
  234. // draw the icon
  235. spriteBatch.Draw(gear.IconTexture, drawPosition + iconOffset, Color.White);
  236. // draw the name
  237. Color color = isSelected ? Fonts.HighlightColor : Fonts.DisplayColor;
  238. drawPosition.Y += listLineSpacing / 4;
  239. drawPosition.X += nameColumnInterval;
  240. spriteBatch.DrawString(Fonts.GearInfoFont, gear.Name, drawPosition, color);
  241. // draw the power
  242. drawPosition.X += powerColumnInterval;
  243. string powerText = gear.GetPowerText();
  244. Vector2 powerTextSize = Fonts.GearInfoFont.MeasureString(powerText);
  245. Vector2 powerPosition = drawPosition;
  246. powerPosition.Y -= (float)Math.Ceiling((powerTextSize.Y - 30f) / 2);
  247. spriteBatch.DrawString(Fonts.GearInfoFont, powerText,
  248. powerPosition, color);
  249. // draw the quantity
  250. drawPosition.X += quantityColumnInterval;
  251. spriteBatch.DrawString(Fonts.GearInfoFont, entry.Count.ToString(),
  252. drawPosition, color);
  253. // turn on or off the select and drop buttons
  254. if (isSelected)
  255. {
  256. selectButtonText = "Select";
  257. xButtonText = entry.Content.IsDroppable ? "Drop" : String.Empty;
  258. }
  259. }
  260. /// <summary>
  261. /// Draw the description of the selected item.
  262. /// </summary>
  263. protected override void DrawSelectedDescription(ContentEntry<Gear> entry)
  264. {
  265. // check the parameter
  266. if (entry == null)
  267. {
  268. throw new ArgumentNullException("entry");
  269. }
  270. Gear gear = entry.Content as Gear;
  271. if (gear == null)
  272. {
  273. return;
  274. }
  275. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  276. Vector2 position = descriptionTextPosition;
  277. // draw the description
  278. // -- it's up to the content owner to fit the description
  279. string text = gear.Description;
  280. if (!String.IsNullOrEmpty(text))
  281. {
  282. spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
  283. Fonts.DescriptionColor);
  284. position.Y += Fonts.DescriptionFont.LineSpacing;
  285. }
  286. // draw additional information for equipment
  287. Equipment equipment = entry.Content as Equipment;
  288. if (equipment != null)
  289. {
  290. // draw the modifiers
  291. text = equipment.OwnerBuffStatistics.GetModifierString();
  292. if (!String.IsNullOrEmpty(text))
  293. {
  294. spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
  295. Fonts.DescriptionColor);
  296. position.Y += Fonts.DescriptionFont.LineSpacing;
  297. }
  298. }
  299. // draw the restrictions
  300. text = entry.Content.GetRestrictionsText();
  301. if (!String.IsNullOrEmpty(text))
  302. {
  303. spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
  304. Fonts.DescriptionColor);
  305. position.Y += Fonts.DescriptionFont.LineSpacing;
  306. }
  307. }
  308. /// <summary>
  309. /// Draw the column headers above the gear list.
  310. /// </summary>
  311. protected override void DrawColumnHeaders()
  312. {
  313. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  314. Vector2 position = listEntryStartPosition;
  315. position.X += nameColumnInterval;
  316. if (!String.IsNullOrEmpty(nameColumnText))
  317. {
  318. spriteBatch.DrawString(Fonts.CaptionFont, nameColumnText, position,
  319. Fonts.CaptionColor);
  320. }
  321. position.X += powerColumnInterval;
  322. if (!String.IsNullOrEmpty(powerColumnText))
  323. {
  324. spriteBatch.DrawString(Fonts.CaptionFont, powerColumnText, position,
  325. Fonts.CaptionColor);
  326. }
  327. position.X += quantityColumnInterval;
  328. if (!String.IsNullOrEmpty(quantityColumnText))
  329. {
  330. spriteBatch.DrawString(Fonts.CaptionFont, quantityColumnText, position,
  331. Fonts.CaptionColor);
  332. }
  333. }
  334. }
  335. }