3
\$\begingroup\$

I am making a GUI for my C# Monogame project. So since this was the first time I ever made a GUI that is not winforms-based I took a wrong approach in making it and thus I decided to change it entirely. But now the behavior is not the same.

TitleScreenOld.cs

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Abducted.GUIElements;
using Abducted.Helpers;

namespace Abducted.GameScreens
{
    public class TitleScreenOld : GameScreen
    {
        string[] menuTxt;
        ButtonGUI[] buttons;
        WindowGUI[] windows;
        CheckBoxGUI[] checkBoxes;
        WindowGUI playWindow, achieveWindow, optionsWindow, exitPromptBox;

        public TitleScreenOld() { }

        public override void LoadContent(ContentManager Content)
        {
            base.LoadContent(Content);

            if (tempMPBtn == null) tempMPBtn = Content.Load<Texture2D>(MainGame.TempObjPath + @"0") as Texture2D;

            xPos = 50;

            if (menuTxt == null)
            {
                menuTxt = new string[18];
                menuTxt[0] = "";

                menuTxt[1] = @"Version 1.0-alpha";
                menuTxt[2] = @"Copyright (C) 2016 PowerUser Studios";

                menuTxt[3] = @"Play Game";
                menuTxt[4] = @"Choose a game mode";
                menuTxt[5] = @"Single Player";
                menuTxt[6] = @"Multi Player";

                menuTxt[7] = @"Achievements";
                menuTxt[8] = @"Your Achievements";

                menuTxt[9] = @"Settings";
                menuTxt[10] = @"Game Settings";
                menuTxt[11] = @"Display FPS";
                menuTxt[12] = @"Enable Sound";

                menuTxt[13] = @"Exit Game";
                menuTxt[14] = @"Confirm?";
                menuTxt[15] = @"OK";
                menuTxt[16] = @"Cancel";
            }

            if (buttons == null)
            {
                buttons = new ButtonGUI[9];
                // Unused Dummy Button
                buttons[0] = new ButtonGUI(false, new Rectangle((int)MainGame.DefaultPos.X, (int)MainGame.DefaultPos.Y, (int)ButtonGUI.btnSize.X, (int)ButtonGUI.btnSize.Y), MainGame.EmptyString, new Rectangle((int)MainGame.DefaultPos.X, (int)MainGame.DefaultPos.Y, (int)MainGame.DefaultPos.X, (int)MainGame.DefaultPos.Y), MainGame.GameFontLarge, MainGame.NoTint, Content);

                // Real Buttons
                buttons[1] = new ButtonGUI(false, new Rectangle(xPos, 310, (int)ButtonGUI.btnSize.X, (int)ButtonGUI.btnSize.Y), menuTxt[3], new Rectangle(101, 328, (int)MainGame.GameFontLarge.MeasureString(menuTxt[3]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[3]).Y), MainGame.GameFontLarge, MainGame.NoTint, Content);
                buttons[2] = new ButtonGUI(false, new Rectangle(xPos, 390, (int)ButtonGUI.btnSize.X, (int)ButtonGUI.btnSize.Y), menuTxt[7], new Rectangle(73, 411, (int)MainGame.GameFontLarge.MeasureString(menuTxt[7]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[7]).Y), MainGame.GameFontLarge, MainGame.NoTint, Content);
                buttons[3] = new ButtonGUI(false, new Rectangle(xPos, 470, (int)ButtonGUI.btnSize.X, (int)ButtonGUI.btnSize.Y), menuTxt[9], new Rectangle(120, 488, (int)MainGame.GameFontLarge.MeasureString(menuTxt[9]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[9]).Y), MainGame.GameFontLarge, MainGame.NoTint, Content);
                buttons[4] = new ButtonGUI(false, new Rectangle(xPos, 550, (int)ButtonGUI.btnSize.X, (int)ButtonGUI.btnSize.Y), menuTxt[13], new Rectangle(106, 571, (int)MainGame.GameFontLarge.MeasureString(menuTxt[13]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[13]).Y), MainGame.GameFontLarge, MainGame.NoTint, Content);
                buttons[5] = new ButtonGUI(false, new Rectangle(550, 199, (int)ButtonGUI.btnSize.X, (int)ButtonGUI.btnSize.Y), menuTxt[5], new Rectangle(580, 217, (int)MainGame.GameFontLarge.MeasureString(menuTxt[5]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[5]).Y), MainGame.GameFontLarge, MainGame.NoTint, Content);
                buttons[6] = new ButtonGUI(true, new Rectangle(550, 279, (int)ButtonGUI.btnSize.X, (int)ButtonGUI.btnSize.Y), menuTxt[6], new Rectangle(588, 297, (int)MainGame.GameFontLarge.MeasureString(menuTxt[6]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[6]).Y), MainGame.GameFontLarge, MainGame.DisabledColor, Content);
                buttons[7] = new ButtonGUI(false, new Rectangle(548, 362, (int)ButtonGUI.btnSize.X, (int)ButtonGUI.btnSize.Y), menuTxt[15], new Rectangle(659, 382, (int)MainGame.GameFontLarge.MeasureString(menuTxt[15]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[15]).Y), MainGame.GameFontLarge, MainGame.BadColor, Content);
                buttons[8] = new ButtonGUI(false, new Rectangle(548, 442, (int)ButtonGUI.btnSize.X, (int)ButtonGUI.btnSize.Y), menuTxt[16], new Rectangle(629, 462, (int)MainGame.GameFontLarge.MeasureString(menuTxt[16]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[16]).Y), MainGame.GameFontLarge, MainGame.NoTint, Content);
            }

            windowPos = new Vector2(516.0f, 84.0f);
            if (windows == null)
            {
                windows = new WindowGUI[4];
                windows[0] = playWindow = new WindowGUI(windowPos, menuTxt[4], new Vector2(748.0f, 147.0f), true, Content);
                windows[1] = achieveWindow = new WindowGUI(windowPos, menuTxt[8], new Vector2(763.0f, 147.0f), true, Content);
                windows[2] = optionsWindow = new WindowGUI(windowPos, menuTxt[10], new Vector2(801.0f, 147.0f), true, Content);
                windows[3] = exitPromptBox = new WindowGUI(new Vector2(521.0f, 222.0f), menuTxt[14], new Vector2(610.0f, 249.0f), false, Content);
            }

            if (checkBoxes == null)
            {
                checkBoxes = new CheckBoxGUI[2];
                checkBoxes[0] = new CheckBoxGUI(false, false, new Rectangle(800, 200, 16, 16), MainGame.GameFontSmall, MainGame.NoTint, menuTxt[11], content);
                checkBoxes[1] = new CheckBoxGUI(true, true, new Rectangle(800, 300, 16, 16), MainGame.GameFontSmall, MainGame.NoTint, menuTxt[12], content);
            }

            if (MainGame.IsFPSEnabled) checkBoxes[0].IsChecked = true;
            else checkBoxes[0].IsChecked = false;
            checkBoxes[0].Click += new EventHandler(CheckBox0_Click);
        }

