1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-29 15:58:33 +01:00
MLEM/MLEM.Ui/Elements/Tooltip.cs

56 lines
2 KiB
C#
Raw Normal View History

2019-08-13 23:54:29 +02:00
using System;
using Microsoft.Xna.Framework;
using MLEM.Extensions;
using MLEM.Font;
using MLEM.Ui.Style;
namespace MLEM.Ui.Elements {
public class Tooltip : Paragraph {
public Vector2 MouseOffset = new Vector2(2, 3);
public Tooltip(float width, string text, Element elementToHover = null) :
2019-08-24 00:07:54 +02:00
base(Anchor.TopLeft, width, text) {
this.AutoAdjustWidth = true;
2019-08-13 23:54:29 +02:00
this.Padding = new Point(2);
2019-08-28 18:27:17 +02:00
this.CanBeSelected = false;
if (elementToHover != null) {
2019-08-28 18:27:17 +02:00
elementToHover.OnMouseEnter += element => element.System.Add(element.GetType().Name + "Tooltip", this).CanSelectContent = false;
elementToHover.OnMouseExit += element => {
if (this.System != null)
this.System.Remove(element.GetType().Name + "Tooltip");
};
}
2019-08-13 23:54:29 +02:00
}
public override void Update(GameTime time) {
base.Update(time);
var viewport = this.System.Viewport.Size;
var offset = this.Input.MousePosition.ToVector2() / this.Scale + this.MouseOffset;
if (offset.X < 0)
offset.X = 0;
if (offset.Y < 0)
offset.Y = 0;
if (offset.X * this.Scale + this.Area.Width >= viewport.X)
offset.X = (viewport.X - this.Area.Width) / this.Scale;
if (offset.Y * this.Scale + this.Area.Height >= viewport.Y)
offset.Y = (viewport.Y - this.Area.Height) / this.Scale;
this.PositionOffset = offset;
2019-08-13 23:54:29 +02:00
}
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 = style.TooltipBackground;
this.BackgroundColor = style.TooltipBackgroundColor;
}
}
}