185 lines
6.3 KiB
C#
185 lines
6.3 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using Microsoft.Xna.Framework.Input;
|
|
using RecrownedAthenaeum.Camera;
|
|
using RecrownedAthenaeum.ContentSystem;
|
|
using RecrownedAthenaeum.Input;
|
|
using RecrownedAthenaeum.UI.SkinSystem;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace RecrownedAthenaeum.UI.BookSystem
|
|
{
|
|
/// <summary>
|
|
/// Contains the pages.
|
|
/// </summary>
|
|
public class Book : IInputListener
|
|
{
|
|
readonly ContentManagerController assets;
|
|
readonly ISkin skin;
|
|
Camera2D camera;
|
|
Page targetPage;
|
|
Rectangle dimensions;
|
|
Dictionary<string, Page> pages = new Dictionary<string, Page>();
|
|
List<Page> orderedPages = new List<Page>();
|
|
|
|
/// <summary>
|
|
/// Creates a book.
|
|
/// </summary>
|
|
/// <param name="assets"><see cref="ContentManagerController"/> that holds the assets that are to be used in the pages for this book during initialization.</param>
|
|
/// <param name="skin">The skin that will be passed to pages during their initialization.</param>
|
|
public Book(ContentManagerController assets, ISkin skin)
|
|
{
|
|
this.assets = assets;
|
|
this.skin = skin;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Should be called whenever a valid camera and dimensions for the book exist.
|
|
/// Initializes book with given parameters.
|
|
/// Generally used with a <see cref="ScreenSystem.Screen"/> and called in the <see cref="ScreenSystem.Screen.Initiate(Rectangle, Camera2D)"/> function.
|
|
/// </summary>
|
|
/// <param name="camera">Camera game is currently using.</param>
|
|
/// <param name="dimensions">Dimensions of the book and the dimensions the pages will use.</param>
|
|
public void Initiate(Camera2D camera, Rectangle dimensions)
|
|
{
|
|
this.camera = camera;
|
|
this.dimensions = dimensions;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draws the pages.
|
|
/// </summary>
|
|
/// <param name="batch">Batch used to draw.</param>
|
|
public void Draw(SpriteBatch batch)
|
|
{
|
|
for (int pageIndex = 0; pageIndex < pages.Count; pageIndex++)
|
|
{
|
|
Page page = orderedPages[pageIndex];
|
|
page.Draw(batch);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the book.
|
|
/// </summary>
|
|
/// <param name="gameTime">Snapshot of information of the game time.</param>
|
|
public void Update(GameTime gameTime)
|
|
{
|
|
if (targetPage != null)
|
|
{
|
|
Vector2 position;
|
|
Rectangle targetBounds = targetPage.situation;
|
|
position.X = targetBounds.X + (targetBounds.Width * 0.5f);
|
|
position.Y = targetBounds.Y + (targetBounds.Height * 0.5f);
|
|
camera.LinearInterpolationToPosition(0.4f, position, (float)gameTime.ElapsedGameTime.TotalSeconds);
|
|
if (camera.Position == position)
|
|
{
|
|
targetPage = null;
|
|
}
|
|
}
|
|
|
|
for (int pageIndex = 0; pageIndex < pages.Count; pageIndex++)
|
|
{
|
|
Page page = pages.ElementAt(pageIndex).Value;
|
|
if (page.requiresSizeUpdate) page.ApplySize(dimensions.Width, dimensions.Height);
|
|
page.Update(gameTime);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds the page(s).
|
|
/// </summary>
|
|
/// <param name="pages">The page(s) to add.</param>
|
|
public void AddPages(params Page[] pages)
|
|
{
|
|
foreach (Page page in pages)
|
|
{
|
|
page.camera = camera;
|
|
page.Initialize(assets, skin);
|
|
orderedPages.Add(page);
|
|
this.pages.Add(page.Name, page);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the page.
|
|
/// </summary>
|
|
/// <param name="page">Page to remove.</param>
|
|
public void RemovePage(Page page)
|
|
{
|
|
RemovePage(page.Name);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the page.
|
|
/// </summary>
|
|
/// <param name="name">Name of page to remove.</param>
|
|
public void RemovePage(string name)
|
|
{
|
|
orderedPages.Remove(pages[name]);
|
|
pages.Remove(name);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Removes all pages.
|
|
/// </summary>
|
|
public void ClearPages()
|
|
{
|
|
orderedPages.Clear();
|
|
pages.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Perform a step of linear interpolation to the given page.
|
|
/// </summary>
|
|
/// <param name="page">The page to lerp to.</param>
|
|
public void LerpToPage(Page page)
|
|
{
|
|
targetPage = page;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Goes to page instantly.
|
|
/// </summary>
|
|
/// <param name="page">Page to go to.</param>
|
|
public void GoToPage(Page page)
|
|
{
|
|
Rectangle targetBounds = page.situation;
|
|
camera.position.X = targetBounds.X + (targetBounds.Width * 0.5f);
|
|
camera.position.Y = targetBounds.Y + (targetBounds.Height * 0.5f);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Passes the new keyboard state down to each page in order of when it was added.
|
|
/// </summary>
|
|
/// <param name="state"></param>
|
|
/// <returns>True if the state change should to trigger further input listeners.</returns>
|
|
public bool KeyboardStateChanged(KeyboardState state)
|
|
{
|
|
for (int pageIndex = 0; pageIndex < pages.Count; pageIndex++)
|
|
{
|
|
Page page = orderedPages[pageIndex];
|
|
if (!page.KeyboardStateChanged(state)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Passes the new mouse state down to each page in order of when it was added.
|
|
/// </summary>
|
|
/// <param name="state"></param>
|
|
/// <returns>True if the state change should to trigger further input listeners.</returns>
|
|
public bool MouseStateChanged(MouseState state)
|
|
{
|
|
for (int pageIndex = 0; pageIndex < pages.Count; pageIndex++)
|
|
{
|
|
Page page = orderedPages[pageIndex];
|
|
if (!page.MouseStateChanged(state)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|