Author Topic: [SOLVED] UIDraggablePanel.RestrictWithinBounds(...) barely working once  (Read 3272 times)

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Hi!

Been struggling with this annoying issue for a long time, basically settling for UIDraggablePanel.ResetPosition() up until now. Problem is, we really need this to work and the deadline is... now.

Problem really just seems to be down to the ClippingArea not recalculating or something when calling RestrictWithinBounds(false), because as soon as a drag I little and release, it springs back as expected. Even added a simple "RestrictWithinBoundsNow"-bool which just calls the method at any time when clicked in editor, and that didn't do shit either so it's not a delay-issue in any calculations etc.

As title says, this works sometimes, and sometimes not. In the exact same place, with the exact same content swopped within the panel...

Pls halp, ripping my hair off over here!

EDIT:
Okey, if I set my tweens to debugmode (superslow) and call RestrictWithinBounds(false) when done, it works everytime (it seems). Problem is, when tweening "fast", it's still about 0.2s so there is PLENTY of time for any bounds to be recalculated etc... The content is swopped in just a few ms, so it seems unlikely for that to be the issue (since as described above, RestrictWithinBounds does nothing even if called manually a long time after the "fast" tween finishes).

EDIT2:
As I thought it is the clippingBounds that is not recalculated properly after all content has been swopped (which ofcourse is not instantaneous, might take a few ms) so when all is setup somehow the clippingBounds is "out-of-date". As soon as I move something in the scene (from editor), it recalculates to correct bounds. What to do?
« Last Edit: July 10, 2013, 03:36:11 AM by helmesjo »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIDraggablePanel.RestrictWithinBounds(...) barely working once
« Reply #1 on: June 28, 2013, 03:57:21 PM »
First of all... what tweens are you using? If you are tweening contents within a draggable panel, its bounds are likely changing, and unless you tell the draggable panel that the bounds have changed, they will indeed be out of date. Also, did you note the comment for the ResetPosition function?
  1.         /// <summary>
  2.         /// Reset the panel's position to the top-left corner.
  3.         /// It's recommended to call this function before AND after you re-populate the panel's contents (ex: switching window tabs).
  4.         /// Another option is to populate the panel's contents, reset its position, then call this function to reposition the clipping.
  5.         /// </summary>
Note the "before AND after" part.

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Re: UIDraggablePanel.RestrictWithinBounds(...) barely working once
« Reply #2 on: July 01, 2013, 04:49:18 AM »
I'm using tween position, and I'm only tweening the "Screen"-object that has a panel as child so only the "root" for the screen is moved. The content inside the panel is swapped however, basically it's tables being repopulated with new content passed from the previous screen depending on user-choices.

Yeah, I did notice that! However ResetPosition is only temporary, and not the behaviour I actually want (and calling it before is not always obvious since stuff can change from places where the panel is not available, without dirty special-code atleast). Basically, I just want the EXACT same thing that happens when you drag a few pixels and it springs back into position (which I thought RestrictWithinBounds was supposed to do). Somehow there just seems to be some "RecalculateBounds"-function missing, that just happens when actually dragging manually... Or am I out on a wierd bicycleride on my own here?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIDraggablePanel.RestrictWithinBounds(...) barely working once
« Reply #3 on: July 01, 2013, 11:34:12 PM »
Have a look inside TweenScale. It has an option to update the table, which in turn updates the panel. Sounds like you need something similar in your case.

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Re: UIDraggablePanel.RestrictWithinBounds(...) barely working once
« Reply #4 on: July 02, 2013, 02:43:32 AM »
Looked in there but I should've mentioned that I'm not using the UITable, but my own table-class which reuses cells. In short, it will reload the table, adjusting the tables collider to the size, and a button hooked under the table (to the bottom of the collider) will move along to the new position. After this, the bounds for the panel is out-of-date and calling "RestrictWithinBounds" won't do anything. However, dragging and releasing the panel manually will recalculate the bounds and spring it back into position (and this is what I want).
Also, as mentioned, dragging any widget in the scene from editor (button etc.) will cause the panel-bounds to be recalculated (and calling "RestrictWithinBounds" after this will work).

Ty!

EDIT
Inside UIDraggablePanel I found pretty much exactly what I was looking for: mCalculatedBounds = false;
Setting mCalculatedBounds to false first of all in "RestrictWithinBounds" made it work flawlessly so far. Is this intentionally left out, or just a missed case? Maybe should be a second paramater with default-value (bool recalculateBounds = true/false)...

This is what it looks like now:
  1. public bool RestrictWithinBounds (bool instant)
  2.         {
  3.                 mCalculatedBounds = false;
  4.                 Vector3 constraint = mPanel.CalculateConstrainOffset(bounds.min, bounds.max);
  5.  
  6.                 if (constraint.magnitude > 0.001f)
  7.                 {
  8.                         if (!instant && dragEffect == DragEffect.MomentumAndSpring)
  9.                         {
  10.                                 // Spring back into place
  11.                                 SpringPanel.Begin(mPanel.gameObject, mTrans.localPosition + constraint, 13f);
  12.                         }
  13.                         else
  14.                         {
  15.                                 // Jump back into place
  16.                                 MoveRelative(constraint);
  17.                                 mMomentum = Vector3.zero;
  18.                                 mScroll = 0f;
  19.                         }
  20.                         return true;
  21.                 }
  22.                 return false;
  23.         }
  24.  

EDIT2
Okey, I see what you mean now with the UITable updating the panel, actually saw "UpdateScrollbars" while checking out mCalculatedBounds but just felt that it should be included inside "RestrictWithinBounds". It's not very obvious to call "UpdateScrollbars" first without checking what it actually does. "RecalculateBounds" is the first thing one looks for imo.
« Last Edit: July 02, 2013, 08:07:09 AM by helmesjo »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIDraggablePanel.RestrictWithinBounds(...) barely working once
« Reply #5 on: July 02, 2013, 11:21:46 AM »
Agreed. I need to rethink that whole approach to be honest.