        public override void UnloadContent()
        {
            if (tempMPBtn != null) tempMPBtn = null;
            if (menuTxt != null) menuTxt = null;

            foreach (ButtonGUI btn in buttons) btn.UnloadContent();
            foreach (WindowGUI win in windows) win.UnloadContent();
            foreach (CheckBoxGUI checkBox in checkBoxes) checkBox.UnloadContent();

            base.UnloadContent();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            foreach (ButtonGUI btn in buttons) if (btn != buttons[6]) btn.Update();
            foreach (WindowGUI win in windows) win.Update();

            if ((!playWindow.IsOpen) && (!achieveWindow.IsOpen) && (!optionsWindow.IsOpen) && (!exitPromptBox.IsOpen)) for (int i = 0; i < 5; i++) CheckBtnInputTS(buttons[i]);

            CheckWinInputTS(playWindow, buttons[5], buttons[6]);
            CheckWinInputTS(achieveWindow, null);
            CheckWinInputTS(optionsWindow, null);
            CheckWinInputTS(exitPromptBox, buttons[7], buttons[8]);

            CheckBoxInputTS(optionsWindow, gameTime, checkBoxes[0]);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < 5; i++) buttons[i].Draw(spriteBatch);
            foreach (WindowGUI win in windows) win.Draw(spriteBatch);

            if (playWindow.IsOpen)
            {
                buttons[5].Draw(spriteBatch);
                // buttons[6].Draw(spriteBatch);
                // Dirty hack until the button bugs are fixed.
                spriteBatch.Draw(tempMPBtn, new Vector2(550.0f, 279.0f), MainGame.NoTint);
            }
            if (achieveWindow.IsOpen)
            {
                // TODO: Draw stuff
            }
            if (optionsWindow.IsOpen)
            {
                checkBoxes[0].Draw(spriteBatch);
                checkBoxes[1].Draw(spriteBatch);
            }
            if (exitPromptBox.IsOpen)
            {
                buttons[7].Draw(spriteBatch);
                buttons[8].Draw(spriteBatch);
            }

            base.Draw(spriteBatch);
        }

