mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-12-25 01:39:23 +01:00
added image component
This commit is contained in:
parent
ef3726b077
commit
18e634cad8
4 changed files with 68 additions and 10 deletions
|
@ -11,10 +11,10 @@ namespace MLEM.Ui.Elements {
|
|||
public Color HoveredColor;
|
||||
public AutoScaledText Text;
|
||||
|
||||
public Button(Anchor anchor, Vector2 size, NinePatch texture, string text = null, NinePatch hoveredTexture = null, Color? hoveredColor = null) : base(anchor, size) {
|
||||
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.White;
|
||||
this.HoveredColor = hoveredColor ?? Color.LightGray;
|
||||
|
||||
if (text != null) {
|
||||
this.Text = new AutoScaledText(Anchor.Center, Vector2.One, text, true) {
|
||||
|
|
|
@ -58,7 +58,14 @@ namespace MLEM.Ui.Elements {
|
|||
public UiSystem System { get; private set; }
|
||||
public Element Parent { get; private set; }
|
||||
public bool IsMouseOver { get; private set; }
|
||||
public bool IsHidden;
|
||||
private bool isHidden;
|
||||
public bool IsHidden {
|
||||
get => this.isHidden;
|
||||
set {
|
||||
this.isHidden = value;
|
||||
this.SetDirty();
|
||||
}
|
||||
}
|
||||
public bool IgnoresMouse;
|
||||
|
||||
private Rectangle area;
|
||||
|
@ -90,11 +97,14 @@ namespace MLEM.Ui.Elements {
|
|||
this.SetDirty();
|
||||
}
|
||||
|
||||
public void AddChild(Element element) {
|
||||
this.children.Add(element);
|
||||
public Element AddChild(Element element, int index = -1) {
|
||||
if (index < 0 || index > this.children.Count)
|
||||
index = this.children.Count;
|
||||
this.children.Insert(index, element);
|
||||
element.Parent = this;
|
||||
element.System = this.System;
|
||||
this.SetDirty();
|
||||
return element;
|
||||
}
|
||||
|
||||
public void RemoveChild(Element element) {
|
||||
|
@ -104,8 +114,24 @@ namespace MLEM.Ui.Elements {
|
|||
this.SetDirty();
|
||||
}
|
||||
|
||||
public void MoveToFront() {
|
||||
if (this.Parent != null) {
|
||||
this.Parent.RemoveChild(this);
|
||||
this.Parent.AddChild(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveToBack() {
|
||||
if (this.Parent != null) {
|
||||
this.Parent.RemoveChild(this);
|
||||
this.Parent.AddChild(this, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetDirty() {
|
||||
this.areaDirty = true;
|
||||
if (this.Parent != null)
|
||||
this.Parent.SetDirty();
|
||||
}
|
||||
|
||||
public void UpdateAreaIfDirty() {
|
||||
|
@ -176,7 +202,7 @@ namespace MLEM.Ui.Elements {
|
|||
}
|
||||
|
||||
if (this.Anchor >= Anchor.AutoLeft) {
|
||||
var previousChild = this.GetPreviousChild();
|
||||
var previousChild = this.GetPreviousChild(false);
|
||||
if (previousChild != null) {
|
||||
var prevArea = previousChild.Area;
|
||||
switch (this.Anchor) {
|
||||
|
@ -214,12 +240,14 @@ namespace MLEM.Ui.Elements {
|
|||
(this.size.Y > 1 ? this.size.Y : parentArea.Height * this.size.Y).Floor());
|
||||
}
|
||||
|
||||
protected Element GetPreviousChild() {
|
||||
protected Element GetPreviousChild(bool hiddenAlso) {
|
||||
if (this.Parent == null)
|
||||
return null;
|
||||
|
||||
Element lastChild = null;
|
||||
foreach (var child in this.Parent.children) {
|
||||
if (!hiddenAlso && child.IsHidden)
|
||||
continue;
|
||||
if (child == this)
|
||||
break;
|
||||
lastChild = child;
|
||||
|
@ -257,8 +285,8 @@ namespace MLEM.Ui.Elements {
|
|||
return null;
|
||||
if (!this.Area.Contains(mousePos))
|
||||
return null;
|
||||
foreach (var child in this.children) {
|
||||
var element = child.GetMousedElement(mousePos);
|
||||
for (var i = this.children.Count - 1; i >= 0; i--) {
|
||||
var element = this.children[i].GetMousedElement(mousePos);
|
||||
if (element != null)
|
||||
return element;
|
||||
}
|
||||
|
|
29
MLEM.Ui/Elements/Image.cs
Normal file
29
MLEM.Ui/Elements/Image.cs
Normal file
|
@ -0,0 +1,29 @@
|
|||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MLEM.Extensions;
|
||||
using MLEM.Textures;
|
||||
|
||||
namespace MLEM.Ui.Elements {
|
||||
public class Image : Element {
|
||||
|
||||
public Color Color = Color.White;
|
||||
private readonly TextureRegion texture;
|
||||
private readonly bool scaleToImage;
|
||||
|
||||
public Image(Anchor anchor, Vector2 size, TextureRegion texture, bool scaleToImage = false) : base(anchor, size) {
|
||||
this.texture = texture;
|
||||
this.scaleToImage = scaleToImage;
|
||||
this.Padding = new Point(1);
|
||||
}
|
||||
|
||||
protected override Point CalcActualSize(Rectangle parentArea) {
|
||||
return this.scaleToImage ? this.texture.Size : base.CalcActualSize(parentArea);
|
||||
}
|
||||
|
||||
public override void Draw(GameTime time, SpriteBatch batch, Color color) {
|
||||
batch.Draw(this.texture, this.DisplayArea, this.Color.CopyAlpha(color));
|
||||
base.Draw(time, batch, color);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -32,10 +32,11 @@ namespace Tests {
|
|||
|
||||
var root = new Panel(Anchor.BottomLeft, new Vector2(100, 100), new Point(5, 5), this.testPatch);
|
||||
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is a test text that is hopefully long enough to cover at least a few lines, otherwise it would be very sad.", 0.2F));
|
||||
var image = root.AddChild(new Image(Anchor.AutoCenter, new Vector2(20, 20), new TextureRegion(this.testTexture, 0, 0, 8, 8)) {IsHidden = true});
|
||||
root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 15), new NinePatch(new TextureRegion(this.testTexture, 24, 8, 16, 16), 4), "Test Button") {
|
||||
OnClicked = (element, pos, button) => {
|
||||
if (button == MouseButton.Left)
|
||||
Console.WriteLine("Clicked");
|
||||
image.IsHidden = !image.IsHidden;
|
||||
}
|
||||
});
|
||||
this.uiSystem.Add("Test", root);
|
||||
|
|
Loading…
Reference in a new issue