Merge branch 'master' into Mario/vaporizer_damage
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / crosshairpicker.qc
1 #ifndef CROSSHAIRPICKER_H
2 #define CROSSHAIRPICKER_H
3 #include "picker.qc"
4 CLASS(XonoticCrosshairPicker, XonoticPicker)
5         METHOD(XonoticCrosshairPicker, configureXonoticCrosshairPicker, void(entity));
6
7         ATTRIB(XonoticCrosshairPicker, rows, float, 3)
8         ATTRIB(XonoticCrosshairPicker, columns, float, 12)
9
10         METHOD(XonoticCrosshairPicker, cellSelect, void(entity, vector));
11         METHOD(XonoticCrosshairPicker, cellIsValid, bool(entity, vector));
12         METHOD(XonoticCrosshairPicker, cellDraw, void(entity, vector, vector));
13 ENDCLASS(XonoticCrosshairPicker)
14 entity makeXonoticCrosshairPicker();
15 #endif
16
17 #ifdef IMPLEMENTATION
18
19 string crosshairpicker_cellToCrosshair(entity me, vector cell)
20 {
21         if(cell.x < 0 || cell.x >= me.columns || cell.y < 0 || cell.y >= me.rows)
22                 return "";
23         return ftos(31 + cell.y * me.columns + cell.x);
24 }
25
26 vector crosshairpicker_crosshairToCell(entity me, string crosshair_str)
27 {
28         float crosshair = stof(crosshair_str) - 31;
29         if(crosshair - floor(crosshair) > 0)
30                 return '-1 -1 0';
31         return mod(crosshair, me.columns) * eX + floor(crosshair / me.columns) * eY;
32 }
33
34 entity makeXonoticCrosshairPicker()
35 {
36         entity me;
37         me = NEW(XonoticCrosshairPicker);
38         me.configureXonoticCrosshairPicker(me);
39         return me;
40 }
41
42 void XonoticCrosshairPicker_configureXonoticCrosshairPicker(entity me)
43 {
44         me.configureXonoticPicker(me);
45         SUPER(XonoticCrosshairPicker).cellSelect(me, crosshairpicker_crosshairToCell(me, cvar_string("crosshair")));
46 }
47
48 void XonoticCrosshairPicker_cellSelect(entity me, vector cell)
49 {
50         cvar_set("crosshair", crosshairpicker_cellToCrosshair(me, me.focusedCell));
51         SUPER(XonoticCrosshairPicker).cellSelect(me, me.focusedCell);
52 }
53
54 bool XonoticCrosshairPicker_cellIsValid(entity me, vector cell)
55 {
56         if(crosshairpicker_cellToCrosshair(me, cell) == "")
57                 return false;
58         return true;
59 }
60
61 void XonoticCrosshairPicker_cellDraw(entity me, vector cell, vector cellPos)
62 {
63         vector sz;
64         string cross = strcat("/gfx/crosshair", crosshairpicker_cellToCrosshair(me, cell));
65         sz = draw_PictureSize(cross);
66         sz = globalToBoxSize(sz, me.size);
67
68         float ar = sz.x / sz.y;
69         sz.x = me.realCellSize.x;
70         sz.y = sz.x / ar;
71         sz = sz * 0.95;
72
73         vector crosshairPos = cellPos + 0.5 * me.realCellSize;
74         draw_Picture(crosshairPos - 0.5 * sz, cross, sz, SKINCOLOR_CROSSHAIRPICKER_CROSSHAIR, SKINALPHA_CROSSHAIRPICKER_CROSSHAIR);
75
76         if(cvar("crosshair_dot"))
77                 draw_Picture(crosshairPos - 0.5 * sz * cvar("crosshair_dot_size"), "/gfx/crosshairdot", sz * cvar("crosshair_dot_size"), SKINCOLOR_CROSSHAIRPICKER_CROSSHAIR, SKINALPHA_CROSSHAIRPICKER_CROSSHAIR);
78 }
79 #endif