        public void CheckBtnInputTS(ButtonGUI btn)
        {
            if (MainGame.InputManager.MouseRect.Intersects(btn.texRect)) if ((!btn.IsHeld) || (!btn.IsDisabled)) btn.IsHovered = true;
            if (!btn.IsDisabled)
            {
                if (MainGame.InputManager.MouseRect.Intersects(btn.texRect))
                {
                    btn.IsHovered = true;
                    if (btn == buttons[7]) btn.Color = Color.Red;
                    else btn.Color = MainGame.HoverColor;

                    if (MainGame.InputManager.IsLeftClicked())
                    {
                        if (btn == buttons[1])
                        {
                            playWindow.IsOpen = true;
                            achieveWindow.IsOpen = false;
                            optionsWindow.IsOpen = false;
                            exitPromptBox.IsOpen = false;
                        }
                        if (btn == buttons[2])
                        {
                            achieveWindow.IsOpen = true;
                            playWindow.IsOpen = false;
                            optionsWindow.IsOpen = false;
                            exitPromptBox.IsOpen = false;
                        }

                        if (btn == buttons[3])
                        {
                            optionsWindow.IsOpen = true;
                            playWindow.IsOpen = false;
                            achieveWindow.IsOpen = false;
                            exitPromptBox.IsOpen = false;
                        }
                        if (btn == buttons[4])
                        {
                            exitPromptBox.IsOpen = true;
                            optionsWindow.IsOpen = false;
                            playWindow.IsOpen = false;
                            achieveWindow.IsOpen = false;
                        }
                        if (btn == buttons[5])
                        {
                            playWindow.IsOpen = false;
                            StopSong();
                            MainGame.ScreenManager.AddScreen(new CharacterScreen());
                        }
                        if (btn == buttons[6])
                        {
                            // TODO
                        }
                        if (btn == buttons[7])
                        {
                            exitPromptBox.IsOpen = false;
                            optionsWindow.IsOpen = false;
                            playWindow.IsOpen = false;
                            achieveWindow.IsOpen = false;
                            MainGame.ClassExit = true;
                        }
                        if (btn == buttons[8])
                        {
                            exitPromptBox.IsOpen = false;
                            optionsWindow.IsOpen = false;
                            playWindow.IsOpen = false;
                            achieveWindow.IsOpen = false;
                        }
                    }
                    if (MainGame.InputManager.IsLeftHeld()) if (btn == buttons[1]) btn.IsHeld = true;
                    if (MainGame.InputManager.IsLeftHeld()) if (btn == buttons[2]) btn.IsHeld = true;
                    if (MainGame.InputManager.IsLeftHeld()) if (btn == buttons[3]) btn.IsHeld = true;
                    if (MainGame.InputManager.IsLeftHeld()) if (btn == buttons[4]) btn.IsHeld = true;
                    if (MainGame.InputManager.IsLeftHeld()) if (btn == buttons[5]) btn.IsHeld = true;
                    if (MainGame.InputManager.IsLeftHeld()) if (btn == buttons[6]) btn.IsHeld = true;
                    if (MainGame.InputManager.IsLeftHeld()) if (btn == buttons[7]) btn.IsHeld = true;
                    if (MainGame.InputManager.IsLeftHeld()) if (btn == buttons[8]) btn.IsHeld = true;
                    if (!exitPromptBox.IsOpen) buttons[8].IsHeld = false;
                }
                else
                {
                    btn.IsHovered = false;
                    btn.IsHeld = false;
                    btn.Color = MainGame.NoTint;
                }
            }
            else btn.Color = MainGame.DisabledColor;

            if (MainGame.InputManager.IsKeyPressed(Keys.F1))
            {
                if ((!achieveWindow.IsOpen) && (!optionsWindow.IsOpen) && (!exitPromptBox.IsOpen))
                {
                    playWindow.IsOpen = true;
                }
            }
            if (MainGame.InputManager.IsKeyPressed(Keys.F2))
            {
                if ((!playWindow.IsOpen) && (!optionsWindow.IsOpen) && (!exitPromptBox.IsOpen))
                {
                    achieveWindow.IsOpen = true;
                }
            }
            if (MainGame.InputManager.IsKeyPressed(Keys.F3))
            {
                if ((!playWindow.IsOpen) && (!achieveWindow.IsOpen) && (!exitPromptBox.IsOpen))
                {
                    optionsWindow.IsOpen = true;
                }
            }
            if (MainGame.InputManager.IsKeyPressed(Keys.F4))
            {
                if ((!playWindow.IsOpen) && (!achieveWindow.IsOpen) && (!optionsWindow.IsOpen))
                {
                    exitPromptBox.IsOpen = true;
                }
            }
        }

        ///
        /// <summary>
        /// ButtonGUI value can be null not because we are passing the button
        /// that opened the window but the buttons that exist within the window.
        /// The main buttons are already calling CheckBtnInputTs(ButtonGUI btn)
        /// so no need to do that again.
        /// </summary>
        /// 
        public void CheckWinInputTS(WindowGUI win, params ButtonGUI[] btn)
        {
            if ((MainGame.InputManager.MouseRect.Intersects(win.ExitRect)) && (win.IsOpen))
            {
                win.IsExitHovered = true;
                if (MainGame.InputManager.IsLeftClicked()) win.IsOpen = false;

                if (MainGame.InputManager.IsLeftHeld()) win.IsExitHeld = true;
            }
            else if ((!MainGame.InputManager.MouseRect.Intersects(win.ExitRect)) || (!win.IsOpen))
            {
                win.IsExitHovered = false;
                win.IsExitHeld = false;
            }
            for (int i = 5; i < buttons.Length; i++) if ((btn != null) && (win.IsOpen)) CheckBtnInputTS(buttons[i]);
        }

        public void CheckBoxInputTS(WindowGUI win, GameTime gameTime, params CheckBoxGUI[] cb)
        {
            foreach (CheckBoxGUI checkBox in checkBoxes)
            {
                if (!checkBox.IsDisabled)
                {
                    if ((MainGame.InputManager.MouseRect.Intersects(checkBox.CheckBoxRect)) || (MainGame.InputManager.MouseRect.Intersects(checkBox.CheckBoxTxtRect)) || (MainGame.InputManager.MouseRect.Intersects(checkBox.CheckBoxMiddleRect)))
                    {
                        checkBox.IsHovered = true;
                        checkBox.Update();
                        checkBox.TxtColor = MainGame.HoverColor;
                    }
                    else if ((!MainGame.InputManager.MouseRect.Intersects(checkBox.CheckBoxRect)) || (!win.IsOpen))
                    {
                        checkBox.IsHovered = false;
                        checkBox.TxtColor = MainGame.NoTint;
                    }
                }
                else checkBox.TxtColor = MainGame.DisabledColor;
            }
        }

