[C#] Event-programming, Delegates, and EventHandler

Not about OpenMW? Just about Morrowind in general? Have some random babble? Kindly direct it here.
Locked
User avatar
lgromanowski
Site Admin
Posts: 1193
Joined: 05 Aug 2011, 22:21
Location: Wroclaw, Poland
Contact:

[C#] Event-programming, Delegates, and EventHandler

Post by lgromanowski »

Star-Demon wrote: So - My current goal with my own projects is to make a good set of GUI elements and Input standards in C#/XNA then combine it with a game state system.

Once I fill that out, and get a nice base of code to be able to make what I want, it's all game programming from there.

So, I started with defining a base guielement, from which I'll abstract Menus, buttons, Dialogue windows, and so on.

Code: Select all

class GUIElement {

    //Variables
    String name;
    Vector2 position;   // X,Y position
    Vector2 size;       //X,Y Size
    bool hasFocus;
//    EventHandler ElementHandler; //How does this compare to using delegates?

    //Constructor
    public GUIElement() {
        name = "UNNAMED";
        position = Vector2.Zero;
        size = Vector2.Zero;
        hasFocus = false;

    }

    public GUIElement(String nameMe, Vector2 PosMe, Vector2 sizeMe, bool focusMe) {
        name = nameMe;
        position = PosMe;
        size = sizeMe;
        hasFocus = focusMe;
    }

    //Load
    public void loadContent() { }

    //Draw
    public void draw() { }

    //Update
    public void update() { }

    //public void hndleInput(){}

    //public void handleEvents(){}

    // Delegate definition of an event.
    public delegate void FiredEvent(object sender);

    // Instances of delegate event.
    //Mouse
    public FiredEvent onMouseOver;
    public FiredEvent onMouseOut;
    public FiredEvent onMouseClick;
    //D-keys and D-pad
    public FiredEvent onUpKey;
    public FiredEvent onDownKey;
    public FiredEvent onLeftKey;
    public FiredEvent onRightKey;
    //Button events
    public FiredEvent onActivate;
    public FiredEvent onCancel;
    public FiredEvent onInfoKey;
    public FiredEvent onExtraKey;
    public FiredEvent onCycleLeft;
    public FiredEvent onCycleRight;
    public FiredEvent onLStick;
    public FiredEvent onRStick;
    }
}
I am still very fuzzy on how delegates work, as well as what code should go in what classes. Each gui element will fire events, so where does the delegate declaration go, and where do the firing and implementation parts go?

I'm also not sure if I should have a gui manager - I would like to leave all the handleinput() and handleevents() to the gamestate.

I've been trying to find something decent online, but so far nothing has really ironed it out for me. If I can finally understand how to use events, I can understand and finish the game states.
Hircine wrote: http://neoforce.codeplex.com/

for all your XNA/C# gui needs.

full source code, so troll thru it all and see if that can help you understand it.
Star-Demon wrote:
Hircine wrote:http://neoforce.codeplex.com/

for all your XNA/C# gui needs.

full source code, so troll thru it all and see if that can help you understand it.
That looks really nice, (better than Cegui and SDL GUI samples) but I also would like to learn how to do it myself so I understand how it works.
Star-Demon wrote:
Hircine wrote:http://neoforce.codeplex.com/

for all your XNA/C# gui needs.

full source code, so troll thru it all and see if that can help you understand it.
Doesn't work in 2008, which my windows XNa demands...

I tried taking a look just now but the applications don't run...did XNA get an update or is the latest version for windows development only good on 2008?

EDIT: AHH scratch that. Got it installed properly and working...just have to start playing with it.
Star-Demon wrote: having trouble with this skin thing - the samples structure their program very differently, so I don't know if this will work properly anymore, but I think I can't load the skin directory properly, since the directory exception keeps getting thrown or an autodetect error happens.

Other than that, I'm not liking it so far - poorly supported, not a lot of documentation or practical tutorials online.
Hircine wrote: have you loaded the custom content processing files? there should be two.
i think one for cursor file and one for the skin IIRC
and have you added the required libraries and what not?

http://neoforce.codeplex.com/wikipage?t ... =Tutorials
Star-Demon wrote:
Hircine wrote:have you loaded the custom content processing files? there should be two.
i think one for cursor file and one for the skin IIRC
and have you added the required libraries and what not?

http://neoforce.codeplex.com/wikipage?t ... =Tutorials
The library is easy to reference, but none of the google-fu told me anything about anything referring to custom content importing - most of it wsa out-of-context stuff for the same error.

Yeah, I read that, it's one of three half-complete tutorials on getting started, one requires a second library that looks like a game library.

This stuff should always have:
Something like this:
http://msdn.microsoft.com/en-us/library/bb200104.aspx
or this:
http://www.php.net/
or even this:
http://download.oracle.com/javase/1.5.0 ... tring.html

and someone should make something like this:
http://www.lazyfoo.net/SDL_tutorials/

Anyways, I'll keep playing with it over time, but maybe I should also shop around for a GUI system before I just off and use one. In the meantime I'll keep working on mine.
Hircine wrote: i could zip up a demo of mine to show you how to get started. would you be interested in it?
Star-Demon wrote:
Hircine wrote:i could zip up a demo of mine to show you how to get started. would you be interested in it?
Sure - it can't hurt to see a normal implementation.
Locked