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