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