Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. /// <summary>
  2. /// Reposition the tooltip so that it is within the bounds but not within
  3. /// the source
  4. /// </summary>
  5. public static void RepositionTooltip(RectTransform tooltip,
  6. RectTransform source,
  7. RectTransform bounds)
  8. {
  9. tooltip.position = source.position;
  10.  
  11. // work in world space because it's easier
  12. var tooltipRect = tooltip.GetWorldRect();
  13. var sourceRect = source.GetWorldRect();
  14. var boundsRect = bounds.GetWorldRect();
  15.  
  16. // divide the bounds into four overlapping rectangles on each side of
  17. // the source rect
  18. var xMinRect = Rect.MinMaxRect(boundsRect.xMin, boundsRect.yMin,
  19. sourceRect.xMin, boundsRect.yMax);
  20. var yMinRect = Rect.MinMaxRect(boundsRect.xMin, boundsRect.yMin,
  21. boundsRect.xMax, sourceRect.yMin);
  22. var xMaxRect = Rect.MinMaxRect(sourceRect.xMax, boundsRect.yMin,
  23. boundsRect.xMax, boundsRect.yMax);
  24. var yMaxRect = Rect.MinMaxRect(boundsRect.xMin, sourceRect.yMax,
  25. boundsRect.xMax, boundsRect.yMax);
  26.  
  27. var rects = new[] { xMinRect, xMaxRect, yMinRect, yMaxRect };
  28.  
  29. // bounds bounds
  30. //+------------------------+ +--------+--------+------+
  31. //| | | | | |
  32. //| | | | | |
  33. //| yMin | | xMin | | xMax |
  34. //| | | | | |
  35. //| | | | | |
  36. //+--------+--------+------+ | +--------+ |
  37. //| | | | | | | |
  38. //| | source | | | | source | |
  39. //| | | | | | | |
  40. //+--------+--------+------+ | +--------+ |
  41. //| yMax | | | | |
  42. //| | | | | |
  43. //+------------------------+ +--------+--------+------+
  44.  
  45. // find the first division in which the tooltip rect will fit
  46. for (int i = 0; i < rects.Length; ++i)
  47. {
  48. Rect rect = rects[i];
  49.  
  50. if (tooltipRect.width <= rect.width
  51. && tooltipRect.height <= rect.height)
  52. {
  53. // push the tooltip rect into the division
  54. float pushR = Mathf.Max(0f, rect.xMin - tooltipRect.xMin);
  55. float pushU = Mathf.Max(0f, rect.yMin - tooltipRect.yMin);
  56.  
  57. float pushL = Mathf.Min(0f, rect.xMax - tooltipRect.xMax);
  58. float pushD = Mathf.Min(0f, rect.yMax - tooltipRect.yMax);
  59.  
  60. var push = new Vector2(pushL + pushR, pushU + pushD);
  61.  
  62. tooltip.position += (Vector3) push;
  63.  
  64. break;
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement