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

made tooltips groups so that more stuff can be added to them easily

This commit is contained in:
Ellpeck 2019-12-28 14:23:40 +01:00
parent b53588c32e
commit 73f9653ddc

View file

@ -5,18 +5,26 @@ using MLEM.Font;
using MLEM.Ui.Style; using MLEM.Ui.Style;
namespace MLEM.Ui.Elements { namespace MLEM.Ui.Elements {
public class Tooltip : Paragraph { public class Tooltip : Group {
public Vector2 MouseOffset = new Vector2(2, 3); public Vector2 MouseOffset = new Vector2(2, 3);
public Paragraph Paragraph;
public Tooltip(float width, string text, Element elementToHover = null) : public Tooltip(float width, string text = null, Element elementToHover = null) :
base(Anchor.TopLeft, width, text) { base(Anchor.TopLeft, Vector2.One) {
this.AutoAdjustWidth = true; if (text != null) {
this.Padding = new Vector2(2); this.Paragraph = this.AddChild(new Paragraph(Anchor.TopLeft, width, text));
this.CanBeSelected = false; this.Paragraph.AutoAdjustWidth = true;
this.Paragraph.Padding = new Vector2(2);
this.SetWidthBasedOnChildren = true;
}
this.CanBeMoused = false;
if (elementToHover != null) { if (elementToHover != null) {
elementToHover.OnMouseEnter += element => element.System.Add(element.GetType().Name + "Tooltip", this); elementToHover.OnMouseEnter += element => {
element.System.Add(element.GetType().Name + "Tooltip", this);
this.SnapPositionToMouse();
};
elementToHover.OnMouseExit += element => { elementToHover.OnMouseExit += element => {
if (this.System != null) if (this.System != null)
this.System.Remove(element.GetType().Name + "Tooltip"); this.System.Remove(element.GetType().Name + "Tooltip");
@ -26,7 +34,24 @@ namespace MLEM.Ui.Elements {
public override void Update(GameTime time) { public override void Update(GameTime time) {
base.Update(time); base.Update(time);
this.SnapPositionToMouse();
}
public override void ForceUpdateArea() {
if (this.Parent != null)
throw new NotSupportedException($"A tooltip shouldn't be the child of another element ({this.Parent})");
base.ForceUpdateArea();
}
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
if (this.Paragraph != null) {
this.Paragraph.Background.SetFromStyle(style.TooltipBackground);
this.Paragraph.BackgroundColor.SetFromStyle(style.TooltipBackgroundColor);
}
}
public void SnapPositionToMouse() {
var viewport = this.System.Viewport.Size; var viewport = this.System.Viewport.Size;
var offset = this.Input.MousePosition.ToVector2() / this.Scale + this.MouseOffset; var offset = this.Input.MousePosition.ToVector2() / this.Scale + this.MouseOffset;
if (offset.X < 0) if (offset.X < 0)
@ -39,18 +64,5 @@ namespace MLEM.Ui.Elements {
offset.Y = (viewport.Y - this.Area.Height) / this.Scale; offset.Y = (viewport.Y - this.Area.Height) / this.Scale;
this.PositionOffset = offset; this.PositionOffset = offset;
} }
public override void ForceUpdateArea() {
if (this.Parent != null)
throw new NotSupportedException($"A tooltip shouldn't be the child of another element ({this.Parent})");
base.ForceUpdateArea();
}
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.Background.SetFromStyle(style.TooltipBackground);
this.BackgroundColor.SetFromStyle(style.TooltipBackgroundColor);
}
} }
} }