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