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