From dc6a6c7408a4de48421b2828d034156fb82297a4 Mon Sep 17 00:00:00 2001 From: div0 Date: Fri, 18 Jan 2008 13:44:30 +0000 Subject: [PATCH] map list can now search a map name while you type it git-svn-id: svn://svn.icculus.org/nexuiz/trunk@3171 f962a42d-fe04-0410-a3ab-8c8b0445ebaa --- data/qcsrc/common/mapinfo.qc | 24 ++++++++------- data/qcsrc/common/mapinfo.qh | 2 ++ data/qcsrc/common/util.qc | 11 +++++++ data/qcsrc/common/util.qh | 3 ++ data/qcsrc/menu-div0test/nexuiz/maplist.c | 36 +++++++++++++++++++++++ 5 files changed, 65 insertions(+), 11 deletions(-) diff --git a/data/qcsrc/common/mapinfo.qc b/data/qcsrc/common/mapinfo.qc index 9eea2dc19..00fc9c5bd 100644 --- a/data/qcsrc/common/mapinfo.qc +++ b/data/qcsrc/common/mapinfo.qc @@ -500,7 +500,6 @@ float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametype return 0; } -string _MapInfo_FindName_match; float MapInfo_FindName(string s) { // if there is exactly one map of prefix s, return it @@ -513,8 +512,8 @@ float MapInfo_FindName(string s) while(l != r) { m = floor((l + r) / 2); - _MapInfo_FindName_match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, m)); - cmp = strcasecmp(_MapInfo_FindName_match, s); + MapInfo_FindName_match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, m)); + cmp = strcasecmp(MapInfo_FindName_match, s); if(cmp == 0) return m; // found and good if(cmp < 0) @@ -522,26 +521,29 @@ float MapInfo_FindName(string s) else r = m; // behind s } - _MapInfo_FindName_match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l)); + MapInfo_FindName_match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l)); + MapInfo_FindName_firstResult = l; // r == l, so: l is behind s, l-1 is before // SO: if there is any, l is the one with the right prefix // and l+1 may be one too if(l == MapInfo_count) { - _MapInfo_FindName_match = string_null; - return -1; // no _MapInfo_FindName_match, behind last item + MapInfo_FindName_match = string_null; + MapInfo_FindName_firstResult = -1; + return -1; // no MapInfo_FindName_match, behind last item } - if(!startsWith(_MapInfo_FindName_match, s)) + if(!startsWith(MapInfo_FindName_match, s)) { - _MapInfo_FindName_match = string_null; + MapInfo_FindName_match = string_null; + MapInfo_FindName_firstResult = -1; return -1; // wrong prefix } if(l == MapInfo_count - 1) return l; // last one, nothing can follow => unique if(startsWith(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l + 1)), s)) { - _MapInfo_FindName_match = string_null; - return -1; // ambigous _MapInfo_FindName_match + MapInfo_FindName_match = string_null; + return -1; // ambigous MapInfo_FindName_match } return l; } @@ -549,7 +551,7 @@ float MapInfo_FindName(string s) string MapInfo_FixName(string s) { MapInfo_FindName(s); - return _MapInfo_FindName_match; + return MapInfo_FindName_match; } float MapInfo_CurrentFeatures() diff --git a/data/qcsrc/common/mapinfo.qh b/data/qcsrc/common/mapinfo.qh index fc9cc8038..a9442ec8d 100644 --- a/data/qcsrc/common/mapinfo.qh +++ b/data/qcsrc/common/mapinfo.qh @@ -41,6 +41,8 @@ string MapInfo_BSPName_ByID(float i); float MapInfo_Get_ByName(string s, float allowGenerate, float gametypeToSet); // 1 on success, 0 on failure, 2 if it autogenerated a mapinfo file // look for a map by a prefix, returns the actual map name on success, string_null on failure or ambigous match +string MapInfo_FindName_match; // the name of the map that was found +float MapInfo_FindName_firstResult; // -1 if none were found, index of first one if not unique but found (FindName then returns -1) float MapInfo_FindName(string s); string MapInfo_FixName(string s); diff --git a/data/qcsrc/common/util.qc b/data/qcsrc/common/util.qc index 88c2f9a49..f42ff1ec2 100644 --- a/data/qcsrc/common/util.qc +++ b/data/qcsrc/common/util.qc @@ -222,3 +222,14 @@ vector colormapPaletteColor(float c, float isPants) default: return '0.000 0.000 0.000'; } } + +// unzone the string, and return it as tempstring. Safe to be called on string_null +string fstrunzone(string s) +{ + string sc; + if not(s) + return s; + sc = strcat(s, ""); + strunzone(s); + return sc; +} diff --git a/data/qcsrc/common/util.qh b/data/qcsrc/common/util.qh index 00c998c11..d4390abe3 100644 --- a/data/qcsrc/common/util.qh +++ b/data/qcsrc/common/util.qh @@ -32,3 +32,6 @@ float median(float a, float b, float c); string ftos_decimals(float number, float decimals); vector colormapPaletteColor(float c, float isPants); + +// unzone the string, and return it as tempstring. Safe to be called on string_null +string fstrunzone(string s); diff --git a/data/qcsrc/menu-div0test/nexuiz/maplist.c b/data/qcsrc/menu-div0test/nexuiz/maplist.c index 803fb1390..c651302a7 100644 --- a/data/qcsrc/menu-div0test/nexuiz/maplist.c +++ b/data/qcsrc/menu-div0test/nexuiz/maplist.c @@ -36,6 +36,9 @@ CLASS(NexuizMapList) EXTENDS(NexuizListBox) ATTRIB(NexuizMapList, cvarName, string, "dummy") METHOD(NexuizMapList, loadCvars, void(entity)) + + ATTRIB(NexuizMapList, typeToSearchString, string, string_null) + ATTRIB(NexuizMapList, typeToSearchTime, float, 0) ENDCLASS(NexuizMapList) entity makeNexuizMapList(); void MapList_All(entity btn, entity me); @@ -254,6 +257,7 @@ void MapList_LoadMap(entity btn, entity me) float keyDownNexuizMapList(entity me, float scan, float ascii, float shift) { + string ch, save; if(scan == K_ENTER) { // pop up map info screen @@ -274,6 +278,38 @@ float keyDownNexuizMapList(entity me, float scan, float ascii, float shift) if(me.g_maplistCacheQuery(me, me.selectedItem)) me.g_maplistCacheToggle(me, me.selectedItem); } + else if(scan == K_BACKSPACE) + { + if(time < me.typeToSearchTime) + { + save = substring(me.typeToSearchString, 0, strlen(me.typeToSearchString) - 1); + if(me.typeToSearchString) + strunzone(me.typeToSearchString); + me.typeToSearchString = strzone(save); + me.typeToSearchTime = time + 0.5; + if(strlen(me.typeToSearchString)) + { + MapInfo_FindName(me.typeToSearchString); + if(MapInfo_FindName_firstResult >= 0) + me.setSelected(me, MapInfo_FindName_firstResult); + } + } + } + else if(ascii >= 32 && ascii != 127) + { + ch = chr(ascii); + if(time > me.typeToSearchTime) + save = ch; + else + save = strcat(me.typeToSearchString, ch); + if(me.typeToSearchString) + strunzone(me.typeToSearchString); + me.typeToSearchString = strzone(save); + me.typeToSearchTime = time + 0.5; + MapInfo_FindName(me.typeToSearchString); + if(MapInfo_FindName_firstResult >= 0) + me.setSelected(me, MapInfo_FindName_firstResult); + } else return keyDownListBox(me, scan, ascii, shift); return 1; -- 2.39.2