I'm at the stage when I need to develop some kind of GUI with windows, clickable buttons, etc. It's going to be a RPG game so these components will be used extensively so I want them to be as flexible as possible.
I was thinking about something like winform controls funcionality though much simpler, I don't think I'll need anything more than a window/panel, button, label and image controls.
So far I made abstract Component class which basic attributes like position, width/height and two events - mouseover and click. All controls will be derived from this base class.
public abstract class Component
{
public event EventHandler MouseOver;
public void OnMouseOver()
{
if(MouseOver != null)
MouseOver(this, null);
}
public event EventHandler Click;
public void OnClick()
{
if (Click != null)
Click(this, null);
}
private Rectangle componentRectangle;
private bool isVisible;
}
My question is how to integrate my controls with the rest of the game, I was thinking about something like GUIManager class that would contain the list of controls and manage displaying them and firing events. I'll be calling the update method somewhere in the game loop.
public class GUIManager
{
private List<Component> componenets;
public void Update(Vector2 mouseLocation)
{
foreach (Component component in componenets)
{
if (component.IsVisible)
{ // mouse is over component
if (component.ComponentRectangle.Contains((int)mouseLocation.X, (int)mouseLocation.Y))
{
component.OnMouseOver();
}
}
}
}
}
Now lets say I want to show character's inventory screen. Is it a good way to just create the controls, add events and then them to GUIManager's controls list to take care of it? Or maybe you can suggest some entirely diffrent approach?