Author Topic: Find Bug in UI Widget code  (Read 4712 times)

Ivan

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 3
    • View Profile
Find Bug in UI Widget code
« on: February 06, 2017, 10:30:00 AM »
Find little but maybe imortant bug.

public Color color
   {
      get
      {
         return mColor;
      }
      set
      {
         if (mColor.a != value)
                        {
            bool alphaChange = Mathf.Abs(mColor.a - value.a)> Mathf.Epsilon;
            mColor = value;
            Invalidate(alphaChange);
         }
      }
   }

if (mColor.a != value) is always return true if using tween even if it change color from white to white. U cant use "==" to colors which are taken from different behaviours becuse its different structures.

thats my fix

public Color color
   {
      get
      {
         return mColor;
      }
      set
      {
         if (Mathf.Abs(mColor.a - value.a) > 0.005f || Mathf.Abs(mColor.r - value.r) > 0.005f || Mathf.Abs(mColor.b - value.b) > 0.005f || Mathf.Abs(mColor.g - value.g) > 0.005f)
            {
            bool alphaChange = Mathf.Abs(mColor.a - value.a)> Mathf.Epsilon;
            mColor = value;
            Invalidate(alphaChange);
         }
      }
   }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Find Bug in UI Widget code
« Reply #1 on: February 08, 2017, 08:36:15 AM »
Why would it be mColor.a != value? That's not even compile-able. I assume you mean mColor != value, which is what it is in NGUI.

  1.         public Color color
  2.         {
  3.                 get
  4.                 {
  5.                         return mColor;
  6.                 }
  7.                 set
  8.                 {
  9.                         if (mColor != value) // <-----
  10.                         {
  11.                                 bool alphaChange = (mColor.a != value.a);
  12.                                 mColor = value;
  13.                                 Invalidate(alphaChange);
  14.                         }
  15.                 }
  16.         }

The inequality is completely intentional the way it is (mColor != value). Even slightest inequality is still an inequality. Colors are floating point based in the shader. They are not byte-based.

Ivan

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Find Bug in UI Widget code
« Reply #2 on: February 08, 2017, 11:03:37 AM »
Sorry, I certainly meant
  1. (mColor != value)
I have buttons without hover state (I just made it's normal and hover colors white), but when I move mouse over button its make button mark as changed. I think that's wrong.
So it seems to me wrong to compare floats that are in different structures.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Find Bug in UI Widget code
« Reply #3 on: February 13, 2017, 11:14:36 AM »
No, it's not wrong. When you compare a color, it has to compare each float individually, and if floats differ even slightly then they are different values. Identical colors will always match.

For example:
  1. var c0 = new Color(0.5f, 0.25f, 0.35f);
  2. var c1 = new Color(0.5f, 0.25f, 0.35f);
  3. Debug.Log(c0 == c1);
You will see 'true'.

Ivan

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Find Bug in UI Widget code
« Reply #4 on: February 14, 2017, 08:42:08 AM »
My Visual Studio apparently decided to make a joke on me. This is what I see in debug mode , and i decided that this is floating point inaccuracy.  But when i deside to look at additional info i found this. . This is not floating point inaccuracy, this is 1 / 255. Sorry I go to kill my UI designer. He made all buttons hover state color (255,254,254,255).