        private void CheckBox0_Click(object sender, EventArgs args)
        {
            MainGame.IsFPSEnabled = !MainGame.IsFPSEnabled;
        }
    }
}

ButtonGUIOld.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Abducted.Helpers;

namespace Abducted.GUIElements
{
    public class ButtonGUIOld
    {
        // TODO: Fix disabled buttons still emitting sound upon click.
        static Texture2D btnTex, btnTexHover, btnTexDisabled, btnTexHeld;
        public static Vector2 btnSize;
        SoundEffect clickSFX;
        string BTNTxt { get; set; }
        public bool IsClicked { get; set; }
        public bool IsHovered { get; set; }
        public bool IsDisabled { get; set; }
        public bool IsHeld { get; set; }
        public Rectangle BTNRect { get; set; }
        public SpriteFont SpriteFont { get; set; }
        public Color Color { get; set; }
        public Rectangle texRect { get; set; }

        public ButtonGUIOld(bool btnDisabled, Rectangle newTexRect, string newTxt, Rectangle newRect, SpriteFont newSpriteFont, Color newColor, ContentManager content)
        {
            texRect = newTexRect;
            IsDisabled = btnDisabled;
            SpriteFont = newSpriteFont;
            BTNRect = newRect;
            BTNTxt = newTxt;
            Color = newColor;
            if (btnTex == null) btnTex = content.Load<Texture2D>(MainGame.ButtonPath + @"0") as Texture2D;
            if (btnTexHover == null) btnTexHover = content.Load<Texture2D>(MainGame.ButtonPath + @"1") as Texture2D;
            if (btnTexDisabled == null) btnTexDisabled = content.Load<Texture2D>(MainGame.ButtonPath + @"2") as Texture2D;
            if (btnTexHeld == null) btnTexHeld = content.Load<Texture2D>(MainGame.ButtonPath + @"3") as Texture2D;
            if (clickSFX == null) clickSFX = content.Load<SoundEffect>(MainGame.BGSoundPath + @"1") as SoundEffect;
            if (btnTex != null) btnSize = new Vector2(btnTex.Width, btnTex.Height);
        }

        public void UnloadContent()
        {
            if (btnTex != null) btnTex = null;
            if (SpriteFont != null) SpriteFont = null;
            if (BTNRect != MainGame.EmptyRect) BTNRect = MainGame.EmptyRect;
            if (!string.IsNullOrEmpty(BTNTxt)) BTNTxt = null;
            if (Color != MainGame.SmallColor) Color = MainGame.SmallColor;
            if (btnTexHover != null) btnTexHover = null;
            if (btnTexDisabled != null) btnTexDisabled = null;
            if (btnTexHeld != null) btnTexHeld = null;
            if (clickSFX != null) clickSFX = null;
        }

