mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-12-25 01:39:23 +01:00
added defaults for most element properties
This commit is contained in:
parent
9d5d00434c
commit
88da47bc00
6 changed files with 25 additions and 23 deletions
|
@ -8,11 +8,9 @@ namespace MLEM.Ui.Elements {
|
|||
public class AutoScaledText : Element {
|
||||
|
||||
private readonly IGenericFont font;
|
||||
private readonly bool centered;
|
||||
private float scale;
|
||||
private string text;
|
||||
|
||||
public IGenericFont Font => this.font ?? this.System.DefaultFont;
|
||||
public string Text {
|
||||
get => this.text;
|
||||
set {
|
||||
|
@ -22,10 +20,9 @@ namespace MLEM.Ui.Elements {
|
|||
}
|
||||
public Color Color;
|
||||
|
||||
public AutoScaledText(Anchor anchor, Vector2 size, string text, bool centered = false, Color? color = null, IGenericFont font = null) : base(anchor, size) {
|
||||
public AutoScaledText(Anchor anchor, Vector2 size, string text, Color? color = null, IGenericFont font = null) : base(anchor, size) {
|
||||
this.Text = text;
|
||||
this.centered = centered;
|
||||
this.font = font;
|
||||
this.font = font ?? Paragraph.DefaultFont;
|
||||
this.Color = color ?? Color.White;
|
||||
}
|
||||
|
||||
|
@ -36,13 +33,13 @@ namespace MLEM.Ui.Elements {
|
|||
Vector2 measure;
|
||||
do {
|
||||
this.scale += 0.1F;
|
||||
measure = this.Font.MeasureString(this.Text) * this.scale;
|
||||
measure = this.font.MeasureString(this.Text) * this.scale;
|
||||
} while (measure.X <= this.DisplayArea.Size.X && measure.Y <= this.DisplayArea.Size.Y);
|
||||
}
|
||||
|
||||
public override void Draw(GameTime time, SpriteBatch batch, Color color) {
|
||||
var pos = this.DisplayArea.Location.ToVector2() + this.DisplayArea.Size.ToVector2() / 2;
|
||||
this.Font.DrawCenteredString(batch, this.Text, pos, this.scale, this.Color.CopyAlpha(color), true, true);
|
||||
this.font.DrawCenteredString(batch, this.Text, pos, this.scale, this.Color.CopyAlpha(color), true, true);
|
||||
base.Draw(time, batch, color);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,18 +6,22 @@ using MLEM.Textures;
|
|||
namespace MLEM.Ui.Elements {
|
||||
public class Button : Element {
|
||||
|
||||
public static NinePatch DefaultTexture;
|
||||
public static NinePatch DefaultHoveredTexture;
|
||||
public static Color DefaultHoveredColor = Color.LightGray;
|
||||
|
||||
public NinePatch Texture;
|
||||
public NinePatch HoveredTexture;
|
||||
public Color HoveredColor;
|
||||
public AutoScaledText Text;
|
||||
|
||||
public Button(Anchor anchor, Vector2 size, NinePatch texture, string text = null, Color? hoveredColor = null, NinePatch hoveredTexture = null) : base(anchor, size) {
|
||||
this.Texture = texture;
|
||||
this.HoveredTexture = hoveredTexture;
|
||||
this.HoveredColor = hoveredColor ?? Color.LightGray;
|
||||
public Button(Anchor anchor, Vector2 size, NinePatch texture = null, string text = null, Color? hoveredColor = null, NinePatch hoveredTexture = null) : base(anchor, size) {
|
||||
this.Texture = texture ?? DefaultTexture;
|
||||
this.HoveredTexture = hoveredTexture ?? DefaultHoveredTexture;
|
||||
this.HoveredColor = hoveredColor ?? DefaultHoveredColor;
|
||||
|
||||
if (text != null) {
|
||||
this.Text = new AutoScaledText(Anchor.Center, Vector2.One, text, true) {
|
||||
this.Text = new AutoScaledText(Anchor.Center, Vector2.One, text) {
|
||||
IgnoresMouse = true
|
||||
};
|
||||
this.AddChild(this.Text);
|
||||
|
|
|
@ -5,10 +5,12 @@ using MLEM.Textures;
|
|||
namespace MLEM.Ui.Elements {
|
||||
public class Panel : Element {
|
||||
|
||||
public static NinePatch DefaultTexture;
|
||||
|
||||
private readonly NinePatch texture;
|
||||
|
||||
public Panel(Anchor anchor, Vector2 size, Point positionOffset, NinePatch texture) : base(anchor, size) {
|
||||
this.texture = texture;
|
||||
public Panel(Anchor anchor, Vector2 size, Point positionOffset, NinePatch texture = null) : base(anchor, size) {
|
||||
this.texture = texture ?? DefaultTexture;
|
||||
this.PositionOffset = positionOffset;
|
||||
this.ChildPadding = new Point(5);
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ using MLEM.Font;
|
|||
namespace MLEM.Ui.Elements {
|
||||
public class Paragraph : Element {
|
||||
|
||||
public static IGenericFont DefaultFont;
|
||||
|
||||
private string text;
|
||||
private float lineHeight;
|
||||
private string[] splitText;
|
||||
|
@ -24,23 +26,22 @@ namespace MLEM.Ui.Elements {
|
|||
}
|
||||
|
||||
}
|
||||
public IGenericFont Font => this.font ?? this.System.DefaultFont;
|
||||
|
||||
public Paragraph(Anchor anchor, float width, string text, float textScale = 1, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) {
|
||||
this.text = text;
|
||||
this.font = font;
|
||||
this.font = font ?? DefaultFont;
|
||||
this.TextScale = textScale;
|
||||
this.centerText = centerText;
|
||||
}
|
||||
|
||||
protected override Point CalcActualSize(Rectangle parentArea) {
|
||||
var size = base.CalcActualSize(parentArea);
|
||||
this.splitText = this.Font.SplitString(this.text, size.X, this.TextScale).ToArray();
|
||||
this.splitText = this.font.SplitString(this.text, size.X, this.TextScale).ToArray();
|
||||
|
||||
this.lineHeight = 0;
|
||||
var height = 0F;
|
||||
foreach (var strg in this.splitText) {
|
||||
var strgHeight = this.Font.MeasureString(strg).Y * this.TextScale;
|
||||
var strgHeight = this.font.MeasureString(strg).Y * this.TextScale;
|
||||
height += strgHeight + 1;
|
||||
if (strgHeight > this.lineHeight)
|
||||
this.lineHeight = strgHeight;
|
||||
|
@ -55,9 +56,9 @@ namespace MLEM.Ui.Elements {
|
|||
var offset = new Vector2();
|
||||
foreach (var line in this.splitText) {
|
||||
if (this.centerText) {
|
||||
this.Font.DrawCenteredString(batch, line, pos + offset + new Vector2(this.DisplayArea.Width / 2, 0), this.TextScale, color);
|
||||
this.font.DrawCenteredString(batch, line, pos + offset + new Vector2(this.DisplayArea.Width / 2, 0), this.TextScale, color);
|
||||
} else {
|
||||
this.Font.DrawString(batch, line, pos + offset, color, 0, Vector2.Zero, this.TextScale, SpriteEffects.None, 0);
|
||||
this.font.DrawString(batch, line, pos + offset, color, 0, Vector2.Zero, this.TextScale, SpriteEffects.None, 0);
|
||||
}
|
||||
offset.Y += this.lineHeight + 1;
|
||||
}
|
||||
|
|
|
@ -24,7 +24,6 @@ namespace MLEM.Ui {
|
|||
root.Element.ForceUpdateArea();
|
||||
}
|
||||
}
|
||||
public IGenericFont DefaultFont;
|
||||
public Rectangle ScaledViewport {
|
||||
get {
|
||||
var bounds = this.graphicsDevice.Viewport.Bounds;
|
||||
|
|
|
@ -9,7 +9,6 @@ using MLEM.Startup;
|
|||
using MLEM.Textures;
|
||||
using MLEM.Ui;
|
||||
using MLEM.Ui.Elements;
|
||||
using MonoGame.Extended.TextureAtlases;
|
||||
|
||||
namespace Tests {
|
||||
public class GameImpl : MlemGame {
|
||||
|
@ -26,7 +25,7 @@ namespace Tests {
|
|||
this.testTexture = LoadContent<Texture2D>("Textures/Test");
|
||||
this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8);
|
||||
|
||||
this.UiSystem.DefaultFont = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
|
||||
Paragraph.DefaultFont = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
|
||||
this.UiSystem.GlobalScale = 5;
|
||||
|
||||
var root = new Panel(Anchor.BottomLeft, new Vector2(100, 100), new Point(5, 5), this.testPatch);
|
||||
|
|
Loading…
Reference in a new issue