Jump to content
  • Advertisement
Sign in to follow this  
Askr

Custom GUI - Assigning functions to buttons

This topic is 2012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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. Edited by NightCreature83

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.
}



Edited by Nypyren

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!