        public void Update()
        {
            if (!MainGame.IsSoundDisabled)
            {
                if ((IsHovered) && (!IsDisabled))
                {
                    if (MainGame.InputManager.IsLeftClicked())
                    {
                        clickSFX.Play();
                        IsHovered = false;
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if ((SpriteFont != null) && (!string.IsNullOrEmpty(BTNTxt)))
            {
                if ((btnTex != null) && (btnTexHover != null) && (btnTexHeld != null) && (btnTexDisabled != null))
                {
                    if (IsHeld) spriteBatch.Draw(btnTexHeld, texRect, MainGame.NoTint);
                    else if (IsHovered) spriteBatch.Draw(btnTexHover, texRect, MainGame.NoTint);
                    else if (IsDisabled) spriteBatch.Draw(btnTexDisabled, texRect, MainGame.NoTint);
                    else spriteBatch.Draw(btnTex, texRect, MainGame.NoTint);
                }
                TextOutliner.DrawBorderedText(spriteBatch, SpriteFont, BTNTxt, new Vector2(BTNRect.X, BTNRect.Y), Color);
            }
        }
    }
}


From here start the new classes.

GUIElement.cs

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Abducted.Helpers;

namespace Abducted.GUIElements
{
    public abstract class GUIElement
    {
        protected SpriteFont Font;
        protected string Text;
        protected SoundEffect ClickSFX;
        public Rectangle Bounds, TextRect;
        public Color ForeColor, BackColor;
        public bool IsDisabled, IsHovered, IsClicked, IsHeld;
        public event EventHandler Click;

        public GUIElement(Rectangle newBounds, Rectangle textRect, SpriteFont font, string newTxt, bool isDisabled, ContentManager content)
        {
            Bounds = newBounds;
            TextRect = textRect;
            Text = newTxt;
            IsDisabled = isDisabled;
            Font = font;
            ForeColor = MainGame.NoTint;
            BackColor = MainGame.NoTint;
            ClickSFX = content.Load<SoundEffect>(MainGame.BGSoundPath + @"1") as SoundEffect;
        }

        public virtual void UnloadContent()
        {
            if (Bounds != Rectangle.Empty) Bounds = MainGame.EmptyRect;
            if (TextRect != Rectangle.Empty) TextRect = MainGame.EmptyRect;
            if (Font != null) Font = null;
            if (!string.IsNullOrEmpty(Text)) Text = null;
            if (ForeColor != MainGame.SmallColor) ForeColor = MainGame.SmallColor;
            if (BackColor != MainGame.SmallColor) BackColor = MainGame.SmallColor;
            if (ClickSFX != null) ClickSFX = null;
        }

        public virtual void Update()
        {
            if (!IsDisabled)
            {
                if (Bounds.Contains(MainGame.InputManager.MouseRect))
                {
                    if (MainGame.InputManager.IsLeftClicked()) IsClicked = true;
                    ForeColor = MainGame.HoverColor;
                    if (!IsHeld) IsHovered = true;
                    if (MainGame.InputManager.IsLeftClicked()) if (Click != null) Click(this, EventArgs.Empty);
                    if (MainGame.InputManager.IsLeftHeld()) IsHeld = true;
                    else IsHeld = false;
                }
                else
                {
                    IsHovered = false;
                    IsHeld = false;
                    ForeColor = MainGame.NoTint;
                }
            }
            else ForeColor = MainGame.DisabledColor;
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if ((Font != null) && (!string.IsNullOrEmpty(Text))) TextOutliner.DrawBorderedText(spriteBatch, Font, Text, new Vector2(TextRect.X, TextRect.Y), ForeColor);
        }
    }
}

ButtonGUINew.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Abducted.GUIElements
{
    public class ButtonGUINew : GUIElement
    {
        static Texture2D btnTex, btnTexHover, btnTexDisabled, btnTexHeld;
        public static Vector2 btnSize;
        public Rectangle boundsRect { get; set; }
        public Rectangle txtRect { get; set; }
        string BTNTxt { get; set; }

        public ButtonGUINew(Rectangle boundsRectangle, Rectangle txtRectangle, SpriteFont font, string newTxt, bool isDisabled, ContentManager content)
        : base(boundsRectangle, txtRectangle, font, newTxt, isDisabled, content)
        {
            boundsRect = boundsRectangle;
            BTNTxt = newTxt;
            IsDisabled = isDisabled;
            if (btnTex == null) btnTex = content.Load<Texture2D>(MainGame.ButtonPath + @"0") as Texture2D;
            if (btnTexHover == null) btnTexHover = content.Load<Texture2D>(MainGame.ButtonPath + @"1") as Texture2D;
            if (btnTexDisabled == null) btnTexDisabled = content.Load<Texture2D>(MainGame.ButtonPath + @"2") as Texture2D;
            if (btnTexHeld == null) btnTexHeld = content.Load<Texture2D>(MainGame.ButtonPath + @"3") as Texture2D;
            if (btnTex != null) btnSize = new Vector2(btnTex.Width, btnTex.Height);
        }

        public override void UnloadContent()
        {
            base.UnloadContent();
            if (btnTex != null) btnTex = null;
            if (boundsRect != MainGame.EmptyRect) boundsRect = MainGame.EmptyRect;
            if (txtRect != MainGame.EmptyRect) txtRect = MainGame.EmptyRect;
            if (!string.IsNullOrEmpty(BTNTxt)) BTNTxt = null;
            if (btnTexHover != null) btnTexHover = null;
            if (btnTexDisabled != null) btnTexDisabled = null;
            if (btnTexHeld != null) btnTexHeld = null;
        }

        public override void Update()
        {
            base.Update();
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if ((btnTex != null) && (btnTexHover != null) && (btnTexHeld != null) && (btnTexDisabled != null))
            {
                if (IsHeld) spriteBatch.Draw(btnTexHeld, boundsRect, MainGame.NoTint);
                else if (IsHovered) spriteBatch.Draw(btnTexHover, boundsRect, MainGame.NoTint);
                else if (IsDisabled) spriteBatch.Draw(btnTexDisabled, boundsRect, MainGame.NoTint);
                else spriteBatch.Draw(btnTex, boundsRect, MainGame.NoTint);
            }

            base.Draw(spriteBatch);
        }
    }
}

TitleScreenNew.cs

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Abducted.GUIElements;
using Abducted.Helpers;

namespace Abducted.GameScreens
{
    public class TitleScreenNew : GameScreen
    {
        string[] menuTxt;
        ButtonGUINew[] buttons;
        WindowGUI[] windows;
        WindowGUI playWindow, achieveWindow, optionsWindow, exitPromptBox;

        public TitleScreenNew() { }

        public override void LoadContent(ContentManager Content)
        {
            base.LoadContent(Content);

            if (tempMPBtn == null) tempMPBtn = Content.Load<Texture2D>(MainGame.TempObjPath + @"0") as Texture2D;

            xPos = 50;

            if (menuTxt == null)
            {
                menuTxt = new string[18];
                menuTxt[0] = "";

                menuTxt[1] = @"Version 1.0-alpha";
                menuTxt[2] = @"Copyright (C) 2016 PowerUser Studios";

                menuTxt[3] = @"Play Game";
                menuTxt[4] = @"Choose a game mode";
                menuTxt[5] = @"Single Player";
                menuTxt[6] = @"Multi Player";

                menuTxt[7] = @"Achievements";
                menuTxt[8] = @"Your Achievements";

                menuTxt[9] = @"Settings";
                menuTxt[10] = @"Game Settings";
                menuTxt[11] = @"Display FPS";
                menuTxt[12] = @"Enable Sound";

                menuTxt[13] = @"Exit Game";
                menuTxt[14] = @"Confirm?";
                menuTxt[15] = @"OK";
                menuTxt[16] = @"Cancel";
            }

            if (buttons == null)
            {
                buttons = new ButtonGUINew[9];
                // Unused Dummy Button
                buttons[0] = new ButtonGUINew(new Rectangle((int)MainGame.DefaultPos.X, (int)MainGame.DefaultPos.Y, (int)ButtonGUINew.btnSize.X, (int)ButtonGUINew.btnSize.Y), new Rectangle((int)MainGame.DefaultPos.X, (int)MainGame.DefaultPos.Y, (int)MainGame.DefaultPos.X, (int)MainGame.DefaultPos.Y), MainGame.GameFontLarge, MainGame.EmptyString, false, Content);

                // Real Buttons
                buttons[1] = new ButtonGUINew(new Rectangle(xPos, 310, (int)ButtonGUINew.btnSize.X, (int)ButtonGUINew.btnSize.Y), new Rectangle(101, 328, (int)MainGame.GameFontLarge.MeasureString(menuTxt[3]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[3]).Y), MainGame.GameFontLarge, menuTxt[3], false, Content);
                buttons[2] = new ButtonGUINew(new Rectangle(xPos, 390, (int)ButtonGUINew.btnSize.X, (int)ButtonGUINew.btnSize.Y), new Rectangle(73, 411, (int)MainGame.GameFontLarge.MeasureString(menuTxt[7]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[7]).Y), MainGame.GameFontLarge, menuTxt[7], false, Content);
                buttons[3] = new ButtonGUINew(new Rectangle(xPos, 470, (int)ButtonGUINew.btnSize.X, (int)ButtonGUINew.btnSize.Y), new Rectangle(120, 488, (int)MainGame.GameFontLarge.MeasureString(menuTxt[9]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[9]).Y), MainGame.GameFontLarge, menuTxt[9], false, Content);
                buttons[4] = new ButtonGUINew(new Rectangle(xPos, 550, (int)ButtonGUINew.btnSize.X, (int)ButtonGUINew.btnSize.Y), new Rectangle(106, 571, (int)MainGame.GameFontLarge.MeasureString(menuTxt[13]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[13]).Y), MainGame.GameFontLarge, menuTxt[13], false, Content);
                buttons[5] = new ButtonGUINew(new Rectangle(550, 199, (int)ButtonGUINew.btnSize.X, (int)ButtonGUINew.btnSize.Y), new Rectangle(580, 217, (int)MainGame.GameFontLarge.MeasureString(menuTxt[5]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[5]).Y), MainGame.GameFontLarge, menuTxt[5], false, Content);
                buttons[6] = new ButtonGUINew(new Rectangle(550, 279, (int)ButtonGUINew.btnSize.X, (int)ButtonGUINew.btnSize.Y), new Rectangle(588, 297, (int)MainGame.GameFontLarge.MeasureString(menuTxt[6]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[6]).Y), MainGame.GameFontLarge, menuTxt[6], true, Content);
                buttons[7] = new ButtonGUINew(new Rectangle(548, 362, (int)ButtonGUINew.btnSize.X, (int)ButtonGUINew.btnSize.Y), new Rectangle(659, 382, (int)MainGame.GameFontLarge.MeasureString(menuTxt[15]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[15]).Y), MainGame.GameFontLarge, menuTxt[15], false, Content);
                buttons[8] = new ButtonGUINew(new Rectangle(548, 442, (int)ButtonGUINew.btnSize.X, (int)ButtonGUINew.btnSize.Y), new Rectangle(629, 462, (int)MainGame.GameFontLarge.MeasureString(menuTxt[16]).X, (int)MainGame.GameFontLarge.MeasureString(menuTxt[16]).Y), MainGame.GameFontLarge, menuTxt[16], false, Content);
            }

            windowPos = new Vector2(516.0f, 84.0f);
            if (windows == null)
            {
                windows = new WindowGUI[4];
                windows[0] = playWindow = new WindowGUI(windowPos, menuTxt[4], new Vector2(748.0f, 147.0f), true, Content);
                windows[1] = achieveWindow = new WindowGUI(windowPos, menuTxt[8], new Vector2(763.0f, 147.0f), true, Content);
                windows[2] = optionsWindow = new WindowGUI(windowPos, menuTxt[10], new Vector2(801.0f, 147.0f), true, Content);
                windows[3] = exitPromptBox = new WindowGUI(new Vector2(521.0f, 222.0f), menuTxt[14], new Vector2(610.0f, 249.0f), false, Content);
            }

            if (checkBoxes == null)
            {
                checkBoxes = new CheckBoxGUI[2];
                checkBoxes[0] = new CheckBoxGUI(false, false, new Rectangle(800, 200, 16, 16), MainGame.GameFontSmall, MainGame.NoTint, menuTxt[11], content);
                checkBoxes[1] = new CheckBoxGUI(true, true, new Rectangle(800, 300, 16, 16), MainGame.GameFontSmall, MainGame.NoTint, menuTxt[12], content);
            }

            if (MainGame.IsFPSEnabled) checkBoxes[0].IsChecked = true;
            else checkBoxes[0].IsChecked = false;
            checkBoxes[0].Click += new EventHandler(CheckBox0_Click);
        }

        public override void UnloadContent()
        {
            if (tempMPBtn != null) tempMPBtn = null;
            if (menuTxt != null) menuTxt = null;

            foreach (ButtonGUINew btn in buttons) btn.UnloadContent();
            foreach (WindowGUI win in windows) win.UnloadContent();
            foreach (CheckBoxGUI checkBox in checkBoxes) checkBox.UnloadContent();

            base.UnloadContent();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            foreach (WindowGUI win in windows) win.Update();

            if ((!playWindow.IsOpen) && (!achieveWindow.IsOpen) && (!optionsWindow.IsOpen) && (!exitPromptBox.IsOpen))
            {
                for (int i = 1; i < 5; i++)
                {
                    buttons[i].Update();
                    buttons[i].IsHeld = false;
                }
                buttons[1].Click += new EventHandler(Button1_Click);
                buttons[2].Click += new EventHandler(Button2_Click);
                buttons[3].Click += new EventHandler(Button3_Click);
                buttons[4].Click += new EventHandler(Button4_Click);
            }
            else
            {
                if (playWindow.IsOpen) buttons[1].IsHeld = true;
                if (achieveWindow.IsOpen) buttons[2].IsHeld = true;
                if (optionsWindow.IsOpen) buttons[3].IsHeld = true;
                if (exitPromptBox.IsOpen) buttons[4].IsHeld = true;
            }

            if (playWindow.IsOpen)
            {
                buttons[5].Update();
                buttons[6].Update();
                buttons[5].Click += new EventHandler(Button5_Click);
                buttons[6].Click += new EventHandler(Button6_Click);
            }
            if (exitPromptBox.IsOpen)
            {
                buttons[7].Update();
                buttons[8].Update();
                buttons[7].Click += new EventHandler(Button7_Click);
                buttons[8].Click += new EventHandler(Button8_Click);
            }

            if (MainGame.InputManager.MouseRect.Intersects(buttons[7].Bounds)) buttons[7].ForeColor = Color.Red;

            if (MainGame.InputManager.IsKeyPressed(Keys.F1))
            {
                if ((!achieveWindow.IsOpen) && (!optionsWindow.IsOpen) && (!exitPromptBox.IsOpen))
                {
                    playWindow.IsOpen = true;
                }
            }
            if (MainGame.InputManager.IsKeyPressed(Keys.F2))
            {
                if ((!playWindow.IsOpen) && (!optionsWindow.IsOpen) && (!exitPromptBox.IsOpen))
                {
                    achieveWindow.IsOpen = true;
                }
            }
            if (MainGame.InputManager.IsKeyPressed(Keys.F3))
            {
                if ((!playWindow.IsOpen) && (!achieveWindow.IsOpen) && (!exitPromptBox.IsOpen))
                {
                    optionsWindow.IsOpen = true;
                }
            }
            if (MainGame.InputManager.IsKeyPressed(Keys.F4))
            {
                if ((!playWindow.IsOpen) && (!achieveWindow.IsOpen) && (!optionsWindow.IsOpen))
                {
                    exitPromptBox.IsOpen = true;
                }
            }

            CheckWinInputTS(playWindow);
            CheckWinInputTS(achieveWindow);
            CheckWinInputTS(optionsWindow);
            CheckWinInputTS(exitPromptBox);

            CheckBoxInputTS(optionsWindow, gameTime, checkBoxes[0]);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < 5; i++) buttons[i].Draw(spriteBatch);

            foreach (WindowGUI win in windows) win.Draw(spriteBatch);

            if (playWindow.IsOpen)
            {
                buttons[5].Draw(spriteBatch);
                // buttons[6].Draw(spriteBatch);
                // Dirty hack until the button bugs are fixed.
                spriteBatch.Draw(tempMPBtn, new Vector2(550.0f, 279.0f), MainGame.NoTint);
            }
            if (achieveWindow.IsOpen)
            {
                // TODO: Draw stuff
            }
            if (optionsWindow.IsOpen)
            {
                checkBoxes[0].Draw(spriteBatch);
                checkBoxes[1].Draw(spriteBatch);
            }
            if (exitPromptBox.IsOpen)
            {
                buttons[7].Draw(spriteBatch);
                buttons[8].Draw(spriteBatch);
            }

            base.Draw(spriteBatch);
        }
        public void CheckWinInputTS(WindowGUI win)
        {
            if ((MainGame.InputManager.MouseRect.Intersects(win.ExitRect)) && (win.IsOpen))
            {
                win.IsExitHovered = true;
                if (MainGame.InputManager.IsLeftClicked()) win.IsOpen = false;

                if (MainGame.InputManager.IsLeftHeld()) win.IsExitHeld = true;
            }
            else if ((!MainGame.InputManager.MouseRect.Intersects(win.ExitRect)) || (!win.IsOpen))
            {
                win.IsExitHovered = false;
                win.IsExitHeld = false;
            }
        }

        public void CheckBoxInputTS(WindowGUI win, GameTime gameTime, params CheckBoxGUI[] cb)
        {
            foreach (CheckBoxGUI checkBox in checkBoxes)
            {
                if (!checkBox.IsDisabled)
                {
                    if ((MainGame.InputManager.MouseRect.Intersects(checkBox.CheckBoxRect)) || (MainGame.InputManager.MouseRect.Intersects(checkBox.CheckBoxTxtRect)) || (MainGame.InputManager.MouseRect.Intersects(checkBox.CheckBoxMiddleRect)))
                    {
                        checkBox.IsHovered = true;
                        checkBox.Update();
                        checkBox.TxtColor = MainGame.HoverColor;
                    }
                    else if ((!MainGame.InputManager.MouseRect.Intersects(checkBox.CheckBoxRect)) || (!win.IsOpen))
                    {
                        checkBox.IsHovered = false;
                        checkBox.TxtColor = MainGame.NoTint;
                    }
                }
                else checkBox.TxtColor = MainGame.DisabledColor;
            }
        }

        private void ClearBtn()
        {
            foreach (ButtonGUINew btn in buttons)
            {
                btn.ForeColor = MainGame.NoTint;
                btn.IsHovered = false;
                btn.IsHeld = false;
            }
        }

        private void Button1_Click(object sender, EventArgs args)
        {
            playWindow.IsOpen = true;
            achieveWindow.IsOpen = false;
            optionsWindow.IsOpen = false;
            exitPromptBox.IsOpen = false;
        }
        private void Button2_Click(object sender, EventArgs args)
        {
            achieveWindow.IsOpen = true;
            playWindow.IsOpen = false;
            optionsWindow.IsOpen = false;
            exitPromptBox.IsOpen = false;
        }
        private void Button3_Click(object sender, EventArgs args)
        {
            optionsWindow.IsOpen = true;
            playWindow.IsOpen = false;
            achieveWindow.IsOpen = false;
            exitPromptBox.IsOpen = false;
        }

        private void Button4_Click(object sender, EventArgs args)
        {
            exitPromptBox.IsOpen = true;
            optionsWindow.IsOpen = false;
            playWindow.IsOpen = false;
            achieveWindow.IsOpen = false;
        }

        private void Button5_Click(object sender, EventArgs args)
        {
            ClearBtn();
            playWindow.IsOpen = false;
            StopSong();
            MainGame.ScreenManager.AddScreen(new CharacterScreen());
        }

        private void Button6_Click(object sender, EventArgs args)
        {
            // TODO
            // ClearBtn();
        }

        private void Button7_Click(object sender, EventArgs args)
        {
            ClearBtn();
            exitPromptBox.IsOpen = false;
            optionsWindow.IsOpen = false;
            playWindow.IsOpen = false;
            achieveWindow.IsOpen = false;
            MainGame.ClassExit = true;
        }

        private void Button8_Click(object sender, EventArgs args)
        {
            exitPromptBox.IsOpen = false;
            optionsWindow.IsOpen = false;
            playWindow.IsOpen = false;
            achieveWindow.IsOpen = false;
            ClearBtn();
        }

        private void CheckBox0_Click(object sender, EventArgs args)
        {
            MainGame.IsFPSEnabled = !MainGame.IsFPSEnabled;
        }
    }
}

Every time I click on a ButtonGUI it while the left mouse button is held the ButtonGUI also has IsHeld = true.

Now the old behavior was:

