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