]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/picker.qc
Fix crosshair picker indentation
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / picker.qc
1 #ifdef INTERFACE
2 CLASS(XonoticPicker) EXTENDS(Item)
3         METHOD(XonoticPicker, configureXonoticPicker, void(entity))
4         METHOD(XonoticPicker, mousePress, float(entity, vector))
5         METHOD(XonoticPicker, mouseRelease, float(entity, vector))
6         METHOD(XonoticPicker, mouseMove, float(entity, vector))
7         METHOD(XonoticPicker, mouseDrag, float(entity, vector))
8         METHOD(XonoticPicker, keyDown, float(entity, float, float, float))
9         METHOD(XonoticPicker, draw, void(entity))
10         ATTRIB(XonoticPicker, focusable, float, 1)
11         ATTRIB(XonoticPicker, disabled, float, 0)
12         ATTRIB(XonoticPicker, alpha, float, 1)
13         ATTRIB(XonoticPicker, disabledAlpha, float, SKINALPHA_DISABLED)
14
15         ATTRIB(XonoticPicker, rows, float, 3)
16         ATTRIB(XonoticPicker, columns, float, 2)
17
18         METHOD(XonoticPicker, moveFocus, void(entity, vector, vector))
19         METHOD(XonoticPicker, cellSelect, void(entity, vector))
20         METHOD(XonoticPicker, cellDraw, void(entity, vector, vector))
21         METHOD(XonoticPicker, cellIsValid, bool(entity, vector))
22         ATTRIB(XonoticPicker, realCellSize, vector, '0 0 0')
23         ATTRIB(XonoticPicker, selectedCell, vector, '-1 -1 0')
24         ATTRIB(XonoticPicker, focusedCell, vector, '-1 -1 0')
25         ATTRIB(XonoticPicker, focusedCellAlpha, float, 0)
26         ATTRIB(XonoticPicker, focusedCellTime, float, 0)
27         ATTRIB(XonoticPicker, pressedCell, vector, '-1 -1 0')
28 ENDCLASS(XonoticPicker)
29 entity makeXonoticPicker();
30 #endif
31
32 #ifdef IMPLEMENTATION
33
34 entity makeXonoticPicker()
35 {
36         entity me;
37         me = spawnXonoticPicker();
38         me.configureXonoticPicker(me);
39         return me;
40 }
41
42 void XonoticPicker_configureXonoticPicker(entity me)
43 {
44         me.realCellSize = eX / me.columns + eY / me.rows;
45 }
46
47 float XonoticPicker_mouseMove(entity me, vector coords)
48 {
49         vector prevFocusedCell = me.focusedCell;
50         me.focusedCell_x = floor(coords.x * me.columns);
51         me.focusedCell_y = floor(coords.y * me.rows);
52
53         if(me.focusedCell.x < 0 || me.focusedCell.y < 0 ||
54            me.focusedCell.x >= me.columns || me.focusedCell.y >= me.rows)
55         {
56                 me.focusedCell = '-1 -1 0';
57                 return 0;
58         }
59
60         if(me.focusedCell != prevFocusedCell)
61                 me.focusedCellAlpha = SKINALPHA_LISTBOX_FOCUSED;
62
63         return 1;
64 }
65
66 float XonoticPicker_mouseDrag(entity me, vector coords)
67 {
68         return me.mouseMove(me, coords);
69 }
70
71 float XonoticPicker_mousePress(entity me, vector coords)
72 {
73         me.mouseMove(me, coords);
74
75         if(me.focusedCell.x >= 0)
76         {
77                 me.pressed = 1;
78                 me.pressedCell = me.focusedCell;
79         }
80
81         return 1;
82 }
83
84 float XonoticPicker_mouseRelease(entity me, vector coords)
85 {
86         if(!me.pressed)
87                 return 0;
88
89         me.mouseMove(me, coords);
90
91         if(me.focusedCell == me.pressedCell)
92                 me.cellSelect(me, me.focusedCell);
93
94         me.pressed = 0;
95         return 1;
96 }
97
98 float XonoticPicker_keyDown(entity me, float key, float ascii, float shift)
99 {
100         switch(key)
101         {
102                 case K_LEFTARROW:
103                 case K_KP_LEFTARROW:
104                         me.moveFocus(me, me.focusedCell, '-1 0 0');
105                         return 1;
106                 case K_RIGHTARROW:
107                 case K_KP_RIGHTARROW:
108                         me.moveFocus(me, me.focusedCell, '1 0 0');
109                         return 1;
110                 case K_UPARROW:
111                 case K_KP_UPARROW:
112                         me.moveFocus(me, me.focusedCell, '0 -1 0');
113                         return 1;
114                 case K_DOWNARROW:
115                 case K_KP_DOWNARROW:
116                         me.moveFocus(me, me.focusedCell, '0 1 0');
117                         return 1;
118                 case K_HOME:
119                 case K_KP_HOME:
120                         me.focusedCell = '0 0 0';
121                         return 1;
122                 case K_END:
123                 case K_KP_END:
124                         me.focusedCell_x = me.columns - 1;
125                         me.focusedCell_y = me.rows - 1;
126                         return 1;
127                 case K_ENTER:
128                 case K_KP_ENTER:
129                 case K_INS:
130                 case K_KP_INS:
131                         me.cellSelect(me, me.focusedCell);
132                         return 1;
133         }
134         return 0;
135 }
136
137 void XonoticPicker_moveFocus(entity me, vector initialCell, vector step)
138 {
139         me.focusedCell_x = mod(me.focusedCell.x + step.x + me.columns, me.columns);
140         me.focusedCell_y = mod(me.focusedCell.y + step.y + me.rows, me.rows);
141
142         if(me.focusedCell != initialCell) // Recursion break
143                 if(!me.cellIsValid(me, me.focusedCell))
144                         me.moveFocus(me, initialCell, step);
145 }
146
147 void XonoticPicker_cellSelect(entity me, vector cell)
148 {
149         me.selectedCell = cell;
150 }
151
152 bool XonoticPicker_cellIsValid(entity me, vector cell)
153 {
154         return true;
155 }
156
157 void XonoticPicker_cellDraw(entity me, vector cell, vector cellPos)
158 {
159 }
160
161 void XonoticPicker_draw(entity me)
162 {
163         float save;
164
165         me.focusable = !me.disabled;
166
167         save = draw_alpha;
168         if(me.disabled)
169                 draw_alpha *= me.disabledAlpha;
170
171         vector cell, cellPos;
172         cell = '0 0 0';
173         cellPos = '0 0 0';
174
175         for(cell_y = 0; cell.y < me.rows; ++cell.y)
176         {
177                 cellPos_y = mod(cell.y, me.rows) / me.rows;
178                 for(cell_x = 0; cell.x < me.columns; ++cell.x)
179                 {
180                         if(!me.cellIsValid(me, cell))
181                                 continue;
182
183                         cellPos_x = mod(cell.x, me.columns) / me.columns;
184
185                         if(cell == me.selectedCell)
186                                 draw_Fill(cellPos, me.realCellSize, SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
187                         else if(cell == me.focusedCell && me.focused)
188                         {
189                                 if(!me.pressed || me.focusedCell == me.pressedCell)
190                                 {
191                                         me.focusedCellAlpha = getFadedAlpha(me.focusedCellAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
192                                         draw_Fill(cellPos, me.realCellSize, SKINCOLOR_LISTBOX_FOCUSED, me.focusedCellAlpha);
193                                 }
194                         }
195
196                         me.cellDraw(me, cell, cellPos);
197                 }
198         }
199
200         draw_alpha = save;
201
202         SUPER(XonoticPicker).draw(me);
203 }
204 #endif