mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
added ElementHelper and did some textfield changes
This commit is contained in:
parent
404e95c8f2
commit
cb5594bada
4 changed files with 101 additions and 33 deletions
|
@ -155,6 +155,10 @@ namespace Demos {
|
|||
PositionOffset = new Vector2(0, 1)
|
||||
});
|
||||
|
||||
root.AddChild(new VerticalSpace(3));
|
||||
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "There are also some additional \"components\" which are created as combinations of other components. You can find all of them in the ElementHelper class. Here are some examples:"));
|
||||
root.AddChild(ElementHelper.NumberField(Anchor.AutoLeft, new Vector2(1, 10))).PositionOffset = new Vector2(0, 1);
|
||||
|
||||
// Below are some querying examples that help you find certain elements easily
|
||||
|
||||
var children = root.GetChildren();
|
||||
|
|
|
@ -85,6 +85,7 @@ namespace MLEM.Ui.Elements {
|
|||
public MouseCallback OnMouseEnter;
|
||||
public MouseCallback OnMouseExit;
|
||||
public TextInputCallback OnTextInput;
|
||||
public GenericCallback OnAreaUpdated;
|
||||
|
||||
private UiSystem system;
|
||||
public UiSystem System {
|
||||
|
@ -289,6 +290,8 @@ namespace MLEM.Ui.Elements {
|
|||
}
|
||||
|
||||
this.area = new Rectangle(pos, actualSize);
|
||||
this.OnAreaUpdated?.Invoke(this);
|
||||
|
||||
foreach (var child in this.Children)
|
||||
child.ForceUpdateArea();
|
||||
|
||||
|
|
42
MLEM.Ui/Elements/ElementHelper.cs
Normal file
42
MLEM.Ui/Elements/ElementHelper.cs
Normal file
|
@ -0,0 +1,42 @@
|
|||
using Microsoft.Xna.Framework;
|
||||
using MLEM.Input;
|
||||
|
||||
namespace MLEM.Ui.Elements {
|
||||
public static class ElementHelper {
|
||||
|
||||
public static Group NumberField(Anchor anchor, Vector2 size, int defaultValue = 0, int stepPerClick = 1, TextField.Rule rule = null, TextField.TextChanged onTextChange = null) {
|
||||
var group = new Group(anchor, size, false);
|
||||
|
||||
var field = new TextField(Anchor.TopLeft, Vector2.One, rule ?? TextField.OnlyNumbers);
|
||||
field.OnTextChange = onTextChange;
|
||||
field.AppendText(defaultValue.ToString());
|
||||
group.AddChild(field);
|
||||
group.OnAreaUpdated += e => field.Size = new Vector2((e.Area.Width - e.Area.Height / 2) / e.Scale, 1);
|
||||
|
||||
var upButton = new Button(Anchor.TopRight, Vector2.One, "+") {
|
||||
OnClicked = (element, button) => {
|
||||
if (button == MouseButton.Left) {
|
||||
var text = field.Text.ToString();
|
||||
field.SetText(int.Parse(text) + stepPerClick);
|
||||
}
|
||||
}
|
||||
};
|
||||
group.AddChild(upButton);
|
||||
group.OnAreaUpdated += e => upButton.Size = new Vector2(e.Area.Height / 2 / e.Scale);
|
||||
|
||||
var downButton = new Button(Anchor.BottomRight, Vector2.One, "-") {
|
||||
OnClicked = (element, button) => {
|
||||
if (button == MouseButton.Left) {
|
||||
var text = field.Text.ToString();
|
||||
field.SetText(int.Parse(text) - stepPerClick);
|
||||
}
|
||||
}
|
||||
};
|
||||
group.AddChild(downButton);
|
||||
group.OnAreaUpdated += e => downButton.Size = new Vector2(e.Area.Height / 2 / e.Scale);
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -21,10 +21,10 @@ namespace MLEM.Ui.Elements {
|
|||
public NinePatch HoveredTexture;
|
||||
public Color HoveredColor;
|
||||
public float TextScale;
|
||||
public readonly StringBuilder Text = new StringBuilder();
|
||||
private readonly StringBuilder text = new StringBuilder();
|
||||
public string Text => this.text.ToString();
|
||||
public string PlaceholderText;
|
||||
public TextChanged OnTextChange;
|
||||
public int MaxTextLength = int.MaxValue;
|
||||
public float TextOffsetX = 4;
|
||||
private IGenericFont font;
|
||||
private double caretBlinkTimer;
|
||||
|
@ -37,24 +37,25 @@ namespace MLEM.Ui.Elements {
|
|||
this.OnTextInput += (element, key, character) => {
|
||||
if (!this.IsSelected)
|
||||
return;
|
||||
var textChanged = false;
|
||||
if (key == Keys.Back) {
|
||||
if (this.Text.Length > 0) {
|
||||
this.Text.Remove(this.Text.Length - 1, 1);
|
||||
textChanged = true;
|
||||
if (this.text.Length > 0) {
|
||||
this.RemoveText(this.text.Length - 1, 1);
|
||||
}
|
||||
} else if (this.InputRule(this, character.ToString())) {
|
||||
if (this.Text.Length < this.MaxTextLength) {
|
||||
this.Text.Append(character);
|
||||
textChanged = true;
|
||||
} else {
|
||||
this.AppendText(character);
|
||||
}
|
||||
};
|
||||
}
|
||||
if (textChanged) {
|
||||
var length = this.font.MeasureString(this.Text).X * this.TextScale;
|
||||
|
||||
private void HandleTextChange() {
|
||||
// not initialized yet
|
||||
if (this.font == null)
|
||||
return;
|
||||
var length = this.font.MeasureString(this.text).X * this.TextScale;
|
||||
var maxWidth = this.DisplayArea.Width / this.Scale - this.TextOffsetX * 2;
|
||||
if (length > maxWidth) {
|
||||
for (var i = 0; i < this.Text.Length; i++) {
|
||||
var substring = this.Text.ToString(i, this.Text.Length - i);
|
||||
for (var i = 0; i < this.text.Length; i++) {
|
||||
var substring = this.text.ToString(i, this.text.Length - i);
|
||||
if (this.font.MeasureString(substring).X * this.TextScale <= maxWidth) {
|
||||
this.textStartIndex = i;
|
||||
break;
|
||||
|
@ -64,9 +65,7 @@ namespace MLEM.Ui.Elements {
|
|||
this.textStartIndex = 0;
|
||||
}
|
||||
|
||||
this.OnTextChange?.Invoke(this, this.Text.ToString());
|
||||
}
|
||||
};
|
||||
this.OnTextChange?.Invoke(this, this.text.ToString());
|
||||
}
|
||||
|
||||
public override void Update(GameTime time) {
|
||||
|
@ -88,16 +87,36 @@ namespace MLEM.Ui.Elements {
|
|||
batch.Draw(tex, this.DisplayArea.OffsetCopy(offset), color, this.Scale);
|
||||
|
||||
var textPos = this.DisplayArea.Location.ToVector2() + new Vector2(offset.X + this.TextOffsetX * this.Scale, offset.Y + this.DisplayArea.Height / 2);
|
||||
if (this.Text.Length > 0 || this.IsSelected) {
|
||||
if (this.text.Length > 0 || this.IsSelected) {
|
||||
var caret = this.IsSelected && this.caretBlinkTimer >= 0.5F ? "|" : "";
|
||||
var text = this.Text.ToString(this.textStartIndex, this.Text.Length - this.textStartIndex) + caret;
|
||||
this.font.DrawCenteredString(batch, text, textPos, this.TextScale * this.Scale, Color.White * alpha, false, true);
|
||||
var display = this.text.ToString(this.textStartIndex, this.text.Length - this.textStartIndex) + caret;
|
||||
this.font.DrawCenteredString(batch, display, textPos, this.TextScale * this.Scale, Color.White * alpha, false, true);
|
||||
} else if (this.PlaceholderText != null) {
|
||||
this.font.DrawCenteredString(batch, this.PlaceholderText, textPos, this.TextScale * this.Scale, Color.Gray * alpha, false, true);
|
||||
}
|
||||
base.Draw(time, batch, alpha, offset);
|
||||
}
|
||||
|
||||
public void SetText(object text) {
|
||||
if (!this.InputRule(this, text.ToString()))
|
||||
return;
|
||||
this.text.Clear();
|
||||
this.text.Append(text);
|
||||
this.HandleTextChange();
|
||||
}
|
||||
|
||||
public void AppendText(object text) {
|
||||
if (!this.InputRule(this, text.ToString()))
|
||||
return;
|
||||
this.text.Append(text);
|
||||
this.HandleTextChange();
|
||||
}
|
||||
|
||||
public void RemoveText(int index, int length) {
|
||||
this.text.Remove(index, length);
|
||||
this.HandleTextChange();
|
||||
}
|
||||
|
||||
protected override void InitStyle(UiStyle style) {
|
||||
base.InitStyle(style);
|
||||
this.TextScale = style.TextScale;
|
||||
|
|
Loading…
Reference in a new issue