]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/crosshairbutton.c
Use only one cvar to define the crosshair color and add the color picker in the menu...
[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         me.src3 = strzone(strcat("/gfx/crosshair", ftos(me.cvarValueFloat)));
37         me.src4 = "/gfx/crosshairdot";
38 }
39 void XonoticCrosshairButton_setChecked(entity me, float val)
40 {
41         if(val != me.checked)
42         {
43                 me.checked = val;
44                 me.saveCvars(me);
45         }
46 }
47 void XonoticCrosshairButton_loadCvars(entity me)
48 {
49         if not(me.cvarName)
50                 return;
51
52         me.checked = (cvar(me.cvarName) == me.cvarValueFloat);
53 }
54 void XonoticCrosshairButton_saveCvars(entity me)
55 {
56         if not(me.cvarName)
57                 return;
58
59         if(me.checked)
60                 cvar_set(me.cvarName, ftos(me.cvarValueFloat));
61         // TODO on an apply button, read _cl_color and execute the color command for it
62 }
63 void XonoticCrosshairButton_draw(entity me)
64 {
65         vector sz, rgb;
66         float a;
67
68         rgb = stov(cvar_string("crosshair_color"));
69         a = cvar("crosshair_alpha");
70
71         if(!me.checked && !me.focused)
72         {
73                 a *= me.disabledAlpha;
74                 rgb = '1 1 1';
75         }
76
77         SUPER(XonoticCrosshairButton).draw(me);
78
79         sz = draw_PictureSize(me.src3);
80         sz = globalToBoxSize(sz, draw_scale);
81         sz = (10 * '1 1 0' + sz * cvar("crosshair_size")) * 0.05; // (10 * '1 1 0' + ...) * 0.05 here to make visible size changes happen also at bigger sizes
82         if(sz_x > 0.95)
83                 sz = sz * (0.95 / sz_x);
84         if(sz_y > 0.95)
85                 sz = sz * (0.95 / sz_y);
86
87         draw_Picture('0.5 0.5 0' - 0.5 * sz, me.src3, sz, rgb, a);
88         if(cvar("crosshair_dot"))
89                 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"));
90 }
91 #endif