Implement module var name refactor and added way to keep insertion order.

This commit is contained in:
Harrison Deng 2019-03-09 00:55:44 -06:00
parent ce2f402153
commit 782ca31f15
2 changed files with 53 additions and 8 deletions

View File

@ -1,7 +1,9 @@
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;
@ -11,7 +13,7 @@ namespace RecrownedAthenaeum.UI.BookSystem
/// <summary>
/// Contains the pages.
/// </summary>
public class Book
public class Book : IInputListener
{
readonly ContentManagerController assets;
readonly ISkin skin;
@ -19,6 +21,7 @@ namespace RecrownedAthenaeum.UI.BookSystem
Page targetPage;
Rectangle dimensions;
Dictionary<string, Page> pages = new Dictionary<string, Page>();
List<Page> orderedPages = new List<Page>();
/// <summary>
/// Creates a book.
@ -52,7 +55,7 @@ namespace RecrownedAthenaeum.UI.BookSystem
{
for (int pageIndex = 0; pageIndex < pages.Count; pageIndex++)
{
Page page = pages.ElementAt(pageIndex).Value;
Page page = orderedPages[pageIndex];
page.Draw(batch);
}
}
@ -66,7 +69,7 @@ namespace RecrownedAthenaeum.UI.BookSystem
if (targetPage != null)
{
Vector2 position;
Rectangle targetBounds = targetPage.bounds;
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);
@ -94,6 +97,7 @@ namespace RecrownedAthenaeum.UI.BookSystem
{
page.camera = camera;
page.Initialize(assets, skin);
orderedPages.Add(page);
this.pages.Add(page.Name, page);
}
}
@ -113,9 +117,20 @@ namespace RecrownedAthenaeum.UI.BookSystem
/// <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>
@ -131,9 +146,39 @@ namespace RecrownedAthenaeum.UI.BookSystem
/// <param name="page">Page to go to.</param>
public void GoToPage(Page page)
{
Rectangle targetBounds = page.bounds;
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;
}
}
}

View File

@ -35,10 +35,10 @@ namespace RecrownedAthenaeum.UI.BookSystem
/// <param name="height">New Height</param>
public virtual void ApplySize(int width, int height)
{
bounds.X = pageX * width;
bounds.Y = pageY * height;
bounds.Width = width;
bounds.Height = height;
situation.X = pageX * width;
situation.Y = pageY * height;
situation.Width = width;
situation.Height = height;
requiresSizeUpdate = false;
}