From de702b6a8ed76d385bf9898438e158ddb184513f Mon Sep 17 00:00:00 2001 From: polten0 Date: Fri, 18 Aug 2023 02:00:58 +0300 Subject: [PATCH] FirstNotGoodWorkUI --- DangerousD/GameCore/GUI/AbstractGui.cs | 133 +++++++++++++++++++++++++ MonogameLibrary/UI/Elements/Button.cs | 6 +- 2 files changed, 138 insertions(+), 1 deletion(-) diff --git a/DangerousD/GameCore/GUI/AbstractGui.cs b/DangerousD/GameCore/GUI/AbstractGui.cs index 7f7b212..6615820 100644 --- a/DangerousD/GameCore/GUI/AbstractGui.cs +++ b/DangerousD/GameCore/GUI/AbstractGui.cs @@ -1,8 +1,13 @@ using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Xml; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; using MonogameLibrary.UI.Base; +using MonogameLibrary.UI.Elements; namespace DangerousD.GameCore.GUI; @@ -10,6 +15,9 @@ public abstract class AbstractGui : IDrawableObject { protected UIManager Manager = new(); protected List Elements = new(); + private List ActiveElements; + protected DrawableUIElement SelectedElement; + private bool isPressed = false; public AbstractGui() { @@ -22,6 +30,16 @@ public abstract class AbstractGui : IDrawableObject Manager.Initialize(AppManager.Instance.GraphicsDevice); this.graphicsDevice = graphicsDevice; CreateUI(); + ActiveElements = new List(); + foreach (var element in Elements) + { + if (CheckOnBadElements(element)) + { + ActiveElements.Add(element); + } + } + if (ActiveElements.Count > 0) { SelectedElement = ActiveElements.First(); } + } public virtual void LoadContent() @@ -31,6 +49,25 @@ public abstract class AbstractGui : IDrawableObject public virtual void Update(GameTime gameTime) { + string state = AppManager.Instance.InputManager.currentControlsState; + + if (ActiveElements.Count != 0) + { + switch (state) + { + case "Gamepad": + GamePadState gamePadState = GamePad.GetState(0); + GamepadInput(gamePadState); + break; + case "Keyboard": + KeyboardState keyBoardState = Keyboard.GetState(0); + KeyBoardInput(keyBoardState); + break; + default: + break; + } + } + Manager.Update(); } @@ -38,4 +75,100 @@ public abstract class AbstractGui : IDrawableObject { Manager.Draw(spriteBatch); } + protected virtual void GamepadInput(GamePadState gamePadState) + { + if (gamePadState.DPad.Up == ButtonState.Pressed && !isPressed) + { + isPressed = true; + ChangeSelectedElement(-1); + } + else if (gamePadState.DPad.Down == ButtonState.Pressed && !isPressed) + { + isPressed = true; + ChangeSelectedElement(1); + } + else if (gamePadState.Buttons.A == ButtonState.Pressed && !isPressed) + { + Debug.WriteLine("ssss"); + isPressed = true; + if (SelectedElement is ButtonText) + { + Button button = SelectedElement as ButtonText; + button.CallLeftBtnEvent(); + } + } + else if (isPressed) + { + isPressed = false; + } + } + protected virtual void KeyBoardInput(KeyboardState keyboardState) + { + if (keyboardState.IsKeyDown(Keys.Up) && !isPressed) + { + isPressed = true; + ChangeSelectedElement(-1); + } + else if (keyboardState.IsKeyDown(Keys.Down) && !isPressed) + { + isPressed = true; + ChangeSelectedElement(1); + } + else if (keyboardState.IsKeyDown(Keys.Enter) && !isPressed) + { + isPressed = true; + if (SelectedElement is Button) + { + Button button = SelectedElement as Button; + button.CallLeftBtnEvent(); + } + } + else if (isPressed) + { + isPressed = false; + } + } + private void ChangeSelectedElement(int x) // Меняет выбранный элемент + { + for (int i = 0; i < ActiveElements.Count; i++) + { + if (ActiveElements[i] == SelectedElement) + { + if (i == 0) + { + SelectedElement = ActiveElements.Last(); + } + else + { + if (i == ActiveElements.Count - 1 && x >= 1) + { + SelectedElement = ActiveElements.First(); + } + else + { + SelectedElement = ActiveElements[i + x]; + } + + } + + } + + } + } + private bool CheckOnBadElements(DrawableUIElement element) + { + if (element is Button) + { + return true; + } + else if (element is ButtonText) + { + return true; + } + else if (element is CheckBox) + { + return true; + } + else return false; + } } \ No newline at end of file diff --git a/MonogameLibrary/UI/Elements/Button.cs b/MonogameLibrary/UI/Elements/Button.cs index cf7d5ce..051f31f 100644 --- a/MonogameLibrary/UI/Elements/Button.cs +++ b/MonogameLibrary/UI/Elements/Button.cs @@ -40,7 +40,7 @@ namespace MonogameLibrary.UI.Elements if (mouseState.LeftButton != prevmouseState.LeftButton) { hoverState = HoverState.Pressing; - LeftButtonPressed?.Invoke(); + CallLeftBtnEvent(); return true; } } @@ -77,5 +77,9 @@ namespace MonogameLibrary.UI.Elements DrawText(_spriteBatch); } + public void CallLeftBtnEvent() + { + LeftButtonPressed?.Invoke(); + } } }