]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/charmap.c
Remove obsolete charmap images
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / charmap.c
1 #ifdef INTERFACE
2 CLASS(XonoticCharmap) EXTENDS(Item)
3         METHOD(XonoticCharmap, configureXonoticCharmap, void(entity, entity))
4         METHOD(XonoticCharmap, mousePress, float(entity, vector))
5         METHOD(XonoticCharmap, mouseRelease, float(entity, vector))
6         METHOD(XonoticCharmap, mouseMove, float(entity, vector))
7         METHOD(XonoticCharmap, mouseDrag, float(entity, vector))
8         METHOD(XonoticCharmap, keyDown, float(entity, float, float, float))
9         METHOD(XonoticCharmap, focusLeave, void(entity))
10         METHOD(XonoticCharmap, resizeNotify, void(entity, vector, vector, vector, vector))
11         METHOD(XonoticCharmap, draw, void(entity))
12         ATTRIB(XonoticCharmap, focusable, float, 1)
13
14         METHOD(XonoticCharmap, moveFocus, void(entity, vector, vector))
15         METHOD(XonoticCharmap, enterChar, void(entity))
16         ATTRIB(XonoticCharmap, inputBox, entity, NULL)
17         ATTRIB(XonoticCharmap, realFontSize, vector, '0 0 0')
18         ATTRIB(XonoticCharmap, realCellSize, vector, '0 0 0')
19         ATTRIB(XonoticCharmap, focusedCell, vector, '-1 -1 0')
20         ATTRIB(XonoticCharmap, previouslyFocusedCell, vector, '-1 -1 0')
21 ENDCLASS(XonoticCharmap)
22 entity makeXonoticCharmap(entity controlledInputBox);
23 #endif
24
25 #ifdef IMPLEMENTATION
26
27 #define CHARMAP_COLS 14
28 #define CHARMAP_ROWS 10
29 string charmapRow0[CHARMAP_COLS];
30 string charmapRow1[CHARMAP_COLS];
31 string charmapRow2[CHARMAP_COLS];
32 string charmapRow3[CHARMAP_COLS];
33 string charmapRow4[CHARMAP_COLS];
34 string charmapRow5[CHARMAP_COLS];
35 string charmapRow6[CHARMAP_COLS];
36 string charmapRow7[CHARMAP_COLS];
37 string charmapRow8[CHARMAP_COLS];
38 string charmapRow9[CHARMAP_COLS];
39
40 void charmap_initializeRows()
41 {
42         if (charmapRow0[0] != "")
43                 return;
44
45         charmapRow0[0]  = strzone(chr(0x2190));
46         charmapRow0[1]  = strzone(chr(0x2191));
47         charmapRow0[2]  = strzone(chr(0x2192));
48         charmapRow0[3]  = strzone(chr(0x2193));
49         charmapRow0[4]  = strzone(chr(0x25c0));
50         charmapRow0[5]  = strzone(chr(0x25b2));
51         charmapRow0[6]  = strzone(chr(0x25b6));
52         charmapRow0[7]  = strzone(chr(0x25bc));
53         charmapRow0[8]  = strzone(chr(0x25e3));
54         charmapRow0[9]  = strzone(chr(0x25e4));
55         charmapRow0[10] = strzone(chr(0x25e5));
56         charmapRow0[11] = strzone(chr(0x25e2));
57         charmapRow0[12] = strzone(chr(0x2770));
58         charmapRow0[13] = strzone(chr(0x2771));
59
60         charmapRow1[0]  = strzone(chr(0x25A0));
61         charmapRow1[1]  = strzone(chr(0x25AC));
62         charmapRow1[2]  = strzone(chr(0x25AE));
63         charmapRow1[3]  = strzone(chr(0x25B0));
64         charmapRow1[4]  = strzone(chr(0x25C6));
65         charmapRow1[5]  = strzone(chr(0x25CF));
66         charmapRow1[6]  = strzone(chr(0x2605));
67         charmapRow1[7]  = strzone(chr(0x2764));
68         charmapRow1[8]  = strzone(chr(0x1F52B));
69         charmapRow1[9]  = strzone(chr(0x2316));
70         charmapRow1[10] = strzone(chr(0x2747));
71         charmapRow1[11] = strzone(chr(0x2748));
72         charmapRow1[12] = strzone(chr(0x2772));
73         charmapRow1[13] = strzone(chr(0x2773));
74
75         charmapRow2[0]  = strzone(chr(0x1F47D));
76         charmapRow2[1]  = strzone(chr(0x2620));
77         charmapRow2[2]  = strzone(chr(0x2622));
78         charmapRow2[3]  = strzone(chr(0x2623));
79         charmapRow2[4]  = strzone(chr(0x2699));
80         charmapRow2[5]  = strzone(chr(0x269B));
81         charmapRow2[6]  = strzone(chr(0x26A0));
82         charmapRow2[7]  = strzone(chr(0x26A1));
83         charmapRow2[8]  = strzone(chr(0x274C));
84         charmapRow2[9]  = strzone(chr(0x1F680));
85         charmapRow2[10] = strzone(chr(0x1F30C));
86         charmapRow2[11] = strzone(chr(0x1F30D));
87         charmapRow2[12] = strzone(chr(0x1F30E));
88         charmapRow2[13] = strzone(chr(0x1F30F));
89
90         charmapRow3[0]  = strzone(chr(0x1F601));
91         charmapRow3[1]  = strzone(chr(0x1F603));
92         charmapRow3[2]  = strzone(chr(0x1F604));
93         charmapRow3[3]  = strzone(chr(0x1F606));
94         charmapRow3[4]  = strzone(chr(0x1F607));
95         charmapRow3[5]  = strzone(chr(0x1F608));
96         charmapRow3[6]  = strzone(chr(0x1F609));
97         charmapRow3[7]  = strzone(chr(0x1F60A));
98         charmapRow3[8]  = strzone(chr(0x1F60E));
99         charmapRow3[9]  = strzone(chr(0x1F610));
100         charmapRow3[10] = strzone(chr(0x1F612));
101         charmapRow3[11] = strzone(chr(0x1F618));
102         charmapRow3[12] = string_null;
103         charmapRow3[13] = string_null;
104
105         charmapRow4[0]  = strzone(chr(0x1F615));
106         charmapRow4[1]  = strzone(chr(0x1F61B));
107         charmapRow4[2]  = strzone(chr(0x1F61D));
108         charmapRow4[3]  = strzone(chr(0x1F61E));
109         charmapRow4[4]  = strzone(chr(0x1F61F));
110         charmapRow4[5]  = strzone(chr(0x1F620));
111         charmapRow4[6]  = strzone(chr(0x1F623));
112         charmapRow4[7]  = strzone(chr(0x1F62D));
113         charmapRow4[8]  = strzone(chr(0x1F62E));
114         charmapRow4[9]  = strzone(chr(0x1F632));
115         charmapRow4[10] = strzone(chr(0x1F635));
116         charmapRow4[11] = strzone(chr(0x1F634));
117         charmapRow4[12] = string_null;
118         charmapRow4[13] = string_null;
119
120         // Legacy characters:
121
122         float i;
123         for(i = 0; i < 14; ++i)
124                 charmapRow5[i] = strzone(chr(0xE0E1 + i));
125
126         for(i = 0; i < 12; ++i)
127                 charmapRow6[i] = strzone(chr(0xE0EF + i));
128         charmapRow6[12] = strzone(chr(0xE010));
129         charmapRow6[13] = strzone(chr(0xE011));
130
131         for(i = 0; i < 10; ++i)
132                 charmapRow7[i] = strzone(chr(0xE0B0 + i));
133         charmapRow7[10] = strzone(chr(0xE0A1));
134         charmapRow7[11] = strzone(chr(0xE0BF));
135         charmapRow7[12] = strzone(chr(0xE0A6));
136         charmapRow7[13] = strzone(chr(0xE0A5));
137
138         for(i = 0; i < 14; ++i)
139                 charmapRow8[i] = strzone(chr(0xE0C1 + i));
140
141         for(i = 0; i < 12; ++i)
142                 charmapRow9[i] = strzone(chr(0xE0CF + i));
143         charmapRow9[12] = strzone(chr(0xE05B));
144         charmapRow9[13] = strzone(chr(0xE05D));
145 }
146
147 string charmap_cellToChar(vector cell)
148 {
149         if(!cvar("utf8_enable"))
150                 return "";
151
152         if(cell_x >= CHARMAP_COLS)
153                 return "";
154
155         switch(cell_y)
156         {
157                 case 0: return charmapRow0[cell_x];
158                 case 1: return charmapRow1[cell_x];
159                 case 2: return charmapRow2[cell_x];
160                 case 3: return charmapRow3[cell_x];
161                 case 4: return charmapRow4[cell_x];
162                 case 5: return charmapRow5[cell_x];
163                 case 6: return charmapRow6[cell_x];
164                 case 7: return charmapRow7[cell_x];
165                 case 8: return charmapRow8[cell_x];
166                 case 9: return charmapRow9[cell_x];
167         }
168         return "";
169 }
170
171 entity makeXonoticCharmap(entity controlledInputBox)
172 {
173         charmap_initializeRows();
174
175         entity me;
176         me = spawnXonoticCharmap();
177         me.configureXonoticCharmap(me, controlledInputBox);
178         return me;
179 }
180
181 void XonoticCharmap_configureXonoticCharmap(entity me, entity controlledInputBox)
182 {
183         me.inputBox = controlledInputBox;
184         me.realCellSize = eX / CHARMAP_COLS + eY / CHARMAP_ROWS;
185 }
186
187 void XonoticCharmap_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
188 {
189         SUPER(XonoticCharmap).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
190
191         if((me.realCellSize_x * absSize_x) > (me.realCellSize_y * absSize_y))
192         {
193                 me.realFontSize_x = me.realCellSize_y * absSize_y / absSize_x;
194                 me.realFontSize_y = me.realCellSize_y;
195         }
196         else
197         {
198                 me.realFontSize_x = me.realCellSize_x;
199                 me.realFontSize_y = me.realCellSize_x * absSize_x / absSize_y;
200         }
201 }
202
203 float XonoticCharmap_mouseMove(entity me, vector coords)
204 {
205         me.focusedCell_x = floor(coords_x * CHARMAP_COLS);
206         me.focusedCell_y = floor(coords_y * CHARMAP_ROWS);
207
208         if(me.focusedCell_x < 0 || me.focusedCell_y < 0 ||
209            me.focusedCell_x >= CHARMAP_COLS || me.focusedCell_y >= CHARMAP_ROWS)
210         {
211                 me.focusedCell = '-1 -1 0';
212                 return 0;
213         }
214
215         return 1;
216 }
217
218 float XonoticCharmap_mouseDrag(entity me, vector coords)
219 {
220         return me.mouseMove(me, coords);
221 }
222
223 float XonoticCharmap_mousePress(entity me, vector coords)
224 {
225         me.mouseMove(me, coords);
226
227         if(me.focusedCell_x >= 0)
228         {
229                 me.pressed = 1;
230                 me.previouslyFocusedCell = me.focusedCell;
231         }
232
233         return 1;
234 }
235
236 float XonoticCharmap_mouseRelease(entity me, vector coords)
237 {
238         if(!me.pressed)
239                 return 0;
240
241         me.mouseMove(me, coords);
242
243         if(me.focusedCell == me.previouslyFocusedCell)
244                 me.enterChar(me);
245
246         me.pressed = 0;
247         return 1;
248 }
249
250 float XonoticCharmap_keyDown(entity me, float key, float ascii, float shift)
251 {
252         switch(key)
253         {
254                 case K_LEFTARROW:
255                 case K_KP_LEFTARROW:
256                         me.moveFocus(me, me.focusedCell, '-1 0 0');
257                         return 1;
258                 case K_RIGHTARROW:
259                 case K_KP_RIGHTARROW:
260                         me.moveFocus(me, me.focusedCell, '1 0 0');
261                         return 1;
262                 case K_UPARROW:
263                 case K_KP_UPARROW:
264                         me.moveFocus(me, me.focusedCell, '0 -1 0');
265                         return 1;
266                 case K_DOWNARROW:
267                 case K_KP_DOWNARROW:
268                         me.moveFocus(me, me.focusedCell, '0 1 0');
269                         return 1;
270                 case K_HOME:
271                 case K_KP_HOME:
272                         me.focusedCell = '0 0 0';
273                         return 1;
274                 case K_END:
275                 case K_KP_END:
276                         me.focusedCell_x = CHARMAP_COLS - 1;
277                         me.focusedCell_y = CHARMAP_ROWS - 1;
278                         return 1;
279                 case K_ENTER:
280                 case K_KP_ENTER:
281                 case K_INS:
282                 case K_KP_INS:
283                         me.enterChar(me);
284                         return 1;
285                 default:
286                         return me.inputBox.keyDown(me.inputBox, key, ascii, shift);
287         }
288 }
289
290 void XonoticCharmap_moveFocus(entity me, vector initialCell, vector step)
291 {
292         me.focusedCell_x = mod(me.focusedCell_x + step_x + CHARMAP_COLS, CHARMAP_COLS);
293         me.focusedCell_y = mod(me.focusedCell_y + step_y + CHARMAP_ROWS, CHARMAP_ROWS);
294
295         if(me.focusedCell != initialCell) // Recursion break
296                 if(charmap_cellToChar(me.focusedCell) == "")
297                         me.moveFocus(me, initialCell, step);
298 }
299
300 void XonoticCharmap_enterChar(entity me)
301 {
302         string character = charmap_cellToChar(me.focusedCell);
303         if(character != "")
304                 me.inputBox.enterText(me.inputBox, character);
305 }
306
307 void XonoticCharmap_focusLeave(entity me)
308 {
309         me.inputBox.saveCvars(me.inputBox);
310 }
311
312 void XonoticCharmap_draw(entity me)
313 {
314         string character;
315         vector cell, cellPos, charPos;
316         cell = '0 0 0';
317         cellPos = '0 0 0';
318         charPos = '0 0 0';
319
320         for(cell_y = 0; cell_y < CHARMAP_ROWS; ++cell_y)
321         {
322                 charPos_y = cell_y / CHARMAP_ROWS + (me.realCellSize_y - me.realFontSize_y) / 2;
323                 for(cell_x = 0; cell_x < CHARMAP_COLS; ++cell_x)
324                 {
325                         character = charmap_cellToChar(cell);
326
327                         if(character == "")
328                                 continue;
329
330                         // Draw focused cell
331                         if(cell == me.focusedCell && me.focused)
332                         {
333                                 if(!me.pressed || me.focusedCell == me.previouslyFocusedCell)
334                                 {
335                                         cellPos_x = mod(me.focusedCell_x, CHARMAP_COLS) / CHARMAP_COLS;
336                                         cellPos_y = mod(me.focusedCell_y, CHARMAP_ROWS) / CHARMAP_ROWS;
337                                         draw_Fill(cellPos, me.realCellSize, SKINCOLOR_CHARMAP_FOCUS, SKINALPHA_CHARMAP_FOCUS);
338                                 }
339                         }
340
341                         // Draw character
342                         charPos_x = cell_x / CHARMAP_COLS + me.realCellSize_x / 2;
343                         draw_CenterText(charPos, character, me.realFontSize, SKINCOLOR_CHARMAP_CHAR, SKINALPHA_CHARMAP_CHAR, 0);
344                 }
345         }
346 }
347 #endif