From 73f9653ddcfe8ae3d239b2dea009cf3913331c77 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 28 Dec 2019 14:23:40 +0100 Subject: [PATCH] made tooltips groups so that more stuff can be added to them easily --- MLEM.Ui/Elements/Tooltip.cs | 52 +++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/MLEM.Ui/Elements/Tooltip.cs b/MLEM.Ui/Elements/Tooltip.cs index 09d8e2d..fda0a3e 100644 --- a/MLEM.Ui/Elements/Tooltip.cs +++ b/MLEM.Ui/Elements/Tooltip.cs @@ -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); - } - } } \ No newline at end of file