mercredi 21 octobre 2015

C# XNA if-else and defaulting to first value

So I am developing a custom ButtonGUI class for my game. Here's the initialization of the button object:

// Button code:
ButtonGUI btn1 = new ButtonGUI("Button 1", new Rectangle(150, 300, (int)myFont.MeasureString(menuButtons[0]).X, (int)myFont.MeasureString(menuButtons[0]).Y), myFont, Color.CornflowerBlue);

Now consider this code:

// Draw() method:
btn1.Draw(spriteBatch);

if (btnHover)
{
    btn1.btnRect = new Rectangle(140, 300, (int)hoverFont.MeasureString(menuButtons[0]).X, (int)hoverFont.MeasureString(menuButtons[0]).Y);
    btn1.btnFont = hoverFont;
    btn1.btnColour = Color.Red;
}
else
{
    btn1.btnRect = new Rectangle(150, 300, (int)myFont.MeasureString(menuButtons[0]).X, (int)myFont.MeasureString(menuButtons[0]).Y);
    btn1.btnFont = myFont;
    btn1.btnColour = Color.CornflowerBlue;
}

This would be OK if I had only 1 button... But if I have like 10 buttons or more? This really isn't what DRY suggests. I feel like I'm missing something, there must be a way to return button properties to their default values once the condition is no longer met without doing the whole thing manually, or is there? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire