Author Topic: Panel tool isn't sorted alphabetically  (Read 4443 times)

Arcanor

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 46
    • View Profile
Panel tool isn't sorted alphabetically
« on: March 14, 2014, 10:05:35 AM »
Using NGUI 3.5.3, Unity Pro, Win 7.

In looking at UIPanelTool.cs, line 124, the comment says the panel tool's entries should be being sorted alphabetically.  However, the sort function is comparing using UIPanel.CompareFunc(), which apparently defaults to sorting based on panel depth and then GetInstanceID(), and does no alphabetical comparison.  It would be far more useful (to me anyway) if the panels of the same depth were actually sorted alphabetically before resorting to using the instance IDs.

I propose changing UIPanel.cs to use this updated CompareFunc():

  1.         /// <summary>
  2.         /// Function that can be used to depth-sort panels.
  3.         /// </summary>
  4.  
  5.         static public int CompareFunc (UIPanel a, UIPanel b)
  6.         {
  7.                 if (a != b && a != null && b != null)
  8.                 {
  9.                         if (a.mDepth < b.mDepth) return -1;
  10.                         if (a.mDepth > b.mDepth) return 1;
  11.                         int sortRet = string.Compare(a.name, b.name);
  12.                         if (sortRet != 0) return sortRet;
  13.                         return (a.GetInstanceID() < b.GetInstanceID()) ? -1 : 1;
  14.                 }
  15.                 return 0;
  16.         }
  17.  

I have tested this and it seems to work fine here.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Panel tool isn't sorted alphabetically
« Reply #1 on: March 14, 2014, 10:41:23 AM »
It's never a good idea to leave panels using the same depth. I'll fix the comment, but adding a change that makes them sort alphabetically is a mistake as it may change the order of existing UIs where people also didn't specify unique depth values. Furthermore, instance IDs can change and are unreliable -- another reason why it's important to give your panels unique depth values.

Arcanor

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: Panel tool isn't sorted alphabetically
« Reply #2 on: March 14, 2014, 10:46:58 AM »
Fair enough, but the next question is why should the Panel Tool use the same sort algorithm as panels sorted in a scene at runtime?  Maybe there needs to be a simple alphabetical sort for the Panel Tool, while keeping the non-alphabetized version for other uses.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Panel tool isn't sorted alphabetically
« Reply #3 on: March 14, 2014, 10:50:45 AM »
Yes it should. It should give you the exact overview of the order of your panels.

Arcanor

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: Panel tool isn't sorted alphabetically
« Reply #4 on: March 14, 2014, 10:54:47 AM »
It's never a good idea to leave panels using the same depth. I'll fix the comment, but adding a change that makes them sort alphabetically is a mistake as it may change the order of existing UIs where people also didn't specify unique depth values. Furthermore, instance IDs can change and are unreliable -- another reason why it's important to give your panels unique depth values.

Actually, I'm not understanding this.  Why is it never a good idea to leave panels using the same depth?  I'm using UIWindow's Show(), Hide(), and GoBack() functions to turn panels on and off.  I've got all my panels set to depth 1 (with contained subpanels at higher depths), and it's working great.  Am I asking for trouble somehow?  I don't see what the problem is.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Panel tool isn't sorted alphabetically
« Reply #5 on: March 14, 2014, 11:16:47 AM »
If you have two panels on the screen both using the same depth value, the order of which one is on top may differ from one platform to the next.

Arcanor

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: Panel tool isn't sorted alphabetically
« Reply #6 on: March 14, 2014, 11:27:54 AM »
Of course.  I manage my "top level" panels (all depth=1) using show/hide so that never happens.  If there's a sub-panel or overlay it'll always have a higher depth to ensure it appears in front of the panel that is its parent.  I don't depend on panel depth to determine which panel is showing.

So I guess this comes down to how one manages their panels.  Given my setup, I'm going to put the alphabetical compare back in, since it's getting hard for me to find panels in the list.

I think that others who use a system like UIWindow to manage their top level panels will have a similar situation.  Perhaps alphabetical sorting could be made an optional addition, i.e. clicking on the Panel Tool's "Panel Name" header sorts on that, or clicking on the "DP" header sorts on depth?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Panel tool isn't sorted alphabetically
« Reply #7 on: March 14, 2014, 11:30:52 AM »
Just change the UIPanelTool.Compare function instead. It's safer.
  1.         /// <summary>
  2.         /// First sort by depth, then alphabetically, then by instance ID.
  3.         /// </summary>
  4.  
  5.         static int Compare (Entry a, Entry b)
  6.         {
  7.         if (a != b && a != null && b != null)
  8.         {
  9.                         if (a.panel.depth < b.panel.depth) return -1;
  10.                         if (a.panel.depth > b.panel.depth) return 1;
  11.                         int sortRet = string.Compare(a.panel.name, b.panel.name);
  12.             if (sortRet != 0) return sortRet;
  13.                         return (a.panel.GetInstanceID() < b.panel.GetInstanceID()) ? -1 : 1;
  14.         }
  15.         return 0;
  16.         }

Arcanor

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: Panel tool isn't sorted alphabetically
« Reply #8 on: March 14, 2014, 01:40:48 PM »
Thanks, will do!