  1. I click on a ButtonGUI.
  2. The specified WindowGUI opens.
  3. ButtonGUI has IsHeld = true while window is open and false when I close it.

Current code behavior:

  1. I click on a ButtonGUI.
  2. The specified WindowGUI opens. Good so far...
  3. But now ButtonGUI has IsHeld = false while window is open. So I was forced to do the dirty hacks that exist in TitleScreenNew.cs just to make it look the way I want it to.

Is this caused because firing events has delay? It would make sense as when I click the ButtonGUI it still has the same behavior but the WindowGUI opens with delay and the ButtonGUI no longer has IsHeld = true since the mouse was released.

InputManager.cs

public bool IsLeftClicked()
{
    if ((PrevMouseState.LeftButton == ButtonState.Pressed) && (CurrMouseState.LeftButton == ButtonState.Released)) return true;
    PrevMouseState = CurrMouseState;
    return false;
}

public bool IsLeftHeld()
{
    if ((PrevMouseState.LeftButton == ButtonState.Pressed) && (CurrMouseState.LeftButton == ButtonState.Pressed)) return true;
    PrevMouseState = CurrMouseState;
    return false;
}

I'd like someone to analyze this for me. I don't want to be forced to keep these hacks in my TitleScreen class.

\$\endgroup\$
10
  • \$\begingroup\$ Welcome to Code Review! I think your code does work, I just want to clarify: does your code work, or is it broken? \$\endgroup\$
    – SirPython
    Commented Mar 13, 2016 at 22:16
  • \$\begingroup\$ It works! What I am concerned about is a visual issue. When ButtonGUI.IsHeld = true the button texture changes as you can see in both ButtonGUI classes in Draw() method. \$\endgroup\$
    – Johny P.
    Commented Mar 13, 2016 at 22:18
  • \$\begingroup\$ Is that intended? Because, if not and you are asking for help for how to fix that problem, then this post is off-topic. \$\endgroup\$
    – SirPython
    Commented Mar 13, 2016 at 22:22
  • \$\begingroup\$ The old behavior as I described it in the question body is intended. The new one, however is not. I just made a bunch of workarounds to make it look the same. And I'm not happy with that. \$\endgroup\$
    – Johny P.
    Commented Mar 13, 2016 at 22:28
  • \$\begingroup\$ So can you help me out @SirPython ? \$\endgroup\$
    – Johny P.
    Commented Mar 14, 2016 at 11:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.