So, I’m building a GUI for my game from scratch and the further I’ve progressed, the harder it gets.
So far, I’ve used the following method:
I’ve got a button class, which represents a button. It can be hovered, clicked and rendered to the screen. My strategy has been to assign an integer to each button and when I click it, if it’s hovered, it returns this integer. I then use the integer in the parent class and perform actions in regards to it (like a switch statement with different methods, or assigning the index to arrays and such).
private final int PLAY = 100;
private final int SETTINGS = 101;
private final int INFO = 102;
private final int EXIT = 103;
private final int SAVE = 104;
private final int RESET = 105;
private final int MAIN = 106;
private final int SETTINGS2 = 107;
private final int MESSAGE = 108;
… construct the buttons, passing a particular int from above. Once a mouse event occurs I then iterate through the buttons and get the code from that which was pressed. I then do something like:
switch(code){
case -1:
break;
case PLAY: exit(true);
break;
case SETTINGS: resetSetts(); pageNr = 1;
break;
case INFO:
break;
case EXIT: exit(false);
break;
case SAVE: uSure = false; saveSetts(); pageNr = 0;
break;
case RESET: uSure = false; resetSetts();
break;
case MAIN: pageNr = 0; resetSetts();
break;
case SETTINGS2:
break;
case MESSAGE: uSure = true;
}
Now, to say the least, this is messy and I feel I can’t do the advanced stuff I want without confusing myself. I’ve now figured out another solution:
button.addClickAction(new CLICK_ACTION() {
@Override
public void action() {
exit(false);
}
});
This way I don’t need to keep track of all the integers and what they do, but the logic of the button is encapsulated in the button itself. My only question is whether this is good design?
E.g. Let’s say I have the gamecore - class. This class controls whether the game runs or not, obviously. Now, the gamecore is the root of the class hierarchy and imagine that at the end of the hierarchy three, there is a guiclass, which holds a button which is supposed to exit the game. With my new method, I’ll be able to call the exit method directly in that button, as in apposed to before when I had to return a particular int back to the core. But I feel like I’m violating something… I want to keep to a MVC model as much as I can. What are your thoughts on it?