StoreSellScreen.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. //-----------------------------------------------------------------------------
  2. // StoreSellScreen.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. using System.IO;
  15. namespace RolePlaying
  16. {
  17. /// <summary>
  18. /// Displays the gear in the party inventory and allows the user to sell them.
  19. /// </summary>
  20. class StoreSellScreen : InventoryScreen
  21. {
  22. /// <summary>
  23. /// The left-facing quantity arrow.
  24. /// </summary>
  25. private Texture2D leftQuantityArrow;
  26. /// <summary>
  27. /// The right-facing quantity arrow.
  28. /// </summary>
  29. private Texture2D rightQuantityArrow;
  30. private const int nameColumnInterval = 80;
  31. private const int powerColumnInterval = 270;
  32. private const int quantityColumnInterval = 340;
  33. private string priceColumnText = "Price";
  34. private const int priceColumnInterval = 120;
  35. /// <summary>
  36. /// The store whose goods are being displayed.
  37. /// </summary>
  38. private Store store;
  39. /// <summary>
  40. /// The selected quantity of the current entry.
  41. /// </summary>
  42. private int selectedQuantity = 0;
  43. /// <summary>
  44. /// Resets the selected quantity to the maximum value for the selected entry.
  45. /// </summary>
  46. private void ResetQuantities()
  47. {
  48. selectedQuantity = 1;
  49. }
  50. /// <summary>
  51. /// Move the current selection up one entry.
  52. /// </summary>
  53. protected override void MoveCursorUp()
  54. {
  55. base.MoveCursorUp();
  56. ResetQuantities();
  57. }
  58. /// <summary>
  59. /// Move the current selection down one entry.
  60. /// </summary>
  61. protected override void MoveCursorDown()
  62. {
  63. base.MoveCursorDown();
  64. ResetQuantities();
  65. }
  66. /// <summary>
  67. /// Decrease the selected quantity by one.
  68. /// </summary>
  69. protected override void MoveCursorLeft()
  70. {
  71. ReadOnlyCollection<ContentEntry<Gear>> entries = GetDataList();
  72. if (entries.Count > 0)
  73. {
  74. // loop to one if the selected quantity is already at maximum.
  75. if (selectedQuantity > 1)
  76. {
  77. selectedQuantity--;
  78. }
  79. else
  80. {
  81. selectedQuantity = entries[SelectedIndex].Count;
  82. }
  83. }
  84. else
  85. {
  86. selectedQuantity = 0;
  87. }
  88. }
  89. /// <summary>
  90. /// Increase the selected quantity by one.
  91. /// </summary>
  92. protected override void MoveCursorRight()
  93. {
  94. ReadOnlyCollection<ContentEntry<Gear>> entries = GetDataList();
  95. if (entries.Count > 0)
  96. {
  97. // loop to one if the selected quantity is already at maximum.
  98. selectedQuantity = selectedQuantity < entries[SelectedIndex].Count ?
  99. selectedQuantity + 1 : 1;
  100. }
  101. else
  102. {
  103. selectedQuantity = 0;
  104. }
  105. }
  106. /// <summary>
  107. /// Creates a new StoreSellScreen object for the given store.
  108. /// </summary>
  109. public StoreSellScreen(Store store)
  110. : base(true)
  111. {
  112. // check the parameter
  113. if ((store == null) || (store.StoreCategories.Count <= 0))
  114. {
  115. throw new ArgumentNullException("store");
  116. }
  117. this.store = store;
  118. // configure the menu text
  119. selectButtonText = "Sell";
  120. backButtonText = "Back";
  121. xButtonText = String.Empty;
  122. yButtonText = String.Empty;
  123. ResetQuantities();
  124. }
  125. /// <summary>
  126. /// Load the graphics content from the content manager.
  127. /// </summary>
  128. public override void LoadContent()
  129. {
  130. base.LoadContent();
  131. ContentManager content = ScreenManager.Game.Content;
  132. leftQuantityArrow =
  133. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "QuantityArrowLeft"));
  134. rightQuantityArrow =
  135. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "QuantityArrowRight"));
  136. }
  137. /// <summary>
  138. /// Respond to the triggering of the Select action.
  139. /// </summary>
  140. protected override void SelectTriggered(ContentEntry<Gear> entry)
  141. {
  142. // check the parameter
  143. if ((entry == null) || (entry.Content == null))
  144. {
  145. return;
  146. }
  147. // make sure the selected quantity is valid
  148. selectedQuantity = Math.Min(selectedQuantity, entry.Count);
  149. // add the gold to the party's inventory
  150. Session.Party.PartyGold += selectedQuantity *
  151. (int)Math.Ceiling(entry.Content.GoldValue * store.SellMultiplier);
  152. // remove the items from the party's inventory
  153. Session.Party.RemoveFromInventory(entry.Content, selectedQuantity);
  154. // reset the quantities - either gold has gone down or the total was bad
  155. ResetQuantities();
  156. }
  157. /// <summary>
  158. /// Switch to the previous store category.
  159. /// </summary>
  160. protected override void PageScreenLeft()
  161. {
  162. isItems = !isItems;
  163. ResetTriggerText();
  164. ResetQuantities();
  165. }
  166. /// <summary>
  167. /// Switch to the next store category.
  168. /// </summary>
  169. protected override void PageScreenRight()
  170. {
  171. isItems = !isItems;
  172. ResetTriggerText();
  173. ResetQuantities();
  174. }
  175. /// <summary>
  176. /// Reset the trigger button text to the names of the
  177. /// previous and next UI screens.
  178. /// </summary>
  179. protected override void ResetTriggerText()
  180. {
  181. leftTriggerText = rightTriggerText = isItems ? "Equipment" : "Inventory";
  182. }
  183. /// <summary>
  184. /// Draw the entry at the given position in the list.
  185. /// </summary>
  186. /// <param name="contentEntry">The entry to draw.</param>
  187. /// <param name="position">The position to draw the entry at.</param>
  188. /// <param name="isSelected">If true, this item is selected.</param>
  189. protected override void DrawEntry(ContentEntry<Gear> entry, Vector2 position,
  190. bool isSelected)
  191. {
  192. // check the parameter
  193. if ((entry == null) || (entry.Content == null))
  194. {
  195. throw new ArgumentNullException("entry");
  196. }
  197. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  198. Vector2 drawPosition = position;
  199. // draw the icon
  200. spriteBatch.Draw(entry.Content.IconTexture, drawPosition + iconOffset,
  201. Color.White);
  202. // draw the name
  203. Color color = isSelected ? Fonts.HighlightColor : Fonts.DisplayColor;
  204. drawPosition.Y += listLineSpacing / 4;
  205. drawPosition.X += nameColumnInterval;
  206. spriteBatch.DrawString(Fonts.GearInfoFont, entry.Content.Name,
  207. drawPosition, color);
  208. // draw the power
  209. drawPosition.X += powerColumnInterval;
  210. string powerText = entry.Content.GetPowerText();
  211. Vector2 powerTextSize = Fonts.GearInfoFont.MeasureString(powerText);
  212. Vector2 powerPosition = drawPosition;
  213. powerPosition.Y -= (float)Math.Ceiling((powerTextSize.Y - 30f) / 2);
  214. spriteBatch.DrawString(Fonts.GearInfoFont, powerText,
  215. powerPosition, color);
  216. // draw the quantity
  217. drawPosition.X += quantityColumnInterval;
  218. if (isSelected)
  219. {
  220. Vector2 quantityPosition = drawPosition;
  221. // draw the left selection arrow
  222. quantityPosition.X -= leftQuantityArrow.Width;
  223. spriteBatch.Draw(leftQuantityArrow,
  224. new Vector2(quantityPosition.X, quantityPosition.Y - 4),
  225. Color.White);
  226. quantityPosition.X += leftQuantityArrow.Width;
  227. // draw the selected quantity ratio
  228. string quantityText = selectedQuantity.ToString() + "/" +
  229. entry.Count.ToString();
  230. spriteBatch.DrawString(Fonts.GearInfoFont, quantityText,
  231. quantityPosition, color);
  232. quantityPosition.X +=
  233. Fonts.GearInfoFont.MeasureString(quantityText).X;
  234. // draw the right selection arrow
  235. spriteBatch.Draw(rightQuantityArrow,
  236. new Vector2(quantityPosition.X, quantityPosition.Y - 4),
  237. Color.White);
  238. quantityPosition.X += rightQuantityArrow.Width;
  239. // draw the purchase button
  240. selectButtonText = "Sell";
  241. }
  242. else
  243. {
  244. spriteBatch.DrawString(Fonts.GearInfoFont, entry.Count.ToString(),
  245. drawPosition, color);
  246. }
  247. // draw the price
  248. drawPosition.X += priceColumnInterval;
  249. string priceText = String.Empty;
  250. if (isSelected)
  251. {
  252. int totalPrice = selectedQuantity *
  253. (int)Math.Ceiling(entry.Content.GoldValue * store.SellMultiplier);
  254. priceText = totalPrice.ToString();
  255. }
  256. else
  257. {
  258. priceText = ((int)Math.Ceiling(entry.Content.GoldValue *
  259. store.SellMultiplier)).ToString();
  260. }
  261. spriteBatch.DrawString(Fonts.GearInfoFont, priceText,
  262. drawPosition, color);
  263. }
  264. /// <summary>
  265. /// Draw the description of the selected item.
  266. /// </summary>
  267. protected override void DrawSelectedDescription(ContentEntry<Gear> entry)
  268. {
  269. // check the parameter
  270. if ((entry == null) || (entry.Content == null))
  271. {
  272. throw new ArgumentNullException("entry");
  273. }
  274. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  275. Vector2 position = descriptionTextPosition;
  276. // draw the description
  277. // -- it's up to the content owner to fit the description
  278. string text = entry.Content.Description;
  279. if (!String.IsNullOrEmpty(text))
  280. {
  281. spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
  282. Fonts.DescriptionColor);
  283. position.Y += Fonts.DescriptionFont.LineSpacing;
  284. }
  285. // draw the modifiers
  286. Equipment equipment = entry.Content as Equipment;
  287. if (equipment != null)
  288. {
  289. text = equipment.OwnerBuffStatistics.GetModifierString();
  290. if (!String.IsNullOrEmpty(text))
  291. {
  292. spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
  293. Fonts.DescriptionColor);
  294. position.Y += Fonts.DescriptionFont.LineSpacing;
  295. }
  296. }
  297. // draw the restrictions
  298. text = entry.Content.GetRestrictionsText();
  299. if (!String.IsNullOrEmpty(text))
  300. {
  301. spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
  302. Fonts.DescriptionColor);
  303. position.Y += Fonts.DescriptionFont.LineSpacing;
  304. }
  305. }
  306. /// <summary>
  307. /// Draw the column headers above the list.
  308. /// </summary>
  309. protected override void DrawColumnHeaders()
  310. {
  311. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  312. Vector2 position = listEntryStartPosition;
  313. position.X += nameColumnInterval;
  314. if (!String.IsNullOrEmpty(nameColumnText))
  315. {
  316. spriteBatch.DrawString(Fonts.CaptionFont, nameColumnText, position,
  317. Fonts.CaptionColor);
  318. }
  319. position.X += powerColumnInterval;
  320. if (!String.IsNullOrEmpty(powerColumnText))
  321. {
  322. spriteBatch.DrawString(Fonts.CaptionFont, powerColumnText, position,
  323. Fonts.CaptionColor);
  324. }
  325. position.X += quantityColumnInterval;
  326. if (!String.IsNullOrEmpty(quantityColumnText))
  327. {
  328. spriteBatch.DrawString(Fonts.CaptionFont, quantityColumnText, position,
  329. Fonts.CaptionColor);
  330. }
  331. position.X += priceColumnInterval;
  332. if (!String.IsNullOrEmpty(priceColumnText))
  333. {
  334. spriteBatch.DrawString(Fonts.CaptionFont, priceColumnText, position,
  335. Fonts.CaptionColor);
  336. }
  337. }
  338. }
  339. }