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