[quake3-commits] r1617 - trunk/code/qcommon

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Tue Sep 15 01:43:53 EDT 2009


Author: icculus
Date: 2009-09-15 01:43:53 -0400 (Tue, 15 Sep 2009)
New Revision: 1617

Modified:
   trunk/code/qcommon/cvar.c
Log:
Allow Cvar_Toggle_f() to iterate through a list of values.

  Fixes Bugzilla #3591.


Modified: trunk/code/qcommon/cvar.c
===================================================================
--- trunk/code/qcommon/cvar.c	2009-09-15 05:38:27 UTC (rev 1616)
+++ trunk/code/qcommon/cvar.c	2009-09-15 05:43:53 UTC (rev 1617)
@@ -690,21 +690,44 @@
 ============
 Cvar_Toggle_f
 
-Toggles a cvar for easy single key binding
+Toggles a cvar for easy single key binding, optionally through a list of
+given values
 ============
 */
 void Cvar_Toggle_f( void ) {
-	int		v;
+	int		i, c = Cmd_Argc();
+	char		*curval;
 
-	if ( Cmd_Argc() != 2 ) {
-		Com_Printf ("usage: toggle <variable>\n");
+	if(c < 2) {
+		Com_Printf("usage: toggle <variable> [value1, value2, ...]\n");
 		return;
 	}
 
-	v = Cvar_VariableValue( Cmd_Argv( 1 ) );
-	v = !v;
+	if(c == 2) {
+		Cvar_Set2(Cmd_Argv(1), va("%d", 
+			!Cvar_VariableValue(Cmd_Argv(1))), 
+			qfalse);
+		return;
+	}
 
-	Cvar_Set2 (Cmd_Argv(1), va("%i", v), qfalse);
+	if(c == 3) {
+		Com_Printf("toggle: nothing to toggle to\n");
+		return;
+	}
+
+	curval = Cvar_VariableString(Cmd_Argv(1));
+
+	// don't bother checking the last arg for a match since the desired
+	// behaviour is the same as no match (set to the first argument)
+	for(i = 2; i + 1 < c; i++) {
+		if(strcmp(curval, Cmd_Argv(i)) == 0) {
+			Cvar_Set2(Cmd_Argv(1), Cmd_Argv(i + 1), qfalse);
+			return;
+		}
+	}
+
+	// fallback
+	Cvar_Set2(Cmd_Argv(1), Cmd_Argv(2), qfalse);
 }
 
 /*



More information about the quake3-commits mailing list