ChestScreen.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. //-----------------------------------------------------------------------------
  2. // ChestScreen.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. class ChestScreen : InventoryScreen
  17. {
  18. /// <summary>
  19. /// The left-facing quantity arrow.
  20. /// </summary>
  21. private Texture2D leftQuantityArrow;
  22. /// <summary>
  23. /// The right-facing quantity arrow.
  24. /// </summary>
  25. private Texture2D rightQuantityArrow;
  26. private const int nameColumnInterval = 80;
  27. private const int powerColumnInterval = 270;
  28. private const int quantityColumnInterval = 450;
  29. /// <summary>
  30. /// The chest entry that is displayed here.
  31. /// </summary>
  32. private MapEntry<Chest> chestEntry;
  33. /// <summary>
  34. /// Retrieve the list of gear shown in this menu.
  35. /// </summary>
  36. /// <returns></returns>
  37. public override ReadOnlyCollection<ContentEntry<Gear>> GetDataList()
  38. {
  39. if ((chestEntry == null) || (chestEntry.Content == null))
  40. {
  41. return null;
  42. }
  43. return chestEntry.Content.Entries.AsReadOnly();
  44. }
  45. /// <summary>
  46. /// The selected quantity of the current entry.
  47. /// </summary>
  48. private int selectedQuantity = 0;
  49. /// <summary>
  50. /// Resets the selected quantity to the maximum value for the selected entry.
  51. /// </summary>
  52. private void ResetSelectedQuantity()
  53. {
  54. // safety-check on the chest
  55. if ((chestEntry == null) || (chestEntry.Content == null))
  56. {
  57. selectedQuantity = 0;
  58. return;
  59. }
  60. // set the quantity to the maximum
  61. if (IsGoldSelected)
  62. {
  63. selectedQuantity = chestEntry.Content.Gold;
  64. }
  65. else if ((SelectedGearIndex >= 0) &&
  66. (SelectedGearIndex < chestEntry.Content.Entries.Count))
  67. {
  68. selectedQuantity = chestEntry.Content[SelectedGearIndex].Count;
  69. }
  70. }
  71. /// <summary>
  72. /// If true, the phantom ContentEntry for the chest's gold is selected
  73. /// </summary>
  74. public bool IsGoldSelected
  75. {
  76. get
  77. {
  78. return ((chestEntry != null) && (chestEntry.Content != null) &&
  79. (chestEntry.Content.Gold > 0) && (SelectedIndex == 0));
  80. }
  81. }
  82. /// <summary>
  83. /// Retrieve the zero-based selection of the gear in the chestEntry.Content.
  84. /// </summary>
  85. /// <remarks>
  86. /// If there is gold in the chestEntry.Content, its phantom ContentEntry shifts
  87. /// ListScreen.SelectedIndex by one. If IsGoldSelected is true, this property
  88. /// return -1.
  89. /// </remarks>
  90. public int SelectedGearIndex
  91. {
  92. get
  93. {
  94. return ((chestEntry != null) && (chestEntry.Content != null) &&
  95. (chestEntry.Content.Gold > 0)) ? SelectedIndex - 1 : SelectedIndex;
  96. }
  97. }
  98. /// <summary>
  99. /// Move the current selection up one entry.
  100. /// </summary>
  101. protected override void MoveCursorUp()
  102. {
  103. base.MoveCursorUp();
  104. ResetSelectedQuantity();
  105. }
  106. /// <summary>
  107. /// Move the current selection down one entry.
  108. /// </summary>
  109. protected override void MoveCursorDown()
  110. {
  111. base.MoveCursorDown();
  112. ResetSelectedQuantity();
  113. }
  114. /// <summary>
  115. /// Decrease the selected quantity by one.
  116. /// </summary>
  117. protected override void MoveCursorLeft()
  118. {
  119. // decrement the quantity, looping around if necessary
  120. if (selectedQuantity > 0)
  121. {
  122. selectedQuantity--;
  123. }
  124. else if (IsGoldSelected)
  125. {
  126. selectedQuantity = chestEntry.Content.Gold;
  127. }
  128. else if (SelectedGearIndex < chestEntry.Content.Entries.Count)
  129. {
  130. selectedQuantity = chestEntry.Content[SelectedGearIndex].Count;
  131. }
  132. }
  133. /// <summary>
  134. /// Increase the selected quantity by one.
  135. /// </summary>
  136. protected override void MoveCursorRight()
  137. {
  138. int maximumQuantity = 0;
  139. // get the maximum quantity for the selected entry
  140. if (IsGoldSelected)
  141. {
  142. maximumQuantity = chestEntry.Content.Gold;
  143. }
  144. else if (SelectedGearIndex < chestEntry.Content.Entries.Count)
  145. {
  146. maximumQuantity = chestEntry.Content[SelectedGearIndex].Count;
  147. }
  148. else
  149. {
  150. return;
  151. }
  152. // loop to zero if the selected quantity is already at maximum.
  153. selectedQuantity = selectedQuantity < maximumQuantity ?
  154. selectedQuantity + 1 : 0;
  155. }
  156. /// <summary>
  157. /// Creates a new ChestScreen object.
  158. /// </summary>
  159. /// <param name="chestEntry">The chest entry shown in the screen</param>
  160. public ChestScreen(MapEntry<Chest> chestEntry)
  161. : base(true)
  162. {
  163. // check the parameter
  164. if ((chestEntry == null) || (chestEntry.Content == null))
  165. {
  166. throw new ArgumentNullException("chestEntry.Content");
  167. }
  168. this.chestEntry = chestEntry;
  169. // clean up any empty entries
  170. this.chestEntry.Content.Entries.RemoveAll(
  171. delegate(ContentEntry<Gear> contentEntry)
  172. {
  173. return (contentEntry.Count <= 0);
  174. });
  175. // sort the chest entries by name
  176. this.chestEntry.Content.Entries.Sort(
  177. delegate(ContentEntry<Gear> gearEntry1, ContentEntry<Gear> gearEntry2)
  178. {
  179. // handle null values
  180. if ((gearEntry1 == null) || (gearEntry1.Content == null))
  181. {
  182. return ((gearEntry2 == null) || (gearEntry2.Content == null) ?
  183. 0 : 1);
  184. }
  185. else if ((gearEntry2 == null) || (gearEntry2.Content == null))
  186. {
  187. return -1;
  188. }
  189. // sort by name
  190. return gearEntry1.Content.Name.CompareTo(gearEntry2.Content.Name);
  191. });
  192. // set up the initial selected-quantity values
  193. ResetSelectedQuantity();
  194. // configure the menu strings
  195. titleText = chestEntry.Content.Name;
  196. selectButtonText = "Take";
  197. backButtonText = "Close";
  198. xButtonText = String.Empty;
  199. yButtonText = "Take All";
  200. leftTriggerText = String.Empty;
  201. rightTriggerText = String.Empty;
  202. }
  203. /// <summary>
  204. /// Load the graphics content from the content manager.
  205. /// </summary>
  206. public override void LoadContent()
  207. {
  208. base.LoadContent();
  209. ContentManager content = ScreenManager.Game.Content;
  210. leftQuantityArrow =
  211. content.Load<Texture2D>(@"Textures\Buttons\QuantityArrowLeft");
  212. rightQuantityArrow =
  213. content.Load<Texture2D>(@"Textures\Buttons\QuantityArrowRight");
  214. }
  215. /// <summary>
  216. /// Allows the screen to handle user input.
  217. /// </summary>
  218. public override void HandleInput()
  219. {
  220. // if the chestEntry.Content is empty, exit immediately
  221. if (chestEntry.Content.IsEmpty)
  222. {
  223. BackTriggered();
  224. return;
  225. }
  226. if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
  227. {
  228. MoveCursorUp();
  229. }
  230. else if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
  231. {
  232. MoveCursorDown();
  233. }
  234. else if (InputManager.IsActionTriggered(InputManager.Action.IncreaseAmount))
  235. {
  236. MoveCursorRight();
  237. }
  238. else if (InputManager.IsActionTriggered(InputManager.Action.DecreaseAmount))
  239. {
  240. MoveCursorLeft();
  241. }
  242. // Close is pressed
  243. else if (InputManager.IsActionTriggered(InputManager.Action.Back))
  244. {
  245. BackTriggered();
  246. }
  247. // Take is pressed
  248. else if (InputManager.IsActionTriggered(InputManager.Action.Ok))
  249. {
  250. if (IsGoldSelected)
  251. {
  252. SelectTriggered(null);
  253. }
  254. else
  255. {
  256. ReadOnlyCollection<ContentEntry<Gear>> dataList = GetDataList();
  257. SelectTriggered(dataList[SelectedGearIndex]);
  258. }
  259. }
  260. // Take All is pressed
  261. else if (InputManager.IsActionTriggered(InputManager.Action.TakeView))
  262. {
  263. ButtonYPressed(null); // take-all doesn't need an entry
  264. }
  265. }
  266. /// <summary>
  267. /// Respond to the triggering of the Back action.
  268. /// </summary>
  269. protected override void BackTriggered()
  270. {
  271. // clean up any empty entries
  272. chestEntry.Content.Entries.RemoveAll(
  273. delegate(ContentEntry<Gear> contentEntry)
  274. {
  275. return (contentEntry.Count <= 0);
  276. });
  277. // if the chestEntry.Content is empty, remove it from the game and exit
  278. if (chestEntry.Content.IsEmpty)
  279. {
  280. Session.RemoveChest(chestEntry);
  281. }
  282. else
  283. {
  284. // otherwise, store the modified chestEntry.Content
  285. Session.StoreModifiedChest(chestEntry);
  286. }
  287. // exit the screen
  288. base.BackTriggered();
  289. }
  290. /// <summary>
  291. /// Respond to the triggering of the Select action.
  292. /// </summary>
  293. protected override void SelectTriggered(ContentEntry<Gear> entry)
  294. {
  295. // if the quantity is zero, don't bother
  296. if (selectedQuantity <= 0)
  297. {
  298. return;
  299. }
  300. // check to see if gold is selected
  301. if (IsGoldSelected)
  302. {
  303. // play the "pick up gold" cue
  304. AudioManager.PlayCue("Money");
  305. // add the gold to the party
  306. Session.Party.PartyGold += selectedQuantity;
  307. chestEntry.Content.Gold -= selectedQuantity;
  308. if (chestEntry.Content.Gold > 0)
  309. {
  310. selectedQuantity =
  311. Math.Min(selectedQuantity, chestEntry.Content.Gold);
  312. }
  313. else
  314. {
  315. ResetSelectedQuantity();
  316. }
  317. }
  318. else
  319. {
  320. // remove the selected quantity of gear from the chest
  321. int quantity = selectedQuantity;
  322. if ((entry.Content != null) && (quantity > 0))
  323. {
  324. Session.Party.AddToInventory(entry.Content, quantity);
  325. entry.Count -= quantity;
  326. }
  327. if (entry.Count > 0)
  328. {
  329. selectedQuantity = Math.Min(entry.Count, selectedQuantity);
  330. }
  331. else
  332. {
  333. // if the entry is now empty, remove it from the chest
  334. chestEntry.Content.Entries.RemoveAt(SelectedGearIndex);
  335. ResetSelectedQuantity();
  336. }
  337. }
  338. }
  339. /// <summary>
  340. /// Respond to the triggering of the Y button (and related key).
  341. /// </summary>
  342. protected override void ButtonYPressed(ContentEntry<Gear> entry)
  343. {
  344. // add the entire amount of gold
  345. if (chestEntry.Content.Gold > 0)
  346. {
  347. AudioManager.PlayCue("Money");
  348. Session.Party.PartyGold += chestEntry.Content.Gold;
  349. chestEntry.Content.Gold = 0;
  350. }
  351. // add all items at full quantity
  352. // -- there is no limit to the party's inventory
  353. ReadOnlyCollection<ContentEntry<Gear>> entries = GetDataList();
  354. foreach (ContentEntry<Gear> gearEntry in entries)
  355. {
  356. Session.Party.AddToInventory(gearEntry.Content, gearEntry.Count);
  357. }
  358. // clear the entries, as they're all gone now
  359. chestEntry.Content.Entries.Clear();
  360. selectedQuantity = 0;
  361. }
  362. /// <summary>
  363. /// Draws the screen.
  364. /// </summary>
  365. public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
  366. {
  367. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  368. // get the content list
  369. ReadOnlyCollection<ContentEntry<Gear>> dataList = GetDataList();
  370. // fix the indices for the current list size
  371. int maximumCount = chestEntry.Content.Gold > 0 ? dataList.Count + 1 :
  372. dataList.Count;
  373. SelectedIndex = (int)MathHelper.Clamp(SelectedIndex, 0, maximumCount - 1);
  374. StartIndex = (int)MathHelper.Clamp(StartIndex, 0,
  375. maximumCount - MaximumListEntries);
  376. EndIndex = Math.Min(StartIndex + MaximumListEntries, maximumCount);
  377. spriteBatch.Begin();
  378. DrawBackground();
  379. if (dataList.Count > 0)
  380. {
  381. DrawListPosition(SelectedIndex + 1, maximumCount);
  382. }
  383. DrawButtons();
  384. DrawPartyGold();
  385. DrawColumnHeaders();
  386. DrawTitle();
  387. // draw each item currently shown
  388. Vector2 position = listEntryStartPosition +
  389. new Vector2(0f, listLineSpacing / 2);
  390. for (int index = StartIndex; index < EndIndex; index++)
  391. {
  392. if ((index == 0) && (chestEntry.Content.Gold > 0))
  393. {
  394. if (index == SelectedIndex)
  395. {
  396. DrawSelection(position);
  397. DrawGoldEntry(position, true);
  398. }
  399. else
  400. {
  401. DrawGoldEntry(position, false);
  402. }
  403. }
  404. else
  405. {
  406. int currentIndex = chestEntry.Content.Gold > 0 ? index - 1 : index;
  407. ContentEntry<Gear> entry = dataList[currentIndex];
  408. if (index == SelectedIndex)
  409. {
  410. DrawSelection(position);
  411. DrawEntry(entry, position, true);
  412. DrawSelectedDescription(entry);
  413. }
  414. else
  415. {
  416. DrawEntry(entry, position, false);
  417. }
  418. }
  419. position.Y += listLineSpacing;
  420. }
  421. spriteBatch.End();
  422. }
  423. /// <summary>
  424. /// Draw the chest's gold at the given position in the list.
  425. /// </summary>
  426. /// <param name="position">The position to draw the gold at.</param>
  427. /// <param name="isSelected">If true, the gold is selected.</param>
  428. protected virtual void DrawGoldEntry(Vector2 position, bool isSelected)
  429. {
  430. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  431. Vector2 drawPosition = position;
  432. // draw the icon
  433. spriteBatch.Draw(goldTexture,
  434. drawPosition + iconOffset + new Vector2(0f, 7f), Color.White);
  435. // draw the name
  436. Color color = isSelected ? Fonts.HighlightColor : Fonts.DisplayColor;
  437. drawPosition.Y += listLineSpacing / 4;
  438. drawPosition.X += nameColumnInterval;
  439. spriteBatch.DrawString(Fonts.GearInfoFont, "Gold", drawPosition, color);
  440. // skip the power text
  441. drawPosition.X += powerColumnInterval;
  442. // draw the quantity
  443. drawPosition.X += quantityColumnInterval;
  444. if (isSelected)
  445. {
  446. // draw the left selection arrow
  447. drawPosition.X -= leftQuantityArrow.Width;
  448. spriteBatch.Draw(leftQuantityArrow,
  449. new Vector2(drawPosition.X, drawPosition.Y - 4), Color.White);
  450. drawPosition.X += leftQuantityArrow.Width;
  451. // draw the selected quantity ratio
  452. string quantityText = selectedQuantity.ToString() + "/" +
  453. chestEntry.Content.Gold.ToString();
  454. spriteBatch.DrawString(Fonts.GearInfoFont, quantityText,
  455. drawPosition, color);
  456. drawPosition.X += Fonts.GearInfoFont.MeasureString(quantityText).X;
  457. // draw the right selection arrow
  458. spriteBatch.Draw(rightQuantityArrow,
  459. new Vector2(drawPosition.X, drawPosition.Y - 4), Color.White);
  460. drawPosition.X += rightQuantityArrow.Width;
  461. }
  462. else
  463. {
  464. // draw the remaining quantity
  465. spriteBatch.DrawString(Fonts.GearInfoFont,
  466. chestEntry.Content.Gold.ToString(), drawPosition, color);
  467. }
  468. }
  469. /// <summary>
  470. /// Draw the gear's content entry at the given position in the list.
  471. /// </summary>
  472. /// <param name="contentEntry">The content entry to draw.</param>
  473. /// <param name="position">The position to draw the entry at.</param>
  474. /// <param name="isSelected">If true, this item is selected.</param>
  475. protected override void DrawEntry(ContentEntry<Gear> entry, Vector2 position,
  476. bool isSelected)
  477. {
  478. // check the parameter
  479. if (entry == null)
  480. {
  481. throw new ArgumentNullException("entry");
  482. }
  483. Gear gear = entry.Content as Gear;
  484. if (gear == null)
  485. {
  486. return;
  487. }
  488. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  489. Vector2 drawPosition = position;
  490. // draw the icon
  491. spriteBatch.Draw(gear.IconTexture, drawPosition + iconOffset, Color.White);
  492. // draw the name
  493. Color color = isSelected ? Fonts.HighlightColor : Fonts.DisplayColor;
  494. drawPosition.Y += listLineSpacing / 4;
  495. drawPosition.X += nameColumnInterval;
  496. spriteBatch.DrawString(Fonts.GearInfoFont, gear.Name, drawPosition, color);
  497. // draw the power
  498. drawPosition.X += powerColumnInterval;
  499. string powerText = gear.GetPowerText();
  500. Vector2 powerTextSize = Fonts.GearInfoFont.MeasureString(powerText);
  501. Vector2 powerPosition = drawPosition;
  502. powerPosition.Y -= (float)Math.Ceiling((powerTextSize.Y - 30f) / 2);
  503. spriteBatch.DrawString(Fonts.GearInfoFont, powerText,
  504. powerPosition, color);
  505. // draw the quantity
  506. drawPosition.X += quantityColumnInterval;
  507. if (isSelected)
  508. {
  509. // draw the left selection arrow
  510. drawPosition.X -= leftQuantityArrow.Width;
  511. spriteBatch.Draw(leftQuantityArrow,
  512. new Vector2(drawPosition.X, drawPosition.Y - 4), Color.White);
  513. drawPosition.X += leftQuantityArrow.Width;
  514. // draw the selected quantity ratio
  515. string quantityText = selectedQuantity.ToString() + "/" +
  516. entry.Count.ToString();
  517. spriteBatch.DrawString(Fonts.GearInfoFont, quantityText,
  518. drawPosition, color);
  519. drawPosition.X += Fonts.GearInfoFont.MeasureString(quantityText).X;
  520. // draw the right selection arrow
  521. spriteBatch.Draw(rightQuantityArrow,
  522. new Vector2(drawPosition.X, drawPosition.Y - 4), Color.White);
  523. drawPosition.X += rightQuantityArrow.Width;
  524. }
  525. else
  526. {
  527. // draw the remaining quantity
  528. spriteBatch.DrawString(Fonts.GearInfoFont, entry.Count.ToString(),
  529. drawPosition, color);
  530. }
  531. }
  532. }
  533. }