Author Topic: Detecting if ui sprite is partially off screen  (Read 4453 times)

prinky

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 13
    • View Profile
Detecting if ui sprite is partially off screen
« on: November 04, 2014, 03:40:23 AM »
So my problem is fairly simple: if a ui sprite is partially offscreen (like more than half of it), move it toward the center. The context is that I can select objects in the game, and when I do a menu pops up over the selected object. It doesn't look good if the object is near the edge of the screen and the menu is partially cut off after it appears (i.e. with parts of the menu being rendered off screen).

So one option is to detect if a ui sprite button is offscreen. I've noticed that if the sprite's transform position (not local position) is less than -1.0 in any dimension or more than 1.0 in any dimension, then at least half of it is off-screen. It seems as if NGUI objects end up in a coordinate system that goes from -1.0 to 1.0 in X and Y. Is that a reasonable assumption?

The next problem would be: how would I make sure that the sprite appears onscreen, or with no portions being rendered offscreen? One good solution is that I can push it toward the center by some offset (based on the size of the sprite portion that is off screen).

Edit: I found this thread http://www.tasharen.com/forum/index.php?topic=9889.msg46501#msg46501

Has references to HUDText (which I don't have). There are is also a warning regarding directly using Screen.width and height since the UIRoot's settings may not jive with that idea. So I thought it would be best to rely on the (-1.0, 1.0) transformed coordinates to figure out how much of the sprite is offscreen.
« Last Edit: November 04, 2014, 03:49:54 AM by prinky »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detecting if ui sprite is partially off screen
« Reply #1 on: November 04, 2014, 10:10:12 AM »
-1 to 1 only works if you are working with view space coordinates, which are not going to be used unless you take the world space position and convert it using camera.WorldToViewPortPoint. Any NGUI widget's rect can be retrieved using worldCorners property.

prinky

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: Detecting if ui sprite is partially off screen
« Reply #2 on: November 05, 2014, 02:54:18 AM »
-1 to 1 only works if you are working with view space coordinates, which are not going to be used unless you take the world space position and convert it using camera.WorldToViewPortPoint. Any NGUI widget's rect can be retrieved using worldCorners property.

I see. Seems like worldCorners property is based on the -1.0 to 1.0 coord system too. I can probably use those corners to figure out what percentage of the item is outside of the view, and then shift the item over by that percentage...right?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detecting if ui sprite is partially off screen
« Reply #3 on: November 06, 2014, 01:27:42 AM »
World corners are not based on -1 to 1. They are world coordinates, not view coordinates. They may match if your game window size happens to match the world coordinates, but don't rely on it. You can convert from world to view space using camera.WorldToViewportPoint, as I mentioned.