using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Camera;
using RecrownedAthenaeum.ContentSystem;
using RecrownedAthenaeum.UI.SkinSystem;
using System.Collections.Generic;
using System.Linq;
namespace RecrownedAthenaeum.UI.BookSystem
{
///
/// Contains the pages.
///
public class Book
{
readonly ContentManagerController assets;
readonly ISkin skin;
Camera2D camera;
Page targetPage;
Rectangle dimensions;
Dictionary pages = new Dictionary();
///
/// Creates a book.
///
/// that holds the assets that are to be used in the pages for this book during initialization.
/// The skin that will be passed to pages during their initialization.
public Book(ContentManagerController assets, ISkin skin)
{
this.assets = assets;
this.skin = skin;
}
///
/// Should be called whenever a valid camera and dimensions for the book exist.
/// Initializes book with given parameters.
/// Generally used with a and called in the function.
///
/// Camera game is currently using.
/// Dimensions of the book and the dimensions the pages will use.
public void Initiate(Camera2D camera, Rectangle dimensions)
{
this.camera = camera;
this.dimensions = dimensions;
}
///
/// Draws the pages.
///
/// Batch used to draw.
public void Draw(SpriteBatch batch)
{
for (int pageIndex = 0; pageIndex < pages.Count; pageIndex++)
{
Page page = pages.ElementAt(pageIndex).Value;
page.Draw(batch);
}
}
///
/// Updates the book.
///
/// Snapshot of information of the game time.
public void Update(GameTime gameTime)
{
if (targetPage != null)
{
Vector2 position;
Rectangle targetBounds = targetPage.bounds;
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);
}
}
///
/// Adds the page(s).
///
/// The page(s) to add.
public void AddPages(params Page[] pages)
{
foreach (Page page in pages)
{
page.camera = camera;
page.Initialize(assets, skin);
this.pages.Add(page.Name, page);
}
}
///
/// Removes the page.
///
/// Page to remove.
public void RemovePage(Page page)
{
RemovePage(page.Name);
}
///
/// Removes the page.
///
/// Name of page to remove.
public void RemovePage(string name)
{
pages.Remove(name);
}
///
/// Perform a step of linear interpolation to the given page.
///
/// The page to lerp to.
public void LerpToPage(Page page)
{
targetPage = page;
}
///
/// Goes to page instantly.
///
/// Page to go to.
public void GoToPage(Page page)
{
Rectangle targetBounds = page.bounds;
camera.position.X = targetBounds.X + (targetBounds.Width * 0.5f);
camera.position.Y = targetBounds.Y + (targetBounds.Height * 0.5f);
}
}
}