libgui 3.00
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
GuiKeyboard.cpp
1/****************************************************************************
2 * libgui
3 *
4 * Daryl Borth 2009-2026
5 * GuiKeyboard.cpp
6 ***************************************************************************/
7
8#include "libgui/Gui.h"
9#include "GuiKeyboard.h"
10#include "../filelist.h"
11
12#define KB_FONTSIZE 20
13
14static char tmptxt[MAX_KEYBOARD_DISPLAY+1];
15
16static const char * GetDisplayText(const char * t)
17{
18 if(!t)
19 return nullptr;
20
21 strncpy(tmptxt, t, MAX_KEYBOARD_DISPLAY);
22 tmptxt[MAX_KEYBOARD_DISPLAY] = '\0';
23 return &tmptxt[0];
24}
25
26GuiKeyboard::GuiKeyboard(char * t, uint32_t max)
27{
28 width = 540;
29 height = 400;
30 shift = 0;
31 caps = 0;
32 selectable = true;
33 focus = 0; // allow focus
34 alignmentHor = ALIGN_H::CENTRE;
35 alignmentVert = ALIGN_V::MIDDLE;
36 snprintf(kbtextstr, 255, "%s", t);
37 kbtextmaxlen = max;
38
39 Key thekeys[KB_ROWS][KB_COLUMNS] = {
40 {
41 {'1','!'},
42 {'2','@'},
43 {'3','#'},
44 {'4','$'},
45 {'5','%'},
46 {'6','^'},
47 {'7','&'},
48 {'8','*'},
49 {'9','('},
50 {'0',')'},
51 {'\0','\0'}
52 },
53 {
54 {'q','Q'},
55 {'w','W'},
56 {'e','E'},
57 {'r','R'},
58 {'t','T'},
59 {'y','Y'},
60 {'u','U'},
61 {'i','I'},
62 {'o','O'},
63 {'p','P'},
64 {'-','_'}
65 },
66 {
67 {'a','A'},
68 {'s','S'},
69 {'d','D'},
70 {'f','F'},
71 {'g','G'},
72 {'h','H'},
73 {'j','J'},
74 {'k','K'},
75 {'l','L'},
76 {';',':'},
77 {'\'','"'}
78 },
79
80 {
81 {'z','Z'},
82 {'x','X'},
83 {'c','C'},
84 {'v','V'},
85 {'b','B'},
86 {'n','N'},
87 {'m','M'},
88 {',','<'},
89 {'.','>'},
90 {'/','?'},
91 {'\0','\0'}
92 }
93 };
94 memcpy(keys, thekeys, sizeof(thekeys));
95
96 keyTextbox = new GuiImageData(keyboard_textbox_png);
97 keyTextboxImg = new GuiImage(keyTextbox);
98 keyTextboxImg->setAlignment(ALIGN_H::CENTRE, ALIGN_V::TOP);
99 keyTextboxImg->setPosition(0, 0);
100 this->append(keyTextboxImg);
101
102 kbText = new GuiText(GetDisplayText(kbtextstr), KB_FONTSIZE, (PixelColor){0, 0, 0, 0xff});
103 kbText->setAlignment(ALIGN_H::CENTRE, ALIGN_V::TOP);
104 kbText->setPosition(0, 13);
105 this->append(kbText);
106
107 key = new GuiImageData(keyboard_key_png);
108 keyOver = new GuiImageData(keyboard_key_over_png);
109 keyMedium = new GuiImageData(keyboard_mediumkey_png);
110 keyMediumOver = new GuiImageData(keyboard_mediumkey_over_png);
111 keyLarge = new GuiImageData(keyboard_largekey_png);
112 keyLargeOver = new GuiImageData(keyboard_largekey_over_png);
113
114 trigA = new GuiTrigger;
115 trigA->setPrimaryTrigger();
116
117 keyBackImg = new GuiImage(keyMedium);
118 keyBackOverImg = new GuiImage(keyMediumOver);
119 keyBackText = new GuiText("Back", KB_FONTSIZE, (PixelColor){0, 0, 0, 0xff});
120 keyBack = new GuiButton(keyMedium->getWidth(), keyMedium->getHeight());
121 keyBack->setImage(keyBackImg);
122 keyBack->setImageOver(keyBackOverImg);
123 keyBack->setLabel(keyBackText);
124 keyBack->setTrigger(trigA);
125 keyBack->setPosition(10*42+40, 0*42+80);
126 keyBack->setEffectGrow();
127 this->append(keyBack);
128
129 keyCapsImg = new GuiImage(keyMedium);
130 keyCapsOverImg = new GuiImage(keyMediumOver);
131 keyCapsText = new GuiText("Caps", KB_FONTSIZE, (PixelColor){0, 0, 0, 0xff});
132 keyCaps = new GuiButton(keyMedium->getWidth(), keyMedium->getHeight());
133 keyCaps->setImage(keyCapsImg);
134 keyCaps->setImageOver(keyCapsOverImg);
135 keyCaps->setLabel(keyCapsText);
136 keyCaps->setTrigger(trigA);
137 keyCaps->setPosition(0, 2*42+80);
138 keyCaps->setEffectGrow();
139 this->append(keyCaps);
140
141 keyShiftImg = new GuiImage(keyMedium);
142 keyShiftOverImg = new GuiImage(keyMediumOver);
143 keyShiftText = new GuiText("Shift", KB_FONTSIZE, (PixelColor){0, 0, 0, 0xff});
144 keyShift = new GuiButton(keyMedium->getWidth(), keyMedium->getHeight());
145 keyShift->setImage(keyShiftImg);
146 keyShift->setImageOver(keyShiftOverImg);
147 keyShift->setLabel(keyShiftText);
148 keyShift->setTrigger(trigA);
149 keyShift->setPosition(21, 3*42+80);
150 keyShift->setEffectGrow();
151 this->append(keyShift);
152
153 keySpaceImg = new GuiImage(keyLarge);
154 keySpaceOverImg = new GuiImage(keyLargeOver);
155 keySpace = new GuiButton(keyLarge->getWidth(), keyLarge->getHeight());
156 keySpace->setImage(keySpaceImg);
157 keySpace->setImageOver(keySpaceOverImg);
158 keySpace->setTrigger(trigA);
159 keySpace->setPosition(0, 4*42+80);
160 keySpace->setAlignment(ALIGN_H::CENTRE, ALIGN_V::TOP);
161 keySpace->setEffectGrow();
162 this->append(keySpace);
163
164 char txt[2] = { 0, 0 };
165
166 for(int i=0; i<KB_ROWS; i++)
167 {
168 for(int j=0; j<KB_COLUMNS; j++)
169 {
170 if(keys[i][j].ch != '\0')
171 {
172 txt[0] = keys[i][j].ch;
173 keyImg[i][j] = new GuiImage(key);
174 keyImgOver[i][j] = new GuiImage(keyOver);
175 keyTxt[i][j] = new GuiText(txt, KB_FONTSIZE, (PixelColor){0, 0, 0, 0xff});
176 keyTxt[i][j]->setAlignment(ALIGN_H::CENTRE, ALIGN_V::BOTTOM);
177 keyTxt[i][j]->setPosition(0, -10);
178 keyBtn[i][j] = new GuiButton(key->getWidth(), key->getHeight());
179 keyBtn[i][j]->setImage(keyImg[i][j]);
180 keyBtn[i][j]->setImageOver(keyImgOver[i][j]);
181 keyBtn[i][j]->setTrigger(trigA);
182 keyBtn[i][j]->setLabel(keyTxt[i][j]);
183 keyBtn[i][j]->setPosition(j*42+21*i+40, i*42+80);
184 keyBtn[i][j]->setEffectGrow();
185 this->append(keyBtn[i][j]);
186 }
187 }
188 }
189}
190
191GuiKeyboard::~GuiKeyboard()
192{
193 delete kbText;
194 delete keyTextbox;
195 delete keyTextboxImg;
196 delete keyCapsText;
197 delete keyCapsImg;
198 delete keyCapsOverImg;
199 delete keyCaps;
200 delete keyShiftText;
201 delete keyShiftImg;
202 delete keyShiftOverImg;
203 delete keyShift;
204 delete keyBackText;
205 delete keyBackImg;
206 delete keyBackOverImg;
207 delete keyBack;
208 delete keySpaceImg;
209 delete keySpaceOverImg;
210 delete keySpace;
211 delete key;
212 delete keyOver;
213 delete keyMedium;
214 delete keyMediumOver;
215 delete keyLarge;
216 delete keyLargeOver;
217 delete trigA;
218
219 for(int i=0; i<KB_ROWS; i++)
220 {
221 for(int j=0; j<KB_COLUMNS; j++)
222 {
223 if(keys[i][j].ch != '\0')
224 {
225 delete keyImg[i][j];
226 delete keyImgOver[i][j];
227 delete keyTxt[i][j];
228 delete keyBtn[i][j];
229 }
230 }
231 }
232}
233
235{
236 if(_elements.size() == 0 || (state == STATE::DISABLED && parentElement))
237 return;
238
239 for (uint8_t i = 0; i < _elements.size(); i++)
240 {
241 _elements.at(i)->update(c);
242 }
243
244 bool update = false;
245
246 if(keySpace->getState() == STATE::CLICKED)
247 {
248 size_t len = strlen(kbtextstr);
249 if(len < kbtextmaxlen-1)
250 {
251 kbtextstr[len] = ' ';
252 kbtextstr[len+1] = '\0';
253 kbText->setText(kbtextstr);
254 }
255 keySpace->setState(STATE::SELECTED, c->getChannel());
256 }
257 else if(keyBack->getState() == STATE::CLICKED)
258 {
259 size_t len = strlen(kbtextstr);
260 if(len > 0)
261 {
262 kbtextstr[len-1] = '\0';
263 kbText->setText(GetDisplayText(kbtextstr));
264 }
265 keyBack->setState(STATE::SELECTED, c->getChannel());
266 }
267 else if(keyShift->getState() == STATE::CLICKED)
268 {
269 shift ^= 1;
270 keyShift->setState(STATE::SELECTED, c->getChannel());
271 update = true;
272 }
273 else if(keyCaps->getState() == STATE::CLICKED)
274 {
275 caps ^= 1;
276 keyCaps->setState(STATE::SELECTED, c->getChannel());
277 update = true;
278 }
279
280 char txt[2] = { 0, 0 };
281
282 startloop:
283
284 for(int i=0; i<KB_ROWS; i++)
285 {
286 for(int j=0; j<KB_COLUMNS; j++)
287 {
288 if(keys[i][j].ch != '\0')
289 {
290 if(update)
291 {
292 if(shift || caps)
293 txt[0] = keys[i][j].chShift;
294 else
295 txt[0] = keys[i][j].ch;
296
297 keyTxt[i][j]->setText(txt);
298 }
299
300 if(keyBtn[i][j]->getState() == STATE::CLICKED)
301 {
302 size_t len = strlen(kbtextstr);
303
304 if(len < kbtextmaxlen-1)
305 {
306 if(shift || caps)
307 {
308 kbtextstr[len] = keys[i][j].chShift;
309 }
310 else
311 {
312 kbtextstr[len] = keys[i][j].ch;
313 }
314 kbtextstr[len+1] = '\0';
315 }
316 kbText->setText(GetDisplayText(kbtextstr));
317 keyBtn[i][j]->setState(STATE::SELECTED, c->getChannel());
318
319 if(shift)
320 {
321 shift ^= 1;
322 update = true;
323 goto startloop;
324 }
325 }
326 }
327 }
328 }
329
330 this->toggleFocus(c);
331
332 if(focus) // only send actions to this window if it's in focus
333 {
334 // pad/joystick navigation
335 if(c->right())
336 this->moveSelectionHor(1);
337 else if(c->left())
338 this->moveSelectionHor(-1);
339 else if(c->down())
340 this->moveSelectionVert(1);
341 else if(c->up())
342 this->moveSelectionVert(-1);
343 }
344}
Umbrella header for the platform-agnostic libgui core.
Display, manage, and manipulate buttons in the GUI.
Definition GuiButton.h:13
bool selectable
Whether or not this element selectable (can change to SELECTED state)
Definition GuiElement.h:298
STATE getState()
Gets the element's current state.
void setTrigger(GuiTrigger *t)
Set a new GuiTrigger for the element.
int width
Element width.
Definition GuiElement.h:273
ALIGN_H alignmentHor
Horizontal element alignment, respective to parent element (LEFT, RIGHT, CENTRE)
Definition GuiElement.h:294
void setEffectGrow(int percent=110)
Shortcut to SetEffectOnOver(EFFECT::SCALE, 4, percent)
int height
Element height.
Definition GuiElement.h:274
GuiElement * parentElement
Parent element.
Definition GuiElement.h:270
ALIGN_V alignmentVert
Horizontal element alignment, respective to parent element (TOP, BOTTOM, MIDDLE)
Definition GuiElement.h:295
int focus
Element focus (-1 = focus disabled, 0 = not focused, 1 = focused)
Definition GuiElement.h:272
STATE state
Element state (DEFAULT, SELECTED, CLICKED, DISABLED)
Definition GuiElement.h:296
void setPosition(int x, int y)
Sets the element's position.
Decodes compressed image data (PNG) into a platform-native texture created from it.
Display, manage, and manipulate images in the GUI.
Definition GuiImage.h:18
GuiKeyboard(char *t, uint32_t m)
char kbtextstr[256]
Current entered text - read this after the keyboard closes.
Definition GuiKeyboard.h:32
void update(InputController *c)
Called constantly to allow the element to respond to the current input data.
Display, manage, and manipulate text in the GUI.
Definition GuiText.h:10
void setAlignment(ALIGN_H hor, ALIGN_V vert)
Sets the text alignment.
Definition GuiText.cpp:307
Input-condition matcher attached to a GuiElement (via setSimpleTrigger() etc.) and evaluated against ...
Definition GuiTrigger.h:34
void setPrimaryTrigger(int ch=-1)
Semantic Triggers.
void toggleFocus(InputController *c)
Changes window focus to the next focusable window or element If no element is in focus,...
void append(GuiElement *e)
Appends a GuiElement to the GuiWindow.
Definition GuiWindow.cpp:31
void moveSelectionVert(int d)
Moves the selected element to the element above or below.
std::vector< GuiElement * > _elements
Contains all elements within the GuiWindow.
Definition GuiWindow.h:96
void moveSelectionHor(int d)
Moves the selected element to the element to the left or right.
Logical controller for one input channel.
int getChannel() const
8-bit-per-channel RGBA color used by the video interfaces.
Definition Mesh.h:16