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