Author Topic: Resizing a sprite depending on draggable children size and position  (Read 1705 times)

bac9

  • Full Member
  • ***
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 113
    • View Profile
Hi! I've encountered a bit complicated task and would like your opinion on how should I best approach implementing it.



I'm making an measurement screen where you can drag and drop several elements around while a sprite under them gets resized as sort of a bounding box, with two side clones following it's width and height and displaying the size of said sprite. Think of it as a room. You drag the furniture around, trying to arrange it into the most optimized square possible.

I'm a bit stalled on figuring out how to handle the resizing of that sprite best. What I'll attempt to do right now is:

1. Set all objects and a room to the bottom left origin mode to simplify the work (so that stuff like pos.x - width/2 won't be necessary);
2. To determine the X position of a room, loop through all objects and find one with the smallest X position, use it.
3. To determine the Y position of a room, loop through all objects and find one with the smallest Y position, use it.
4. To determine the X size of a room, loop through all objects and find one with the biggest sum of the X position and width, use it.
4. To determine the Y size of a room, loop through all object and find one with the biggest sum of the Y position and height, use it.
5. Force those new values into the NGUI sprite of a room.
6. Resize the X/Y "shadows" of a room accordingly.
7. Update the labels from the room sprite size values.

Is that a good way of achieving the functionality, or there is a faster or easier one? For instance, I'm not very familiar with NGUI table functionality, only ever using them for simple horizonal and vertical scrolling lists. Is it possible to repurpose the table for something like that, seeing that it already has some of the similar functionality (checking children positions/size, resizing itself, etc)?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Resizing a sprite depending on draggable children size and position
« Reply #1 on: February 24, 2014, 04:38:45 PM »
I wouldn't use anchors in this case. I would write custom code to position things based on the room's dimensions. You can do it using anchors, but it's going to be quite complicated. Writing a script will be easier (and quite a bit more performant)