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