Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Reposition the tooltip so that it is within the bounds but not within
- /// the source
- /// </summary>
- public static void RepositionTooltip(RectTransform tooltip,
- RectTransform source,
- RectTransform bounds)
- {
- tooltip.position = source.position;
- // work in world space because it's easier
- var tooltipRect = tooltip.GetWorldRect();
- var sourceRect = source.GetWorldRect();
- var boundsRect = bounds.GetWorldRect();
- // divide the bounds into four overlapping rectangles on each side of
- // the source rect
- var xMinRect = Rect.MinMaxRect(boundsRect.xMin, boundsRect.yMin,
- sourceRect.xMin, boundsRect.yMax);
- var yMinRect = Rect.MinMaxRect(boundsRect.xMin, boundsRect.yMin,
- boundsRect.xMax, sourceRect.yMin);
- var xMaxRect = Rect.MinMaxRect(sourceRect.xMax, boundsRect.yMin,
- boundsRect.xMax, boundsRect.yMax);
- var yMaxRect = Rect.MinMaxRect(boundsRect.xMin, sourceRect.yMax,
- boundsRect.xMax, boundsRect.yMax);
- var rects = new[] { xMinRect, xMaxRect, yMinRect, yMaxRect };
- // bounds bounds
- //+------------------------+ +--------+--------+------+
- //| | | | | |
- //| | | | | |
- //| yMin | | xMin | | xMax |
- //| | | | | |
- //| | | | | |
- //+--------+--------+------+ | +--------+ |
- //| | | | | | | |
- //| | source | | | | source | |
- //| | | | | | | |
- //+--------+--------+------+ | +--------+ |
- //| yMax | | | | |
- //| | | | | |
- //+------------------------+ +--------+--------+------+
- // find the first division in which the tooltip rect will fit
- for (int i = 0; i < rects.Length; ++i)
- {
- Rect rect = rects[i];
- if (tooltipRect.width <= rect.width
- && tooltipRect.height <= rect.height)
- {
- // push the tooltip rect into the division
- float pushR = Mathf.Max(0f, rect.xMin - tooltipRect.xMin);
- float pushU = Mathf.Max(0f, rect.yMin - tooltipRect.yMin);
- float pushL = Mathf.Min(0f, rect.xMax - tooltipRect.xMax);
- float pushD = Mathf.Min(0f, rect.yMax - tooltipRect.yMax);
- var push = new Vector2(pushL + pushR, pushU + pushD);
- tooltip.position += (Vector3) push;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement