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