Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tooltips voor knoppen rechtsboven #260

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 54 additions & 26 deletions Assets/Scripts/UI/Components/TooltipDialog.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using DG.Tweening;
using System;
using System.Collections;
using TMPro;
Expand All @@ -14,7 +15,11 @@ public class TooltipDialog : MonoBehaviour

private TextMeshProUGUI tooltiptext;
private RectTransform rectTransform;
private RectTransform lastTarget;
private RectTransform currentTarget;
private ContentSizeFitter contentSizeFitter;
private Sequence animationSequence;
private const float animationDuration = 0.25f;
private Vector3[] worldCorners = new Vector3[4];

#region Singleton
Expand Down Expand Up @@ -49,39 +54,29 @@ private void Awake()
private void FollowPointer()
{
rectTransform.position = Mouse.current.position.ReadValue();

SwapPivot();
}

private void SwapPivot()
public void AlignOnElement(RectTransform element)
{
var pivot = Vector2.zero;
//Swap pivot based on place in screen (to try to stay in the screen horizontally)
if (rectTransform.position.x + GetRectTransformBounds(rectTransform).size.x > Screen.width)
{
pivot.x = 1;
}

//Swap pivot based on place in screen (to try to stay in the screen vertically)
if (rectTransform.position.y + GetRectTransformBounds(rectTransform).size.y > Screen.height)
{
pivot.y = 1;
}
if (!element) return;

rectTransform.pivot = pivot;
lastTarget = currentTarget;
currentTarget = element;
}

public void AlignOnElement(RectTransform element)
private void UpdatePosition()
{
if (!element) return;

var elementCenter = GetRectTransformBounds(element).center;
var elementMax = GetRectTransformBounds(element).max;
var elementCenter = GetRectTransformBounds(currentTarget).center;
var elementMax = GetRectTransformBounds(currentTarget).max;
var elementMin = GetRectTransformBounds(currentTarget).min;
Vector2 tooltipSize = GetRectTransformBounds(rectTransform).size;
Vector2 elementSize = GetRectTransformBounds(currentTarget).size;

var tooltipPosition = new Vector2(elementMax.x + offset.x, elementCenter.y + offset.y);
if (currentTarget.position.x > Screen.width * 0.5f)
tooltipPosition = new Vector2(elementMin.x - tooltipSize.x - offset.x, elementCenter.y + offset.y);

var tooltipPosition = new Vector2(elementMax.x, elementCenter.y) + offset;
rectTransform.position = tooltipPosition;

SwapPivot();
}

public void ShowMessage(string message = "Tooltip", RectTransform hoverTarget = null)
Expand All @@ -96,6 +91,8 @@ public void ShowMessage(string message = "Tooltip", RectTransform hoverTarget =
tooltiptext.text = message;

StartCoroutine(FitContent());

StartAnimation(1f);
}

private IEnumerator FitContent()
Expand All @@ -106,8 +103,11 @@ private IEnumerator FitContent()
}

public void Hide()
{
gameObject.SetActive(false);
{
StartAnimation(0f, ()=> {
if(lastTarget == currentTarget || currentTarget == null)
gameObject.SetActive(false);
});
}

private Bounds GetRectTransformBounds(RectTransform transform)
Expand All @@ -121,5 +121,33 @@ private Bounds GetRectTransformBounds(RectTransform transform)

return bounds;
}


private void StartAnimation(float targetScale, Action onFinish = null)
{
// If the animation is playing, quickly complete it and then start a new one
if (animationSequence != null && animationSequence.IsPlaying())
{
animationSequence.Complete(true);
}
animationSequence = CreateAnimationSequence(targetScale, onFinish);
animationSequence.Play();
}

private Sequence CreateAnimationSequence(float scale, Action onFinish = null)
{
Sequence sequence = DOTween.Sequence(rectTransform);
sequence.SetEase(scale > 0 ? Ease.OutBounce : Ease.InBack);
sequence.Join(rectTransform.DOScale(scale, scale > 0 ? animationDuration : 0.5f * animationDuration).OnUpdate(() => UpdatePosition()));

// Ensure animation sequence is nulled after completing to clean up
sequence.OnComplete(() =>
{
onFinish?.Invoke();
animationSequence = null;
});

return sequence;
}
}
}
4 changes: 2 additions & 2 deletions Assets/Textures/UI/Icons/Icon_Help.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading