123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668 |
- #region File Description
- //-----------------------------------------------------------------------------
- // ListScreen.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Content;
- using RolePlayingGameData;
- using System.Collections.ObjectModel;
- #endregion
- namespace RolePlaying
- {
- abstract class ListScreen<T> : GameScreen
- {
- #region Graphics Data
- protected readonly Vector2 iconOffset = new Vector2(0f, 0f);
- protected readonly Vector2 descriptionTextPosition = new Vector2(200, 550);
- private readonly Vector2 listPositionTopPosition = new Vector2(1160, 354);
- private readonly Vector2 listPositionBottomPosition = new Vector2(1160, 384);
- private Texture2D backgroundTexture;
- private readonly Rectangle backgroundDestination =
- new Rectangle(0, 0, 1280, 720);
- private Texture2D fadeTexture;
- private Texture2D listTexture;
- private readonly Vector2 listTexturePosition = new Vector2(187f, 180f);
- protected readonly Vector2 listEntryStartPosition = new Vector2(200f, 202f);
- protected const int listLineSpacing = 76;
- private Texture2D plankTexture;
- private Vector2 plankTexturePosition;
- protected string titleText = String.Empty;
- protected Texture2D goldTexture;
- private readonly Vector2 goldTexturePosition = new Vector2(490f, 640f);
- protected string goldText = String.Empty;
- private readonly Vector2 goldTextPosition = new Vector2(565f, 648f);
- private Texture2D highlightTexture;
- private readonly Vector2 highlightStartPosition = new Vector2(170f, 237f);
- private Texture2D selectionArrowTexture;
- private readonly Vector2 selectionArrowPosition = new Vector2(135f, 245f);
- private Texture2D leftTriggerTexture;
- private readonly Vector2 leftTriggerTexturePosition = new Vector2(340f, 50f);
- protected string leftTriggerText = String.Empty;
- private Texture2D rightTriggerTexture;
- private readonly Vector2 rightTriggerTexturePosition = new Vector2(900f, 50f);
- protected string rightTriggerText = String.Empty;
- private Texture2D leftQuantityArrowTexture;
- private Texture2D rightQuantityArrowTexture;
- private Texture2D backButtonTexture;
- private readonly Vector2 backButtonTexturePosition = new Vector2(80f, 640f);
- protected string backButtonText = String.Empty;
- private Vector2 backButtonTextPosition = new Vector2(90f, 645f); // + tex width
- private Texture2D selectButtonTexture;
- private readonly Vector2 selectButtonTexturePosition = new Vector2(1150f, 640f);
- protected string selectButtonText = String.Empty;
- private Texture2D xButtonTexture;
- private readonly Vector2 xButtonTexturePosition = new Vector2(240f, 640f);
- protected string xButtonText = String.Empty;
- private Vector2 xButtonTextPosition = new Vector2(250f, 645f); // + tex width
- private Texture2D yButtonTexture;
- private readonly Vector2 yButtonTexturePosition = new Vector2(890f, 640f);
- protected string yButtonText = String.Empty;
- #endregion
- #region Data Access
- /// <summary>
- /// Get the list that this screen displays.
- /// </summary>
- /// <returns></returns>
- public abstract ReadOnlyCollection<T> GetDataList();
- #endregion
- #region List Navigation
- /// <summary>
- /// The index of the selected entry.
- /// </summary>
- private int selectedIndex = 0;
- /// <summary>
- /// The index of the selected entry.
- /// </summary>
- public int SelectedIndex
- {
- get { return selectedIndex; }
- set
- {
- if (selectedIndex != value)
- {
- selectedIndex = value;
- EnsureVisible(selectedIndex);
- }
- }
- }
- /// <summary>
- /// Ensure that the given index is visible on the screen.
- /// </summary>
- public void EnsureVisible(int index)
- {
- if (index < startIndex)
- {
- // if it's above the current selection, set the first entry
- startIndex = index;
- }
- if (selectedIndex > (endIndex - 1))
- {
- startIndex += selectedIndex - (endIndex - 1);
- }
- // otherwise, it should be in the current selection already
- // -- note that the start and end indices are checked in Draw.
- }
- /// <summary>
- /// Move the current selection up one entry.
- /// </summary>
- protected virtual void MoveCursorUp()
- {
- if (SelectedIndex > 0)
- {
- SelectedIndex--;
- }
- }
- /// <summary>
- /// Move the current selection down one entry.
- /// </summary>
- protected virtual void MoveCursorDown()
- {
- SelectedIndex++; // safety-checked in Draw()
- }
- /// <summary>
- /// Decrease the selected quantity by one.
- /// </summary>
- protected virtual void MoveCursorLeft() { }
- /// <summary>
- /// Increase the selected quantity by one.
- /// </summary>
- protected virtual void MoveCursorRight() { }
-
-
- /// <summary>
- /// The first index displayed on the screen from the list.
- /// </summary>
- private int startIndex = 0;
- /// <summary>
- /// The first index displayed on the screen from the list.
- /// </summary>
- public int StartIndex
- {
- get { return startIndex; }
- set { startIndex = value; } // safety-checked in Draw
- }
- /// <summary>
- /// The last index displayed on the screen from the list.
- /// </summary>
- private int endIndex = 0;
- /// <summary>
- /// The last index displayed on the screen from the list.
- /// </summary>
- public int EndIndex
- {
- get { return endIndex; }
- set { endIndex = value; } // safety-checked in Draw
- }
- /// <summary>
- /// The maximum number of list entries that the screen can show at once.
- /// </summary>
- public const int MaximumListEntries = 4;
- #endregion
- #region Initialization
- /// <summary>
- /// Constructs a new ListScreen object.
- /// </summary>
- public ListScreen()
- : base()
- {
- this.IsPopup = true;
- }
-
- /// <summary>
- /// Load the graphics content from the content manager.
- /// </summary>
- public override void LoadContent()
- {
- ContentManager content = ScreenManager.Game.Content;
- // load the background textures
- fadeTexture = content.Load<Texture2D>(@"Textures\GameScreens\FadeScreen");
- backgroundTexture =
- content.Load<Texture2D>(@"Textures\GameScreens\GameScreenBkgd");
- listTexture =
- content.Load<Texture2D>(@"Textures\GameScreens\InfoDisplay");
- plankTexture =
- content.Load<Texture2D>(@"Textures\MainMenu\MainMenuPlank03");
- goldTexture =
- content.Load<Texture2D>(@"Textures\GameScreens\GoldIcon");
- // load the foreground textures
- highlightTexture =
- content.Load<Texture2D>(@"Textures\GameScreens\HighlightLarge");
- selectionArrowTexture =
- content.Load<Texture2D>(@"Textures\GameScreens\SelectionArrow");
- // load the trigger images
- leftTriggerTexture =
- content.Load<Texture2D>(@"Textures\Buttons\LeftTriggerButton");
- rightTriggerTexture =
- content.Load<Texture2D>(@"Textures\Buttons\RightTriggerButton");
- leftQuantityArrowTexture =
- content.Load<Texture2D>(@"Textures\Buttons\QuantityArrowLeft");
- rightQuantityArrowTexture =
- content.Load<Texture2D>(@"Textures\Buttons\QuantityArrowRight");
- backButtonTexture =
- content.Load<Texture2D>(@"Textures\Buttons\BButton");
- selectButtonTexture =
- content.Load<Texture2D>(@"Textures\Buttons\AButton");
- xButtonTexture =
- content.Load<Texture2D>(@"Textures\Buttons\XButton");
- yButtonTexture =
- content.Load<Texture2D>(@"Textures\Buttons\YButton");
- // calculate the centered positions
- Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
- plankTexturePosition = new Vector2(
- viewport.X + (viewport.Width - plankTexture.Width) / 2f, 67f);
- // adjust positions for texture sizes
- if (backButtonTexture != null)
- {
- backButtonTextPosition.X += backButtonTexture.Width;
- }
- if (xButtonTexture != null)
- {
- xButtonTextPosition.X += xButtonTexture.Width;
- }
-
- base.LoadContent();
- }
- #endregion
- #region Input Handling
- /// <summary>
- /// Handle user input.
- /// </summary>
- public override void HandleInput()
- {
- if (InputManager.IsActionTriggered(InputManager.Action.PageLeft))
- {
- PageScreenLeft();
- }
- else if (InputManager.IsActionTriggered(InputManager.Action.PageRight))
- {
- PageScreenRight();
- }
- else if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
- {
- MoveCursorUp();
- }
- else if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
- {
- MoveCursorDown();
- }
- else if (InputManager.IsActionTriggered(InputManager.Action.IncreaseAmount))
- {
- MoveCursorRight();
- }
- else if (InputManager.IsActionTriggered(InputManager.Action.DecreaseAmount))
- {
- MoveCursorLeft();
- }
- else if (InputManager.IsActionTriggered(InputManager.Action.Back))
- {
- BackTriggered();
- }
- else if (InputManager.IsActionTriggered(InputManager.Action.Ok))
- {
- ReadOnlyCollection<T> dataList = GetDataList();
- if ((selectedIndex >= 0) && (selectedIndex < dataList.Count))
- {
- SelectTriggered(dataList[selectedIndex]);
- }
- }
- else if (InputManager.IsActionTriggered(InputManager.Action.DropUnEquip))
- {
- ReadOnlyCollection<T> dataList = GetDataList();
- if ((selectedIndex >= 0) && (selectedIndex < dataList.Count))
- {
- ButtonXPressed(dataList[selectedIndex]);
- }
- }
- else if (InputManager.IsActionTriggered(InputManager.Action.TakeView))
- {
- ReadOnlyCollection<T> dataList = GetDataList();
- if ((selectedIndex >= 0) && (selectedIndex < dataList.Count))
- {
- ButtonYPressed(dataList[selectedIndex]);
- }
- }
- base.HandleInput();
- }
- /// <summary>
- /// Switch to the screen to the "left" of this one in the UI, if any.
- /// </summary>
- protected virtual void PageScreenLeft() { }
- /// <summary>
- /// Switch to the screen to the "right" of this one in the UI, if any.
- /// </summary>
- protected virtual void PageScreenRight() { }
- /// <summary>
- /// Respond to the triggering of the Back action.
- /// </summary>
- protected virtual void BackTriggered()
- {
- ExitScreen();
- }
- /// <summary>
- /// Respond to the triggering of the Select action.
- /// </summary>
- protected virtual void SelectTriggered(T entry) { }
- /// <summary>
- /// Respond to the triggering of the X button (and related key).
- /// </summary>
- protected virtual void ButtonXPressed(T entry) { }
- /// <summary>
- /// Respond to the triggering of the Y button (and related key).
- /// </summary>
- protected virtual void ButtonYPressed(T entry) { }
- #endregion
- #region Drawing
- /// <summary>
- /// Draws the screen.
- /// </summary>
- public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
- {
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- // get the content list
- ReadOnlyCollection<T> dataList = GetDataList();
- // turn off the buttons if the list is empty
- if (dataList.Count <= 0)
- {
- selectButtonText = String.Empty;
- xButtonText = String.Empty;
- yButtonText = String.Empty;
- }
- // fix the indices for the current list size
- SelectedIndex = (int)MathHelper.Clamp(SelectedIndex, 0, dataList.Count - 1);
- startIndex = (int)MathHelper.Clamp(startIndex, 0,
- dataList.Count - MaximumListEntries);
- endIndex = Math.Min(startIndex + MaximumListEntries, dataList.Count);
- spriteBatch.Begin();
- DrawBackground();
- if (dataList.Count > 0)
- {
- DrawListPosition(SelectedIndex + 1, dataList.Count);
- }
- DrawButtons();
- DrawPartyGold();
- DrawColumnHeaders();
- DrawTitle();
- // draw each item currently shown
- Vector2 position = listEntryStartPosition +
- new Vector2(0f, listLineSpacing / 2);
- if (startIndex >= 0)
- {
- for (int index = startIndex; index < endIndex; index++)
- {
- T entry = dataList[index];
- if (index == selectedIndex)
- {
- DrawSelection(position);
- DrawEntry(entry, position, true);
- DrawSelectedDescription(entry);
- }
- else
- {
- DrawEntry(entry, position, false);
- }
- position.Y += listLineSpacing;
- }
- }
- spriteBatch.End();
- }
- /// <summary>
- /// Draw the entry at the given position in the list.
- /// </summary>
- /// <param name="entry">The entry to draw.</param>
- /// <param name="position">The position to draw the entry at.</param>
- /// <param name="isSelected">If true, this entry is selected.</param>
- protected abstract void DrawEntry(T entry, Vector2 position, bool isSelected);
- /// <summary>
- /// Draw the selection graphics over the selected item.
- /// </summary>
- /// <param name="position"></param>
- protected virtual void DrawSelection(Vector2 position)
- {
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- spriteBatch.Draw(highlightTexture,
- new Vector2(highlightStartPosition.X, position.Y), Color.White);
- spriteBatch.Draw(selectionArrowTexture,
- new Vector2(selectionArrowPosition.X, position.Y + 10f), Color.White);
- }
- /// <summary>
- /// Draw the background of the screen.
- /// </summary>
- protected virtual void DrawBackground()
- {
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- spriteBatch.Draw(fadeTexture, backgroundDestination, Color.White);
- spriteBatch.Draw(backgroundTexture, backgroundDestination, Color.White);
- spriteBatch.Draw(listTexture, listTexturePosition, Color.White);
- }
- /// <summary>
- /// Draw the current list position in the appropriate location on the screen.
- /// </summary>
- /// <param name="position">The current position in the list.</param>
- /// <param name="total">The total elements in the list.</param>
- protected virtual void DrawListPosition(int position, int total)
- {
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- // draw the top number - the current position in the list
- string listPositionTopText = position.ToString();
- Vector2 drawPosition = listPositionTopPosition;
- drawPosition.X -= (float)Math.Ceiling(
- Fonts.GearInfoFont.MeasureString(listPositionTopText).X / 2);
- spriteBatch.DrawString(Fonts.GearInfoFont, listPositionTopText,
- drawPosition, Fonts.CountColor);
- // draw the bottom number - the current position in the list
- string listPositionBottomText = total.ToString();
- drawPosition = listPositionBottomPosition;
- drawPosition.X -= (float)Math.Ceiling(
- Fonts.GearInfoFont.MeasureString(listPositionBottomText).X / 2);
- spriteBatch.DrawString(Fonts.GearInfoFont, listPositionBottomText,
- drawPosition, Fonts.CountColor);
- }
-
- /// <summary>
- /// Draw the party gold text.
- /// </summary>
- protected virtual void DrawPartyGold()
- {
- if (!IsActive)
- {
- return;
- }
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- spriteBatch.Draw(goldTexture, goldTexturePosition, Color.White);
- spriteBatch.DrawString(Fonts.ButtonNamesFont,
- Fonts.GetGoldString(Session.Party.PartyGold), goldTextPosition,
- Color.White);
- }
- /// <summary>
- /// Draw the description of the selected item.
- /// </summary>
- protected abstract void DrawSelectedDescription(T entry);
- /// <summary>
- /// Draw the column headers above the list.
- /// </summary>
- protected abstract void DrawColumnHeaders();
- /// <summary>
- /// Draw all of the buttons used by the screen.
- /// </summary>
- protected virtual void DrawButtons()
- {
- if (!IsActive)
- {
- return;
- }
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- // draw the left trigger texture and text
- if ((leftTriggerTexture != null) && !String.IsNullOrEmpty(leftTriggerText))
- {
- Vector2 position = leftTriggerTexturePosition + new Vector2(
- leftTriggerTexture.Width / 2f - (float)Math.Ceiling(
- Fonts.PlayerStatisticsFont.MeasureString(leftTriggerText).X / 2f),
- 90f);
- spriteBatch.Draw(leftTriggerTexture, leftTriggerTexturePosition,
- Color.White);
- spriteBatch.DrawString(Fonts.PlayerStatisticsFont, leftTriggerText,
- position, Color.Black);
- }
- // draw the right trigger texture and text
- if ((rightTriggerTexture != null) && !String.IsNullOrEmpty(rightTriggerText))
- {
- Vector2 position = rightTriggerTexturePosition + new Vector2(
- rightTriggerTexture.Width / 2f - (float)Math.Ceiling(
- Fonts.PlayerStatisticsFont.MeasureString(rightTriggerText).X / 2f),
- 90f);
- spriteBatch.Draw(rightTriggerTexture, rightTriggerTexturePosition,
- Color.White);
- spriteBatch.DrawString(Fonts.PlayerStatisticsFont, rightTriggerText,
- position, Color.Black);
- }
- // draw the left trigger texture and text
- if ((backButtonTexture != null) && !String.IsNullOrEmpty(backButtonText))
- {
- spriteBatch.Draw(backButtonTexture, backButtonTexturePosition,
- Color.White);
- spriteBatch.DrawString(Fonts.ButtonNamesFont, backButtonText,
- backButtonTextPosition, Color.White);
- }
- // draw the left trigger texture and text
- if ((selectButtonTexture != null) && !String.IsNullOrEmpty(selectButtonText))
- {
- spriteBatch.Draw(selectButtonTexture, selectButtonTexturePosition,
- Color.White);
- Vector2 position = selectButtonTexturePosition - new Vector2(
- Fonts.ButtonNamesFont.MeasureString(selectButtonText).X, 0f) +
- new Vector2(0f, 5f);
- spriteBatch.DrawString(Fonts.ButtonNamesFont, selectButtonText,
- position, Color.White);
- }
- // draw the left trigger texture and text
- if ((xButtonTexture != null) && !String.IsNullOrEmpty(xButtonText))
- {
- spriteBatch.Draw(xButtonTexture, xButtonTexturePosition,
- Color.White);
- spriteBatch.DrawString(Fonts.ButtonNamesFont, xButtonText,
- xButtonTextPosition, Color.White);
- }
- // draw the left trigger texture and text
- if ((yButtonTexture != null) && !String.IsNullOrEmpty(yButtonText))
- {
- spriteBatch.Draw(yButtonTexture, yButtonTexturePosition,
- Color.White);
- Vector2 position = yButtonTexturePosition - new Vector2(
- Fonts.ButtonNamesFont.MeasureString(yButtonText).X, 0f) +
- new Vector2(0f, 5f);
- spriteBatch.DrawString(Fonts.ButtonNamesFont, yButtonText,
- position, Color.White);
- }
- }
- /// <summary>
- /// Draw the title of the screen, if any.
- /// </summary>
- protected virtual void DrawTitle()
- {
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- // draw the left trigger texture and text
- if ((plankTexture != null) && !String.IsNullOrEmpty(titleText))
- {
- Vector2 titleTextSize = Fonts.HeaderFont.MeasureString(titleText);
- Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
- Vector2 position = new Vector2(
- (float)Math.Floor(viewport.X + viewport.Width / 2 -
- titleTextSize.X / 2f), 90f);
- spriteBatch.Draw(plankTexture, plankTexturePosition, Color.White);
- spriteBatch.DrawString(Fonts.HeaderFont, titleText, position,
- Fonts.TitleColor);
- }
- }
- #endregion
- }
- }
|