Author Topic: PixelPerfect causing 1-pixel resizes  (Read 2670 times)

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
PixelPerfect causing 1-pixel resizes
« on: September 16, 2012, 03:05:16 AM »
I'm fighting with sliced sprite and single sprites that NGUI keeps being a bitch and resizing widgets by one pixel.

For example, I've got a backdrop for text areas that is a 150x25 sliced sprite with correctly specified border.  I want these backdrop boxes to be exactly 25 pixels tall.  However, at runtime NGUI consistently bumps the height to 24 pixels.  If I bump the height to 26 pixels, NGUI doesn't touch it.

I also have a button that uses a sprite that is 15x12 pixels.  NGUI consistently bumps this to 16 pixels wide at runtime.  However, the underlying backdrop that is under the two halves of the button control (it's a plus/minus spinner) is a 15x25 pixel sprite which NGUI doesn't resize... although it does correctly give it a half-pixel offset, which it doesn't do for the overlying button sprites.

What the hell do I have to do here... only work with sprites that are evenly divisible by 2 to avoid flaky math errors in the PixelPerfect logic?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: PixelPerfect causing 1-pixel resizes
« Reply #1 on: September 16, 2012, 03:21:54 AM »
Sliced sprites will always be rounded to an even number when you have them set to be centered. Think about it: divide 25 by 2, what do you get? How can it be pixel-perfect?

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: PixelPerfect causing 1-pixel resizes
« Reply #2 on: September 16, 2012, 03:36:52 AM »
That assumes the centroid of the sprite falls on a vertex between four pixels and isn't centered on one specific pixel.  Which is to say that when the sprite size is even in a given direction you'd assume the logical center origin would be the gap/line/boundary between two pixels, but when the size is odd in a given direction you can instead use the center of a pixel (and not need the half-pixel offset on that axis.)

I'm going to have to dig into the PixelPerfect logic a bit, because what really strikes me as odd is that a 15x12 sprite is nudged to 16x12, while a 15x25 sprite is left at its native size.  I'm guessing that it's because you're switching over to multiple-of-2 resizing if both axes are not even.  Is there a non-obvious reason why the logical positioning can't be handled independently for the axes in a situation where one axis is an odd number of pixels and the other is even?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: PixelPerfect causing 1-pixel resizes
« Reply #3 on: September 16, 2012, 06:46:56 PM »
UISlicedSprite.MakePixelPerfect would be the function you want to look into. You can actually adjust it to what you prefer, or even derive from the class and implement your own handling. As I mentioned in order for the UI to look crisp I had to do it this way. You can play with it if you like, perhaps you can get it to work better. If you do, just let me know and I'll fix it up in the code repository.