2018-11-30 02:41:06 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
2018-12-04 13:45:09 +00:00
|
|
|
|
using RecrownedAthenaeum.Input;
|
2019-12-28 20:35:01 +00:00
|
|
|
|
using RecrownedAthenaeum.Graphics.Render;
|
2018-11-30 02:41:06 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
2018-12-04 13:45:09 +00:00
|
|
|
|
namespace RecrownedAthenaeum.UI.Modular
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2019-01-14 06:34:35 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Module for UI layout.
|
|
|
|
|
/// </summary>
|
2019-04-09 04:58:27 +00:00
|
|
|
|
public abstract class UIModule : IInputListener
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// <summary>
|
2019-04-09 04:58:27 +00:00
|
|
|
|
/// The width of the module.
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// </summary>
|
2019-04-09 04:58:27 +00:00
|
|
|
|
public virtual int Width { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The height of the module.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual int Height { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The X position of the module.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual int X { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The Y position of the module.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual int Y { get; set; }
|
2019-03-02 05:10:53 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-03-02 05:24:08 +00:00
|
|
|
|
/// Bounds of this module (after factoring in the origin).
|
2019-03-02 05:10:53 +00:00
|
|
|
|
/// </summary>
|
2019-03-09 06:57:29 +00:00
|
|
|
|
public Rectangle Boundaries
|
2019-03-02 05:24:08 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-04-09 04:58:27 +00:00
|
|
|
|
return new Rectangle((int)(X - origin.X), (int)(Y - origin.Y), Width, Height);
|
2019-03-02 05:24:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-02 05:10:53 +00:00
|
|
|
|
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Origin of this module.
|
|
|
|
|
/// </summary>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
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>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
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>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public virtual void Update(GameTime gameTime)
|
|
|
|
|
{
|
2019-01-14 05:23:03 +00:00
|
|
|
|
|
2018-11-30 02:41:06 +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>
|
2019-03-24 00:04:43 +00:00
|
|
|
|
public virtual void Draw(ConsistentSpriteBatch batch)
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2019-03-10 06:49:25 +00:00
|
|
|
|
if (parent != null)
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
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
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2019-01-14 05:23:03 +00:00
|
|
|
|
return rectangle;
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 05:23:03 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes this module from the parent.
|
|
|
|
|
/// </summary>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public void RemoveFromParent()
|
|
|
|
|
{
|
2019-03-10 06:49:25 +00:00
|
|
|
|
if (parent == null) throw new InvalidOperationException("Parent is null.");
|
|
|
|
|
parent.RemoveModule(this);
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
2019-01-28 00:06:55 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-04-09 04:58:27 +00:00
|
|
|
|
/// Sets the origin to be the center using the <see cref="Width"/> and <see cref="Height"/>.
|
2019-01-28 00:06:55 +00:00
|
|
|
|
/// </summary>
|
2019-04-09 04:58:27 +00:00
|
|
|
|
public virtual void CenterOrigin()
|
2019-01-28 00:06:55 +00:00
|
|
|
|
{
|
2019-04-09 04:58:27 +00:00
|
|
|
|
origin.X = Width / 2f;
|
|
|
|
|
origin.Y = Height / 2f;
|
2019-01-28 00:06:55 +00:00
|
|
|
|
}
|
2019-03-08 16:14:51 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-04-09 04:58:27 +00:00
|
|
|
|
/// Centers this module's 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)
|
|
|
|
|
{
|
2019-04-09 04:58:27 +00:00
|
|
|
|
X = rectangle.Width / 2 + X;
|
2019-03-10 06:49:25 +00:00
|
|
|
|
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-04-09 04:58:27 +00:00
|
|
|
|
public virtual 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)
|
|
|
|
|
{
|
2019-04-09 04:58:27 +00:00
|
|
|
|
Y = rectangle.Height / 2 + Y;
|
2019-03-10 06:49:25 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-03-08 16:14:51 +00:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-09 04:58:27 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the position and dimension of this module.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="rectangle">The rectangle that represents the position and dimensions of the module.</param>
|
|
|
|
|
public virtual void SetPositionAndDimensions(Rectangle rectangle)
|
|
|
|
|
{
|
|
|
|
|
X = rectangle.X;
|
|
|
|
|
Y = rectangle.Y;
|
|
|
|
|
Width = rectangle.Width;
|
|
|
|
|
Height = rectangle.Height;
|
|
|
|
|
}
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|