]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/menu/xonotic/maplist.qc
Optimize vehicle impact code by only calling vlen() if damage would be taken
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / maplist.qc
index 4003a36d78f682a894a3a0ef9596d0f72bfc8f95..5184ee1fd546a0f90ad3838462a57c005b7b4a9e 100644 (file)
@@ -1,54 +1,12 @@
-#ifdef INTERFACE
-CLASS(XonoticMapList) EXTENDS(XonoticListBox)
-       METHOD(XonoticMapList, configureXonoticMapList, void(entity))
-       ATTRIB(XonoticMapList, rowsPerItem, float, 4)
-       METHOD(XonoticMapList, draw, void(entity))
-       METHOD(XonoticMapList, drawListBoxItem, void(entity, int, vector, bool, float))
-       METHOD(XonoticMapList, clickListBoxItem, void(entity, float, vector))
-       METHOD(XonoticMapList, doubleClickListBoxItem, void(entity, float, vector))
-       METHOD(XonoticMapList, resizeNotify, void(entity, vector, vector, vector, vector))
-       METHOD(XonoticMapList, refilter, void(entity))
-       METHOD(XonoticMapList, refilterCallback, void(entity, entity))
-       METHOD(XonoticMapList, keyDown, float(entity, float, float, float))
-
-       ATTRIB(XonoticMapList, realFontSize, vector, '0 0 0')
-       ATTRIB(XonoticMapList, columnPreviewOrigin, float, 0)
-       ATTRIB(XonoticMapList, columnPreviewSize, float, 0)
-       ATTRIB(XonoticMapList, columnNameOrigin, float, 0)
-       ATTRIB(XonoticMapList, columnNameSize, float, 0)
-       ATTRIB(XonoticMapList, checkMarkOrigin, vector, '0 0 0')
-       ATTRIB(XonoticMapList, checkMarkSize, vector, '0 0 0')
-       ATTRIB(XonoticMapList, realUpperMargin1, float, 0)
-       ATTRIB(XonoticMapList, realUpperMargin2, float, 0)
-
-       ATTRIB(XonoticMapList, lastGametype, float, 0)
-       ATTRIB(XonoticMapList, lastFeatures, float, 0)
-
-       ATTRIB(XonoticMapList, origin, vector, '0 0 0')
-       ATTRIB(XonoticMapList, itemAbsSize, vector, '0 0 0')
-
-       ATTRIB(XonoticMapList, g_maplistCache, string, string_null)
-       METHOD(XonoticMapList, g_maplistCacheToggle, void(entity, float))
-       METHOD(XonoticMapList, g_maplistCacheQuery, float(entity, float))
-
-       ATTRIB(XonoticMapList, startButton, entity, NULL)
-
-       METHOD(XonoticMapList, loadCvars, void(entity))
-
-       ATTRIB(XonoticMapList, typeToSearchString, string, string_null)
-       ATTRIB(XonoticMapList, typeToSearchTime, float, 0)
-
-       METHOD(XonoticMapList, destroy, void(entity))
-
-       ATTRIB(XonoticListBox, alphaBG, float, 0)
-ENDCLASS(XonoticMapList)
-entity makeXonoticMapList();
-void MapList_All(entity btn, entity me);
-void MapList_None(entity btn, entity me);
-void MapList_LoadMap(entity btn, entity me);
-#endif
-
-#ifdef IMPLEMENTATION
+#include "maplist.qh"
+
+#include <common/mapinfo.qh>
+#include "dialog_multiplayer_create_mapinfo.qh"
+#include "mainwindow.qh"
+#include "inputbox.qh"
+
+.bool disabled;
+
 void XonoticMapList_destroy(entity me)
 {
        MapInfo_Shutdown();
@@ -57,11 +15,17 @@ void XonoticMapList_destroy(entity me)
 entity makeXonoticMapList()
 {
        entity me;
-       me = spawnXonoticMapList();
+       me = NEW(XonoticMapList);
        me.configureXonoticMapList(me);
        return me;
 }
 
+entity MapList_Set_String_Filter_Box(entity me, entity e)
+{
+       me.stringFilterBox = e;
+       return e;
+}
+
 void XonoticMapList_configureXonoticMapList(entity me)
 {
        me.configureXonoticListBox(me);
@@ -73,6 +37,7 @@ void XonoticMapList_loadCvars(entity me)
        me.refilter(me);
 }
 
+
 float XonoticMapList_g_maplistCacheQuery(entity me, float i)
 {
        return stof(substring(me.g_maplistCache, i, 1));
@@ -123,8 +88,10 @@ void XonoticMapList_resizeNotify(entity me, vector relOrigin, vector relSize, ve
        me.itemAbsSize = '0 0 0';
        SUPER(XonoticMapList).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.realUpperMargin1 = 0.5 * (1 - 2.5 * me.realFontSize.y);
        me.realUpperMargin2 = me.realUpperMargin1 + 1.5 * me.realFontSize.y;
 
@@ -159,7 +126,7 @@ void XonoticMapList_doubleClickListBoxItem(entity me, float i, vector where)
                }
 }
 
-void XonoticMapList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, float highlightedTime)
+void XonoticMapList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
 {
        // layout: Ping, Map name, Map name, NP, TP, MP
        string s;
@@ -181,8 +148,11 @@ void XonoticMapList_drawListBoxItem(entity me, int i, vector absSize, bool isSel
        {
                if(included)
                        draw_Fill('0 0 0', '1 1 0', SKINCOLOR_MAPLIST_INCLUDEDBG, SKINALPHA_MAPLIST_INCLUDEDBG);
-               if(highlightedTime)
-                       draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, getHighlightAlpha(SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED, highlightedTime));
+               if(isFocused)
+               {
+                       me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
+                       draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
+               }
        }
 
        if(draw_PictureSize(strcat("/maps/", MapInfo_Map_bspname)) == '0 0 0')
@@ -204,15 +174,15 @@ void XonoticMapList_refilter(entity me)
 {
        float i, j, n;
        string s;
-       float gt, f;
-       gt = MapInfo_CurrentGametype();
-       f = MapInfo_CurrentFeatures();
+       Gametype gt = MapInfo_CurrentGametype();
+       int f = MapInfo_CurrentFeatures();
        MapInfo_FilterGametype(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
+       if (me.stringFilter)
+               MapInfo_FilterString(me.stringFilter);
        me.nItems = MapInfo_count;
+
        for(i = 0; i < MapInfo_count; ++i)
                draw_PreloadPicture(strcat("/maps/", MapInfo_BSPName_ByID(i)));
-       if(me.g_maplistCache)
-               strunzone(me.g_maplistCache);
        s = "0";
        for(i = 1; i < MapInfo_count; i *= 2)
                s = strcat(s, s);
@@ -221,13 +191,17 @@ void XonoticMapList_refilter(entity me)
        {
                j = MapInfo_FindName(argv(i));
                if(j >= 0)
-                       s = strcat(
-                               substring(s, 0, j),
-                               "1",
-                               substring(s, j+1, MapInfo_count - (j+1))
-                       );
+               {
+                       // double check that the two mapnames are "identical", not just share the same prefix
+                       if (strlen(MapInfo_BSPName_ByID(j)) == strlen(argv(i)))
+                               s = strcat(
+                                       substring(s, 0, j),
+                                       "1",
+                                       substring(s, j+1, MapInfo_count - (j+1))
+                               );
+               }
        }
-       me.g_maplistCache = strzone(s);
+       strcpy(me.g_maplistCache, s);
        if(gt != me.lastGametype || f != me.lastFeatures)
        {
                me.lastGametype = gt;
@@ -241,11 +215,44 @@ void XonoticMapList_refilterCallback(entity me, entity cb)
        me.refilter(me);
 }
 
-void MapList_All(entity btn, entity me)
+void MapList_StringFilterBox_Change(entity box, entity me)
+{
+       strfree(me.stringFilter);
+       if(box.text != "")
+               me.stringFilter = strzone(box.text);
+
+       me.refilter(me);
+}
+
+void MapList_Add_Shown(entity btn, entity me)
+{
+       float i, n;
+       n = strlen(me.g_maplistCache);
+       for (i = 0 ; i < n; i++)
+       {
+               if (!me.g_maplistCacheQuery(me, i))
+                       me.g_maplistCacheToggle(me, i);
+       }
+       me.refilter(me);
+}
+
+void MapList_Remove_Shown(entity btn, entity me)
+{
+       float i, n;
+       n = strlen(me.g_maplistCache);
+       for (i = 0 ; i < n; i++)
+       {
+               if (me.g_maplistCacheQuery(me, i))
+                       me.g_maplistCacheToggle(me, i);
+       }
+       me.refilter(me);
+}
+
+void MapList_Add_All(entity btn, entity me)
 {
        float i;
        string s;
-       MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0); // all
+       _MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0); // all
        s = "";
        for(i = 0; i < MapInfo_count; ++i)
                s = strcat(s, " ", MapInfo_BSPName_ByID(i));
@@ -253,7 +260,7 @@ void MapList_All(entity btn, entity me)
        me.refilter(me);
 }
 
-void MapList_None(entity btn, entity me)
+void MapList_Remove_All(entity btn, entity me)
 {
        cvar_set("g_maplist", "");
        me.refilter(me);
@@ -278,7 +285,7 @@ void MapList_LoadMap(entity btn, entity me)
        m = MapInfo_BSPName_ByID(i);
        if (!m)
        {
-               print(_("Huh? Can't play this (m is NULL). Refiltering so this won't happen again.\n"));
+               LOG_INFO(_("Huh? Can't play this (m is NULL). Refiltering so this won't happen again."));
                me.refilter(me);
                return;
        }
@@ -291,7 +298,7 @@ void MapList_LoadMap(entity btn, entity me)
        }
        else
        {
-               print(_("Huh? Can't play this (invalid game type). Refiltering so this won't happen again.\n"));
+               LOG_INFO(_("Huh? Can't play this (invalid game type). Refiltering so this won't happen again."));
                me.refilter(me);
                return;
        }
@@ -335,9 +342,7 @@ float XonoticMapList_keyDown(entity me, float scan, float ascii, float shift)
                if(time < me.typeToSearchTime)
                {
                        save = substring(me.typeToSearchString, 0, strlen(me.typeToSearchString) - 1);
-                       if(me.typeToSearchString)
-                               strunzone(me.typeToSearchString);
-                       me.typeToSearchString = strzone(save);
+                       strcpy(me.typeToSearchString, save);
                        me.typeToSearchTime = time + 0.5;
                        if(strlen(me.typeToSearchString))
                        {
@@ -354,17 +359,43 @@ float XonoticMapList_keyDown(entity me, float scan, float ascii, float shift)
                        save = ch;
                else
                        save = strcat(me.typeToSearchString, ch);
-               if(me.typeToSearchString)
-                       strunzone(me.typeToSearchString);
-               me.typeToSearchString = strzone(save);
+               strcpy(me.typeToSearchString, save);
                me.typeToSearchTime = time + 0.5;
                MapInfo_FindName(me.typeToSearchString);
                if(MapInfo_FindName_firstResult >= 0)
                        me.setSelected(me, MapInfo_FindName_firstResult);
        }
+       else if(shift & S_CTRL && scan == 'f') // ctrl-f (as in "F"ind)
+       {
+               me.parent.setFocus(me.parent, me.stringFilterBox);
+       }
+       else if(shift & S_CTRL && scan == 'u') // ctrl-u (remove stringFilter line
+       {
+               me.stringFilterBox.setText(me.stringFilterBox, "");
+               MapList_StringFilterBox_Change(me.stringFilterBox, me);
+       }
        else
                return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
        return 1;
 }
 
-#endif
+float MapList_StringFilterBox_keyDown(entity me, float scan, float ascii, float shift)
+{
+       // in this section, note that onChangeEntity has the ref to mapListBox
+       // we make use of that, instead of extending a class to add one more attrib
+       switch(scan)
+       {
+               case K_KP_ENTER:
+               case K_ENTER:
+                       // move the focus to the mapListBox
+                       me.onChangeEntity.parent.setFocus(me.onChangeEntity.parent, me.onChangeEntity);
+                       return 1;
+               case K_KP_UPARROW:
+               case K_UPARROW:
+               case K_KP_DOWNARROW:
+               case K_DOWNARROW:
+                       // pass the event to the mapListBox (to scroll up and down)
+                       return me.onChangeEntity.keyDown(me.onChangeEntity, scan, ascii, shift);
+       }
+       return SUPER(XonoticInputBox).keyDown(me, scan, ascii, shift);
+}