]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/slider.qc
Fix sliders not setting cvars when dragged
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / slider.qc
1 #ifndef SLIDER_H
2 #define SLIDER_H
3 #include "../item/slider.qc"
4 CLASS(XonoticSlider, Slider)
5         METHOD(XonoticSlider, configureXonoticSlider, void(entity, float, float, float, string, string));
6         METHOD(XonoticSlider, setValue, void(entity, float));
7         METHOD(XonoticSlider, setValue_noAnim, void(entity, float));
8         ATTRIB(XonoticSlider, fontSize, float, SKINFONTSIZE_NORMAL)
9         ATTRIB(XonoticSlider, valueSpace, float, SKINWIDTH_SLIDERTEXT)
10         ATTRIB(XonoticSlider, image, string, SKINGFX_SLIDER)
11         ATTRIB(XonoticSlider, tolerance, vector, SKINTOLERANCE_SLIDER)
12         ATTRIB(XonoticSlider, align, float, 0.5)
13         ATTRIB(XonoticSlider, color, vector, SKINCOLOR_SLIDER_N)
14         ATTRIB(XonoticSlider, colorC, vector, SKINCOLOR_SLIDER_C)
15         ATTRIB(XonoticSlider, colorF, vector, SKINCOLOR_SLIDER_F)
16         ATTRIB(XonoticSlider, colorD, vector, SKINCOLOR_SLIDER_D)
17         ATTRIB(XonoticSlider, color2, vector, SKINCOLOR_SLIDER_S)
18
19         ATTRIB(XonoticSlider, cvarName, string, string_null)
20         METHOD(XonoticSlider, loadCvars, void(entity));
21         METHOD(XonoticSlider, saveCvars, void(entity));
22         ATTRIB(XonoticSlider, sendCvars, float, 0)
23
24         ATTRIB(XonoticSlider, alpha, float, SKINALPHA_TEXT)
25         ATTRIB(XonoticSlider, disabledAlpha, float, SKINALPHA_DISABLED)
26 ENDCLASS(XonoticSlider)
27 entity makeXonoticSlider_T(float, float, float, string, string theTooltip);
28 entity makeXonoticSlider(float, float, float, string);
29 #endif
30
31 #ifdef IMPLEMENTATION
32 entity makeXonoticSlider_T(float theValueMin, float theValueMax, float theValueStep, string theCvar, string theTooltip)
33 {
34         entity me;
35         me = NEW(XonoticSlider);
36         me.configureXonoticSlider(me, theValueMin, theValueMax, theValueStep, theCvar, theTooltip);
37         return me;
38 }
39 entity makeXonoticSlider(float theValueMin, float theValueMax, float theValueStep, string theCvar)
40 {
41         return makeXonoticSlider_T(theValueMin, theValueMax, theValueStep, theCvar, string_null);
42 }
43 void XonoticSlider_configureXonoticSlider(entity me, float theValueMin, float theValueMax, float theValueStep, string theCvar, string theTooltip)
44 {
45         float vp;
46         vp = theValueStep * 10;
47         while(fabs(vp) < fabs(theValueMax - theValueMin) / 40)
48                 vp *= 10;
49
50         me.configureSliderVisuals(me, me.fontSize, me.align, me.valueSpace, me.image);
51
52         me.cvarName = (theCvar) ? theCvar : string_null;
53         if(theCvar)
54                 // Prevent flickering of the slider button by initialising the
55                 // slider out of bounds to hide the button before loading the cvar
56                 me.configureSliderValues(me, theValueMin, theValueMin-theValueStep, theValueMax, theValueStep, theValueStep, vp);
57         else
58                 me.configureSliderValues(me, theValueMin, theValueMin, theValueMax, theValueStep, theValueStep, vp);
59         me.loadCvars(me);
60         setZonedTooltip(me, theTooltip, theCvar);
61 }
62 void XonoticSlider_setValue(entity me, float val)
63 {
64         if(val != me.value)
65         {
66                 SUPER(XonoticSlider).setValue( me, val );
67                 me.saveCvars(me);
68         }
69 }
70 void XonoticSlider_setValue_noAnim(entity me, float val)
71 {
72         if(val != me.value)
73         {
74                 SUPER(XonoticSlider).setValue_noAnim(me, val);
75                 me.saveCvars(me);
76         }
77 }
78 void XonoticSlider_loadCvars(entity me)
79 {
80         if (!me.cvarName)
81                 return;
82
83         me.setValue_noAnim(me, cvar(me.cvarName));
84 }
85 void XonoticSlider_saveCvars(entity me)
86 {
87         if (!me.cvarName)
88                 return;
89
90         cvar_set(me.cvarName, ftos(me.value));
91
92         CheckSendCvars(me, me.cvarName);
93 }
94 #endif