public bool Handle(IEvent evt)
        {
            if (evt is EnterMouseUIModeEvent)
            {
                UpdateButtonBindingsForKeyboard();
            }
            if (evt is EnterGamePadUIModeEvent)
            {
                UpdateButtonBindingsForGamePad(((EnterGamePadUIModeEvent)evt).PlayerIndex);
            }
            if (evt is GamePadUIModeOperatorChangedEvent)
            {
                UpdateButtonBindingsForGamePad(((GamePadUIModeOperatorChangedEvent)evt).PlayerIndex);
            }

            GamePadButtonDownEvent gpbde = evt as GamePadButtonDownEvent;

            if (gpbde != null)
            {
                if (bindingMode == BindingMode.none && gpbde._pressedButton == Buttons.B)
                {
                    _root.FindSelectedWidget().isSelected = false;
                    if (isOnLeftSide == true)
                    {
                        ChangeState(new UIMenuGameState(GameManager, SharedState));
                        return(true);
                    }
                    if (isOnLeftSide == false)
                    {
                        foreach (DropDownPanel ddp in _root.FindWidgetsByClass("DropDown"))
                        {
                            ddp.ShowDropDown = false;
                            ddp.isSelected   = false;
                        }

                        switch (leftSideIndex)
                        {
                        case 0:
                            ((Panel)_root.FindWidgetByID("display_options_menu_right_panel")).Hidden = true;
                            ((Button)_root.FindWidgetByID("Display")).isSelected = true;
                            isOnLeftSide = true;
                            break;

                        case 1:
                            ((Panel)_root.FindWidgetByID("controls_options_menu_right_panel")).Hidden = true;
                            ((Button)_root.FindWidgetByID("Controls")).isSelected = true;
                            isOnLeftSide = true;
                            break;

                        case 2:
                            ((Panel)_root.FindWidgetByID("game_options_menu_right_panel")).Hidden = true;
                            ((Button)_root.FindWidgetByID("GameSettings")).isSelected             = true;
                            isOnLeftSide = true;
                            break;
                        }
                    }
                }
                if (bindingMode == BindingMode.gamepad)
                {
                    BindingControl other1 = BindingControl.none;
                    BindingControl other2 = BindingControl.none;
                    switch (gpbde._pressedButton)
                    {
                    case Buttons.A:
                    case Buttons.B:
                    case Buttons.X:
                    case Buttons.Y:
                    case Buttons.LeftTrigger:
                    case Buttons.LeftShoulder:
                    case Buttons.LeftStick:
                    case Buttons.RightTrigger:
                    case Buttons.RightShoulder:
                    case Buttons.RightStick:
                    case Buttons.DPadUp:
                    case Buttons.DPadDown:
                    case Buttons.DPadLeft:
                    case Buttons.DPadRight:
                    case Buttons.LeftThumbstickUp:
                    case Buttons.LeftThumbstickDown:
                    case Buttons.LeftThumbstickLeft:
                    case Buttons.LeftThumbstickRight:
                    case Buttons.RightThumbstickUp:
                    case Buttons.RightThumbstickDown:
                    case Buttons.RightThumbstickLeft:
                    case Buttons.RightThumbstickRight:
                        switch (bindingControl)
                        {
                        case BindingControl.rotate_counter_clockwise:
                            other1 = BindingControl.rotate_clockwise;
                            other2 = BindingControl.super_shield;
                            break;

                        case BindingControl.rotate_clockwise:
                            other1 = BindingControl.rotate_counter_clockwise;
                            other2 = BindingControl.super_shield;
                            break;

                        case BindingControl.super_shield:
                            other1 = BindingControl.rotate_counter_clockwise;
                            other2 = BindingControl.rotate_clockwise;
                            break;
                        }
                        // if 'other1' is assigned to the value the user attempts to bind to 'bindingControl' then the two will swap values
                        if (CVars.Get <int>(string.Format($"input_controller_{(int)gpbde._playerIndex}_{other1}")) == (int)gpbde._pressedButton)
                        {
                            CVars.Get <int>(string.Format($"input_controller_{(int)gpbde._playerIndex}_{other1}")) = CVars.Get <int>(string.Format($"input_controller_{(int)gpbde._playerIndex}_{bindingControl}"));
                        }
                        // if 'other2' is assigned to the value the user attempts to bind to 'bindingControl' then the two will swap values
                        if (CVars.Get <int>(string.Format($"input_controller_{(int)gpbde._playerIndex}_{other2}")) == (int)gpbde._pressedButton)
                        {
                            CVars.Get <int>(string.Format($"input_controller_{(int)gpbde._playerIndex}_{other2}")) = CVars.Get <int>(string.Format($"input_controller_{(int)gpbde._playerIndex}_{bindingControl}"));
                        }
                        CVars.Get <int>(string.Format($"input_controller_{(int)gpbde._playerIndex}_{bindingControl}")) = (int)gpbde._pressedButton;
                        break;

                    default:
                        return(true);
                    }
                    CVars.Save();
                    UpdateButtonBindingsForGamePad(gpbde._playerIndex);
                    ((Button)_root.FindWidgetByID(string.Format($"{bindingControl}"))).isSelected = true;
                    bindingControl = BindingControl.none;
                    _root.AutoControlModeSwitching = true;
                    return(true);
                }
            }

            KeyboardKeyDownEvent kbkde = evt as KeyboardKeyDownEvent;

            if (kbkde != null)
            {
                return(HandleKeyboardKeyDownEvent(kbkde));
            }

            // Listen for the 3 types of button settings pressed
            // Consider buttonSelectedEvent and buttonDeselectedEvent to allow showing of right side
            DisplaySettingsButtonPressedEvent displaySBPE = evt as DisplaySettingsButtonPressedEvent;

            if (displaySBPE != null)
            {
                isOnLeftSide  = false;
                leftSideIndex = 0;
                Console.WriteLine("displaySBPE");

                //_root.FindSelectedWidget().isSelected = false;
                ((Button)_root.FindWidgetByID("Display")).isSelected = false;
                ((DropDownPanel)_root.FindWidgetByID("Screen_Size_Settings_Dropdown")).isSelected = true;

                ((Panel)_root.FindWidgetByID("controls_options_menu_right_panel")).Hidden = true;
                ((Panel)_root.FindWidgetByID("game_options_menu_right_panel")).Hidden     = true;
                ((Panel)_root.FindWidgetByID("display_options_menu_right_panel")).Hidden  = false;
            }
            ControlsSettingsButtonPressedEvent controlsSBPE = evt as ControlsSettingsButtonPressedEvent;

            if (controlsSBPE != null)
            {
                isOnLeftSide  = false;
                leftSideIndex = 1;
                Console.WriteLine("controlSBPE");

                //_root.FindSelectedWidget().isSelected = false;
                ((Button)_root.FindWidgetByID("Controls")).isSelected = false;
                ((Button)_root.FindWidgetByID("primary_rotate_counter_clockwise")).isSelected = true;

                ((Panel)_root.FindWidgetByID("controls_options_menu_right_panel")).Hidden = false;
                ((Panel)_root.FindWidgetByID("game_options_menu_right_panel")).Hidden     = true;
                ((Panel)_root.FindWidgetByID("display_options_menu_right_panel")).Hidden  = true;
            }
            GameSettingsButtonPressedEvent gameSBPE = evt as GameSettingsButtonPressedEvent;

            if (gameSBPE != null)
            {
                isOnLeftSide  = false;
                leftSideIndex = 2;
                Console.WriteLine("gameSBPE");

                //_root.FindSelectedWidget().isSelected = false;
                ((Button)_root.FindWidgetByID("GameSettings")).isSelected          = false;
                ((DropDownPanel)_root.FindWidgetByID("Speed_Dropdown")).isSelected = true;

                ((Panel)_root.FindWidgetByID("controls_options_menu_right_panel")).Hidden = true;
                ((Panel)_root.FindWidgetByID("game_options_menu_right_panel")).Hidden     = false;
                ((Panel)_root.FindWidgetByID("display_options_menu_right_panel")).Hidden  = true;
            }

            FullScreenSettingsButtonPressedEvent fullscreenSBPE = evt as FullScreenSettingsButtonPressedEvent;

            if (fullscreenSBPE != null)
            {
                ((Label)_root.FindWidgetByID("Screen_Size_Settings_Dropdown_Label")).Content = "Screen Size: Full Screen";
                // Set to full screen
                CVars.Get <bool>("display_windowed")   = false;
                CVars.Get <bool>("display_borderless") = false;
                CVars.Get <bool>("display_fullscreen") = true;
                // Generate event to force GameManager to change to correct settings
                EventManager.Instance.QueueEvent(new ReloadDisplayOptionsEvent());
            }
            WindowedSettingsButtonPressed windowedSBPE = evt as WindowedSettingsButtonPressed;

            if (windowedSBPE != null)
            {
                ((Label)_root.FindWidgetByID("Screen_Size_Settings_Dropdown_Label")).Content = "Screen Size: Windowed";
                // Set to windowed
                CVars.Get <bool>("display_windowed")   = true;
                CVars.Get <bool>("display_borderless") = false;
                CVars.Get <bool>("display_fullscreen") = false;
                // Generate event to force GameManager to change to correct settings
                EventManager.Instance.QueueEvent(new ReloadDisplayOptionsEvent());
            }
            BorderlessWindowButtonPressedEvent borderlessWindowSBPE = evt as BorderlessWindowButtonPressedEvent;

            if (borderlessWindowSBPE != null)
            {
                ((Label)_root.FindWidgetByID("Screen_Size_Settings_Dropdown_Label")).Content = "Screen Size: Borderless";
                // Set to borderless window
                CVars.Get <bool>("display_windowed")   = false;
                CVars.Get <bool>("display_borderless") = true;
                CVars.Get <bool>("display_fullscreen") = false;
                // Generate event to force GameManager to change to correct settings
                EventManager.Instance.QueueEvent(new ReloadDisplayOptionsEvent());
            }

            /*
             * AAFXAASettingsButtonPressedEvent aafxaaSBPE = evt as AAFXAASettingsButtonPressedEvent;
             * if (aafxaaSBPE != null)
             * {
             *  ((Label)_root.FindWidgetByID("FXAA_Settings_Dropdown_Label")).Content = "Anti-Alias: FXAA";
             *  CVars.Get<bool>("graphics_fxaa") = true;
             *  CVars.Get<bool>("graphics_feathering") = false;
             *  EventManager.Instance.QueueEvent(new ReloadDisplayOptionsEvent());
             * }
             */
            AASMAASettingsButtonPressedEvent aasmaaSBPE = evt as AASMAASettingsButtonPressedEvent;

            if (aasmaaSBPE != null)
            {
                ((Label)_root.FindWidgetByID("AA_Settings_Dropdown_Label")).Content = "Anti-Alias: SMAA";
                CVars.Get <bool>("graphics_smaa")       = true;
                CVars.Get <bool>("graphics_feathering") = false;
                EventManager.Instance.QueueEvent(new ReloadDisplayOptionsEvent());
            }
            AAFeatheringButtonPressedEvent aafeatherSBPE = evt as AAFeatheringButtonPressedEvent;

            if (aafeatherSBPE != null)
            {
                ((Label)_root.FindWidgetByID("AA_Settings_Dropdown_Label")).Content = "Anti-Alias: Feathering";
                CVars.Get <bool>("graphics_smaa")       = false;
                CVars.Get <bool>("graphics_feathering") = true;
                EventManager.Instance.QueueEvent(new ReloadDisplayOptionsEvent());
            }
            AAOffButtonPressedEvent aaoffSBPE = evt as AAOffButtonPressedEvent;

            if (aaoffSBPE != null)
            {
                ((Label)_root.FindWidgetByID("AA_Settings_Dropdown_Label")).Content = "Anti-Alias: Off";
                CVars.Get <bool>("graphics_smaa")       = false;
                CVars.Get <bool>("graphics_feathering") = false;
                EventManager.Instance.QueueEvent(new ReloadDisplayOptionsEvent());
            }

            RotateLeftSettingsButtonPressedEvent rlSBPE = evt as RotateLeftSettingsButtonPressedEvent;

            if (rlSBPE != null)
            {
                Console.WriteLine("rlSBPE");
                // Rotate Left Button Clicked, enter into button binding state
                bindingControl = BindingControl.rotate_counter_clockwise;
                bindingMode    = (_root.MouseMode == true) ? BindingMode.primary : BindingMode.gamepad;

                /*
                 * bindingMode = BindingMode.primary;
                 * bindingGamepad = !_root.MouseMode;
                 */
                _root.AutoControlModeSwitching = false;
                ((Button)_root.FindWidgetByID("primary_rotate_counter_clockwise")).isSelected = false;
            }
            RotateRightSettingsButtonPressedEvent rrSBPE = evt as RotateRightSettingsButtonPressedEvent;

            if (rrSBPE != null)
            {
                Console.WriteLine("rrSBPE");
                // Rotate Right Button Clicked, enter into button binding state
                bindingControl = BindingControl.rotate_clockwise;
                bindingMode    = (_root.MouseMode == true) ? BindingMode.primary : BindingMode.gamepad;
                _root.AutoControlModeSwitching = false;
                ((Button)_root.FindWidgetByID("primary_rotate_clockwise")).isSelected = false;
            }
            if (evt is SuperShieldSettingsButtonPressedEvent)
            {
                Console.WriteLine("Super Shield Pressed");
                bindingControl = BindingControl.super_shield;
                bindingMode    = (_root.MouseMode == true) ? BindingMode.primary : BindingMode.gamepad;
                _root.AutoControlModeSwitching = false;
                ((Button)_root.FindWidgetByID("primary_super_shield")).isSelected = false;
            }
            if (evt is SecondaryRotateLeftSettingsButtonPressedEvent)
            {
                Console.WriteLine("secondary_rlSBPE");
                // Rotate Left Button Clicked, enter into button binding state
                bindingControl = BindingControl.rotate_counter_clockwise;
                bindingMode    = (_root.MouseMode == true) ? BindingMode.secondary : BindingMode.gamepad;
                _root.AutoControlModeSwitching = false;
                ((Button)_root.FindWidgetByID("secondary_rotate_counter_clockwise")).isSelected = false;
            }
            if (evt is SecondaryRotateRightSettingsButtonPressedEvent)
            {
                Console.WriteLine("secondary_rrSBPE");
                // Rotate Right Button Clicked, enter into button binding state
                bindingControl = BindingControl.rotate_clockwise;
                bindingMode    = (_root.MouseMode == true) ? BindingMode.secondary : BindingMode.gamepad;
                _root.AutoControlModeSwitching = false;
                ((Button)_root.FindWidgetByID("secondary_rotate_clockwise")).isSelected = false;
            }
            if (evt is SecondarySuperShieldSettingsButtonPressedEvent)
            {
                Console.WriteLine("Super Shield Pressed");
                bindingControl = BindingControl.super_shield;
                bindingMode    = (_root.MouseMode == true) ? BindingMode.secondary : BindingMode.gamepad;
                _root.AutoControlModeSwitching = false;
                ((Button)_root.FindWidgetByID("secondary_super_shield")).isSelected = false;
            }
            if (evt is ResolutionButtonPressedEvent)
            {
                ((Label)_root.FindWidgetByID("Resolution_Button_Label")).Content = SetNextResolution();
                GameManager.Graphics.IsFullScreen       = false;
                GameManager.Graphics.HardwareModeSwitch = true;
                GameManager.Graphics.ApplyChanges();
                EventManager.Instance.QueueEvent(new ReloadDisplayOptionsEvent());
            }
            if (evt is VSyncButtonPressedEvent)
            {
                bool replacement = !(CVars.Get <bool>("display_vsync"));
                CVars.Get <bool>("display_vsync") = replacement;
                ((Label)_root.FindWidgetByID("VSync_Button_Label")).Content =
                    (replacement == true) ?
                    "V-Sync: On" :
                    "V-Sync: Off";
            }

            return(false);
        }
        private void DrawCVarWindow()
        {
            if (CVars.Get <bool>("debug_show_cvar_viewer"))
            {
                ImGui.SetNextWindowSize(new System.Numerics.Vector2(500, 400), ImGuiCond.FirstUseEver);
                ImGui.Begin("CVar Viewer", ref CVars.Get <bool>("debug_show_cvar_viewer"));

                if (ImGui.Button("Save##Control"))
                {
                    CVars.Save();
                }
                ImGui.SameLine();
                if (ImGui.Button("Load##Control"))
                {
                    CVars.SynchronizeFromFile();
                }

                string[] names = CVars.GetNames();
                Array.Sort(names);

                ImGui.Columns(4);

                ImGui.Text("CVar");
                ImGui.NextColumn();
                ImGui.Text("Value");
                ImGui.NextColumn();
                ImGui.Text("Default");
                ImGui.NextColumn();
                ImGui.Text("Description");
                ImGui.NextColumn();

                ImGui.Separator();

                foreach (string name in names)
                {
                    if (ImGui.Button(name))
                    {
                        _cvarEditing = name;
                    }

                    ImGui.NextColumn();

                    ICVar cvar = CVars.RawGet(name);
                    if (_cvarEditing == name)
                    {
                        switch (cvar)
                        {
                        case CVar <byte> numCVar:
                        {
                            int num = (int)numCVar.Value;
                            ImGui.InputInt("##" + name, ref num);
                            numCVar.Value = (byte)num;
                        }
                        break;

                        case CVar <short> numCVar:
                        {
                            int num = (int)numCVar.Value;
                            ImGui.InputInt("##" + name, ref num);
                            numCVar.Value = (short)num;
                        }
                        break;

                        case CVar <int> numCVar:
                            ImGui.InputInt("##" + name, ref numCVar.Value);
                            break;

                        case CVar <float> numCVar:
                            ImGui.InputFloat("##" + name, ref numCVar.Value);
                            break;

                        case CVar <double> numCVar:
                            ImGui.InputDouble("##" + name, ref numCVar.Value);
                            break;

                        case CVar <bool> boolCVar:
                            ImGui.Checkbox("##" + name, ref boolCVar.Value);
                            break;

                        case CVar <Color> colorCVar:
                        {
                            System.Numerics.Vector3 color = new System.Numerics.Vector3(colorCVar.Value.R / 255.0f,
                                                                                        colorCVar.Value.G / 255.0f,
                                                                                        colorCVar.Value.B / 255.0f);
                            ImGui.ColorEdit3("##" + name, ref color);
                            colorCVar.Value.R = (byte)(color.X * 255);
                            colorCVar.Value.G = (byte)(color.Y * 255);
                            colorCVar.Value.B = (byte)(color.Z * 255);
                        }
                        break;

                        default:
                        {
                            string strValue = cvar.Serialize();
                            byte[] buff     = new byte[strValue.Length + 500];
                            Array.Copy(Encoding.UTF8.GetBytes(strValue), buff, strValue.Length);
                            ImGui.InputText("##" + name, buff, (uint)buff.Length);
                            cvar.Deserialize(Encoding.UTF8.GetString(buff, 0, buff.Length));
                        }
                        break;
                        }
                    }
                    else
                    {
                        ImGui.Text(cvar.Serialize());
                    }

                    ImGui.NextColumn();

                    if (ImGui.Button(cvar.SerializeDefault() + "##" + name))
                    {
                        cvar.Reset();
                    }

                    ImGui.NextColumn();

                    ImGui.TextWrapped(cvar.GetDescription());

                    ImGui.NextColumn();
                }

                ImGui.End();
            }
        }
        private bool HandleKeyboardKeyDownEvent(KeyboardKeyDownEvent keyboardKeyDownEvent)
        {
            if (bindingMode == BindingMode.primary || bindingMode == BindingMode.secondary)
            {
                BindingControl other1 = BindingControl.none;
                BindingControl other2 = BindingControl.none;
                switch (keyboardKeyDownEvent._key)
                {
                case Keys.D0:
                case Keys.D1:
                case Keys.D2:
                case Keys.D3:
                case Keys.D4:
                case Keys.D5:
                case Keys.D6:
                case Keys.D7:
                case Keys.D8:
                case Keys.D9:

                case Keys.A:
                case Keys.B:
                case Keys.C:
                case Keys.D:
                case Keys.E:
                case Keys.F:
                case Keys.G:
                case Keys.H:
                case Keys.I:
                case Keys.J:
                case Keys.K:
                case Keys.L:
                case Keys.M:
                case Keys.N:
                case Keys.O:
                case Keys.P:
                case Keys.Q:
                case Keys.R:
                case Keys.S:
                case Keys.T:
                case Keys.U:
                case Keys.V:
                case Keys.W:
                case Keys.X:
                case Keys.Y:
                case Keys.Z:

                case Keys.Up:
                case Keys.Down:
                case Keys.Left:
                case Keys.Right:

                case Keys.OemOpenBrackets:
                case Keys.OemCloseBrackets:
                case Keys.OemSemicolon:
                case Keys.OemQuotes:
                case Keys.OemComma:
                case Keys.OemPeriod:
                case Keys.OemQuestion:
                case Keys.OemBackslash:
                case Keys.OemMinus:
                case Keys.OemPlus:
                case Keys.Space:
                    switch (bindingControl)
                    {
                    case BindingControl.rotate_counter_clockwise:
                        other1 = BindingControl.rotate_clockwise;
                        other2 = BindingControl.super_shield;
                        break;

                    case BindingControl.rotate_clockwise:
                        other1 = BindingControl.rotate_counter_clockwise;
                        other2 = BindingControl.super_shield;
                        break;

                    case BindingControl.super_shield:
                        other1 = BindingControl.rotate_counter_clockwise;
                        other2 = BindingControl.rotate_clockwise;
                        break;
                    }
                    // if 'other1' is assigned to the value the user attempts to bind to 'bindingControl' then the two will swap values
                    if (CVars.Get <int>(string.Format($"input_keyboard_{bindingMode}_{other1}")) == (int)keyboardKeyDownEvent._key)
                    {
                        CVars.Get <int>(string.Format($"input_keyboard_{bindingMode}_{other1}")) = CVars.Get <int>(string.Format($"input_keyboard_{bindingMode}_{bindingControl}"));
                    }
                    // if 'other2' is assigned to the value the user attempts to bind to 'bindingControl' then the two will swap values
                    if (CVars.Get <int>(string.Format($"input_keyboard_{bindingMode}_{other2}")) == (int)keyboardKeyDownEvent._key)
                    {
                        CVars.Get <int>(string.Format($"input_keyboard_{bindingMode}_{other2}")) = CVars.Get <int>(string.Format($"input_keyboard_{bindingMode}_{bindingControl}"));
                    }
                    CVars.Get <int>(string.Format($"input_keyboard_{bindingMode}_{bindingControl}")) = (int)keyboardKeyDownEvent._key;
                    CVars.Save();
                    UpdateButtonBindingsForKeyboard();
                    bindingMode    = BindingMode.none;
                    bindingControl = BindingControl.none;
                    //((Button)_root.FindWidgetByID("primary_rotate_rotate_counter_clockwise")).isSelected = true;
                    _root.AutoControlModeSwitching = true;
                    return(true);

                default:
                    break;
                }
            }
            else
            {
                if (keyboardKeyDownEvent._key == Keys.Escape)
                {
                    if (isOnLeftSide)
                    {
                        ChangeState(new UIMenuGameState(GameManager, SharedState));
                    }
                    if (!isOnLeftSide)
                    {
                        switch (leftSideIndex)
                        {
                        case 0:
                            ((Button)_root.FindWidgetByID("Display")).isSelected = true;
                            isOnLeftSide = true;
                            // Deslection of button/slider should be handled in the button/slider class
                            ((Panel)_root.FindWidgetByID("display_options_menu_right_panel")).Hidden = true;
                            break;

                        case 1:
                            ((Button)_root.FindWidgetByID("Controls")).isSelected = true;
                            isOnLeftSide = true;
                            // Deslection of button/slider should be handled in the button/slider class
                            ((Panel)_root.FindWidgetByID("controls_options_menu_right_panel")).Hidden = true;
                            break;

                        case 2:
                            ((Button)_root.FindWidgetByID("GameSettings")).isSelected = true;
                            isOnLeftSide = true;
                            // Deslection of button/slider should be handled in the button/slider class
                            ((Panel)_root.FindWidgetByID("game_options_menu_right_panel")).Hidden = true;
                            break;
                        }
                    }
                    return(true);
                }
            }

            return(false);
        }