using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using RecrownedAthenaeum.Input;
using RecrownedAthenaeum.Render;
using System;
namespace RecrownedAthenaeum.UI.Modular
{
///
/// Module for UI layout.
///
public class UIModule : IInputListener
{
///
/// The bounds before factoring in the origin.
///
public Rectangle situation;
///
/// Bounds of this module (after factoring in the origin).
///
public Rectangle Boundaries
{
get
{
return new Rectangle((int)(situation.X - origin.X), (int)(situation.Y - origin.Y), situation.Width, situation.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 Color color = Color.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 of the bounds.
///
public void CenterOrigin()
{
origin.X = situation.Width / 2f;
origin.Y = situation.Height / 2f;
}
///
/// Centers this module's origin 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)
{
situation.X = rectangle.Width / 2 + situation.X;
return true;
}
}
return false;
}
///
/// Centers this module's origin on the vertical axis relative to the parent .
///
/// True if possible.
public bool CenterVertically()
{
if (parent != null)
{
Rectangle rectangle = parent.Boundaries;
if (rectangle.Height >= Boundaries.Height)
{
situation.Y = rectangle.Height / 2 + situation.Y;
return true;
}
}
return false;
}
}
}