]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/checkbox.qc
Sort menu classes
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / checkbox.qc
1 #ifndef CHECKBOX_H
2 #define CHECKBOX_H
3 CLASS(XonoticCheckBox, CheckBox)
4         METHOD(XonoticCheckBox, configureXonoticCheckBox, void(entity, float, float, string, string))
5         METHOD(XonoticCheckBox, setChecked, void(entity, float))
6         ATTRIB(XonoticCheckBox, fontSize, float, SKINFONTSIZE_NORMAL)
7         ATTRIB(XonoticCheckBox, image, string, SKINGFX_CHECKBOX)
8         ATTRIB(XonoticCheckBox, yesValue, float, 1)
9         ATTRIB(XonoticCheckBox, noValue, float, 0)
10
11         ATTRIB(XonoticCheckBox, color, vector, SKINCOLOR_CHECKBOX_N)
12         ATTRIB(XonoticCheckBox, colorC, vector, SKINCOLOR_CHECKBOX_C)
13         ATTRIB(XonoticCheckBox, colorF, vector, SKINCOLOR_CHECKBOX_F)
14         ATTRIB(XonoticCheckBox, colorD, vector, SKINCOLOR_CHECKBOX_D)
15
16         ATTRIB(XonoticCheckBox, cvarName, string, string_null)
17         METHOD(XonoticCheckBox, loadCvars, void(entity))
18         METHOD(XonoticCheckBox, saveCvars, void(entity))
19         ATTRIB(XonoticCheckBox, sendCvars, float, 0)
20
21         ATTRIB(XonoticCheckBox, alpha, float, SKINALPHA_TEXT)
22         ATTRIB(XonoticCheckBox, disabledAlpha, float, SKINALPHA_DISABLED)
23 ENDCLASS(XonoticCheckBox)
24 entity makeXonoticCheckBox(float, string, string);
25 entity makeXonoticCheckBoxEx(float, float, string, string);
26 #endif
27
28 #ifdef IMPLEMENTATION
29 entity makeXonoticCheckBox(float isInverted, string theCvar, string theText)
30 {
31         float y, n;
32         if(isInverted > 1)
33         {
34                 n = isInverted - 1;
35                 y = -n;
36         }
37         else if(isInverted < -1)
38         {
39                 n = isInverted + 1;
40                 y = -n;
41         }
42         else if(isInverted == 1)
43         {
44                 n = 1;
45                 y = 0;
46         }
47         else
48         {
49                 n = 0;
50                 y = 1;
51         }
52         return makeXonoticCheckBoxEx(y, n, theCvar, theText);
53 }
54 entity makeXonoticCheckBoxEx(float theYesValue, float theNoValue, string theCvar, string theText)
55 {
56         entity me;
57         me = NEW(XonoticCheckBox);
58         me.configureXonoticCheckBox(me, theYesValue, theNoValue, theCvar, theText);
59         return me;
60 }
61 void XonoticCheckBox_configureXonoticCheckBox(entity me, float theYesValue, float theNoValue, string theCvar, string theText)
62 {
63         me.yesValue = theYesValue;
64         me.noValue = theNoValue;
65         me.checked = 0;
66         if(theCvar)
67         {
68                 me.cvarName = theCvar;
69                 me.tooltip = getZonedTooltipForIdentifier(theCvar);
70                 me.loadCvars(me);
71         }
72         me.configureCheckBox(me, theText, me.fontSize, me.image);
73 }
74 void XonoticCheckBox_setChecked(entity me, float val)
75 {
76         if(val != me.checked)
77         {
78                 me.checked = val;
79                 me.saveCvars(me);
80         }
81 }
82 void XonoticCheckBox_loadCvars(entity me)
83 {
84         float m, d;
85
86         if (!me.cvarName)
87                 return;
88
89         m = (me.yesValue + me.noValue) * 0.5;
90         d = (cvar(me.cvarName) - m) / (me.yesValue - m);
91         me.checked = (d > 0);
92 }
93 void XonoticCheckBox_saveCvars(entity me)
94 {
95         if (!me.cvarName)
96                 return;
97
98         if(me.checked)
99                 cvar_set(me.cvarName, ftos(me.yesValue));
100         else
101                 cvar_set(me.cvarName, ftos(me.noValue));
102
103         CheckSendCvars(me, me.cvarName);
104 }
105 #endif