140 lines
4.5 KiB
C#
140 lines
4.5 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using Microsoft.Xna.Framework.Input;
|
|
using RecrownedAthenaeum.Input;
|
|
using RecrownedAthenaeum.Render;
|
|
using System;
|
|
|
|
namespace RecrownedAthenaeum.UI.Modular
|
|
{
|
|
|
|
/// <summary>
|
|
/// Module for UI layout.
|
|
/// </summary>
|
|
public class UIModule : IInputListener
|
|
{
|
|
private RectangleRenderer rectangleRenderer;
|
|
|
|
/// <summary>
|
|
/// Draws rectangle using the bounds of this module.
|
|
/// </summary>
|
|
public bool Debugging { set { if (value) { if (rectangleRenderer == null) rectangleRenderer = new RectangleRenderer(); } else { rectangleRenderer?.Dispose(); rectangleRenderer = null; } } get { return rectangleRenderer != null; } }
|
|
|
|
/// <summary>
|
|
/// The bounds before factoring in the origin.
|
|
/// </summary>
|
|
public Rectangle bounds;
|
|
|
|
/// <summary>
|
|
/// Bounds of this module (after factoring in the origin).
|
|
/// </summary>
|
|
public Rectangle ActualBounds
|
|
{
|
|
get
|
|
{
|
|
return new Rectangle((int)(bounds.X - origin.X), (int)(bounds.Y - origin.Y), bounds.Width, bounds.Height);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Origin of this module.
|
|
/// </summary>
|
|
public Vector2 origin;
|
|
|
|
/// <summary>
|
|
/// The parent of this module. May be null.
|
|
/// </summary>
|
|
public UIModuleGroup Parent;
|
|
|
|
/// <summary>
|
|
/// Name of this module. For organizational/referencial purposes mostly.
|
|
/// </summary>
|
|
public string Name;
|
|
|
|
/// <summary>
|
|
/// The color tint of this module.
|
|
/// </summary>
|
|
public Color color = Color.White;
|
|
|
|
/// <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)
|
|
{
|
|
|
|
}
|
|
|
|
/// <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(SpriteBatch batch)
|
|
{
|
|
if (Debugging)
|
|
{
|
|
rectangleRenderer.Begin();
|
|
rectangleRenderer.Draw(ActualBounds.X, ActualBounds.Y, ActualBounds.Width, ActualBounds.Height, Color.Red);
|
|
rectangleRenderer.End();
|
|
}
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes this module from the parent.
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the origin to be the center of the bounds.
|
|
/// </summary>
|
|
public void CenterOrigin()
|
|
{
|
|
origin.X = bounds.Width / 2f;
|
|
origin.Y = bounds.Height / 2f;
|
|
}
|
|
}
|
|
}
|