1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +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;
namespace MLEM.Ui.Elements {
public class Tooltip : Paragraph {
public class Tooltip : Group {
public Vector2 MouseOffset = new Vector2(2, 3);
public Paragraph Paragraph;
public Tooltip(float width, string text, Element elementToHover = null) :
base(Anchor.TopLeft, width, text) {
this.AutoAdjustWidth = true;
this.Padding = new Vector2(2);
this.CanBeSelected = false;
public Tooltip(float width, string text = null, Element elementToHover = null) :
base(Anchor.TopLeft, Vector2.One) {
if (text != null) {
this.Paragraph = this.AddChild(new Paragraph(Anchor.TopLeft, width, text));
this.Paragraph.AutoAdjustWidth = true;
this.Paragraph.Padding = new Vector2(2);
this.SetWidthBasedOnChildren = true;
}
this.CanBeMoused = false;
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 => {
if (this.System != null)
this.System.Remove(element.GetType().Name + "Tooltip");
@ -26,7 +34,24 @@ namespace MLEM.Ui.Elements {
public override void Update(GameTime 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 offset = this.Input.MousePosition.ToVector2() / this.Scale + this.MouseOffset;
if (offset.X < 0)
@ -39,18 +64,5 @@ namespace MLEM.Ui.Elements {
offset.Y = (viewport.Y - this.Area.Height) / this.Scale;
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);
}
}
}