Browse Source

GuiListBoxCtrl and GuiDropDownCtrl Sorting

This code let's you sort by text or id for the list box or the drop down.
Peter Robinson 4 years ago
parent
commit
f0da3ae54d

+ 28 - 0
engine/source/gui/buttons/guiDropDownCtrl_ScriptBinding.h

@@ -433,4 +433,32 @@ ConsoleMethodWithDocs(GuiDropDownCtrl, setArrowProfile, ConsoleVoid, 3, 3, (GuiC
 		object->setControlArrowProfile(profile);
 }
 
+/*! Sorts the items in the list by their text values.
+	@param ascending An optional direction. If true, text will be sorted ascending (A-Z). If false, text will be sorted descending (Z-A). Defaults to Ascending.
+	@return No return value.
+*/
+ConsoleMethodWithDocs(GuiDropDownCtrl, sortByText, ConsoleVoid, 2, 3, "([bool ascending = true])")
+{
+	bool direction = true;
+	if (argc >= 3)
+	{
+		direction = dAtob(argv[3]);
+	}
+	object->getList()->sortByText(direction);
+}
+
+/*! Sorts the items in the list by their IDs.
+	@param ascending An optional direction. If true, IDs will be sorted ascending (1, 2, 3...). If false, IDs will be sorted descending (3, 2, 1...). Defaults to Ascending.
+	@return No return value.
+*/
+ConsoleMethodWithDocs(GuiDropDownCtrl, sortByID, ConsoleVoid, 2, 3, "([bool ascending = true])")
+{
+	bool direction = true;
+	if (argc >= 3)
+	{
+		direction = dAtob(argv[3]);
+	}
+	object->getList()->sortByID(direction);
+}
+
 ConsoleMethodGroupEndWithDocs(GuiDropDownCtrl)

+ 24 - 1
engine/source/gui/guiListBoxCtrl.cc

@@ -19,8 +19,10 @@
 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 // IN THE SOFTWARE.
 //-----------------------------------------------------------------------------
+#include "platform/platform.h"
 #include "gui/guiListBoxCtrl.h"
 #include "gui/guiCanvas.h"
+#include <algorithm>
 
 #include "guiListBoxCtrl_ScriptBinding.h"
 
@@ -134,7 +136,6 @@ void GuiListBoxCtrl::addSelection( S32 index )
    }
 
    addSelection( mItems[index], index );
-
 }
 void GuiListBoxCtrl::addSelection( LBItem *item, S32 index )
 {
@@ -799,5 +800,27 @@ void GuiListBoxCtrl::onTouchDown( const GuiEvent &event )
    mLastClickItem = hitItem;
 
 
+}
+#pragma endregion
+
+#pragma region sorting
+bool GuiListBoxCtrl::LBItem::sIncreasing = true;
+
+void GuiListBoxCtrl::sortByText(bool increasing)
+{
+	if (mItems.size() < 2)
+		return;
+
+	LBItem::sIncreasing = increasing;
+	std::sort(mItems.begin(), mItems.end(), LBItem::compByText);
+}
+
+void GuiListBoxCtrl::sortByID(bool increasing)
+{
+	if (mItems.size() < 2)
+		return;
+
+	LBItem::sIncreasing = increasing;
+	std::sort(mItems.begin(), mItems.end(), LBItem::compByID);
 }
 #pragma endregion

+ 30 - 1
engine/source/gui/guiListBoxCtrl.h

@@ -53,8 +53,9 @@ public:
    ~GuiListBoxCtrl();
    DECLARE_CONOBJECT(GuiListBoxCtrl);
 
-   struct LBItem
+   class LBItem
    {
+   public:
       StringTableEntry  itemText;
       bool              isSelected;
 	  bool				isActive;
@@ -62,6 +63,27 @@ public:
       void*             itemData;
       ColorF            color;
       bool              hasColor;
+
+	  static bool sIncreasing;
+
+	  // Compare Functions
+	  static bool compByID(const LBItem *a, const LBItem *b)
+	  {
+		  bool res = a->ID < b->ID;
+		  return (sIncreasing ? res : !res);
+	  }
+	  static bool compByText(const LBItem *a, const LBItem *b)
+	  {
+		  char buf[512];
+		  char bufB[512];
+
+		  dSprintf(buf, 512, "%s", a->itemText);
+		  dSprintf(bufB, 512, "%s", b->itemText);
+
+		  S32 res = dStricmp(buf, bufB);
+		  bool result = res <= 0;
+		  return (sIncreasing ? result : !result);
+	  }
    };
 
    VectorPtr<LBItem*>   mItems;
@@ -71,6 +93,8 @@ public:
    bool                 mFitParentWidth;
    LBItem*              mLastClickItem;
 
+   
+
    // Persistence
    static void       initPersistFields();   
 
@@ -130,6 +154,11 @@ public:
    // Mouse Events
    virtual void      onTouchDown( const GuiEvent &event );
    virtual void      onTouchDragged(const GuiEvent &event);
+
+   // Sorting
+   virtual void		 sortByText(bool increasing = true);
+   virtual void		 sortByID(bool increasing = true);
+
 protected:
 	GuiControl		*caller;
 };

+ 28 - 0
engine/source/gui/guiListBoxCtrl_ScriptBinding.h

@@ -400,4 +400,32 @@ ConsoleMethodWithDocs(GuiListBoxCtrl, setItemText, ConsoleVoid, 4, 4, "(S32 inde
 	object->setItemText(dAtoi(argv[2]), argv[3]);
 }
 
+/*! Sorts the items in the list by their text values.
+	@param ascending An optional direction. If true, text will be sorted ascending (A-Z). If false, text will be sorted descending (Z-A). Defaults to Ascending.
+	@return No return value.
+*/
+ConsoleMethodWithDocs(GuiListBoxCtrl, sortByText, ConsoleVoid, 2, 3, "([bool ascending = true])")
+{
+	bool direction = true;
+	if (argc >= 3)
+	{
+		direction = dAtob(argv[3]);
+	}
+	object->sortByText(direction);
+}
+
+/*! Sorts the items in the list by their IDs.
+	@param ascending An optional direction. If true, IDs will be sorted ascending (1, 2, 3...). If false, IDs will be sorted descending (3, 2, 1...). Defaults to Ascending.
+	@return No return value.
+*/
+ConsoleMethodWithDocs(GuiListBoxCtrl, sortByID, ConsoleVoid, 2, 3, "([bool ascending = true])")
+{
+	bool direction = true;
+	if (argc >= 3)
+	{
+		direction = dAtob(argv[3]);
+	}
+	object->sortByID(direction);
+}
+
 ConsoleMethodGroupEndWithDocs(GuiListBoxCtrl)

+ 2 - 0
toybox/Sandbox/1/scripts/toolbox.cs

@@ -121,11 +121,13 @@ function initializeToolbox()
         // Add to the list.
 		%colorName = getStockColorName(%i);
         BackgroundColorSelectList.addItem( %colorName, getStockColorI(%colorName) );
+		BackgroundColorSelectList.setItemID(%i, %i+1);
 
         // Select the color if it's the default one.
         if ( %colorName $= $pref::Sandbox::defaultBackgroundColor )
             BackgroundColorSelectList.setSelected( %i );
     }
+	BackgroundColorSelectList.sortByText();
 
     ToyCategorySelectList.initialize();