]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/textslider.qc
Merge branch 'master' into terencehill/dynamic_hud
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / textslider.qc
1 #include "textslider.qh"
2
3 entity makeXonoticTextSlider_T(string theCvar, string theTooltip)
4 {
5         entity me;
6         me = NEW(XonoticTextSlider);
7         me.configureXonoticTextSlider(me, theCvar, theTooltip);
8         return me;
9 }
10 entity makeXonoticTextSlider(string theCvar)
11 {
12         return makeXonoticTextSlider_T(theCvar, string_null);
13 }
14 void XonoticTextSlider_configureXonoticTextSlider(entity me, string theCvar, string theTooltip)
15 {
16         me.configureSliderVisuals(me, me.fontSize, me.align, me.valueSpace, me.image);
17         me.cvarName = (theCvar) ? theCvar : string_null;
18         // me.loadCvars(me); // don't load it yet
19         setZonedTooltip(me, theTooltip, theCvar);
20 }
21 void XonoticTextSlider_setValue(entity me, float val)
22 {
23         if(val != me.value)
24         {
25                 SUPER(XonoticTextSlider).setValue( me, val );
26                 me.saveCvars(me);
27         }
28 }
29 void XonoticTextSlider_setValue_noAnim(entity me, float val)
30 {
31         if(val != me.value)
32         {
33                 SUPER(XonoticTextSlider).setValue_noAnim(me, val);
34                 me.saveCvars(me);
35         }
36 }
37 void XonoticTextSlider_loadCvars(entity me)
38 {
39         if (!me.cvarName)
40                 return;
41
42         float n = tokenize_console(me.cvarName);
43         string s = cvar_string(argv(0));
44         float i;
45         for(i = 1; i < n; ++i)
46                 s = strcat(s, " ", cvar_string(argv(i)));
47         me.setValueFromIdentifier_noAnim(me, s);
48         if(me.value < 0 && n > 1)
49         {
50                 // if it failed: check if all cvars have the same value
51                 // if yes, try its value as 1-word identifier
52                 for(i = 1; i < n; ++i)
53                         if(cvar_string(argv(i)) != cvar_string(argv(i-1)))
54                                 break;
55                 if(i >= n)
56                         me.setValueFromIdentifier_noAnim(me, cvar_string(argv(0)));
57         }
58 }
59 void XonoticTextSlider_saveCvars(entity me)
60 {
61         if (!me.cvarName)
62                 return;
63
64         if(me.value >= 0 && me.value < me.nValues)
65         {
66                 float n = tokenize_console(me.cvarName);
67                 if(n == 1)
68                 {
69                         // this is a special case to allow spaces in the identifiers
70                         cvar_set(argv(0), me.getIdentifier(me));
71                         CheckSendCvars(me, argv(0));
72                 }
73                 else
74                 {
75                         float i;
76                         float m = tokenize_console(strcat(me.cvarName, " ", me.getIdentifier(me)));
77                         if(m == n + 1)
78                         {
79                                 for(i = 0; i < n; ++i)
80                                 {
81                                         cvar_set(argv(i), argv(n));
82                                         CheckSendCvars(me, argv(i));
83                                 }
84                         }
85                         else if(m == n * 2)
86                         {
87                                 for(i = 0; i < n; ++i)
88                                 {
89                                         cvar_set(argv(i), argv(i + n));
90                                         CheckSendCvars(me, argv(i));
91                                 }
92                         }
93                         else
94                                 error("XonoticTextSlider: invalid identifier ", me.getIdentifier(me), " does not match cvar list ", me.cvarName);
95                 }
96         }
97 }
98 void XonoticTextSlider_configureXonoticTextSliderValues(entity me)
99 {
100         me.configureTextSliderValues(me, string_null);
101         me.loadCvars(me);
102 }