Unfortunately that's not my problem. I know which button is clicked (using my "clickCheck" function), but in order to assign a function to a button I have to take every single button and put it into an if statement (or switch-case statement like you used). If I had 30-50 buttons spread out across four different GUIs (like, say, inventory, player profile, main menu, combat, etc) I would have a
very large section of code that would be a bit out of place.
So I would instead like to know if there is a way to "assign" a function to a button so that I could later just call that function without using a click check. Here's an example of how it is now:
if(aMouse.LeftButton == ButtonState.Pressed) {
// check if FirstButton has been clicked
if(FirstButton.clickCheck(aMouse.X, aMouse.Y) == true) {
this.Quit(); // in this case, FirstButton's function is to quit
}
// check if SecondButton has been clicked
if(SecondButton.clickCheck(aMouse.X, aMouse.Y) == true) {
// something else
}
// check if ThirdButton has been clicked...
if(ThirdButton.clickCheck(...) == true) {
.
.
.
// and so on for all buttons, of which there could be too many
}
The problem with this (or a switch-case) statement is that I have to define my buttons from within the mouse's event check. If I have a lot of buttons that's a very very long set of if statements and isn't a very elegant way of doing things.
Instead I would like something like this:
if(aMouse.LeftButton == ButtonState.Pressed) {
// run one loop through a list of buttons
foreach(Button aButton in guiList[currentGUI].buttonList) {
// if a button returns a "true" for clickCheck ( i.e. it's been clicked)
if(aButton.clickCheck == true && aButton.Enabled == true) {
// run that Button's function
aButton.Function();
}
}
}
Let's say that FirstButton is the button that returns a "true" result of clickCheck() (i.e. it's been clicked). Somewhere earlier in the code, when I created that button, I could have made the button in such a way that when "aButton.Function()" was called it ran "this.Quit()".
So I was able to create the button somewhere else and assign a function to it for use later if its Function() method was called.
The best part about this method is that the main game code can be relatively clean as all the buttons (and their functions) can be created and assigned in another source file.