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