]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/radiobutton.qc
Header police
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / radiobutton.qc
1 #include "radiobutton.qh"
2 #ifndef RADIOBUTTON_H
3 #define RADIOBUTTON_H
4 #include "../item/radiobutton.qc"
5 CLASS(XonoticRadioButton, RadioButton)
6         METHOD(XonoticRadioButton, configureXonoticRadioButton, void(entity, float, string, string, string, string));
7         METHOD(XonoticRadioButton, draw, void(entity));
8         METHOD(XonoticRadioButton, setChecked, void(entity, float));
9         ATTRIB(XonoticRadioButton, fontSize, float, SKINFONTSIZE_NORMAL)
10         ATTRIB(XonoticRadioButton, image, string, SKINGFX_RADIOBUTTON)
11         ATTRIB(XonoticRadioButton, color, vector, SKINCOLOR_RADIOBUTTON_N)
12         ATTRIB(XonoticRadioButton, colorC, vector, SKINCOLOR_RADIOBUTTON_C)
13         ATTRIB(XonoticRadioButton, colorF, vector, SKINCOLOR_RADIOBUTTON_F)
14         ATTRIB(XonoticRadioButton, colorD, vector, SKINCOLOR_RADIOBUTTON_D)
15
16         ATTRIB(XonoticRadioButton, cvarName, string, string_null)
17         ATTRIB(XonoticRadioButton, cvarValue, string, string_null)
18         ATTRIB(XonoticRadioButton, cvarOffValue, string, string_null)
19         ATTRIB(XonoticRadioButton, cvarValueIsAnotherCvar, float, 0)
20         METHOD(XonoticRadioButton, loadCvars, void(entity));
21         METHOD(XonoticRadioButton, saveCvars, void(entity));
22
23         ATTRIB(XonoticRadioButton, alpha, float, SKINALPHA_TEXT)
24         ATTRIB(XonoticRadioButton, disabledAlpha, float, SKINALPHA_DISABLED)
25 ENDCLASS(XonoticRadioButton)
26 entity makeXonoticRadioButton_T(float, string, string, string, string theTooltip);
27 entity makeXonoticRadioButton(float, string, string, string);
28 #endif
29
30 #ifdef IMPLEMENTATION
31 entity makeXonoticRadioButton_T(float theGroup, string theCvar, string theValue, string theText, string theTooltip)
32 {
33         entity me;
34         me = NEW(XonoticRadioButton);
35         me.configureXonoticRadioButton(me, theGroup, theCvar, theValue, theText, theTooltip);
36         return me;
37 }
38 entity makeXonoticRadioButton(float theGroup, string theCvar, string theValue, string theText)
39 {
40         return makeXonoticRadioButton_T(theGroup, theCvar, theValue, theText, string_null);
41 }
42 void XonoticRadioButton_configureXonoticRadioButton(entity me, float theGroup, string theCvar, string theValue, string theText, string theTooltip)
43 {
44         me.cvarName = (theCvar) ? theCvar : string_null;
45         me.cvarValue = theValue;
46         me.loadCvars(me);
47         setZonedTooltip(me, theTooltip, theCvar);
48         me.configureRadioButton(me, theText, me.fontSize, me.image, theGroup, 0);
49 }
50 void XonoticRadioButton_setChecked(entity me, float val)
51 {
52         if(val != me.checked)
53         {
54                 me.checked = val;
55                 me.saveCvars(me);
56         }
57 }
58 void XonoticRadioButton_loadCvars(entity me)
59 {
60         if(me.cvarValue)
61         {
62                 if(me.cvarName)
63                 {
64                         if(me.cvarValueIsAnotherCvar)
65                                 me.checked = (cvar_string(me.cvarName) == cvar_string(me.cvarValue));
66                         else
67                                 me.checked = (cvar_string(me.cvarName) == me.cvarValue);
68                 }
69         }
70         else
71         {
72                 if(me.cvarName)
73                 {
74                         me.checked = boolean(cvar(me.cvarName));
75                 }
76                 else
77                 {
78                         // this is difficult
79                         // this is the "generic" selection... but at this time, not
80                         // everything is constructed yet.
81                         // we need to set this later in draw()
82                         me.checked = 0;
83                 }
84         }
85 }
86 void XonoticRadioButton_draw(entity me)
87 {
88         if (!me.cvarValue)
89                 if (!me.cvarName)
90                 {
91                         // this is the "other" option
92                         // always select this if none other is
93                         entity e;
94                         float found;
95                         found = 0;
96                         for(e = me.parent.firstChild; e; e = e.nextSibling)
97                                 if(e.group == me.group)
98                                         if(e.checked)
99                                                 found = 1;
100                         if(!found)
101                                 me.setChecked(me, 1);
102                 }
103         SUPER(XonoticRadioButton).draw(me);
104 }
105 void XonoticRadioButton_saveCvars(entity me)
106 {
107         if(me.cvarValue)
108         {
109                 if(me.cvarName)
110                 {
111                         if(me.checked)
112                         {
113                                 if(me.cvarValueIsAnotherCvar)
114                                         cvar_set(me.cvarName, cvar_string(me.cvarValue));
115                                 else
116                                         cvar_set(me.cvarName, me.cvarValue);
117                         }
118                         else if(me.cvarOffValue)
119                                 cvar_set(me.cvarName, me.cvarOffValue);
120                 }
121         }
122         else
123         {
124                 if(me.cvarName)
125                 {
126                         cvar_set(me.cvarName, ftos(me.checked));
127                 }
128         }
129 }
130 #endif