]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
Cvar onChange for qc menu for stable branch ColdSpirit/cvar_onchange_stable
authorColdSpirit <coldspiritvolf@gmail.com>
Thu, 30 Dec 2021 11:11:53 +0000 (15:11 +0400)
committerColdSpirit <coldspiritvolf@gmail.com>
Thu, 30 Dec 2021 11:11:53 +0000 (15:11 +0400)
Use m_cvar_changed in qc menu to receive all cvar updates

cvar.c
dpdefs/menudefs.qc
prvm_offsets.h

diff --git a/cvar.c b/cvar.c
index 2a0acae8edefad814c472bf0df404c64b6e7925e..728f53096539e5eccac4931d1268556cff054713 100644 (file)
--- a/cvar.c
+++ b/cvar.c
@@ -21,6 +21,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include "quakedef.h"
 
+#ifdef CONFIG_MENU
+    #include "progsvm.h"
+#endif
+
 const char *cvar_dummy_description = "custom cvar";
 
 cvar_t *cvar_vars = NULL;
@@ -345,6 +349,22 @@ static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
        // LordHavoc: don't reallocate when there is no change
        if (!changed)
                return;
+    
+    // Con_Printf("----> Changing \"%s\" value from \"%s\" to \"%s\"\n", var->name, var->string, value);
+
+#ifdef CONFIG_MENU
+    // prepare values to send into QC
+       prvm_prog_t *prog = MVM_prog;
+    qboolean qcSubscribed = PRVM_menufunction(m_cvar_changed);
+
+    if (qcSubscribed && prog->loaded)
+    {
+        prog->globals.ip[OFS_PARM0] = PRVM_SetTempString(prog, var->name);
+        prog->globals.ip[OFS_PARM1] = PRVM_SetTempString(prog, var->string);
+        prog->globals.ip[OFS_PARM2] = PRVM_SetTempString(prog, value);
+    }
+#endif
+
 
        // LordHavoc: don't reallocate when the buffer is the same size
        valuelen = strlen(value);
@@ -418,6 +438,14 @@ static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
        }
 
        Cvar_UpdateAutoCvar(var);
+
+#ifdef CONFIG_MENU
+    // call "onCvarChanged" like event
+    if (qcSubscribed && prog->loaded)
+    {
+        prog->ExecuteProgram(prog, PRVM_menufunction(m_cvar_changed), "QC function m_cvar_changed is missing");
+    }
+#endif
 }
 
 void Cvar_SetQuick (cvar_t *var, const char *value)
index 8caab4a9279e6afec354b94d2f43236be0564c25..4eee19ed10f2d7d827ff8891fcf10d9d0c9d5ed6 100644 (file)
@@ -15,6 +15,7 @@ void          end_sys_fields;
 
 void() m_init;
 void(float keynr, float ascii) m_keydown;
+void(string cvarName, string oldValue, string newValue) m_cvar_changed;
 void(float width, float height) m_draw;
 void(float mode) m_toggle;
 void() m_shutdown;
index 1563a0510f29264f475cfb436d0ec11b8395231f..306849b790ad33ebd8f4506038b3c743d70d9b9c 100644 (file)
@@ -443,6 +443,7 @@ PRVM_DECLARE_function(m_newmap)
 PRVM_DECLARE_function(m_gethostcachecategory)
 PRVM_DECLARE_function(m_shutdown)
 PRVM_DECLARE_function(m_toggle)
+PRVM_DECLARE_function(m_cvar_changed)
 PRVM_DECLARE_function(main)
 PRVM_DECLARE_global(SV_InitCmd)
 PRVM_DECLARE_global(clientcommandframe)
@@ -591,6 +592,7 @@ PRVM_DECLARE_menufunction(m_keyup)
 PRVM_DECLARE_menufunction(m_newmap)
 PRVM_DECLARE_menufunction(m_shutdown)
 PRVM_DECLARE_menufunction(m_toggle)
+PRVM_DECLARE_menufunction(m_cvar_changed)
 PRVM_DECLARE_menuglobaledict(self)
 PRVM_DECLARE_menuglobalfloat(drawfont)
 PRVM_DECLARE_menuglobalfloat(require_spawnfunc_prefix)