]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/menu/xonotic/hudskinlist.qc
Merge branch 'master' into Mario/waterjump_fix
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / hudskinlist.qc
index 65bdba128c37bfa31b96a06a94310f88f14f91a3..92dee8d85ef3cea77a92dd4e30596852d925b3a9 100644 (file)
@@ -1,49 +1,6 @@
-#ifndef HUDSKINLIST_H
-#define HUDSKINLIST_H
-#include "listbox.qc"
-CLASS(XonoticHUDSkinList, XonoticListBox)
-       METHOD(XonoticHUDSkinList, configureXonoticHUDSkinList, void(entity));
-       ATTRIB(XonoticHUDSkinList, rowsPerItem, float, 1)
-       METHOD(XonoticHUDSkinList, resizeNotify, void(entity, vector, vector, vector, vector));
-       METHOD(XonoticHUDSkinList, draw, void(entity));
-       METHOD(XonoticHUDSkinList, drawListBoxItem, void(entity, int, vector, bool, bool));
-       METHOD(XonoticHUDSkinList, getHUDSkins, void(entity));
-       METHOD(XonoticHUDSkinList, setHUDSkin, void(entity));
-       METHOD(XonoticHUDSkinList, hudskinName, string(entity, float));
-       METHOD(XonoticHUDSkinList, hudskinPath, string(entity, float));
-       METHOD(XonoticHUDSkinList, hudskinTitle, string(entity, float));
-       METHOD(XonoticHUDSkinList, hudskinAuthor, string(entity, float));
-       METHOD(XonoticHUDSkinList, doubleClickListBoxItem, void(entity, float, vector));
-       METHOD(XonoticHUDSkinList, keyDown, float(entity, float, float, float));
-       METHOD(XonoticHUDSkinList, destroy, void(entity));
-       METHOD(XonoticHUDSkinList, showNotify, void(entity));
+#include "hudskinlist.qh"
 
-       ATTRIB(XonoticHUDSkinList, listHUDSkin, float, -1)
-       ATTRIB(XonoticHUDSkinList, realFontSize, vector, '0 0 0')
-       ATTRIB(XonoticHUDSkinList, columnNameOrigin, float, 0)
-       ATTRIB(XonoticHUDSkinList, columnNameSize, float, 0)
-       ATTRIB(XonoticHUDSkinList, realUpperMargin, float, 0)
-       ATTRIB(XonoticHUDSkinList, origin, vector, '0 0 0')
-       ATTRIB(XonoticHUDSkinList, itemAbsSize, vector, '0 0 0')
-
-       ATTRIB(XonoticHUDSkinList, filterString, string, string_null)
-       ATTRIB(XonoticHUDSkinList, delayedRefreshTime, float, 0)
-       ATTRIB(XonoticHUDSkinList, savedName, string, string_null)
-ENDCLASS(XonoticHUDSkinList)
-
-#ifndef IMPLEMENTATION
-// public:
-entity hudskinlist;
-entity makeXonoticHUDSkinList();
-void SaveHUDSkin_Click(entity btn, entity me);
-void SetHUDSkin_Click(entity btn, entity me);
-#endif
-void HUDSkinList_Refresh_Click(entity btn, entity me);
-void HUDSkinList_Filter_Change(entity box, entity me);
-void HUDSkinList_SavedName_Change(entity box, entity me);
-#endif
-
-#ifdef IMPLEMENTATION
+#include "inputbox.qh"
 
 entity makeXonoticHUDSkinList()
 {
@@ -56,7 +13,7 @@ entity makeXonoticHUDSkinList()
 void XonoticHUDSkinList_configureXonoticHUDSkinList(entity me)
 {
        me.configureXonoticListBox(me);
-       me.getHUDSkins(me);
+       me.nItems = 0;
 }
 
 const float HUDSKINPARM_NAME = 0;
@@ -81,7 +38,8 @@ string XonoticHUDSkinList_hudskinAuthor(entity me, float i)
        return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_AUTHOR);
 }
 
