1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

made style props a lot easier to use

This commit is contained in:
Ellpeck 2019-11-05 13:28:41 +01:00
parent 7f81f6469f
commit f28e234392
8 changed files with 26 additions and 14 deletions

View file

@ -81,11 +81,11 @@ namespace Demos {
root.AddChild(new VerticalSpace(3));
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Note that the default style does not contain any textures or font files and, as such, is quite bland. However, the default style is quite easy to override."));
var customButton = root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Change Style") {
root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Change Style") {
OnPressed = element => this.UiSystem.Style = this.UiSystem.Style == untexturedStyle ? style : untexturedStyle,
PositionOffset = new Vector2(0, 1)
PositionOffset = new Vector2(0, 1),
Texture = this.testPatch
});
customButton.Texture.Set(this.testPatch);
root.AddChild(new VerticalSpace(3));

View file

@ -26,8 +26,7 @@ namespace MLEM.Ui.Elements {
var tex = this.Texture;
var color = Color.White * alpha;
if (this.IsMouseOver) {
if (this.HoveredTexture.Value != null)
tex = this.HoveredTexture;
tex = this.HoveredTexture.OrDefault(tex);
color = (Color) this.HoveredColor * alpha;
}
batch.Draw(tex, this.DisplayArea, color, this.Scale);

View file

@ -51,8 +51,7 @@ namespace MLEM.Ui.Elements {
var tex = this.Texture;
var color = Color.White * alpha;
if (this.IsMouseOver) {
if (this.HoveredTexture.Value != null)
tex = this.HoveredTexture;
tex = this.HoveredTexture.OrDefault(tex);
color = (Color) this.HoveredColor * alpha;
}

View file

@ -34,8 +34,8 @@ namespace MLEM.Ui.Elements {
};
if (pressed != null)
paragraph.OnPressed += pressed;
paragraph.OnMouseEnter += e => paragraph.TextColor.Set(Color.LightGray);
paragraph.OnMouseExit += e => paragraph.TextColor.Set(Color.White);
paragraph.OnMouseEnter += e => paragraph.TextColor = Color.LightGray;
paragraph.OnMouseExit += e => paragraph.TextColor = Color.White;
this.AddElement(paragraph);
}

View file

@ -66,9 +66,9 @@ namespace MLEM.Ui.Elements {
if (this.texture == null)
return;
var center = new Vector2(this.texture.Width / 2F, this.texture.Height / 2F);
var color = (this.Color.Value != default ? this.Color : Microsoft.Xna.Framework.Color.White) * alpha;
var color = this.Color.OrDefault(Microsoft.Xna.Framework.Color.White) * alpha;
if (this.MaintainImageAspect) {
var scale = Math.Min(this.DisplayArea.Width / (float) this.texture.Width, this.DisplayArea.Height / (float) this.texture.Height);
var scale = Math.Min(this.DisplayArea.Width / this.texture.Width, this.DisplayArea.Height / this.texture.Height);
var imageOffset = new Vector2(this.DisplayArea.Width / 2F - this.texture.Width * scale / 2, this.DisplayArea.Height / 2F - this.texture.Height * scale / 2);
batch.Draw(this.texture, this.DisplayArea.Location + center * scale + imageOffset, color, this.ImageRotation, center, scale * this.ImageScale, this.ImageEffects, 0);
} else {

View file

@ -81,7 +81,7 @@ namespace MLEM.Ui.Elements {
var pos = this.DisplayArea.Location;
var sc = this.TextScale * this.Scale;
var color = (this.TextColor.Value != default ? this.TextColor : Color.White) * alpha;
var color = this.TextColor.OrDefault(Color.White) * alpha;
// if we don't have any formatting codes, then we don't need to do complex drawing
if (this.codeLocations.Count <= 0) {
this.RegularFont.Value.DrawString(batch, this.splitText, pos, color, 0, Vector2.Zero, sc, SpriteEffects.None, 0);

View file

@ -137,8 +137,7 @@ namespace MLEM.Ui.Elements {
var tex = this.Texture;
var color = Color.White * alpha;
if (this.IsMouseOver) {
if (this.HoveredTexture.Value != null)
tex = this.HoveredTexture;
tex = this.HoveredTexture.OrDefault(tex);
color = (Color) this.HoveredColor * alpha;
}
batch.Draw(tex, this.DisplayArea, color, this.Scale);

View file

@ -1,9 +1,16 @@
using System.Collections.Generic;
namespace MLEM.Ui.Style {
public struct StyleProp<T> {
public T Value { get; private set; }
private bool isCustom;
public StyleProp(T value) {
this.isCustom = true;
this.Value = value;
}
public void SetFromStyle(T value) {
if (!this.isCustom) {
this.Value = value;
@ -15,9 +22,17 @@ namespace MLEM.Ui.Style {
this.Value = value;
}
public T OrDefault(T def) {
return EqualityComparer<T>.Default.Equals(this.Value, default) ? def : this.Value;
}
public static implicit operator T(StyleProp<T> prop) {
return prop.Value;
}
public static implicit operator StyleProp<T>(T prop) {
return new StyleProp<T>(prop);
}
}
}