From d3cdec0708376036289f8cec3a0ea150def16b17 Mon Sep 17 00:00:00 2001 From: ac-stef Date: Sun, 7 Jun 2015 14:25:40 +0200 Subject: [PATCH] allow persistent menu selections menuselectionpersistent Enables persistent selections for the currently displayed menu and also restores a previously saved selection. Probably most efficient inside "menuinit". The position is restored during execution of this command, so the menu entry has to already exist and the alias with the last position has to be read from saved.cfg. In autmatically created menus it may be necessary to execute the command with a short delay (see examples in this commit). DOCREF menuselectionpersistent --- config/menus_settings.cfg | 5 +++++ source/src/menus.cpp | 21 +++++++++++++++++++++ source/src/protocol.h | 1 + source/src/protos.h | 3 ++- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/config/menus_settings.cfg b/config/menus_settings.cfg index 5b038604..7c586352 100644 --- a/config/menus_settings.cfg +++ b/config/menus_settings.cfg @@ -12,6 +12,7 @@ menuitem [Misc settings] [ showmenu Misc ] menuitem [] -1 menuitem [Reset all settings] [ showmenu [Reset settings] ] + newmenu Weapons menumdl weapons/assault/menu mapmodel 75 12 @@ -22,6 +23,7 @@ looplist [ shotgun "V-19 Combat Shotgun" carbine "TMP-M&A Carbine" ] [ w desc ] [execute (format [menuitem [%2] [nextprimary %1 ; echo %2 selected] [chmenumdl Weapons weapons/%1/menu mapmodel 75 12] ] $w $desc) ] +menuinit menuselectionpersistent newmenu CLA @@ -290,12 +292,15 @@ menuitem [Change crosshair in edit mode... ] [ showmenu [Edit mo newmenu Crosshairs menudirlist "packages/crosshairs" png [ loadcrosshair default (concatword $arg1 .png) ] 1 +menuinit [sleep 1 menuselectionpersistent] newmenu [Scope crosshairs] menudirlist "packages/crosshairs" png [ loadcrosshair scope (concatword $arg1 .png) ] 1 +menuinit [sleep 1 menuselectionpersistent] newmenu [Edit mode crosshairs] menudirlist "packages/crosshairs" png [ loadcrosshair edit (concatword $arg1 .png) ] 1 +menuinit [sleep 1 menuselectionpersistent] newmenu [Alternate edit binds] menuitem [Back] [ closecurmenu ] diff --git a/source/src/menus.cpp b/source/src/menus.cpp index cfdb66d0..8cfe768c 100644 --- a/source/src/menus.cpp +++ b/source/src/menus.cpp @@ -133,6 +133,13 @@ void showmenu_(const char *name) } COMMANDN(showmenu, showmenu_, "s"); +const char *persistentmenuselectionalias(const char *name) +{ + static defformatstring(al)("__lastmenusel_%s", name); + filtertext(al, al, FTXT__ALIAS); + return al; +} + void menuselect(void *menu, int sel) { gmenu &m = *(gmenu *)menu; @@ -151,6 +158,11 @@ void menuselect(void *menu, int sel) if(m.items.inrange(oldsel)) m.items[oldsel]->focus(false); m.items[sel]->focus(true); audiomgr.playsound(S_MENUSELECT, SP_HIGHEST); + if(m.persistentselection) + { + defformatstring(val)("%d", sel); + alias(persistentmenuselectionalias(m.name), val); + } } } } @@ -886,6 +898,15 @@ void menuinitselection(int *line) } COMMAND(menuinitselection, "i"); +void menuselectionpersistent() +{ + if(!curmenu) return; + curmenu->persistentselection = true; + const char *val = getalias(persistentmenuselectionalias(curmenu->name)); + if(val) menuselect(curmenu, ATOI(val)); +} +COMMAND(menuselectionpersistent, ""); + void menurenderoffset(int *xoff, int *yoff) { if(!lastmenu) return; diff --git a/source/src/protocol.h b/source/src/protocol.h index 9b26c49d..b00bcc8e 100644 --- a/source/src/protocol.h +++ b/source/src/protocol.h @@ -154,6 +154,7 @@ enum #define FTXT_MAPNAME (1<<21) #define FTXT_CROPWHITE (1<<22) // character classes +#define FTXT__ALIAS (FTXT_NOWHITE | FTXT_NOCOLOR | FTXT_SAFECS | FTXT_MAPNAME) #define FTXT__SERVDESC (FTXT_NOWHITE | FTXT_ALLOWBLANKS | FTXT_TABTOBLANK) #define FTXT__MAPMSG (FTXT_NOWHITE | FTXT_ALLOWBLANKS | FTXT_TABTOBLANK) #define FTXT__MAPNAME (FTXT_NOCOLOR | FTXT_MAPNAME | FTXT_TOLOWER) diff --git a/source/src/protos.h b/source/src/protos.h index 44c0f480..de7dd019 100644 --- a/source/src/protos.h +++ b/source/src/protos.h @@ -160,6 +160,7 @@ struct gmenu char *initaction; char *usefont; bool allowblink; + bool persistentselection; const char *mdl; int anim, rotspeed, scale; int footlen; @@ -167,7 +168,7 @@ struct gmenu char *previewtexture; mdirlist *dirlist; - gmenu() : name(0), title(0), header(0), footer(0), initaction(0), usefont(0), allowblink(0), mdl(0), footlen(0), xoffs(0), yoffs(0), previewtexture(NULL), dirlist(0) {} + gmenu() : name(0), title(0), header(0), footer(0), initaction(0), usefont(0), allowblink(false), persistentselection(false), mdl(0), footlen(0), xoffs(0), yoffs(0), previewtexture(NULL), dirlist(0) {} virtual ~gmenu() { DELETEA(name);