recrownedgtk/RecrownedAthenaeum/UI/Modular/UIModule.cs

163 lines
5.1 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using RecrownedAthenaeum.Input;
using RecrownedAthenaeum.Render;
using System;
namespace RecrownedAthenaeum.UI.Modular
{
2019-01-14 06:34:35 +00:00
/// <summary>
/// Module for UI layout.
/// </summary>
public class UIModule : IInputListener
{
2019-01-14 06:34:35 +00:00
/// <summary>
/// The bounds before factoring in the origin.
2019-01-14 06:34:35 +00:00
/// </summary>
public Rectangle situation;
/// <summary>
/// Bounds of this module (after factoring in the origin).
/// </summary>
public Rectangle Boundaries
{
get
{
return new Rectangle((int)(situation.X - origin.X), (int)(situation.Y - origin.Y), situation.Width, situation.Height);
}
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Origin of this module.
/// </summary>
public Vector2 origin;
2019-03-08 16:14:51 +00:00
2019-01-14 06:34:35 +00:00
/// <summary>
/// The parent of this module. May be null.
/// </summary>
2019-03-10 06:49:25 +00:00
public UIModuleGroup parent;
2019-01-14 06:34:35 +00:00
/// <summary>
/// Name of this module. For organizational/referencial purposes mostly.
/// </summary>
2019-03-10 06:49:25 +00:00
public string name;
2019-01-14 06:34:35 +00:00
/// <summary>
/// The color tint of this module.
/// </summary>
public Color color = Color.White;
2019-01-14 05:23:03 +00:00
/// <summary>
/// Called every frame to update this module. Calculations and movement should go here.
/// </summary>
/// <param name="gameTime">Game time information.</param>
public virtual void Update(GameTime gameTime)
{
2019-01-14 05:23:03 +00:00
}
2019-01-14 05:23:03 +00:00
/// <summary>
/// Called every frame to draw this module. Anything that needs to be drawn should go here.
/// </summary>
/// <param name="batch">Batch used to draw.</param>
public virtual void Draw(ConsistentSpriteBatch batch)
{
}
2019-01-14 05:23:03 +00:00
/// <summary>
/// Converts the given rectangle to the coordinates of the parent.
/// </summary>
/// <param name="rectangle">Rectangle to convert.</param>
/// <returns></returns>
public Rectangle ConvertToParentCoordinates(Rectangle rectangle)
{
2019-03-10 06:49:25 +00:00
if (parent != null)
{
2019-03-10 06:49:25 +00:00
Rectangle parentHitbox = parent.ConvertToParentCoordinates(rectangle);
2019-01-14 05:23:03 +00:00
int tX = rectangle.X + parentHitbox.X;
int tY = rectangle.Y + parentHitbox.Y;
return new Rectangle(tX, tY, rectangle.Width, rectangle.Height);
2019-03-08 16:14:51 +00:00
}
else
{
2019-01-14 05:23:03 +00:00
return rectangle;
}
}
2019-01-14 05:23:03 +00:00
/// <summary>
/// Removes this module from the parent.
/// </summary>
public void RemoveFromParent()
{
2019-03-10 06:49:25 +00:00
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;
}
/// <summary>
/// Sets the origin to be the center of the bounds.
/// </summary>
public void CenterOrigin()
{
origin.X = situation.Width / 2f;
origin.Y = situation.Height / 2f;
}
2019-03-08 16:14:51 +00:00
/// <summary>
2019-03-10 06:49:25 +00:00
/// Centers this module's origin on the horizontal axis relative to the parent <see cref="UIModuleGroup"/>.
2019-03-08 16:14:51 +00:00
/// </summary>
/// <returns>True if possible and false if not.</returns>
2019-03-10 06:49:25 +00:00
public bool CenterHorizontally()
2019-03-08 16:14:51 +00:00
{
2019-03-10 06:49:25 +00:00
if (parent != null)
2019-03-08 16:14:51 +00:00
{
2019-03-10 06:49:25 +00:00
Rectangle rectangle = parent.Boundaries;
if (parent != null && rectangle.Width >= Boundaries.Width)
{
situation.X = rectangle.Width / 2 + situation.X;
return true;
}
2019-03-08 16:14:51 +00:00
}
return false;
}
/// <summary>
2019-03-10 06:49:25 +00:00
/// Centers this module's origin on the vertical axis relative to the parent <see cref="UIModuleGroup"/>.
2019-03-08 16:14:51 +00:00
/// </summary>
/// <returns>True if possible.</returns>
2019-03-10 06:49:25 +00:00
public bool CenterVertically()
2019-03-08 16:14:51 +00:00
{
2019-03-10 06:49:25 +00:00
if (parent != null)
2019-03-08 16:14:51 +00:00
{
2019-03-10 06:49:25 +00:00
Rectangle rectangle = parent.Boundaries;
if (rectangle.Height >= Boundaries.Height)
{
situation.Y = rectangle.Height / 2 + situation.Y;
return true;
}
2019-03-08 16:14:51 +00:00
}
return false;
}
}
}