StoreSellScreen.cs 14 KB

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