]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/crosshairbutton.qc
Sort menu classes
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / crosshairbutton.qc
1 #ifndef CROSSHAIRBUTTON_H
2 #define CROSSHAIRBUTTON_H
3 CLASS(XonoticCrosshairButton, RadioButton)
4         METHOD(XonoticCrosshairButton, configureXonoticCrosshairButton, void(entity, float, float))
5         METHOD(XonoticCrosshairButton, setChecked, void(entity, float))
6         METHOD(XonoticCrosshairButton, draw, void(entity))
7         ATTRIB(XonoticCrosshairButton, fontSize, float, SKINFONTSIZE_NORMAL)
8         ATTRIB(XonoticCrosshairButton, image, string, SKINGFX_CROSSHAIRBUTTON)
9
10         ATTRIB(XonoticCrosshairButton, useDownAsChecked, float, 1)
11         ATTRIB(XonoticCrosshairButton, src3, string, string_null)
12         ATTRIB(XonoticCrosshairButton, src4, string, string_null)
13
14         ATTRIB(XonoticCrosshairButton, cvarName, string, string_null)
15         ATTRIB(XonoticCrosshairButton, cvarValueFloat, float, 0)
16         METHOD(XonoticCrosshairButton, loadCvars, void(entity))
17         METHOD(XonoticCrosshairButton, saveCvars, void(entity))
18 ENDCLASS(XonoticCrosshairButton)
19 entity makeXonoticCrosshairButton(float, float);
20 #endif
21
22 #ifdef IMPLEMENTATION
23 entity makeXonoticCrosshairButton(float theGroup, float theCrosshair)
24 {
25         entity me;
26         me = NEW(XonoticCrosshairButton);
27         me.configureXonoticCrosshairButton(me, theGroup, theCrosshair);
28         return me;
29 }
30 void XonoticCrosshairButton_configureXonoticCrosshairButton(entity me, float theGroup, float theCrosshair)
31 {
32         me.cvarName = "crosshair";
33         me.cvarValueFloat = theCrosshair;
34         me.loadCvars(me);
35         me.configureRadioButton(me, string_null, me.fontSize, me.image, theGroup, 0);
36         me.srcMulti = 1;
37         if(me.cvarValueFloat == -1)
38                 me.src3 = strzone(strcat("/gfx/crosshair", cvar_string("crosshair")));
39         else
40                 me.src3 = strzone(strcat("/gfx/crosshair", ftos(me.cvarValueFloat)));
41         me.src4 = "/gfx/crosshairdot";
42 }
43 void XonoticCrosshairButton_setChecked(entity me, float val)
44 {
45         if(me.cvarValueFloat != -1) // preview shouldn't work as a button
46         if(val != me.checked)
47         {
48                 me.checked = val;
49                 me.saveCvars(me);
50         }
51 }
52 void XonoticCrosshairButton_loadCvars(entity me)
53 {
54         if (!me.cvarName)
55                 return;
56
57         me.checked = (cvar(me.cvarName) == me.cvarValueFloat);
58 }
59 void XonoticCrosshairButton_saveCvars(entity me)
60 {
61         if (!me.cvarName)
62                 return;
63
64         if(me.checked)
65                 cvar_set(me.cvarName, ftos(me.cvarValueFloat));
66         // TODO on an apply button, read _cl_color and execute the color command for it
67 }
68 void XonoticCrosshairButton_draw(entity me)
69 {
70         vector sz, rgb;
71         float a;
72
73
74         if(me.cvarValueFloat == -1)
75         {
76                 rgb = stov(cvar_string("crosshair_color"));
77                 a = cvar("crosshair_alpha");
78         }
79         else if(me.checked || me.focused)
80         {
81                 a = 1;
82                 rgb = '1 1 1';
83         }
84         else
85         {
86                 a = me.disabledAlpha;
87                 rgb = '1 1 1';
88         }
89
90         if(me.cvarValueFloat == -1) // update the preview if this is the preview button
91         {
92                 if(me.src3)
93                         strunzone(me.src3);
94                 me.src3 = strzone(strcat("/gfx/crosshair", cvar_string("crosshair")));
95                 me.focused = 1;
96                 me.checked = 0;
97         }
98
99         SUPER(XonoticCrosshairButton).draw(me);
100
101         sz = draw_PictureSize(me.src3);
102         sz = globalToBoxSize(sz, me.size);
103         if(me.cvarValueFloat == -1)
104         {
105                 sz = sz * cvar("crosshair_size"); // (6 * '1 1 0' + ...) * 0.08 here to make visible size changes happen also at bigger sizes
106                 /*
107                 if(sz_x > 0.95)
108                         sz = sz * (0.95 / sz_x);
109                 if(sz_y > 0.95)
110                         sz = sz * (0.95 / sz_y);
111                 */
112         }
113         else // show the crosshair picker at full size
114         {
115                 sz = sz * (0.95 / sz.x);
116                 if(sz.y > 0.95)
117                         sz = sz * (0.95 / sz.y);
118         }
119
120         draw_Picture('0.5 0.5 0' - 0.5 * sz, me.src3, sz, rgb, a);
121         if(cvar("crosshair_dot"))
122         {
123                 if(cvar("crosshair_dot_color_custom") && (cvar_string("crosshair_dot_color") != "0"))
124                         rgb = stov(cvar_string("crosshair_dot_color"));
125
126                 draw_Picture('0.5 0.5 0' - 0.5 * sz * cvar("crosshair_dot_size"), me.src4, sz * cvar("crosshair_dot_size"), rgb, a * cvar("crosshair_dot_alpha"));
127         }
128 }
129 #endif