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