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