]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/charmap.qc
#include base classes
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / charmap.qc
1 #ifndef CHARMAP_H
2 #define CHARMAP_H
3 #include "../item.qc"
4 CLASS(XonoticCharmap, Item)
5         METHOD(XonoticCharmap, configureXonoticCharmap, void(entity, entity))
6         METHOD(XonoticCharmap, mousePress, float(entity, vector))
7         METHOD(XonoticCharmap, mouseRelease, float(entity, vector))
8         METHOD(XonoticCharmap, mouseMove, float(entity, vector))
9         METHOD(XonoticCharmap, mouseDrag, float(entity, vector))
10         METHOD(XonoticCharmap, keyDown, float(entity, float, float, float))
11         METHOD(XonoticCharmap, focusLeave, void(entity))
12         METHOD(XonoticCharmap, resizeNotify, void(entity, vector, vector, vector, vector))
13         METHOD(XonoticCharmap, draw, void(entity))
14         ATTRIB(XonoticCharmap, focusable, float, 1)
15
16         METHOD(XonoticCharmap, moveFocus, void(entity, vector, vector))
17         METHOD(XonoticCharmap, enterChar, void(entity))
18         ATTRIB(XonoticCharmap, inputBox, entity, NULL)
19         ATTRIB(XonoticCharmap, realFontSize, vector, '0 0 0')
20         ATTRIB(XonoticCharmap, realCellSize, vector, '0 0 0')
21         ATTRIB(XonoticCharmap, focusedCell, vector, '-1 -1 0')
22         ATTRIB(XonoticCharmap, previouslyFocusedCell, vector, '-1 -1 0')
23 ENDCLASS(XonoticCharmap)
24 entity makeXonoticCharmap(entity controlledInputBox);
25 #endif
26
27 #ifdef IMPLEMENTATION
28
29 const float CHARMAP_COLS = 14;
30 const float CHARMAP_ROWS = 10;
31
32 string CHARMAP =
33         "★◆■▮▰▬◣◤◥◢◀▲▶▼"
34         "🌍🌎🌏🚀🌌👽🔫⌖❇❈←↑→↓"
35         "☠☣☢⚛⚡⚙🔥❌⚠⛔❰❱❲❳"
36         "😃😊😁😄😆😎😈😇😉😛😝😘❤ "
37         "😐😒😕😮😲😞😟😠😣😭😵😴  "
38         "\xEE\x83\xA1\xEE\x83\xA2\xEE\x83\xA3\xEE\x83\xA4\xEE\x83\xA5\xEE\x83\xA6\xEE\x83\xA7"
39         "\xEE\x83\xA8\xEE\x83\xA9\xEE\x83\xAA\xEE\x83\xAB\xEE\x83\xAC\xEE\x83\xAD\xEE\x83\xAE"
40         "\xEE\x83\xAF\xEE\x83\xB0\xEE\x83\xB1\xEE\x83\xB2\xEE\x83\xB3\xEE\x83\xB4\xEE\x83\xB5"
41         "\xEE\x83\xB6\xEE\x83\xB7\xEE\x83\xB8\xEE\x83\xB9\xEE\x83\xBA\xEE\x80\x90\xEE\x80\x91"
42         "\xEE\x82\xB0\xEE\x82\xB1\xEE\x82\xB2\xEE\x82\xB3\xEE\x82\xB4\xEE\x82\xB5\xEE\x82\xB6"
43         "\xEE\x82\xB7\xEE\x82\xB8\xEE\x82\xB9\xEE\x82\xA1\xEE\x82\xBF\xEE\x82\xA6\xEE\x82\xA5"
44         "\xEE\x83\x81\xEE\x83\x82\xEE\x83\x83\xEE\x83\x84\xEE\x83\x85\xEE\x83\x86\xEE\x83\x87"
45         "\xEE\x83\x88\xEE\x83\x89\xEE\x83\x8A\xEE\x83\x8B\xEE\x83\x8C\xEE\x83\x8D\xEE\x83\x8E"
46         "\xEE\x83\x8F\xEE\x83\x90\xEE\x83\x91\xEE\x83\x92\xEE\x83\x93\xEE\x83\x94\xEE\x83\x95"
47         "\xEE\x83\x96\xEE\x83\x97\xEE\x83\x98\xEE\x83\x99\xEE\x83\x9A\xEE\x81\x9B\xEE\x81\x9D";
48
49 string charmap_cellToChar(vector cell)
50 {
51         string character = substring(CHARMAP, cell.y * CHARMAP_COLS + cell.x, 1);
52
53         if (character != " ")
54                 return character;
55         else
56                 return "";
57 }
58
59 entity makeXonoticCharmap(entity controlledInputBox)
60 {
61         entity me;
62         me = NEW(XonoticCharmap);
63         me.configureXonoticCharmap(me, controlledInputBox);
64         return me;
65 }
66
67 void XonoticCharmap_configureXonoticCharmap(entity me, entity controlledInputBox)
68 {
69         me.inputBox = controlledInputBox;
70         me.realCellSize = eX / CHARMAP_COLS + eY / CHARMAP_ROWS;
71 }
72
73 void XonoticCharmap_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
74 {
75         SUPER(XonoticCharmap).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
76
77         float maxFontWidth = SKINFONTSIZE_NORMAL / absSize.x;
78         float maxFontHeight = SKINFONTSIZE_NORMAL / absSize.y;
79
80         if((me.realCellSize.x * absSize.x) > (me.realCellSize.y * absSize.y))
81         {
82                 me.realFontSize_x = me.realCellSize.y * absSize.y / absSize.x;
83                 me.realFontSize_y = me.realCellSize.y;
84         }
85         else
86         {
87                 me.realFontSize_x = me.realCellSize.x;
88                 me.realFontSize_y = me.realCellSize.x * absSize.x / absSize.y;
89         }
90
91         if(me.realFontSize.x > maxFontWidth || me.realFontSize.y > maxFontHeight)
92                 me.realFontSize = eX * maxFontWidth + eY * maxFontHeight;
93 }
94
95 float XonoticCharmap_mouseMove(entity me, vector coords)
96 {
97         me.focusedCell_x = floor(coords.x * CHARMAP_COLS);
98         me.focusedCell_y = floor(coords.y * CHARMAP_ROWS);
99
100         if(me.focusedCell.x < 0 || me.focusedCell.y < 0 ||
101            me.focusedCell.x >= CHARMAP_COLS || me.focusedCell.y >= CHARMAP_ROWS)
102         {
103                 me.focusedCell = '-1 -1 0';
104                 return 0;
105         }
106
107         return 1;
108 }
109
110 float XonoticCharmap_mouseDrag(entity me, vector coords)
111 {
112         return me.mouseMove(me, coords);
113 }
114
115 float XonoticCharmap_mousePress(entity me, vector coords)
116 {
117         me.mouseMove(me, coords);
118
119         if(me.focusedCell.x >= 0)
120         {
121                 me.pressed = 1;
122                 me.previouslyFocusedCell = me.focusedCell;
123         }
124
125         return 1;
126 }
127
128 float XonoticCharmap_mouseRelease(entity me, vector coords)
129 {
130         if(!me.pressed)
131                 return 0;
132
133         me.mouseMove(me, coords);
134
135         if(me.focusedCell == me.previouslyFocusedCell)
136                 me.enterChar(me);
137
138         me.pressed = 0;
139         return 1;
140 }
141
142 float XonoticCharmap_keyDown(entity me, float key, float ascii, float shift)
143 {
144         switch(key)
145         {
146                 case K_LEFTARROW:
147                 case K_KP_LEFTARROW:
148                         me.moveFocus(me, me.focusedCell, '-1 0 0');
149                         return 1;
150                 case K_RIGHTARROW:
151                 case K_KP_RIGHTARROW:
152                         me.moveFocus(me, me.focusedCell, '1 0 0');
153                         return 1;
154                 case K_UPARROW:
155                 case K_KP_UPARROW:
156                         me.moveFocus(me, me.focusedCell, '0 -1 0');
157                         return 1;
158                 case K_DOWNARROW:
159                 case K_KP_DOWNARROW:
160                         me.moveFocus(me, me.focusedCell, '0 1 0');
161                         return 1;
162                 case K_HOME:
163                 case K_KP_HOME:
164                         me.focusedCell = '0 0 0';
165                         return 1;
166                 case K_END:
167                 case K_KP_END:
168                         me.focusedCell_x = CHARMAP_COLS - 1;
169                         me.focusedCell_y = CHARMAP_ROWS - 1;
170                         return 1;
171                 case K_ENTER:
172                 case K_KP_ENTER:
173                 case K_INS:
174                 case K_KP_INS:
175                         me.enterChar(me);
176                         return 1;
177                 default:
178                         return me.inputBox.keyDown(me.inputBox, key, ascii, shift);
179         }
180 }
181
182 void XonoticCharmap_moveFocus(entity me, vector initialCell, vector step)
183 {
184         me.focusedCell_x = mod(me.focusedCell.x + step.x + CHARMAP_COLS, CHARMAP_COLS);
185         me.focusedCell_y = mod(me.focusedCell.y + step.y + CHARMAP_ROWS, CHARMAP_ROWS);
186
187         if(me.focusedCell != initialCell) // Recursion break
188                 if(charmap_cellToChar(me.focusedCell) == "")
189                         me.moveFocus(me, initialCell, step);
190 }
191
192 void XonoticCharmap_enterChar(entity me)
193 {
194         string character = charmap_cellToChar(me.focusedCell);
195         if(character != "")
196                 me.inputBox.enterText(me.inputBox, character);
197 }
198
199 void XonoticCharmap_focusLeave(entity me)
200 {
201         me.inputBox.saveCvars(me.inputBox);
202 }
203
204 void XonoticCharmap_draw(entity me)
205 {
206         string character;
207         vector cell, cellPos, charPos;
208         cell = '0 0 0';
209         cellPos = '0 0 0';
210         charPos = '0 0 0';
211
212         float CHAR_OFFSET_X = me.realCellSize.x / 2;
213         float CHAR_OFFSET_Y = (me.realCellSize.y - me.realFontSize.y) / 2;
214
215         for(cell_y = 0; cell.y < CHARMAP_ROWS; ++cell.y)
216         {
217                 charPos_y = cell.y / CHARMAP_ROWS + CHAR_OFFSET_Y;
218                 for(cell_x = 0; cell.x < CHARMAP_COLS; ++cell.x)
219                 {
220                         character = charmap_cellToChar(cell);
221
222                         if(character == "")
223                                 continue;
224
225                         // Draw focused cell
226                         if(cell == me.focusedCell && me.focused)
227                         {
228                                 if(!me.pressed || me.focusedCell == me.previouslyFocusedCell)
229                                 {
230                                         cellPos_x = mod(me.focusedCell.x, CHARMAP_COLS) / CHARMAP_COLS;
231                                         cellPos_y = mod(me.focusedCell.y, CHARMAP_ROWS) / CHARMAP_ROWS;
232                                         draw_Fill(cellPos, me.realCellSize, SKINCOLOR_CHARMAP_FOCUS, SKINALPHA_CHARMAP_FOCUS);
233                                 }
234                         }
235
236                         // Draw character
237                         charPos_x = cell.x / CHARMAP_COLS + CHAR_OFFSET_X;
238                         draw_CenterText(charPos, character, me.realFontSize, SKINCOLOR_CHARMAP_CHAR, SKINALPHA_CHARMAP_CHAR, 0);
239                 }
240         }
241 }
242 #endif