Author Topic: UITooltip going offscreen  (Read 4367 times)

Ryceratops

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
UITooltip going offscreen
« on: April 07, 2014, 10:13:05 AM »
Hello! I'm experiencing something a bit odd with the UITooltip functionality.  Specifically it doesn't seem to be restricting itself to always being on screen.  I opened a new project and checked the Character example scene that utilizes tooltips and it seems to be occurring there too leading me to believe it's not an issue with my current project.  After looking through this code it looks to me like there was intended functionality to always keep the tooltip on screen:

  1. if (uiCamera != null)
  2.             {
  3.                 // Since the screen can be of different than expected size, we want to convert
  4.                 // mouse coordinates to view space, then convert that to world position.
  5.                 mPos.x = Mathf.Clamp01(mPos.x / Screen.width);
  6.                 mPos.y = Mathf.Clamp01(mPos.y / Screen.height);
  7.  
  8.                 // Calculate the ratio of the camera's target orthographic size to current screen size
  9.                 float activeSize = uiCamera.orthographicSize / mTrans.parent.lossyScale.y;
  10.                 float ratio = (Screen.height * 0.5f) / activeSize;
  11.  
  12.                 // Calculate the maximum on-screen size of the tooltip window
  13.                 Vector2 max = new Vector2(ratio * mSize.x / Screen.width, ratio * mSize.y / Screen.height);
  14.  
  15.                 // Limit the tooltip to always be visible
  16.                 mPos.x = Mathf.Min(mPos.x, 1f - max.x);
  17.                 mPos.y = Mathf.Max(mPos.y, max.y);
  18.  
  19.                 // Update the absolute position and save the local one
  20.                 mTrans.position = uiCamera.ViewportToWorldPoint(mPos);
  21.                 mPos = mTrans.localPosition;
  22.                 mPos.x = Mathf.Round(mPos.x);
  23.                 mPos.y = Mathf.Round(mPos.y);
  24.                 mTrans.localPosition = mPos;
  25.  
  26.             }
  27.             else
  28.             {
  29.                                 // Don't let the tooltip leave the screen area
  30.                                 if (mPos.x + mSize.x > Screen.width) mPos.x = Screen.width - mSize.x;
  31.                                 if (mPos.y - mSize.y < 0f) mPos.y = mSize.y;
  32.  
  33.                                 // Simple calculation that assumes that the camera is of fixed size
  34.                                 mPos.x -= Screen.width * 0.5f;
  35.                                 mPos.y -= Screen.height * 0.5f;
  36.                         }


Admittedly I'm struggling to grasp 100% what is all going on in the above code, but I've written tooltip positioning systems before so I played around with the UITooltip code and ended up with some additional positioning code that seems to be working, although it might not be 100% perfect (it's inserted at line 150):

  1. float rightMostPoint = background.width + Input.mousePosition.x;
  2.         float bottomMostPoint = Input.mousePosition.y - background.height;
  3.  
  4.         if(rightMostPoint > Screen.width)
  5.         {
  6.             float rightDelta = rightMostPoint - Screen.width;
  7.             Vector3 newPosition = mPos;
  8.             newPosition.x -= rightDelta;
  9.             mPos = newPosition;
  10.         }
  11.  
  12.         if(bottomMostPoint < 0)
  13.         {
  14.             Vector3 newPosition = mPos;
  15.             newPosition.y += Mathf.Abs(bottomMostPoint);
  16.             mPos = newPosition;
  17.         }

Curious as to what you think about this.  Is it working on your end?  For the tooltip background sprite object I have it using Unified Anchors targeting the tooltip label.  The tooltip label is set to use the ResizeHeight overflow property, aligned left, with the widget anchored to the top right.  The tooltip object itself (which the label and background is parented to) is parented under the UI Root(2D) object.  I initially had it parented under the camera object but I wasn't seeing any difference when I parented it to the parent or root, and currently it just ended up under the root.

Thoughts? Thanks for your time   :D

-Ryceratops
« Last Edit: April 07, 2014, 10:49:13 AM by Ryceratops »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITooltip going offscreen
« Reply #1 on: April 07, 2014, 10:30:49 PM »
Just change line 104 of UITooltip.cs from:
  1. if (background != null && !background.isAnchored)
to:
  1. if (background != null)

Ryceratops

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: UITooltip going offscreen
« Reply #2 on: April 08, 2014, 09:50:04 AM »
That did it! Thanks!