Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
When I worked on an editor with WinForms controls, I simple reference the game/engine to each control. I demand that each control is of a certain interface so that it contains specific properties that I wanted. Then each control can by itself control the game however it wants to. If your game is component-entity based that is. An example is that one control has some buttons that perhaps changes the state of the game and another control listens for these changes and changes its control dynamically. One control can have a listview of entities to be added to your game. This is an editor example. –  Deukalion May 21 '13 at 11:23
    
Short version, if you only want controls you don't have to reinvent the wheels unless you want to draw custom controls that aren't WinForms. It is better to add controls to a game instead of the game controling the controls. This you can customize the editor / game anyway you want it and make it more flexible. gamedev.stackexchange.com/questions/54998/… –  Deukalion May 21 '13 at 11:27
    
@Deukalion I'm trying to be performance wise and I'm afraid including full blown winforms might be a bit too much. I don't need most of it anyway. My controls will have someproperties that winforms doesn't have, for example every window will have a list of images for each corner and lines along the edge, I'm hoping it'll make creating custom sized windows css-like. –  kasztelan May 21 '13 at 13:29
    
Rendering custom controls that are redrawn each frame is much more performance demanding; and it's why many editors and alike use WinForms for controls because they don't demand being redrawn very often and won't use the gpu. –  Deukalion May 25 '13 at 19:31

3 Answers 3

There's a number of things you're sort of missing, but it's not something I blame you for - to be honest, UI frameworks involve quite a lot of logic you wouldn't quite think of, depending on how advanced they need to be. For one thing, to avoid having 'MouseOver' when the user's mouse is simply sitting still, you may want to save the last MouseState value, and make comparisons from the currentMouseState to lastMouseState (make sure it moved)

You'll also likely want a sorting order for UI Components, so you could display submenus over main menus. (Equip Large Shield to which character?) and probably some sort of system to determine which object in your game will "receive" the player's input - and it can only be one object at a time (you don't want your game's character moving around as someone is navigating a menu)

If you're willing to consider the idea of coding your menus in HTML and CSS, another answer I recall linked to this, which appears to be free for indie developments.

share|improve this answer

I use Neoforce controls. They act just like winform controls. If you can use winform, you can use NF.

http://neoforce.codeplex.com/

Works great in my XNA 4.0 project.

share|improve this answer
    
I was just about to post this, also the project is coming back alive! –  Cyral May 22 '13 at 1:28

I agree with Katana, making a GUI system is harder than it may appear. I was working on one until I found that I was spending more time writing & fixing the GUI library than working on my game.

Alternatives that I looked at were Ruminate XNA, and Awesomium (more specifically Awesomium .NET). I ended up picking Awesomium, and MindWorX and I built an Awesomium component for XNA. Personally, I love it. Working with HTML, CSS, and JavaScript libraries like jQuery, jQueryUI, and AngularJS have really made UI development much nicer for me.

If you really want to develop your own GUI for personal reasons, I'd take a quick peek at my abandoned project on BitBucket, just to get a sense of what you're getting in to. It has buttons, panels that can be moved around, list boxes (partially implemented), text boxes (wow hard), and the concept of screens that you can transition between. Your code above looks like a good start.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.