As I write my GUI for my latest game, probably one of the most complicated GUIs I’ve had to write because it’s 100% mouse driven and has a bazillion buttons to press on the interface, I find my code becoming spaghetti. I was curious how you guys handle your GUI code? Mine seems to be turning into a stupid amount of booleans and if/else statement, for example, here’s my current state of my left mouse button:
if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
//Toggle Menu tabs in and out
if ((mouseX > rightHandAlignX+4) && (mouseX < rightHandAlignX+12) && (mouseY > rightHandAlignY+144) && (mouseY < rightHandAlignY+182)){
rightHandToggle();
return;
}
if ((mouseX > leftHandAlignX+44) && (mouseX < leftHandAlignX+52) && (mouseY > leftHandAlignY+144) && (mouseY < leftHandAlignY+182)){
leftHandToggle();
return;
}
//Press buttons on right hand
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 8; y++) {
if ((mouseX > rightHandAlignX+16+(x*36)) && (mouseX < rightHandAlignX+49+(x*36)) && (mouseY > rightHandAlignY+31+(y*20)) && (mouseY < rightHandAlignY+48+(y*20))){
System.out.println("You pressed button: "+((x*8)+y));
//Light placement button
if ((x*8)+y == 0){
if (modePlaceLight){
modePlaceLight = false;
return;
}else{
modePlaceLight = true;
return;
}
}
//Place blood
if ((x*8)+y == 1){
if (modeBlood){
modeBlood = false;
return;
}else{
modeBlood = true;
return;
}
}
if ((x*8)+y == 2){
changeTime("day");
}
if ((x*8)+y == 3){
changeTime("night");
}
}
}
}
if (modePlaceLight){placeLight(getTileX(), getTileY()); return;}
if (modeBlood){addBlood() return;}
selectEntity(getMouseOnMapX(), getMouseOnMapY());
Basically it’s seeing where the mouse is, the forloop just assembles a set of 16 “box areas” in an 2x8 pattern, when you click on it, it checks what box you pressed and enables whatever is under that button. Then, below, it just has a ton of boolean and if/else checks (only 2 for now, but that will quickly expand to about 20 when I have to check for all the other situations) then it goes to mini-methods that tell other classes what to do.
So, do you guys run into this problem with complex GUIs as well? It really feels like a huge tangled mess, because almost everything in the game will be tied to the right and left click mouse buttons, meaning it’s probably going to be a pretty long little section of code that sort of sprawls out in the right direction based on a ton of if/else and booleans.
Really, the GUI itself works great and even though it looks messy it’s not actually hard to code, it just feels wrong. So I was curious what other’s experiences have been coding their own custom GUIs.