1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

added the option to disable buttons

This commit is contained in:
Ellpeck 2020-01-15 17:05:28 +01:00
parent 2ac28eced3
commit 7e6534bfc1
6 changed files with 30 additions and 8 deletions

View file

@ -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(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 // Below are some querying examples that help you find certain elements easily

View file

@ -11,9 +11,21 @@ namespace MLEM.Ui.Elements {
public StyleProp<Color> NormalColor = Color.White; public StyleProp<Color> NormalColor = Color.White;
public StyleProp<NinePatch> HoveredTexture; public StyleProp<NinePatch> HoveredTexture;
public StyleProp<Color> HoveredColor; public StyleProp<Color> HoveredColor;
public StyleProp<NinePatch> DisabledTexture;
public StyleProp<Color> DisabledColor;
public Paragraph Text; public Paragraph Text;
public Tooltip Tooltip; 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) { public Button(Anchor anchor, Vector2 size, string text = null, string tooltipText = null, float tooltipWidth = 50) : base(anchor, size) {
if (text != null) { if (text != null) {
this.Text = new Paragraph(Anchor.Center, 1, text, true); 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) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var tex = this.Texture; var tex = this.Texture;
var color = (Color) this.NormalColor * alpha; 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); tex = this.HoveredTexture.OrDefault(tex);
color = (Color) this.HoveredColor * alpha; color = (Color) this.HoveredColor * alpha;
} }
@ -39,6 +54,8 @@ namespace MLEM.Ui.Elements {
this.Texture.SetFromStyle(style.ButtonTexture); this.Texture.SetFromStyle(style.ButtonTexture);
this.HoveredTexture.SetFromStyle(style.ButtonHoveredTexture); this.HoveredTexture.SetFromStyle(style.ButtonHoveredTexture);
this.HoveredColor.SetFromStyle(style.ButtonHoveredColor); this.HoveredColor.SetFromStyle(style.ButtonHoveredColor);
this.DisabledTexture.SetFromStyle(style.ButtonDisabledTexture);
this.DisabledColor.SetFromStyle(style.ButtonDisabledColor);
} }
} }

View file

@ -113,6 +113,7 @@ namespace MLEM.Ui.Elements {
} }
public bool CanBeSelected = true; public bool CanBeSelected = true;
public bool CanBeMoused = true; public bool CanBeMoused = true;
public bool CanBePressed = true;
public float DrawAlpha = 1; public float DrawAlpha = 1;
public bool SetHeightBasedOnChildren; public bool SetHeightBasedOnChildren;
public bool SetWidthBasedOnChildren; public bool SetWidthBasedOnChildren;

View file

