From 7e6534bfc1ba421221ece265172bf61a795b7262 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 15 Jan 2020 17:05:28 +0100 Subject: [PATCH] added the option to disable buttons --- Demos/UiDemo.cs | 1 + MLEM.Ui/Elements/Button.cs | 19 ++++++++++++++++++- MLEM.Ui/Elements/Element.cs | 1 + MLEM.Ui/Style/UiStyle.cs | 2 ++ MLEM.Ui/Style/UntexturedStyle.cs | 1 + MLEM.Ui/UiControls.cs | 14 +++++++------- 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 765e567..c1fb57a 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -208,6 +208,7 @@ namespace Demos { }); root.AddChild(ElementHelper.ImageButton(Anchor.AutoLeft, new Vector2(1, 10), tree, "Button with image")).PositionOffset = new Vector2(0, 1); + root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Disabled button") {IsDisabled = true}).PositionOffset = new Vector2(0, 1); // Below are some querying examples that help you find certain elements easily diff --git a/MLEM.Ui/Elements/Button.cs b/MLEM.Ui/Elements/Button.cs index f4db2ca..87ee447 100644 --- a/MLEM.Ui/Elements/Button.cs +++ b/MLEM.Ui/Elements/Button.cs @@ -11,9 +11,21 @@ namespace MLEM.Ui.Elements { public StyleProp NormalColor = Color.White; public StyleProp HoveredTexture; public StyleProp HoveredColor; + public StyleProp DisabledTexture; + public StyleProp DisabledColor; public Paragraph Text; public Tooltip Tooltip; + private bool isDisabled; + public bool IsDisabled { + get => this.isDisabled; + set { + this.isDisabled = value; + this.CanBePressed = !value; + this.CanBeSelected = !value; + } + } + public Button(Anchor anchor, Vector2 size, string text = null, string tooltipText = null, float tooltipWidth = 50) : base(anchor, size) { if (text != null) { this.Text = new Paragraph(Anchor.Center, 1, text, true); @@ -26,7 +38,10 @@ namespace MLEM.Ui.Elements { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) { var tex = this.Texture; var color = (Color) this.NormalColor * alpha; - if (this.IsMouseOver) { + if (this.IsDisabled) { + tex = this.DisabledTexture.OrDefault(tex); + color = (Color) this.DisabledColor * alpha; + } else if (this.IsMouseOver) { tex = this.HoveredTexture.OrDefault(tex); color = (Color) this.HoveredColor * alpha; } @@ -39,6 +54,8 @@ namespace MLEM.Ui.Elements { this.Texture.SetFromStyle(style.ButtonTexture); this.HoveredTexture.SetFromStyle(style.ButtonHoveredTexture); this.HoveredColor.SetFromStyle(style.ButtonHoveredColor); + this.DisabledTexture.SetFromStyle(style.ButtonDisabledTexture); + this.DisabledColor.SetFromStyle(style.ButtonDisabledColor); } } diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 42bbeba..f0b1ba8 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -113,6 +113,7 @@ namespace MLEM.Ui.Elements { } public bool CanBeSelected = true; public bool CanBeMoused = true; + public bool CanBePressed = true; public float DrawAlpha = 1; public bool SetHeightBasedOnChildren; public bool SetWidthBasedOnChildren; diff --git a/MLEM.Ui/Style/UiStyle.cs b/MLEM.Ui/Style/UiStyle.cs index 71142a8..5f0e440 100644 --- a/MLEM.Ui/Style/UiStyle.cs +++ b/MLEM.Ui/Style/UiStyle.cs @@ -11,6 +11,8 @@ namespace MLEM.Ui.Style { public NinePatch ButtonTexture; public NinePatch ButtonHoveredTexture; public Color ButtonHoveredColor; + public NinePatch ButtonDisabledTexture; + public Color ButtonDisabledColor; public NinePatch PanelTexture; public NinePatch TextFieldTexture; public NinePatch TextFieldHoveredTexture; diff --git a/MLEM.Ui/Style/UntexturedStyle.cs b/MLEM.Ui/Style/UntexturedStyle.cs index 63415ce..638b9c8 100644 --- a/MLEM.Ui/Style/UntexturedStyle.cs +++ b/MLEM.Ui/Style/UntexturedStyle.cs @@ -13,6 +13,7 @@ namespace MLEM.Ui.Style { this.SelectionIndicator = batch.GenerateTexture(Color.Transparent, Color.Red); this.ButtonTexture = batch.GenerateTexture(Color.CadetBlue); this.ButtonHoveredColor = Color.LightGray; + this.ButtonDisabledColor = Color.Gray; this.PanelTexture = batch.GenerateTexture(Color.Gray); this.TextFieldTexture = batch.GenerateTexture(Color.MediumBlue); this.TextFieldHoveredColor = Color.LightGray; diff --git a/MLEM.Ui/UiControls.cs b/MLEM.Ui/UiControls.cs index edba85c..6a41ee2 100644 --- a/MLEM.Ui/UiControls.cs +++ b/MLEM.Ui/UiControls.cs @@ -64,11 +64,11 @@ namespace MLEM.Ui { this.IsAutoNavMode = false; var selectedNow = mousedNow != null && mousedNow.CanBeSelected ? mousedNow : null; this.ActiveRoot?.SelectElement(selectedNow); - if (mousedNow != null) + if (mousedNow != null && mousedNow.CanBePressed) this.System.OnElementPressed?.Invoke(mousedNow); } else if (this.Input.IsMouseButtonPressed(MouseButton.Right)) { this.IsAutoNavMode = false; - if (mousedNow != null) + if (mousedNow != null && mousedNow.CanBePressed) this.System.OnElementSecondaryPressed?.Invoke(mousedNow); } } @@ -76,7 +76,7 @@ namespace MLEM.Ui { // KEYBOARD INPUT if (this.HandleKeyboard) { if (this.KeyboardButtons.Any(this.Input.IsKeyPressed)) { - if (this.SelectedElement?.Root != null) { + if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) { if (this.Input.IsModifierKeyDown(ModifierKey.Shift)) { // secondary action on element using space or enter this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement); @@ -102,13 +102,13 @@ namespace MLEM.Ui { this.IsAutoNavMode = false; var tapped = this.GetElementUnderPos(tap.Position); this.ActiveRoot?.SelectElement(tapped); - if (tapped != null) + if (tapped != null && tapped.CanBePressed) this.System.OnElementPressed?.Invoke(tapped); } else if (this.Input.GetGesture(GestureType.Hold, out var hold)) { this.IsAutoNavMode = false; var held = this.GetElementUnderPos(hold.Position); this.ActiveRoot?.SelectElement(held); - if (held != null) + if (held != null && held.CanBePressed) this.System.OnElementSecondaryPressed?.Invoke(held); } } @@ -116,10 +116,10 @@ namespace MLEM.Ui { // GAMEPAD INPUT if (this.HandleGamepad) { if (this.GamepadButtons.Any(b => this.IsGamepadPressed(b))) { - if (this.SelectedElement?.Root != null) + if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) this.System.OnElementPressed?.Invoke(this.SelectedElement); } else if (this.SecondaryGamepadButtons.Any(b => this.IsGamepadPressed(b))) { - if (this.SelectedElement?.Root != null) + if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement); } else if (this.DownButtons.Any(this.IsGamepadPressed)) { this.HandleGamepadNextElement(Direction2.Down);