moved utilities for rhythmbullet to separate project for a better custom content loaders for the monogame pipeline as well as for future projects and of course learning how things work.
This commit is contained in:
93
Recrowned-Athenaeum/UI/Book/Book.cs
Normal file
93
Recrowned-Athenaeum/UI/Book/Book.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RhythmBullet.Utilities.Camera;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhythmBullet.UI.Book
|
||||
{
|
||||
class Book
|
||||
{
|
||||
Camera2D camera;
|
||||
Page targetPage;
|
||||
Rectangle dimensions;
|
||||
Dictionary<string, Page> pages = new Dictionary<string, Page>();
|
||||
|
||||
/// <summary>
|
||||
/// Should be called whenever a valid camera and dimensions for the book exist.
|
||||
/// Initializes book with given parameters.
|
||||
/// </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;
|
||||
}
|
||||
|
||||
public void Draw(SpriteBatch batch)
|
||||
{
|
||||
for (int pageIndex = 0; pageIndex < pages.Count; pageIndex++)
|
||||
{
|
||||
Page page = pages.ElementAt(pageIndex).Value;
|
||||
page.Draw(batch);
|
||||
}
|
||||
}
|
||||
|
||||
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.NeedsSizeUpdate) page.ApplySize(dimensions.Width, dimensions.Height);
|
||||
page.Update(gameTime);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddPages(params Page[] pages)
|
||||
{
|
||||
foreach (Page page in pages)
|
||||
{
|
||||
this.pages.Add(page.Name, page);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemovePage(Page page)
|
||||
{
|
||||
RemovePage(page.Name);
|
||||
}
|
||||
|
||||
public void RemovePage(string name)
|
||||
{
|
||||
pages.Remove(name);
|
||||
}
|
||||
|
||||
public void LerpToPage(Page page)
|
||||
{
|
||||
targetPage = page;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
35
Recrowned-Athenaeum/UI/Book/Page.cs
Normal file
35
Recrowned-Athenaeum/UI/Book/Page.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RhythmBullet.UI.Modular;
|
||||
using RhythmBullet.Utilities.Camera;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhythmBullet.UI.Book
|
||||
{
|
||||
class Page : UIModuleGroup
|
||||
{
|
||||
private readonly int pageX, pageY;
|
||||
public bool NeedsSizeUpdate;
|
||||
|
||||
public Page(int pageX, int pageY) : base(false)
|
||||
{
|
||||
this.pageX = pageX;
|
||||
this.pageY = pageY;
|
||||
NeedsSizeUpdate = true;
|
||||
Name = ToString();
|
||||
}
|
||||
|
||||
public virtual void ApplySize(int width, int height)
|
||||
{
|
||||
bounds.X = pageX * width;
|
||||
bounds.Y = pageY * height;
|
||||
bounds.Width = width;
|
||||
bounds.Height = height;
|
||||
NeedsSizeUpdate = false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user