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