refactor: removed dash from project name and underscore from namespaces. Began working on pipeline project.
This commit is contained in:
93
RecrownedAthenaeum/UI/Book/Book.cs
Normal file
93
RecrownedAthenaeum/UI/Book/Book.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.Camera;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.Book
|
||||
{
|
||||
public 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
RecrownedAthenaeum/UI/Book/Page.cs
Normal file
35
RecrownedAthenaeum/UI/Book/Page.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.UI.Modular;
|
||||
using RecrownedAthenaeum.Camera;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.Book
|
||||
{
|
||||
public 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;
|
||||
}
|
||||
}
|
||||
}
|
63
RecrownedAthenaeum/UI/Modular/Modules/Image.cs
Normal file
63
RecrownedAthenaeum/UI/Modular/Modules/Image.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.UI.Modular;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
{
|
||||
public class Image : UIModule
|
||||
{
|
||||
public float rotation = 0f;
|
||||
public Texture2D Texture { get; set; }
|
||||
public float ScaleX
|
||||
{
|
||||
get
|
||||
{
|
||||
return (float)bounds.Width / Texture.Width;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
bounds.Width = (int)(Texture.Width * value);
|
||||
}
|
||||
}
|
||||
|
||||
public float ScaleY
|
||||
{
|
||||
get
|
||||
{
|
||||
return (float)bounds.Height / Texture.Height;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
bounds.Height = (int)(Texture.Height * value);
|
||||
}
|
||||
}
|
||||
|
||||
public float Scale
|
||||
{
|
||||
set
|
||||
{
|
||||
bounds.Height = (int)(Texture.Height * value);
|
||||
bounds.Width = (int)(Texture.Width * value);
|
||||
}
|
||||
}
|
||||
|
||||
public Image(Texture2D texture)
|
||||
{
|
||||
Texture = texture ?? throw new ArgumentException("Image requires a texture.");
|
||||
bounds = texture.Bounds;
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch batch)
|
||||
{
|
||||
batch.Draw(Texture, bounds, null, color, rotation, origin, SpriteEffects.None, 0f);
|
||||
base.Draw(batch);
|
||||
}
|
||||
}
|
||||
}
|
62
RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs
Normal file
62
RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using RecrownedAthenaeum.UI.Modular;
|
||||
using RecrownedAthenaeum.DataTypes;
|
||||
using RecrownedAthenaeum.Input;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
|
||||
{
|
||||
public delegate bool Clicked();
|
||||
|
||||
public class Button : UIModule
|
||||
{
|
||||
private NinePatch background;
|
||||
public event Clicked Listeners;
|
||||
|
||||
public Button(NinePatch background = null)
|
||||
{
|
||||
this.background = background;
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch batch)
|
||||
{
|
||||
background?.Draw(batch, bounds);
|
||||
base.Draw(batch);
|
||||
}
|
||||
|
||||
public sealed override bool MouseStateChanged(MouseState state)
|
||||
{
|
||||
if (InputUtilities.MouseWithinBoundries(bounds))
|
||||
{
|
||||
if (InputUtilities.MouseClicked())
|
||||
{
|
||||
OnClick();
|
||||
}
|
||||
Highlighted = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Highlighted = false;
|
||||
}
|
||||
|
||||
return base.MouseStateChanged(state);
|
||||
}
|
||||
|
||||
public sealed override bool KeyboardStateChanged(KeyboardState state)
|
||||
{
|
||||
return base.KeyboardStateChanged(state);
|
||||
}
|
||||
|
||||
protected void OnClick()
|
||||
{
|
||||
Listeners?.Invoke();
|
||||
}
|
||||
|
||||
public bool Highlighted { get; private set; }
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.UI.Modular.Modules;
|
||||
using RecrownedAthenaeum.DataTypes;
|
||||
using RecrownedAthenaeum.UI.Modular.Modules.Interactive;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
|
||||
{
|
||||
public class TextButton : Button
|
||||
{
|
||||
private TextLabel label;
|
||||
|
||||
public TextButton(string text, SpriteFont font, NinePatch background) : base(background)
|
||||
{
|
||||
label = new TextLabel(font, text);
|
||||
label.autoScale = true;
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
label.bounds = bounds;
|
||||
label.Update(gameTime);
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch batch)
|
||||
{
|
||||
label.Draw(batch);
|
||||
base.Draw(batch);
|
||||
}
|
||||
}
|
||||
}
|
146
RecrownedAthenaeum/UI/Modular/Modules/TextLabel.cs
Normal file
146
RecrownedAthenaeum/UI/Modular/Modules/TextLabel.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.Camera;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.Modular.Modules
|
||||
{
|
||||
public class TextLabel : UIModule
|
||||
{
|
||||
private SpriteFont font;
|
||||
private float scale;
|
||||
private Vector2 position;
|
||||
private string originalText;
|
||||
private string displayedText;
|
||||
private Vector2 modifiedTextSize;
|
||||
public bool autoWrap;
|
||||
public bool autoScale;
|
||||
public bool useEllipses;
|
||||
public string ellipsis = "...";
|
||||
private string ModifiedText
|
||||
{
|
||||
get
|
||||
{
|
||||
return displayedText;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
displayedText = value;
|
||||
modifiedTextSize = font.MeasureString(value);
|
||||
}
|
||||
}
|
||||
public string Text
|
||||
{
|
||||
get
|
||||
{
|
||||
return originalText;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value == null) value = "...";
|
||||
modifiedTextSize = font.MeasureString(value);
|
||||
originalText = value;
|
||||
displayedText = value;
|
||||
}
|
||||
}
|
||||
|
||||
public TextLabel(SpriteFont font, string text = null)
|
||||
{
|
||||
Text = text;
|
||||
this.font = font;
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
position.X = bounds.X;
|
||||
position.Y = bounds.Y;
|
||||
|
||||
if (useEllipses) AttemptToApplyEllipsis();
|
||||
if (autoWrap) AttemptToWrapText();
|
||||
if (autoScale) AttemptToScaleFont();
|
||||
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch batch)
|
||||
{
|
||||
batch.DrawString(font, Text, position, color, 0f, origin, scale, SpriteEffects.None, 0f);
|
||||
base.Draw(batch);
|
||||
}
|
||||
|
||||
public void AttemptToApplyEllipsis()
|
||||
{
|
||||
if (modifiedTextSize.X * scale > bounds.Width && ModifiedText.Length > ellipsis.Length + 1)
|
||||
{
|
||||
RemoveLineBreaks();
|
||||
StringBuilder stringBuilder = new StringBuilder(ModifiedText);
|
||||
do
|
||||
{
|
||||
stringBuilder.Remove(stringBuilder.Length, ellipsis.Length - 1);
|
||||
stringBuilder.Insert(stringBuilder.Length, ellipsis);
|
||||
}
|
||||
while (font.MeasureString(stringBuilder).X * scale > bounds.Width);
|
||||
|
||||
ModifiedText = stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public void AttemptToScaleFont()
|
||||
{
|
||||
if (modifiedTextSize.X * scale > bounds.Width || modifiedTextSize.X * scale < bounds.Width - 5)
|
||||
{
|
||||
scale = bounds.Width / modifiedTextSize.X;
|
||||
}
|
||||
|
||||
if (modifiedTextSize.Y * scale > bounds.Height || modifiedTextSize.Y * scale < bounds.Height - 5)
|
||||
{
|
||||
scale = bounds.Height / modifiedTextSize.Y;
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveLineBreaks()
|
||||
{
|
||||
ModifiedText = ModifiedText.Replace("\n", " ");
|
||||
}
|
||||
|
||||
public void ResetToOriginalText()
|
||||
{
|
||||
ModifiedText = originalText;
|
||||
}
|
||||
|
||||
public void AttemptToWrapText(bool unwrap = false)
|
||||
{
|
||||
if (unwrap) RemoveLineBreaks();
|
||||
if (modifiedTextSize.X * scale > bounds.Width)
|
||||
{
|
||||
ModifiedText = ModifiedText.Replace("\n", " ");
|
||||
string[] words = ModifiedText.Split(' ');
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
float currentScaledLineWidth = 0f;
|
||||
|
||||
for (int w = 0; w < words.Length; w++)
|
||||
{
|
||||
string word = words[w];
|
||||
float scaledWidth = font.MeasureString(word).X * scale;
|
||||
|
||||
if (currentScaledLineWidth + scaledWidth <= bounds.Width)
|
||||
{
|
||||
stringBuilder.Append(word);
|
||||
currentScaledLineWidth += scaledWidth;
|
||||
}
|
||||
else
|
||||
{
|
||||
stringBuilder.AppendLine();
|
||||
currentScaledLineWidth = 0;
|
||||
}
|
||||
}
|
||||
ModifiedText = stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
74
RecrownedAthenaeum/UI/Modular/UIModule.cs
Normal file
74
RecrownedAthenaeum/UI/Modular/UIModule.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using RecrownedAthenaeum.Camera;
|
||||
using RecrownedAthenaeum.Input;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.Modular
|
||||
{
|
||||
public class UIModule : IInputListener
|
||||
{
|
||||
public Rectangle bounds;
|
||||
public Vector2 origin;
|
||||
public UIModuleGroup Parent;
|
||||
public string Name;
|
||||
public Color color = Color.White;
|
||||
|
||||
public virtual void Update(GameTime gameTime)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Draw(SpriteBatch batch)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Rectangle ConvertToParentCoordinates(Rectangle bounds)
|
||||
{
|
||||
if (Parent != null)
|
||||
{
|
||||
Rectangle parentHitbox = Parent.ConvertToParentCoordinates(bounds);
|
||||
int tX = bounds.X + parentHitbox.X;
|
||||
int tY = bounds.Y + parentHitbox.Y;
|
||||
return new Rectangle(tX, tY, bounds.Width, bounds.Height);
|
||||
} else
|
||||
{
|
||||
return bounds;
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveFromParent()
|
||||
{
|
||||
if (Parent == null)
|
||||
{
|
||||
throw new InvalidOperationException("Parent is null.");
|
||||
}
|
||||
Parent.RemoveModule(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called whenever the keyboard state is changed.
|
||||
/// </summary>
|
||||
/// <param name="state">The current keyboard state.</param>
|
||||
/// <returns>Returning whether or not to continue to call the next listener.</returns>
|
||||
public virtual bool KeyboardStateChanged(KeyboardState state)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called whenever the state of the mouse changes. This includes movement.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the mouse.</param>
|
||||
/// <returns>Returning whether or not to continue to call the next listener.</returns>
|
||||
public virtual bool MouseStateChanged(MouseState state)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
109
RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs
Normal file
109
RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using RecrownedAthenaeum.Camera;
|
||||
using RecrownedAthenaeum.Input;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.Modular
|
||||
{
|
||||
public class UIModuleGroup : UIModule
|
||||
{
|
||||
List<UIModule> modules = new List<UIModule>();
|
||||
Rectangle scissorBounds;
|
||||
RasterizerState scissorRasterizer = new RasterizerState();
|
||||
public Camera2D Camera { get; set; }
|
||||
|
||||
public UIModuleGroup(bool crop = false, Camera2D camera = null)
|
||||
{
|
||||
Camera = camera;
|
||||
if (crop)
|
||||
{
|
||||
scissorRasterizer.ScissorTestEnable = true;
|
||||
scissorBounds = new Rectangle();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch batch)
|
||||
{
|
||||
if (scissorBounds != null)
|
||||
{
|
||||
batch.End();
|
||||
batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer, null, Camera?.TransformMatrix);
|
||||
scissorBounds.Width = bounds.Width;
|
||||
scissorBounds.Height = bounds.Height;
|
||||
scissorBounds.X = bounds.X;
|
||||
scissorBounds.Y = bounds.Y;
|
||||
Rectangle scissor = scissorBounds;
|
||||
scissorBounds = batch.GraphicsDevice.ScissorRectangle;
|
||||
batch.GraphicsDevice.ScissorRectangle = scissor;
|
||||
}
|
||||
foreach (UIModule module in modules)
|
||||
{
|
||||
int offsetX = module.bounds.X;
|
||||
int offsetY = module.bounds.Y;
|
||||
module.bounds.X = bounds.X + offsetX - (int)module.origin.X;
|
||||
module.bounds.Y = bounds.Y + offsetY - (int)module.origin.Y;
|
||||
module.Draw(batch);
|
||||
module.bounds.X = offsetX;
|
||||
module.bounds.Y = offsetY;
|
||||
}
|
||||
|
||||
if (scissorBounds != null)
|
||||
{
|
||||
batch.GraphicsDevice.ScissorRectangle = scissorBounds;
|
||||
batch.End();
|
||||
batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, Camera?.TransformMatrix);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
foreach (UIModule module in modules)
|
||||
{
|
||||
module.Update(gameTime);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddModule(params UIModule[] addModules)
|
||||
{
|
||||
foreach (UIModule module in addModules)
|
||||
{
|
||||
if (modules.Contains(module))
|
||||
{
|
||||
throw new InvalidOperationException(module.ToString() + " already exists in " + this.ToString());
|
||||
}
|
||||
module.Parent = this;
|
||||
modules.Add(module);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveModule(UIModule module)
|
||||
{
|
||||
module.Parent = null;
|
||||
modules.Remove(module);
|
||||
}
|
||||
|
||||
public override bool KeyboardStateChanged(KeyboardState state)
|
||||
{
|
||||
foreach (UIModule module in modules)
|
||||
{
|
||||
module.KeyboardStateChanged(state);
|
||||
}
|
||||
return base.KeyboardStateChanged(state);
|
||||
}
|
||||
|
||||
public override bool MouseStateChanged(MouseState state)
|
||||
{
|
||||
foreach (UIModule module in modules)
|
||||
{
|
||||
module.MouseStateChanged(state);
|
||||
}
|
||||
return base.MouseStateChanged(state);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user