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