@ -11,6 +11,8 @@ namespace MLEM.Ui.Style {
public NinePatch ButtonTexture; public NinePatch ButtonTexture;
public NinePatch ButtonHoveredTexture; public NinePatch ButtonHoveredTexture;
public Color ButtonHoveredColor; public Color ButtonHoveredColor;
public NinePatch ButtonDisabledTexture;
public Color ButtonDisabledColor;
public NinePatch PanelTexture; public NinePatch PanelTexture;
public NinePatch TextFieldTexture; public NinePatch TextFieldTexture;
public NinePatch TextFieldHoveredTexture; public NinePatch TextFieldHoveredTexture;

View file

@ -13,6 +13,7 @@ namespace MLEM.Ui.Style {
this.SelectionIndicator = batch.GenerateTexture(Color.Transparent, Color.Red); this.SelectionIndicator = batch.GenerateTexture(Color.Transparent, Color.Red);
this.ButtonTexture = batch.GenerateTexture(Color.CadetBlue); this.ButtonTexture = batch.GenerateTexture(Color.CadetBlue);
this.ButtonHoveredColor = Color.LightGray; this.ButtonHoveredColor = Color.LightGray;
this.ButtonDisabledColor = Color.Gray;
this.PanelTexture = batch.GenerateTexture(Color.Gray); this.PanelTexture = batch.GenerateTexture(Color.Gray);
this.TextFieldTexture = batch.GenerateTexture(Color.MediumBlue); this.TextFieldTexture = batch.GenerateTexture(Color.MediumBlue);
this.TextFieldHoveredColor = Color.LightGray; this.TextFieldHoveredColor = Color.LightGray;

View file

@ -64,11 +64,11 @@ namespace MLEM.Ui {
this.IsAutoNavMode = false; this.IsAutoNavMode = false;
var selectedNow = mousedNow != null && mousedNow.CanBeSelected ? mousedNow : null; var selectedNow = mousedNow != null && mousedNow.CanBeSelected ? mousedNow : null;
this.ActiveRoot?.SelectElement(selectedNow); this.ActiveRoot?.SelectElement(selectedNow);
if (mousedNow != null) if (mousedNow != null && mousedNow.CanBePressed)
this.System.OnElementPressed?.Invoke(mousedNow); this.System.OnElementPressed?.Invoke(mousedNow);
} else if (this.Input.IsMouseButtonPressed(MouseButton.Right)) { } else if (this.Input.IsMouseButtonPressed(MouseButton.Right)) {
this.IsAutoNavMode = false; this.IsAutoNavMode = false;
if (mousedNow != null) if (mousedNow != null && mousedNow.CanBePressed)
this.System.OnElementSecondaryPressed?.Invoke(mousedNow); this.System.OnElementSecondaryPressed?.Invoke(mousedNow);
} }
} }
@ -76,7 +76,7 @@ namespace MLEM.Ui {
// KEYBOARD INPUT // KEYBOARD INPUT
if (this.HandleKeyboard) { if (this.HandleKeyboard) {
if (this.KeyboardButtons.Any(this.Input.IsKeyPressed)) { 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)) { if (this.Input.IsModifierKeyDown(ModifierKey.Shift)) {
// secondary action on element using space or enter // secondary action on element using space or enter
this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement); this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
@ -102,13 +102,13 @@ namespace MLEM.Ui {
this.IsAutoNavMode = false; this.IsAutoNavMode = false;
var tapped = this.GetElementUnderPos(tap.Position); var tapped = this.GetElementUnderPos(tap.Position);
this.ActiveRoot?.SelectElement(tapped); this.ActiveRoot?.SelectElement(tapped);
if (tapped != null) if (tapped != null && tapped.CanBePressed)
this.System.OnElementPressed?.Invoke(tapped); this.System.OnElementPressed?.Invoke(tapped);
} else if (this.Input.GetGesture(GestureType.Hold, out var hold)) { } else if (this.Input.GetGesture(GestureType.Hold, out var hold)) {
this.IsAutoNavMode = false; this.IsAutoNavMode = false;
var held = this.GetElementUnderPos(hold.Position); var held = this.GetElementUnderPos(hold.Position);
this.ActiveRoot?.SelectElement(held); this.ActiveRoot?.SelectElement(held);
if (held != null) if (held != null && held.CanBePressed)
this.System.OnElementSecondaryPressed?.Invoke(held); this.System.OnElementSecondaryPressed?.Invoke(held);
} }
} }
@ -116,10 +116,10 @@ namespace MLEM.Ui {
// GAMEPAD INPUT // GAMEPAD INPUT
if (this.HandleGamepad) { if (this.HandleGamepad) {
if (this.GamepadButtons.Any(b => this.IsGamepadPressed(b))) { 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); this.System.OnElementPressed?.Invoke(this.SelectedElement);
} else if (this.SecondaryGamepadButtons.Any(b => this.IsGamepadPressed(b))) { } 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); this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
} else if (this.DownButtons.Any(this.IsGamepadPressed)) { } else if (this.DownButtons.Any(this.IsGamepadPressed)) {
this.HandleGamepadNextElement(Direction2.Down); this.HandleGamepadNextElement(Direction2.Down);