Author Topic: UIProgressBar and check for value > 0.001f in ForceUpdate causing layout problem  (Read 2040 times)

DirtyHippy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
I have been tracking down something with UISliders.  I create almost all my UI dynamically, in inactive tables, that get activated and repositioned when needed.  When the starting value of my sliders are 0, what happens after the reposition is the tab is shown in the correct position, but the background is displayed way to the right of the tab.  If I manually reposition the table via the context menu after this, it fixes itself.  If I use set any value other than 0 as the starting value it works just fine - everything positions perfectly.  This implied to me that 0 was a special case.

The initial check in ForceUpdate does a check:

mFG.enabled = value > 0.001f;

which fails to active the FG in this case.  Commenting out this line and always forcing an enable has resolved this issue.  However, as i am sure there is a reason for this, I was wondering if there was a better way to resolve this would making ngui code changes.  I am using 3.7.7.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Interesting use case. Try changing UIProgressBar.ForceUpdate to this:
  1.         public virtual void ForceUpdate ()
  2.         {
  3.                 mIsDirty = false;
  4.                 bool turnOff = false;
  5.  
  6.                 if (mFG != null)
  7.                 {
  8.                         UIBasicSprite sprite = mFG as UIBasicSprite;
  9.  
  10.                         if (isHorizontal)
  11.                         {
  12.                                 if (sprite != null && sprite.type == UIBasicSprite.Type.Filled)
  13.                                 {
  14.                                         if (sprite.fillDirection == UIBasicSprite.FillDirection.Horizontal ||
  15.                                                 sprite.fillDirection == UIBasicSprite.FillDirection.Vertical)
  16.                                         {
  17.                                                 sprite.fillDirection = UIBasicSprite.FillDirection.Horizontal;
  18.                                                 sprite.invert = isInverted;
  19.                                         }
  20.                                         sprite.fillAmount = value;
  21.                                 }
  22.                                 else
  23.                                 {
  24.                                         mFG.drawRegion = isInverted ?
  25.                                                 new Vector4(1f - value, 0f, 1f, 1f) :
  26.                                                 new Vector4(0f, 0f, value, 1f);
  27.                                         mFG.enabled = true;
  28.                                         turnOff = value < 0.001f;
  29.                                 }
  30.                         }
  31.                         else if (sprite != null && sprite.type == UIBasicSprite.Type.Filled)
  32.                         {
  33.                                 if (sprite.fillDirection == UIBasicSprite.FillDirection.Horizontal ||
  34.                                         sprite.fillDirection == UIBasicSprite.FillDirection.Vertical)
  35.                                 {
  36.                                         sprite.fillDirection = UIBasicSprite.FillDirection.Vertical;
  37.                                         sprite.invert = isInverted;
  38.                                 }
  39.                                 sprite.fillAmount = value;
  40.                         }
  41.                         else
  42.                         {
  43.                                 mFG.drawRegion = isInverted ?
  44.                                         new Vector4(0f, 1f - value, 1f, 1f) :
  45.                                         new Vector4(0f, 0f, 1f, value);
  46.                                 mFG.enabled = true;
  47.                                 turnOff = value < 0.001f;
  48.                         }
  49.                 }
  50.  
  51.                 if (thumb != null && (mFG != null || mBG != null))
  52.                 {
  53.                         Vector3[] corners = (mFG != null) ? mFG.localCorners : mBG.localCorners;
  54.  
  55.                         Vector4 br = (mFG != null) ? mFG.border : mBG.border;
  56.                         corners[0].x += br.x;
  57.                         corners[1].x += br.x;
  58.                         corners[2].x -= br.z;
  59.                         corners[3].x -= br.z;
  60.  
  61.                         corners[0].y += br.y;
  62.                         corners[1].y -= br.w;
  63.                         corners[2].y -= br.w;
  64.                         corners[3].y += br.y;
  65.  
  66.                         Transform t = (mFG != null) ? mFG.cachedTransform : mBG.cachedTransform;
  67.                         for (int i = 0; i < 4; ++i) corners[i] = t.TransformPoint(corners[i]);
  68.  
  69.                         if (isHorizontal)
  70.                         {
  71.                                 Vector3 v0 = Vector3.Lerp(corners[0], corners[1], 0.5f);
  72.                                 Vector3 v1 = Vector3.Lerp(corners[2], corners[3], 0.5f);
  73.                                 SetThumbPosition(Vector3.Lerp(v0, v1, isInverted ? 1f - value : value));
  74.                         }
  75.                         else
  76.                         {
  77.                                 Vector3 v0 = Vector3.Lerp(corners[0], corners[3], 0.5f);
  78.                                 Vector3 v1 = Vector3.Lerp(corners[1], corners[2], 0.5f);
  79.                                 SetThumbPosition(Vector3.Lerp(v0, v1, isInverted ? 1f - value : value));
  80.                         }
  81.                 }
  82.  
  83.                 if (turnOff) mFG.enabled = false;
  84.         }

DirtyHippy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
This didn't fix it unfortunately.

Not really a big deal, as I haven't seen any side effects with my change, and I put it in my small list of things I modify manually in NGui each time I update.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Well the reason for it is simple: hide the foreground image when the slider's value is zero. Otherwise you'd still see the border of a sliced sprite when it's used as the foreground.