2018-11-30 02:41:06 +00:00
using Microsoft.Xna.Framework ;
using Microsoft.Xna.Framework.Graphics ;
2019-03-09 06:55:44 +00:00
using Microsoft.Xna.Framework.Input ;
2018-12-04 13:45:09 +00:00
using RecrownedAthenaeum.Camera ;
2019-01-27 21:24:31 +00:00
using RecrownedAthenaeum.ContentSystem ;
2019-03-09 06:55:44 +00:00
using RecrownedAthenaeum.Input ;
2019-01-27 23:43:17 +00:00
using RecrownedAthenaeum.UI.SkinSystem ;
2018-11-30 02:41:06 +00:00
using System.Collections.Generic ;
using System.Linq ;
2019-01-27 23:39:18 +00:00
namespace RecrownedAthenaeum.UI.BookSystem
2018-11-30 02:41:06 +00:00
{
2019-01-14 06:34:35 +00:00
/// <summary>
/// Contains the pages.
/// </summary>
2019-03-09 06:55:44 +00:00
public class Book : IInputListener
2018-11-30 02:41:06 +00:00
{
2019-01-27 21:24:31 +00:00
readonly ContentManagerController assets ;
2019-01-28 00:11:45 +00:00
readonly ISkin skin ;
2018-11-30 02:41:06 +00:00
Camera2D camera ;
Page targetPage ;
Rectangle dimensions ;
Dictionary < string , Page > pages = new Dictionary < string , Page > ( ) ;
2019-03-09 06:55:44 +00:00
List < Page > orderedPages = new List < Page > ( ) ;
2018-11-30 02:41:06 +00:00
2019-01-27 21:24:31 +00:00
/// <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>
2019-01-27 23:43:17 +00:00
/// <param name="skin">The skin that will be passed to pages during their initialization.</param>
2019-01-28 00:11:45 +00:00
public Book ( ContentManagerController assets , ISkin skin )
2019-01-27 21:24:31 +00:00
{
this . assets = assets ;
2019-01-28 00:31:19 +00:00
this . skin = skin ;
2019-01-27 21:24:31 +00:00
}
2018-11-30 02:41:06 +00:00
/// <summary>
/// Should be called whenever a valid camera and dimensions for the book exist.
/// Initializes book with given parameters.
2019-01-27 23:43:17 +00:00
/// Generally used with a <see cref="ScreenSystem.Screen"/> and called in the <see cref="ScreenSystem.Screen.Initiate(Rectangle, Camera2D)"/> function.
2018-11-30 02:41:06 +00:00
/// </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 ;
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Draws the pages.
/// </summary>
/// <param name="batch">Batch used to draw.</param>
2018-11-30 02:41:06 +00:00
public void Draw ( SpriteBatch batch )
{
for ( int pageIndex = 0 ; pageIndex < pages . Count ; pageIndex + + )
{
2019-03-09 06:55:44 +00:00
Page page = orderedPages [ pageIndex ] ;
2018-11-30 02:41:06 +00:00
page . Draw ( batch ) ;
}
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Updates the book.
/// </summary>
/// <param name="gameTime">Snapshot of information of the game time.</param>
2018-11-30 02:41:06 +00:00
public void Update ( GameTime gameTime )
{
if ( targetPage ! = null )
{
Vector2 position ;
2019-03-09 06:55:44 +00:00
Rectangle targetBounds = targetPage . situation ;
2018-11-30 02:41:06 +00:00
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 ;
2019-01-14 07:26:46 +00:00
if ( page . requiresSizeUpdate ) page . ApplySize ( dimensions . Width , dimensions . Height ) ;
2018-11-30 02:41:06 +00:00
page . Update ( gameTime ) ;
}
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Adds the page(s).
/// </summary>
/// <param name="pages">The page(s) to add.</param>
2018-11-30 02:41:06 +00:00
public void AddPages ( params Page [ ] pages )
{
foreach ( Page page in pages )
{
2019-02-11 05:32:16 +00:00
page . camera = camera ;
2019-01-27 23:43:17 +00:00
page . Initialize ( assets , skin ) ;
2019-03-09 06:55:44 +00:00
orderedPages . Add ( page ) ;
2018-11-30 02:41:06 +00:00
this . pages . Add ( page . Name , page ) ;
}
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Removes the page.
/// </summary>
/// <param name="page">Page to remove.</param>
2018-11-30 02:41:06 +00:00
public void RemovePage ( Page page )
{
RemovePage ( page . Name ) ;
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Removes the page.
/// </summary>
/// <param name="name">Name of page to remove.</param>
2018-11-30 02:41:06 +00:00
public void RemovePage ( string name )
{
2019-03-09 06:55:44 +00:00
orderedPages . Remove ( pages [ name ] ) ;
2018-11-30 02:41:06 +00:00
pages . Remove ( name ) ;
}
2019-03-09 06:55:44 +00:00
/// <summary>
/// Removes all pages.
/// </summary>
public void ClearPages ( )
{
orderedPages . Clear ( ) ;
pages . Clear ( ) ;
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Perform a step of linear interpolation to the given page.
/// </summary>
/// <param name="page">The page to lerp to.</param>
2018-11-30 02:41:06 +00:00
public void LerpToPage ( Page page )
{
targetPage = page ;
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Goes to page instantly.
/// </summary>
/// <param name="page">Page to go to.</param>
2018-11-30 02:41:06 +00:00
public void GoToPage ( Page page )
{
2019-03-09 06:55:44 +00:00
Rectangle targetBounds = page . situation ;
2019-02-06 06:12:08 +00:00
camera . position . X = targetBounds . X + ( targetBounds . Width * 0.5f ) ;
camera . position . Y = targetBounds . Y + ( targetBounds . Height * 0.5f ) ;
2018-11-30 02:41:06 +00:00
}
2019-03-09 06:55:44 +00:00
/// <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 ;
}
2018-11-30 02:41:06 +00:00
}
}