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