2019-08-09 22:04:26 +02:00
|
|
|
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;
|
2019-08-09 22:04:26 +02:00
|
|
|
|
|
|
|
namespace MLEM.Ui.Elements {
|
|
|
|
public class Button : Element {
|
|
|
|
|
2019-10-14 21:28:12 +02:00
|
|
|
public StyleProp<NinePatch> Texture;
|
2019-12-06 21:18:43 +01:00
|
|
|
public StyleProp<Color> NormalColor = Color.White;
|
2019-10-14 21:28:12 +02:00
|
|
|
public StyleProp<NinePatch> HoveredTexture;
|
|
|
|
public StyleProp<Color> HoveredColor;
|
2020-01-15 17:05:28 +01:00
|
|
|
public StyleProp<NinePatch> DisabledTexture;
|
|
|
|
public StyleProp<Color> DisabledColor;
|
2019-08-13 21:23:20 +02:00
|
|
|
public Paragraph Text;
|
2019-08-13 23:54:29 +02:00
|
|
|
public Tooltip Tooltip;
|
2019-08-09 22:04:26 +02:00
|
|
|
|
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) {
|
2019-08-09 22:04:26 +02:00
|
|
|
if (text != null) {
|
2019-08-13 21:23:20 +02:00
|
|
|
this.Text = new Paragraph(Anchor.Center, 1, text, true);
|
2019-08-09 22:04:26 +02:00
|
|
|
this.AddChild(this.Text);
|
|
|
|
}
|
2019-08-24 20:45:40 +02:00
|
|
|
if (tooltipText != null)
|
|
|
|
this.Tooltip = new Tooltip(tooltipWidth, tooltipText, this);
|
2019-08-09 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 13:22:05 +02:00
|
|
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
|
2019-08-09 22:04:26 +02:00
|
|
|
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);
|
2019-10-14 21:28:12 +02:00
|
|
|
color = (Color) this.HoveredColor * alpha;
|
2019-08-09 22:04:26 +02:00
|
|
|
}
|
2019-09-04 17:19:31 +02:00
|
|
|
batch.Draw(tex, this.DisplayArea, color, this.Scale);
|
2019-09-20 13:22:05 +02:00
|
|
|
base.Draw(time, batch, alpha, blendState, samplerState, matrix);
|
2019-08-09 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
2019-08-10 21:37:10 +02:00
|
|
|
protected override void InitStyle(UiStyle style) {
|
|
|
|
base.InitStyle(style);
|
2019-10-14 21:28:12 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-09 22:04:26 +02:00
|
|
|
}
|
|
|
|
}
|