]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/textslider.qc
Fix sliders not setting cvars when dragged
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / textslider.qc
1 #ifndef TEXTSLIDER_H
2 #define TEXTSLIDER_H
3 #include "../item/textslider.qc"
4 CLASS(XonoticTextSlider, TextSlider)
5         METHOD(XonoticTextSlider, configureXonoticTextSlider, void(entity, string, string));
6         METHOD(XonoticTextSlider, setValue, void(entity, float));
7         METHOD(XonoticTextSlider, setValue_noAnim, void(entity, float));
8         METHOD(XonoticTextSlider, configureXonoticTextSliderValues, void(entity));
9         ATTRIB(XonoticTextSlider, fontSize, float, SKINFONTSIZE_NORMAL)
10         ATTRIB(XonoticTextSlider, valueSpace, float, SKINWIDTH_SLIDERTEXT)
11         ATTRIB(XonoticTextSlider, image, string, SKINGFX_SLIDER)
12         ATTRIB(XonoticTextSlider, tolerance, vector, SKINTOLERANCE_SLIDER)
13         ATTRIB(XonoticTextSlider, align, float, 0.5)
14         ATTRIB(XonoticTextSlider, color, vector, SKINCOLOR_SLIDER_N)
15         ATTRIB(XonoticTextSlider, colorC, vector, SKINCOLOR_SLIDER_C)
16         ATTRIB(XonoticTextSlider, colorF, vector, SKINCOLOR_SLIDER_F)
17         ATTRIB(XonoticTextSlider, colorD, vector, SKINCOLOR_SLIDER_D)
18         ATTRIB(XonoticTextSlider, color2, vector, SKINCOLOR_SLIDER_S)
19
20         ATTRIB(XonoticTextSlider, cvarName, string, string_null)
21         METHOD(XonoticTextSlider, loadCvars, void(entity));
22         METHOD(XonoticTextSlider, saveCvars, void(entity));
23         ATTRIB(XonoticTextSlider, sendCvars, float, 0)
24
25         ATTRIB(XonoticTextSlider, alpha, float, SKINALPHA_TEXT)
26         ATTRIB(XonoticTextSlider, disabledAlpha, float, SKINALPHA_DISABLED)
27 ENDCLASS(XonoticTextSlider)
28 entity makeXonoticTextSlider_T(string, string theTooltip);
29 entity makeXonoticTextSlider(string); // note: you still need to call addValue and configureXonoticTextSliderValues!
30 #endif
31
32 #ifdef IMPLEMENTATION
33 entity makeXonoticTextSlider_T(string theCvar, string theTooltip)
34 {
35         entity me;
36         me = NEW(XonoticTextSlider);
37         me.configureXonoticTextSlider(me, theCvar, theTooltip);
38         return me;
39 }
40 entity makeXonoticTextSlider(string theCvar)
41 {
42         return makeXonoticTextSlider_T(theCvar, string_null);
43 }
44 void XonoticTextSlider_configureXonoticTextSlider(entity me, string theCvar, string theTooltip)
45 {
46         me.configureSliderVisuals(me, me.fontSize, me.align, me.valueSpace, me.image);
47         me.cvarName = (theCvar) ? theCvar : string_null;
48         // me.loadCvars(me); // don't load it yet
49         setZonedTooltip(me, theTooltip, theCvar);
50 }
51 void XonoticTextSlider_setValue(entity me, float val)
52 {
53         if(val != me.value)
54         {
55                 SUPER(XonoticTextSlider).setValue( me, val );
56                 me.saveCvars(me);
57         }
58 }
59 void XonoticTextSlider_setValue_noAnim(entity me, float val)
60 {
61         if(val != me.value)
62         {
63                 SUPER(XonoticTextSlider).setValue_noAnim(me, val);
64                 me.saveCvars(me);
65         }
66 }
67 void XonoticTextSlider_loadCvars(entity me)
68 {
69         if (!me.cvarName)
70                 return;
71
72         float n = tokenize_console(me.cvarName);
73         string s = cvar_string(argv(0));
74         float i;
75         for(i = 1; i < n; ++i)
76                 s = strcat(s, " ", cvar_string(argv(i)));
77         me.setValueFromIdentifier_noAnim(me, s);
78         if(me.value < 0 && n > 1)
79         {
80                 // if it failed: check if all cvars have the same value
81                 // if yes, try its value as 1-word identifier
82                 for(i = 1; i < n; ++i)
83                         if(cvar_string(argv(i)) != cvar_string(argv(i-1)))
84                                 break;
85                 if(i >= n)
86                         me.setValueFromIdentifier_noAnim(me, cvar_string(argv(0)));
87         }
88 }
89 void XonoticTextSlider_saveCvars(entity me)
90 {
91         if (!me.cvarName)
92                 return;
93
94         if(me.value >= 0 && me.value < me.nValues)
95         {
96                 float n = tokenize_console(me.cvarName);
97                 if(n == 1)
98                 {
99                         // this is a special case to allow spaces in the identifiers
100                         cvar_set(argv(0), me.getIdentifier(me));
101                         CheckSendCvars(me, argv(0));
102                 }
103                 else
104                 {
105                         float i;
106                         float m = tokenize_console(strcat(me.cvarName, " ", me.getIdentifier(me)));
107                         if(m == n + 1)
108                         {
109                                 for(i = 0; i < n; ++i)
110                                 {
111                                         cvar_set(argv(i), argv(n));
112                                         CheckSendCvars(me, argv(i));
113                                 }
114                         }
115                         else if(m == n * 2)
116                         {
117                                 for(i = 0; i < n; ++i)
118                                 {
119                                         cvar_set(argv(i), argv(i + n));
120                                         CheckSendCvars(me, argv(i));
121                                 }
122                         }
123                         else
124                                 error("XonoticTextSlider: invalid identifier ", me.getIdentifier(me), " does not match cvar list ", me.cvarName);
125                 }
126         }
127 }
128 void XonoticTextSlider_configureXonoticTextSliderValues(entity me)
129 {
130         me.configureTextSliderValues(me, string_null);
131         me.loadCvars(me);
132 }
133 #endif