using RecrownedAthenaeum.Types;
using OpenTK;
using OpenTK.Graphics;
using RecrownedAthenaeum.Input;
using RecrownedAthenaeum.Graphics.Render;
using System;
namespace RecrownedAthenaeum.Graphics.UI.Modular
{
///
/// Module for UI layout.
///
public abstract class UIModule : IInputListener
{
///
/// The width of the module.
///
public virtual int Width { get; set; }
///
/// The height of the module.
///
public virtual int Height { get; set; }
///
/// The X position of the module.
///
public virtual int X { get; set; }
///
/// The Y position of the module.
///
public virtual int Y { get; set; }
///
/// Bounds of this module (after factoring in the origin).
///
public Rectangle Boundaries
{
get
{
return new Rectangle((int)(X - origin.X), (int)(Y - origin.Y), Width, Height);
}
}
///
/// Origin of this module.
///
public Vector2 origin;
///
/// The parent of this module. May be null.
///
public UIModuleGroup parent;
///
/// Name of this module. For organizational/referencial purposes mostly.
///
public string name;
///
/// The color tint of this module.
///
public Color4 color = Color4.White;
///
/// Called every frame to update this module. Calculations and movement should go here.
///
/// Game time information.
public virtual void Update(GameTime gameTime)
{
}
///
/// Called every frame to draw this module. Anything that needs to be drawn should go here.
///
/// Batch used to draw.
public virtual void Draw(ConsistentSpriteBatch batch)
{
}
///
/// Converts the given rectangle to the coordinates of the parent.
///
/// Rectangle to convert.
///
public Rectangle ConvertToParentCoordinates(Rectangle rectangle)
{
if (parent != null)
{
Rectangle parentHitbox = parent.ConvertToParentCoordinates(rectangle);
int tX = rectangle.X + parentHitbox.X;
int tY = rectangle.Y + parentHitbox.Y;
return new Rectangle(tX, tY, rectangle.Width, rectangle.Height);
}
else
{
return rectangle;
}
}
///
/// Removes this module from the parent.
///
public void RemoveFromParent()
{
if (parent == null) throw new InvalidOperationException("Parent is null.");
parent.RemoveModule(this);
}
///
/// Called whenever the keyboard state is changed.
///
/// The current keyboard state.
/// Returning whether or not to continue to call the next listener.
public virtual bool KeyboardStateChanged(KeyboardState state)
{
return true;
}
///
/// Called whenever the state of the mouse changes. This includes movement.
///
/// The current state of the mouse.
/// Returning whether or not to continue to call the next listener.
public virtual bool MouseStateChanged(MouseState state)
{
return true;
}
///
/// Sets the origin to be the center using the and .
///
public virtual void CenterOrigin()
{
origin.X = Width / 2f;
origin.Y = Height / 2f;
}
///
/// Centers this module's on the horizontal axis relative to the parent .
///
/// True if possible and false if not.
public bool CenterHorizontally()
{
if (parent != null)
{
Rectangle rectangle = parent.Boundaries;
if (parent != null && rectangle.Width >= Boundaries.Width)
{
X = rectangle.Width / 2 + X;
return true;
}
}
return false;
}
///
/// Centers this module's origin on the vertical axis relative to the parent .
///
/// True if possible.
public virtual bool CenterVertically()
{
if (parent != null)
{
Rectangle rectangle = parent.Boundaries;
if (rectangle.Height >= Boundaries.Height)
{
Y = rectangle.Height / 2 + Y;
return true;
}
}
return false;
}
///
/// Sets the position and dimension of this module.
///
/// The rectangle that represents the position and dimensions of the module.
public virtual void SetPositionAndDimensions(Rectangle rectangle)
{
X = rectangle.X;
Y = rectangle.Y;
Width = rectangle.Width;
Height = rectangle.Height;
}
}
}