Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Askr

Member Since 13 Sep 2012
Offline Last Active Jan 08 2013 12:06 AM

Posts I've Made

In Topic: Custom GUI - Assigning functions to buttons

18 December 2012 - 01:13 AM

Try replacing

myButton.Click += new Action(myButton_Click);
with
myButton.Click += myButton_Click;
or
myButton.Click += () => {Console.Write("foo");};

From what I can tell in the MSDN article, the constructor format of Action doesn't do what you're thinking it does. I'll gladly take some correction on that if I didn't search deep enough though.

Sorry, this doesn't work. It doesn't even compile due to syntax errors.

I would track the mouse position and whether or not it's been clicked in the Window class, and then if the position of the cursor is inside of myButton when a click is dispatched call myButton.OnClick().

That was my thought as well. Since you already made a GUI: How did you listen for the mouse? Did you add an own class for this or where did you put it? :)

In Topic: Custom GUI - Assigning functions to buttons

14 December 2012 - 03:10 AM

Sorry for being absent from this topic so long, but I came around testing all your input just now. :]

Everything compiles well enough and I'm almost positive I didn't write anything too stupid, but somehow it just doesn't work. Let me paste some code and tell you what it does (and more importantly, what it does not):

This is from my Program.cs - I spared out the parts I don't consider interesting (FillColor and the like).
Window myWindow = new Window(50, 50, 400, 200);
Button myButton = new Button(0, 0, 150, 25);
myButton.setFillColor(new Color(255, 0, 0));
myButton.Click += new Action(myButton_Click);
myWindow.Add(myButton);
		  
myWindow.Draw();
while (MainWindow.IsOpen())
{
	MainWindow.DispatchEvents();
	MainWindow.Display();
}

[...]

static void myButton_Click()
{
	Console.Write("foo");
}

This is my button class
public event Action Click;
protected virtual void OnClick()
{
	if (Click != null)
		Click();
}

public Button(int x, int y, int w, int h)
{
	this.X = x;
	this.Y = y;
	this.Width = w;
	this.Height = h;
}

When I run the program the window with a button inside of it pops up, but wherever I click, the corresponding method just isn't called. It came to me that I don't have anything defined yet that does any mouse handling, so I figured I have to do this first. Here however I'm at a loss again. :/ Right now I have this Class called "GuiItem" from which "Button" and "Window" inheret. Should I add MouseEventHandlers for keeping track of the mouse in there, or rather create something new?

In Topic: Custom GUI - Assigning functions to buttons

28 November 2012 - 03:46 AM

Gtk# is being used in games as well as other programs.

Is that so? I figured from a quick overview that it was not. Then I shall look into it some more. :)

In Topic: Custom GUI - Assigning functions to buttons

28 November 2012 - 01:34 AM

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.

Sure thing, hit me up with anything you got. I'd love to see how you did it. Posted Image

@Nypyren

// Standard form
button.Click += FunctionName;

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. Posted Image

In Topic: Custom GUI - Assigning functions to buttons

27 November 2012 - 05:19 AM

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?

PARTNERS