[teeworlds] Ticket #734 - Sort by alphabetical order

  • From: xalduin <xalduin@xxxxxxxxx>
  • To: teeworlds@xxxxxxxxxxxxx
  • Date: Sun, 30 May 2010 21:27:29 -0400

This is what I've got to sort the map list in the "Open" dialog in the editor. If there are any issues, I'd be glad to address them.


In addition, I'm having trouble sorting the images in alphabetical order because the 'swap' function in base seems to conflict with std::swap when I use std::sort on an array of CEditorImages. No problems encountered when using std::sort on a list of char* for sorting map names, however. The only way I've managed to get around this is by either sorting manually or by replacing the definition of swap in base.h with 'using std::swap;' but I'm hoping that there's a better way.

Anyways, here's the patch:

Index: trunk/src/game/editor/ed_editor.cpp
===================================================================
--- trunk/src/game/editor/ed_editor.cpp    (revision 2359)
+++ trunk/src/game/editor/ed_editor.cpp    (working copy)
@@ -1,4 +1,5 @@
 // copyright (c) 2007 magnus auvinen, see licence.txt for more info
+#include <algorithm> //sort

 #include <base/system.h>

@@ -1885,6 +1886,7 @@
 static char gs_aFileDialogPath[512] = {0};
 static char gs_aFileDialogCompleteFilename[512] = {0};
 static int gs_FilesNum = 0;
+static array<const char*> gs_FileList;
 int g_FilesStartAt = 0;
 int g_FilesCur = 0;
 int g_FilesStopAt = 999;
@@ -1895,11 +1897,21 @@
     CEditor *m_pEditor;
 };

+static const bool CompareName(const char *pName1, const char *pName2)
+{
+    return str_comp(pName1, pName2) < 0;
+}
+
static void EditorListdirCallback(const char *pName, int IsDir, void *pUser)
 {
     if(pName[0] == '.' || IsDir) // skip this shit!
         return;

+    gs_FileList.add(pName);
+}
+
+static void AddFileDialogEntry(const char *pName, void *pUser)
+{
     if(g_FilesCur > gs_FilesNum)
         gs_FilesNum = g_FilesCur;

@@ -2005,6 +2017,11 @@
     // TODO: lazy ass coding, should store the interface pointer somewere
Kernel()->RequestInterface<IStorage>()->ListDirectory(gs_FileDialogDirTypes, gs_aFileDialogPath, EditorListdirCallback, &Info);

+ std::sort(gs_FileList.base_ptr(), gs_FileList.base_ptr()+gs_FileList.size(), CompareName);
+    for(int i = 0; i < gs_FileList.size(); i++)
+        AddFileDialogEntry(gs_FileList[i], &Info);
+    gs_FileList.clear();
+
     // disable clipping again
     UI()->ClipDisable();

Other related posts: