]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
Cvar onChange for qc menu ColdSpirit/cvar_onchange 126/head
authorColdSpirit <coldspiritvolf@gmail.com>
Thu, 30 Dec 2021 10:44:01 +0000 (14:44 +0400)
committerColdSpirit <coldspiritvolf@gmail.com>
Thu, 30 Dec 2021 10:44:01 +0000 (14:44 +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 7936c92356be359c13748eca7afbec3250df8ac2..6abf324c1fcd9b755b69487a5dfb6900b469ac85 100644 (file)
--- a/cvar.c
+++ b/cvar.c
@@ -22,6 +22,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";
 static const char *cvar_null_string = "";
 
@@ -393,6 +397,21 @@ static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
        // LadyHavoc: 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;
+    bool 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
 
        // LadyHavoc: don't reallocate when the buffer is the same size
        valuelen = strlen(value);
@@ -427,6 +446,14 @@ static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
 
        // Call the function stored in the cvar for bounds checking, cleanup, etc
        Cvar_Callback(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 63d2c6388e8447e90e46445abbe65506e93e074b..5aa7f3f7f4673d0cc9bb566744acc3791ad5b8cc 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 97a2defbcc3d5a0a5fbcd3caef342d97545f9cbd..dd4caa1e3cc7099cc02b5b47496d15dab4319261 100644 (file)
@@ -446,6 +446,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)
@@ -594,6 +595,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)