2018-11-30 02:41:06 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
2018-12-04 13:45:09 +00:00
|
|
|
|
using RecrownedAthenaeum.Input;
|
2019-01-30 13:46:58 +00:00
|
|
|
|
using RecrownedAthenaeum.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>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public class UIModule : IInputListener
|
|
|
|
|
{
|
2019-01-30 13:46:58 +00:00
|
|
|
|
private RectangleRenderer renderer;
|
|
|
|
|
private PrimitiveBatch primitiveBatch;
|
|
|
|
|
|
|
|
|
|
private bool debug = false;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Draws rectangle using the bounds of this module.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool Debugging { set { if (value) { if (renderer == null) renderer = new RectangleRenderer(primitiveBatch = new PrimitiveBatch()); } else { primitiveBatch.Dispose(); primitiveBatch = null; renderer = null; } } get { return debug; } }
|
|
|
|
|
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Bounds of this module.
|
|
|
|
|
/// </summary>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public Rectangle bounds;
|
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-01-14 06:34:35 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The parent of this module. May be null.
|
|
|
|
|
/// </summary>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public UIModuleGroup Parent;
|
2019-01-14 06:34:35 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Name of this module. For organizational/referencial purposes mostly.
|
|
|
|
|
/// </summary>
|
2018-11-30 02:41:06 +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>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public virtual void Draw(SpriteBatch batch)
|
|
|
|
|
{
|
2019-01-30 13:46:58 +00:00
|
|
|
|
if (Debugging) { renderer.Begin(false); renderer.DrawRectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height, Color.Red); renderer.End(); }
|
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
|
|
|
|
{
|
|
|
|
|
if (Parent != null)
|
|
|
|
|
{
|
2019-01-14 05:23:03 +00:00
|
|
|
|
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);
|
2018-11-30 02:41:06 +00:00
|
|
|
|
} else
|
|
|
|
|
{
|
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-01-28 00:06:55 +00:00
|
|
|
|
if (Parent == null) throw new InvalidOperationException("Parent is null.");
|
2018-11-30 02:41:06 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2019-01-28 00:06:55 +00:00
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|