From 6b7f3c5423ede05f45ee2c8dd2352d0dc62d36f8 Mon Sep 17 00:00:00 2001 From: terencehill Date: Sun, 27 Mar 2016 16:10:27 +0200 Subject: [PATCH] Remove trailing 0s before saving float numbers into cvars in the menu. Reason: with a fresh config a few cvars were considered modified because on menu start sliders rewrite the current cvar values adding trailing 0s --- qcsrc/common/util.qh | 2 +- qcsrc/lib/string.qh | 15 ++++++++++++++- qcsrc/menu/xonotic/checkbox.qc | 4 ++-- qcsrc/menu/xonotic/slider.qc | 2 +- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/qcsrc/common/util.qh b/qcsrc/common/util.qh index 7bc867533..da6574c0a 100644 --- a/qcsrc/common/util.qh +++ b/qcsrc/common/util.qh @@ -27,8 +27,8 @@ void depthfirst(entity start, .entity up, .entity downleft, .entity right, void( float median(float a, float b, float c); // converts a number to a string with the indicated number of decimals -// works for up to 10 decimals! string ftos_decimals(float number, float decimals); +string ftos_mindecimals(float number); bool fexists(string f); diff --git a/qcsrc/lib/string.qh b/qcsrc/lib/string.qh index 47a8175ca..380afb9ee 100644 --- a/qcsrc/lib/string.qh +++ b/qcsrc/lib/string.qh @@ -281,7 +281,6 @@ void buf_save(float buf, string pFilename) /** * converts a number to a string with the indicated number of decimals - * works for up to 10 decimals! */ string ftos_decimals(float number, int decimals) { @@ -291,6 +290,20 @@ string ftos_decimals(float number, int decimals) return sprintf("%.*f", decimals, number); } +// strips trailing 0s from a float number, e.g. 1.200000 -> 1.2 +string ftos_mindecimals(float number) +{ + string s = ftos(number); + int k = strstrofs(s, ".", 0); + if(k >= 0) + { + int i = strlen(s); + while(substring(s, i - 1, 1) == "0") i--; + s = substring(s, 0, i); + } + return s; +} + int vercmp_recursive(string v1, string v2) { int dot1 = strstrofs(v1, ".", 0); diff --git a/qcsrc/menu/xonotic/checkbox.qc b/qcsrc/menu/xonotic/checkbox.qc index 21743b9a1..6015e48ba 100644 --- a/qcsrc/menu/xonotic/checkbox.qc +++ b/qcsrc/menu/xonotic/checkbox.qc @@ -77,9 +77,9 @@ void XonoticCheckBox_saveCvars(entity me) return; if(me.checked) - cvar_set(me.cvarName, ftos(me.yesValue)); + cvar_set(me.cvarName, ftos_mindecimals(me.yesValue)); else - cvar_set(me.cvarName, ftos(me.noValue)); + cvar_set(me.cvarName, ftos_mindecimals(me.noValue)); CheckSendCvars(me, me.cvarName); } diff --git a/qcsrc/menu/xonotic/slider.qc b/qcsrc/menu/xonotic/slider.qc index fc4228cf9..c8e057a64 100644 --- a/qcsrc/menu/xonotic/slider.qc +++ b/qcsrc/menu/xonotic/slider.qc @@ -58,7 +58,7 @@ void XonoticSlider_saveCvars(entity me) if (!me.cvarName) return; - cvar_set(me.cvarName, ftos(me.value)); + cvar_set(me.cvarName, ftos_mindecimals(me.value)); CheckSendCvars(me, me.cvarName); } -- 2.39.2