1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-24 01:09:23 +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(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

View file

@ -11,9 +11,21 @@ namespace MLEM.Ui.Elements {
public StyleProp<Color> NormalColor = Color.White;
public StyleProp<NinePatch> HoveredTexture;
public StyleProp<Color> HoveredColor;
public StyleProp<NinePatch> DisabledTexture;
public StyleProp<Color> 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);
}
}

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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);