From d2774ad7d26225451b11c1ead7be51a42eef4df4 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 8 Oct 2024 18:07:38 +0200 Subject: [PATCH] Added Tooltip.IgnoreViewport and allow overriding the default viewport using Tooltip.Viewport --- CHANGELOG.md | 1 + MLEM.Ui/Elements/Tooltip.cs | 39 ++++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b05068..62c4c09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ Fixes Additions - Added Panel.IsVisible method to check if a child element is visible - Added TextField.OnEnterPressed event +- Added Tooltip.IgnoreViewport and allow overriding the default viewport using Tooltip.Viewport ## 7.1.1 diff --git a/MLEM.Ui/Elements/Tooltip.cs b/MLEM.Ui/Elements/Tooltip.cs index 1fc67be..be077c6 100644 --- a/MLEM.Ui/Elements/Tooltip.cs +++ b/MLEM.Ui/Elements/Tooltip.cs @@ -91,6 +91,14 @@ namespace MLEM.Ui.Elements { /// Note that, if is , this value is ignored. /// public virtual Vector2? SnapPosition { get; set; } + /// + /// Determines whether this tooltip should ignore its viewport, which is either this tooltip's or the underlying 's . If this is , the tooltip is allowed to display outside of the viewport, without being bounded in . + /// + public virtual bool IgnoreViewport { get; set; } + /// + /// The viewport that this tooltip should be bound to. If this value is unset, the underlying 's will be used. Note that, if is , this value is ignored. + /// + public virtual Rectangle? Viewport { get; set; } /// public override bool IsHidden => this.autoHidden || base.IsHidden; @@ -207,25 +215,28 @@ namespace MLEM.Ui.Elements { /// Causes this tooltip's position to be snapped to the mouse position, or the element to snap to if is true, or the if set. /// public void SnapPositionToMouse() { - Vector2 snapPosition; + Vector2 snap; + if (this.snapElement != null) { - snapPosition = this.GetSnapOffset(this.AutoNavAnchor, this.snapElement.DisplayArea, this.AutoNavOffset); + snap = this.GetSnapOffset(this.AutoNavAnchor, this.snapElement.DisplayArea, this.AutoNavOffset) / this.Scale; } else { var mouseBounds = new RectangleF(this.SnapPosition ?? this.Input.ViewportMousePosition.ToVector2(), Vector2.Zero); - snapPosition = this.GetSnapOffset(this.MouseAnchor, mouseBounds, this.MouseOffset); + snap = this.GetSnapOffset(this.MouseAnchor, mouseBounds, this.MouseOffset) / this.Scale; } - var viewport = this.System.Viewport; - var offset = snapPosition / this.Scale; - if (offset.X < viewport.X) - offset.X = viewport.X; - if (offset.Y < viewport.Y) - offset.Y = viewport.Y; - if (offset.X * this.Scale + this.Area.Width >= viewport.Right) - offset.X = (viewport.Right - this.Area.Width) / this.Scale; - if (offset.Y * this.Scale + this.Area.Height >= viewport.Bottom) - offset.Y = (viewport.Bottom - this.Area.Height) / this.Scale; - this.PositionOffset = offset; + if (!this.IgnoreViewport) { + var view = this.Viewport ?? this.System.Viewport; + if (snap.X < view.X) + snap.X = view.X; + if (snap.Y < view.Y) + snap.Y = view.Y; + if (snap.X * this.Scale + this.Area.Width >= view.Right) + snap.X = (view.Right - this.Area.Width) / this.Scale; + if (snap.Y * this.Scale + this.Area.Height >= view.Bottom) + snap.Y = (view.Bottom - this.Area.Height) / this.Scale; + } + + this.PositionOffset = snap; } ///