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 {
|
2019-12-31 14:08:13 +01:00
|
|
|
public class Tooltip : Panel {
|
2019-08-13 23:54:29 +02:00
|
|
|
|
2019-12-31 14:08:13 +01:00
|
|
|
public StyleProp<Vector2> MouseOffset;
|
2019-12-28 14:23:40 +01:00
|
|
|
public Paragraph Paragraph;
|
2019-08-13 23:54:29 +02:00
|
|
|
|
2019-12-28 14:23:40 +01:00
|
|
|
public Tooltip(float width, string text = null, Element elementToHover = null) :
|
2019-12-31 14:08:13 +01:00
|
|
|
base(Anchor.TopLeft, Vector2.One, Vector2.Zero) {
|
2019-12-28 14:23:40 +01:00
|
|
|
if (text != null) {
|
|
|
|
this.Paragraph = this.AddChild(new Paragraph(Anchor.TopLeft, width, text));
|
|
|
|
this.Paragraph.AutoAdjustWidth = true;
|
|
|
|
}
|
2019-12-31 14:08:13 +01:00
|
|
|
this.SetWidthBasedOnChildren = true;
|
|
|
|
this.SetHeightBasedOnChildren = true;
|
|
|
|
this.ChildPadding = new Vector2(2);
|
2019-12-28 14:23:40 +01:00
|
|
|
this.CanBeMoused = false;
|
2019-08-24 20:45:40 +02:00
|
|
|
|
|
|
|
if (elementToHover != null) {
|
2019-12-28 14:23:40 +01:00
|
|
|
elementToHover.OnMouseEnter += element => {
|
2020-02-06 01:51:41 +01:00
|
|
|
element.System.Add(element.GetType().Name + "Tooltip", this);
|
2019-12-28 14:23:40 +01:00
|
|
|
this.SnapPositionToMouse();
|
|
|
|
};
|
2019-09-15 02:21:42 +02:00
|
|
|
elementToHover.OnMouseExit += element => {
|
2020-02-06 01:51:41 +01:00
|
|
|
if (this.System != null)
|
|
|
|
this.System.Remove(this.Root.Name);
|
2019-09-15 02:21:42 +02:00
|
|
|
};
|
2019-08-24 20:45:40 +02:00
|
|
|
}
|
2019-08-13 23:54:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Update(GameTime time) {
|
|
|
|
base.Update(time);
|
2019-12-28 14:23:40 +01:00
|
|
|
this.SnapPositionToMouse();
|
|
|
|
}
|
2019-08-15 14:59:15 +02:00
|
|
|
|
2019-12-28 14:23:40 +01: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);
|
2019-12-31 14:08:13 +01:00
|
|
|
this.Texture.SetFromStyle(style.TooltipBackground);
|
|
|
|
this.MouseOffset.SetFromStyle(style.TooltipOffset);
|
2019-12-28 14:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SnapPositionToMouse() {
|
2020-02-06 01:51:41 +01:00
|
|
|
var viewport = this.System.Viewport.Size;
|
2019-08-30 18:15:50 +02:00
|
|
|
var offset = this.Input.MousePosition.ToVector2() / this.Scale + this.MouseOffset;
|
2019-08-15 14:59:15 +02:00
|
|
|
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
|
|
|
}
|
2019-12-29 15:18:49 +01:00
|
|
|
|
2019-08-13 23:54:29 +02:00
|
|
|
}
|
|
|
|
}
|