1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-23 13:18:35 +01:00
MLEM/MLEM.Ui/Elements/Button.cs

62 lines
2.4 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Textures;
2019-08-10 21:37:10 +02:00
using MLEM.Ui.Style;
namespace MLEM.Ui.Elements {
public class Button : Element {
public StyleProp<NinePatch> Texture;
2019-12-06 21:18:43 +01:00
public StyleProp<Color> NormalColor = Color.White;
public StyleProp<NinePatch> HoveredTexture;
public StyleProp<Color> HoveredColor;
2020-01-15 17:05:28 +01:00
public StyleProp<NinePatch> DisabledTexture;
public StyleProp<Color> DisabledColor;
public Paragraph Text;
2019-08-13 23:54:29 +02:00
public Tooltip Tooltip;
2020-01-15 17:05:28 +01:00
private bool isDisabled;
public bool IsDisabled {
get => this.isDisabled;
set {
this.isDisabled = value;
this.CanBePressed = !value;
this.CanBeSelected = !value;
}
}
2019-08-13 23:54:29 +02:00
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);
this.AddChild(this.Text);
}
if (tooltipText != null)
this.Tooltip = new Tooltip(tooltipWidth, tooltipText, this);
}
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var tex = this.Texture;
2019-12-06 21:18:43 +01:00
var color = (Color) this.NormalColor * alpha;
2020-01-15 17:05:28 +01:00
if (this.IsDisabled) {
tex = this.DisabledTexture.OrDefault(tex);
color = (Color) this.DisabledColor * alpha;
} else if (this.IsMouseOver) {
2019-11-05 13:28:41 +01:00
tex = this.HoveredTexture.OrDefault(tex);
color = (Color) this.HoveredColor * alpha;
}
batch.Draw(tex, this.DisplayArea, color, this.Scale);
base.Draw(time, batch, alpha, blendState, samplerState, matrix);
}
2019-08-10 21:37:10 +02:00
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.Texture.SetFromStyle(style.ButtonTexture);
this.HoveredTexture.SetFromStyle(style.ButtonHoveredTexture);
this.HoveredColor.SetFromStyle(style.ButtonHoveredColor);
2020-01-15 17:05:28 +01:00
this.DisabledTexture.SetFromStyle(style.ButtonDisabledTexture);
this.DisabledColor.SetFromStyle(style.ButtonDisabledColor);
2019-08-10 21:37:10 +02:00
}
}
}