Custom GUI - Assigning functions to buttons

Started by
14 comments, last by Khaiy 11 years, 4 months ago
Hey there,

so I've been trying to come up with a GUI (using SFML2.0 and C#) and while it's not really a problem to draw stuff onto the screen (or even checking if the mouse is above a button or if the button is clicked), I can't come up with an idea of how to make the buttons functional.

I want to have a class called "Button" with a Property "ButtonFunction". So when I create a button I can also associate an action to be triggered, when I click the button.

Can anyone give me a kickstart here? I'd also appreciate any other information regarding creating a useful custom GUI.
Thanks in advance!
Advertisement
Use a delegate that should be set on the buttonFunction property or passed as a construction parameter to the button class. This way you can provide completely new actions but use the same button class for all of your buttons.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Thanks for your fast reply. Although I don't quite understand how I can achieve the wanted functionality using delegates. :|
Would you mind elaborating your solution some more?
Yeah basically you do this

delegate void ButtonDelegate(); //This can return a value and take parameters and is only the definition of how the function should look

class Button
{
public Button(ButtonDelegate delegate)
{
m_delegate = delegate;
}

public onButtonClick()
{
m_delegate();
}

private ButtonDelegate m_delegate;
}

static class Wrapper
{
public static void printHelloWorld()
{
Console.WriteLn("Hello World! From delegate call"):
}
}

Button button = new Button(Wrapper.printHelloWorld);
button.onButtonClick();
}



See http://msdn.microsoft.com/en-us/library/900fyy8e%28v=vs.80%29.aspx for more information on delegates. A delegate is effictivly a function pointer and it is a way for you to give a function to something else and call that particular function from that something else, this is how callbacks in C/C++ are usually implemented.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

To build on what NightCreature said, you might try looking into C# events. They may not be necessary for what you're intending, but I didn't quite understand delegates until I started working with events. Here are two links that give a decent intro to events in practice.

I've put together a GUI using SFML 2.0 and C#, so if you'd like more information on what I did (which is functional, if inelegant) let me know.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

Parameterless events:


public event Action Click; // add this as a member of the button class

protected virtual void OnClick() // Add this function as a member of the button class
{
if (Click != null)
Click(); // This causes all of the attached delegates to be called.
}


// elsewhere, you hook the event using any of the following forms:

// Lambda expression form
button.Click += () => Trace.WriteLine("button clicked!");

// Lambda statement form
button.Click += () =>
{
// Inline code here
};

// Anonymous method form
button.Click += delegate
{
// Inline code here
};


// Standard form
button.Click += FunctionName; // Note: Visual studio will auto-complete this for you if you press TAB twice after typing the +=

and:

void FunctionName()
{
// Stuff here.
}


Events with parameters:


public event Action<MouseButton> Click; // add this as a member of the button class

protected virtual void OnClick(MouseButton mb) // Add this function as a member of the button class
{
if (Click != null)
Click(mb); // This causes all of the attached delegates to be called.
}


// elsewhere, you hook the event using any of the following forms:

// Lambda expression form
button.Click += (mb) => Trace.WriteLine("button clicked: " + mb);

// Lambda statement form
button.Click += (mb) =>
{
// Inline code here
Trace.WriteLine("A button was clicked.");
Trace.WriteLine("It was: "+mb);
};

// Anonymous method form when you don't need the parameter list
button.Click += delegate
{
// Inline code here
};


// Standard form
button.Click += FunctionName; // Note: Visual studio will auto-complete this for you if you press TAB twice after typing the +=

and:

void FunctionName(MouseButton mb)
{
// Stuff here.
}




I'd also appreciate any other information regarding creating a useful custom GUI.


Sure. Search for Gtk#, a C# application for making GUIs.

MonoDevelop
http://monodevelop.com/
Mono
http://www.mono-project.com/Main_Page

Gtk# - Can be used to make GUIs in C# targeting Mono and/ or .Net Framework.
http://www.mono-project.com/GtkSharp



Clinton

Personal life and your private thoughts always effect your career. Research is the intellectual backbone of game development and the first order. Version Control is crucial for full management of applications and software. The better the workflow pipeline, then the greater the potential output for a quality game. Completing projects is the last but finest order.

by Clinton, 3Ddreamer

Wow, thanks guys! That's certainly some interesting stuff to work through.

@NightCreature83
Thanks for explaining, I think I got it now! If I understood correctly delegates might be exactly what I've been looking for.

@Khaiy
I've put together a GUI using SFML 2.0 and C#, so if you'd like more information on what I did (which is functional, if inelegant) let me know.[/quote]
Sure thing, hit me up with anything you got. I'd love to see how you did it. biggrin.png

@Nypyren
// Standard form
button.Click += FunctionName;[/quote]
I always did stuff like this. The first time I heard of lambda expressions was from your post and although I read the msdn article about lambda expressions by now I'm still a bit lost. You say I could use any of those forms, but do the prior ones do anything better than the standard form?

@3Ddreamer
Thanks for those links, but I was referring to GUIs in games. Although Gtk# looks promising I don't think I could find a use for it within my current project. smile.png
Gtk# is being used in games as well as other programs.


Clinton

Personal life and your private thoughts always effect your career. Research is the intellectual backbone of game development and the first order. Version Control is crucial for full management of applications and software. The better the workflow pipeline, then the greater the potential output for a quality game. Completing projects is the last but finest order.

by Clinton, 3Ddreamer

Gtk# is being used in games as well as other programs.[/quote]
Is that so? I figured from a quick overview that it was not. Then I shall look into it some more. :)

This topic is closed to new replies.

Advertisement