StoreSellScreen.cs 13 KB

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