-void getAllHUDSkins(entity me, string subdir)
+// subdir can be a regular expression
+void getHUDSkinFiles(entity me, int sortbuf, string subdir)
 {
        string s;
        if(me.filterString)
@@ -89,40 +47,73 @@ void getAllHUDSkins(entity me, string subdir)
        else
                s = "*";
        s = strcat(subdir, "hud_", s, ".cfg");
-       float strlen_subdir = strlen(subdir);
-       subdir = strzone(subdir);
 
-       float list, i, n;
-       list = search_begin(s, false, true);
+       int list = search_begin(s, false, true);
        if(list >= 0)
        {
-               string filename;
-               n = search_getsize(list);
-               int bufsize = buf_getsize(me.listHUDSkin);
-               for(i = 0; i < n; ++i)
+               int n = search_getsize(list);
+               for(int i = 0; i < n; ++i)
                {
-                       bufstr_set(me.listHUDSkin, bufsize + i * HUDSKINPARM_COUNT + HUDSKINPARM_PATH, subdir);
-                       filename = search_getfilename(list, i);
-                       s = substring(filename, strlen_subdir + 4, (strlen(filename) - strlen_subdir - 4 - 4)); // remove "hud_" prefix and ".cfg" suffix
-                       bufstr_set(me.listHUDSkin, bufsize + i * HUDSKINPARM_COUNT + HUDSKINPARM_NAME, s);
+                       string s = search_getfilename(list, i);
+                       int subdir_ofs = strstrofs(s, "/", 0);
+                       if(subdir_ofs >= 0)
+                       {
+                               int ofs = subdir_ofs;
+                               while(ofs != -1)
+                               {
+                                       subdir_ofs = ofs;
+                                       ofs = strstrofs(s, "/", subdir_ofs + 1);
+                               }
+                       }
 
-                       int fh = fopen(filename, FILE_READ);
-                       if(fh < 0)
-                               continue;
-                       while((s = fgets(fh)) && substring(s, 0, 2) == "//")
+                       if(subdir_ofs == -1)
+                               bufstr_add(sortbuf, s, true);
+                       else
                        {
-                               tokenize_console(substring(s, 2, -1));
-                               if(argv(0) == "title")
-                                       bufstr_set(me.listHUDSkin, bufsize + i * HUDSKINPARM_COUNT + HUDSKINPARM_TITLE, argv(1));
-                               else if(argv(0) == "author")
-                                       bufstr_set(me.listHUDSkin, bufsize + i * HUDSKINPARM_COUNT + HUDSKINPARM_AUTHOR, argv(1));
+                               subdir = substring(s, 0, subdir_ofs);
+                               string filename = substring(s, subdir_ofs + 1, -1);
+                               // invert path and filename position so we can sort sortbuf by filename
+                               bufstr_add(sortbuf, strcat(filename, "/", subdir), true);
                        }
-                       fclose(fh);
                }
                search_end(list);
        }
-       if(subdir)
-               strunzone(subdir);
+}
+
+void getAllHUDSkins(entity me, int sortbuf)
+{
+       int n = buf_getsize(sortbuf);
+       for(int i = 0; i < n; ++i)
+       {
+               string entry = bufstr_get(sortbuf, i);
+               int ofs = strstrofs(entry, "/", 0);
+               string s = "";
+               string filename = entry;
+               if(ofs >= 0)
+               {
+                       s = substring(entry, ofs + 1, -1); // skip initial "/"
+                       s = strcat(s, "/");
+                       bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_PATH, s);
+                       filename = strcat(s, substring(entry, 0, ofs));
+               }
+               else
+                       ofs = strlen(entry);
+               s = substring(entry, 4, ofs - 4 - 4); // remove "hud_" prefix and ".cfg" suffix
+               bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_NAME, s);
+
+               int fh = fopen(filename, FILE_READ);
+               if(fh < 0)
+                       continue;
+               while((s = fgets(fh)) && substring(s, 0, 2) == "//")
+               {
+                       tokenize_console(substring(s, 2, -1));
+                       if(argv(0) == "title")
+                               bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_TITLE, argv(1));
+                       else if(argv(0) == "author")
+                               bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_AUTHOR, argv(1));
+               }
+               fclose(fh);
+       }
 }
 
 void XonoticHUDSkinList_getHUDSkins(entity me)
@@ -135,8 +126,12 @@ void XonoticHUDSkinList_getHUDSkins(entity me)
                me.nItems = 0;
                return;
        }
-       getAllHUDSkins(me, "");
-       getAllHUDSkins(me, "data/");
+       int sortbuf = buf_create();
+       getHUDSkinFiles(me, sortbuf, "");
+       getHUDSkinFiles(me, sortbuf, "data/");
+       buf_sort(sortbuf, 128, 0);
+       getAllHUDSkins(me, sortbuf);
+       buf_del(sortbuf);
        me.nItems = buf_getsize(me.listHUDSkin) / HUDSKINPARM_COUNT;
 }
 
@@ -151,8 +146,10 @@ void XonoticHUDSkinList_resizeNotify(entity me, vector relOrigin, vector relSize
        me.itemAbsSize = '0 0 0';
        SUPER(XonoticHUDSkinList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
 
-       me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
-       me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
+       me.itemAbsSize.y = absSize.y * me.itemHeight;
+       me.itemAbsSize.x = absSize.x * (1 - me.controlWidth);
+       me.realFontSize.y = me.fontSize / me.itemAbsSize.y;
+       me.realFontSize.x = me.fontSize / me.itemAbsSize.x;
        me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
 
        me.columnNameOrigin = me.realFontSize.x;
@@ -193,19 +190,15 @@ void HUDSkinList_Refresh_Click(entity btn, entity me)
 
 void HUDSkinList_SavedName_Change(entity box, entity me)
 {
-       if(me.savedName)
-               strunzone(me.savedName);
+       strfree(me.savedName);
 
        if(box.text != "")
                me.savedName = strzone(box.text);
-       else
-               me.savedName = string_null;
 }
 
 void HUDSkinList_Filter_Change(entity box, entity me)
 {
-       if(me.filterString)
-               strunzone(me.filterString);
+       strfree(me.filterString);
 
        if(box.text != "")
        {
@@ -214,8 +207,6 @@ void HUDSkinList_Filter_Change(entity box, entity me)
                else
                        me.filterString = strzone(strcat("*", box.text, "*"));
        }
-       else
-               me.filterString = string_null;
 
        me.getHUDSkins(me);
 }
@@ -268,5 +259,3 @@ float XonoticHUDSkinList_keyDown(entity me, float scan, float ascii, float shift
                return SUPER(XonoticHUDSkinList).keyDown(me, scan, ascii, shift);
        }
 }
-#endif
-