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