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