]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/radiobutton.qc
Merge branch 'master' into Mario/vaporizer_damage
[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));
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(float, string, string, string);
26 #endif
27
28 #ifdef IMPLEMENTATION
29 entity makeXonoticRadioButton(float theGroup, string theCvar, string theValue, string theText)
30 {
31         entity me;
32         me = NEW(XonoticRadioButton);
33         me.configureXonoticRadioButton(me, theGroup, theCvar, theValue, theText);
34         return me;
35 }
36 void XonoticRadioButton_configureXonoticRadioButton(entity me, float theGroup, string theCvar, string theValue, string theText)
37 {
38         if(theCvar)
39         {
40                 me.cvarName = theCvar;
41                 me.cvarValue = theValue;
42                 me.tooltip = getZonedTooltipForIdentifier(theCvar);
43                 me.loadCvars(me);
44         }
45         me.configureRadioButton(me, theText, me.fontSize, me.image, theGroup, 0);
46 }
47 void XonoticRadioButton_setChecked(entity me, float val)
48 {
49         if(val != me.checked)
50         {
51                 me.checked = val;
52                 me.saveCvars(me);
53         }
54 }
55 void XonoticRadioButton_loadCvars(entity me)
56 {
57         if(me.cvarValue)
58         {
59                 if(me.cvarName)
60                 {
61                         if(me.cvarValueIsAnotherCvar)
62                                 me.checked = (cvar_string(me.cvarName) == cvar_string(me.cvarValue));
63                         else
64                                 me.checked = (cvar_string(me.cvarName) == me.cvarValue);
65                 }
66         }
67         else
68         {
69                 if(me.cvarName)
70                 {
71                         me.checked = !!cvar(me.cvarName);
72                 }
73                 else
74                 {
75                         // this is difficult
76                         // this is the "generic" selection... but at this time, not
77                         // everything is constructed yet.
78                         // we need to set this later in draw()
79                         me.checked = 0;
80                 }
81         }
82 }
83 void XonoticRadioButton_draw(entity me)
84 {
85         if (!me.cvarValue)
86                 if (!me.cvarName)
87                 {
88                         // this is the "other" option
89                         // always select this if none other is
90                         entity e;
91                         float found;
92                         found = 0;
93                         for(e = me.parent.firstChild; e; e = e.nextSibling)
94                                 if(e.group == me.group)
95                                         if(e.checked)
96                                                 found = 1;
97                         if(!found)
98                                 me.setChecked(me, 1);
99                 }
100         SUPER(XonoticRadioButton).draw(me);
101 }
102 void XonoticRadioButton_saveCvars(entity me)
103 {
104         if(me.cvarValue)
105         {
106                 if(me.cvarName)
107                 {
108                         if(me.checked)
109                         {
110                                 if(me.cvarValueIsAnotherCvar)
111                                         cvar_set(me.cvarName, cvar_string(me.cvarValue));
112                                 else
113                                         cvar_set(me.cvarName, me.cvarValue);
114                         }
115                         else if(me.cvarOffValue)
116                                 cvar_set(me.cvarName, me.cvarOffValue);
117                 }
118         }
119         else
120         {
121                 if(me.cvarName)
122                 {
123                         cvar_set(me.cvarName, ftos(me.checked));
124                 }
125         }
126 }
127 #endif