]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/slider_decibels.c
fix a VERY stupid sound menu bug: use correct scale of decibels. -20dB is 0.1, not...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / slider_decibels.c
1 #ifdef INTERFACE
2 CLASS(XonoticDecibelsSlider) EXTENDS(XonoticSlider)
3         METHOD(XonoticDecibelsSlider, loadCvars, void(entity))
4         METHOD(XonoticDecibelsSlider, saveCvars, void(entity))
5         METHOD(XonoticDecibelsSlider, valueToText, string(entity, float))
6 ENDCLASS(XonoticDecibelsSlider)
7 entity makeXonoticDecibelsSlider(float, float, float, string);
8 #endif
9
10 #ifdef IMPLEMENTATION
11
12 float toDecibelOfSquare(float f)
13 {
14         return 20.0 * log10(f);
15 }
16
17 float fromDecibelOfSquare(float f)
18 {
19         return pow(10, f / 20.0);
20 }
21
22 entity makeXonoticDecibelsSlider(float theValueMin, float theValueMax, float theValueStep, string theCvar)
23 {
24         entity me;
25         me = spawnXonoticDecibelsSlider();
26         me.configureXonoticSlider(me, theValueMin, theValueMax, theValueStep, theCvar);
27         return me;
28 }
29 void XonoticDecibelsSlider_loadCvars(entity me)
30 {
31         float v;
32
33         if not(me.cvarName)
34                 return;
35
36         v = cvar(me.cvarName);
37
38         // snapping
39         if(v > fromDecibelOfSquare(me.valueMax - 0.5 * me.valueStep))
40                 Slider_setValue(me, me.valueMax);
41         else if(v < fromDecibelOfSquare(me.valueMin - 0.5 * me.valueStep))
42                 Slider_setValue(me, -1000000); // virtually infinite
43         else
44                 Slider_setValue(me, me.valueStep * floor(0.5 + toDecibelOfSquare(v) / me.valueStep) );
45 }
46 void XonoticDecibelsSlider_saveCvars(entity me)
47 {
48         if not(me.cvarName)
49                 return;
50
51         if(me.value > me.valueMax - 0.5 * me.valueStep)
52                 cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.valueMax)));
53         else if(me.value < me.valueMin - 0.5 * me.valueStep)
54                 cvar_set(me.cvarName, "0");
55         else
56                 cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.value)));
57 }
58
59 string XonoticDecibelsSlider_valueToText(entity me, float v)
60 {
61         if(v > me.valueMax - 0.5 * me.valueStep)
62                 return CTX(_("VOL^MAX"));
63         else if(v < me.valueMin - 0.5 * me.valueStep)
64                 return CTX(_("VOL^OFF"));
65         return sprintf(_("%s dB"), SUPER(XonoticDecibelsSlider).valueToText(me, v));
66 }
67
68 #endif