-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathguichan.patch
474 lines (436 loc) · 14.7 KB
/
guichan.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
diff --git a/guichan/include/guichan.hpp b/guichan/include/guichan.hpp
index fa1ae5e..a3b7ab5 100644
--- a/guichan/include/guichan.hpp
+++ b/guichan/include/guichan.hpp
@@ -96,9 +96,6 @@
#include "guichan/platform.hpp"
-
-class Widget;
-
extern "C"
{
/**
diff --git a/guichan/include/guichan/focushandler.hpp b/guichan/include/guichan/focushandler.hpp
index 0e2d840..166effa 100644
--- a/guichan/include/guichan/focushandler.hpp
+++ b/guichan/include/guichan/focushandler.hpp
@@ -51,6 +51,7 @@
namespace gcn
{
+ class KeyInput;
class Widget;
/**
@@ -317,6 +318,8 @@ namespace gcn
*/
virtual void setLastWidgetPressed(Widget* lastWidgetPressed);
+ void checkHotKey(const KeyInput& keyInput);
+
protected:
/**
* Distributes a focus lost event.
diff --git a/guichan/include/guichan/key.hpp b/guichan/include/guichan/key.hpp
index 7b533bb..9bdb63a 100644
--- a/guichan/include/guichan/key.hpp
+++ b/guichan/include/guichan/key.hpp
@@ -86,6 +86,13 @@ namespace gcn
*/
bool isLetter() const;
+ /**
+ * Checks whether a key is printable.
+ *
+ * @return true if the key is a printable.
+ */
+ bool isPrintable() const;
+
/**
* Gets the value of the key. If an ascii value exists it
* will be returned. Otherwise an enum value will be returned.
diff --git a/guichan/include/guichan/keylistener.hpp b/guichan/include/guichan/keylistener.hpp
index 7685558..bc02f79 100644
--- a/guichan/include/guichan/keylistener.hpp
+++ b/guichan/include/guichan/keylistener.hpp
@@ -81,6 +81,9 @@ namespace gcn
*/
virtual void keyReleased(KeyEvent& keyEvent) { }
+ virtual void hotKeyPressed(const Key&) { }
+ virtual void hotKeyReleased(const Key&) { }
+
protected:
/**
* Constructor.
diff --git a/guichan/include/guichan/sdl/sdlpixel.hpp b/guichan/include/guichan/sdl/sdlpixel.hpp
index 49686eb..4838742 100644
--- a/guichan/include/guichan/sdl/sdlpixel.hpp
+++ b/guichan/include/guichan/sdl/sdlpixel.hpp
@@ -165,8 +165,9 @@ namespace gcn
unsigned int b = ((src & 0xff) * a + (dst & 0xff) * (255 - a)) >> 8;
unsigned int g = ((src & 0xff00) * a + (dst & 0xff00) * (255 - a)) >> 8;
unsigned int r = ((src & 0xff0000) * a + (dst & 0xff0000) * (255 - a)) >> 8;
+ unsigned int alpha = ((src >> 24) * a + (dst >> 24) * (255 - a)) >> 8;
- return (b & 0xff) | (g & 0xff00) | (r & 0xff0000);
+ return (b & 0xff) | (g & 0xff00) | (r & 0xff0000) | (a << 24);
}
/**
diff --git a/guichan/include/guichan/widget.hpp b/guichan/include/guichan/widget.hpp
index f303752..e692faf 100644
--- a/guichan/include/guichan/widget.hpp
+++ b/guichan/include/guichan/widget.hpp
@@ -1025,6 +1025,26 @@ namespace gcn
*/
virtual void showPart(Rectangle rectangle);
+ /**
+ * Called when a Widget's hot key is pressed
+ */
+ virtual void hotKeyPressed() { }
+
+ /**
+ * Called when a Widget's hot key is released
+ */
+ virtual void hotKeyReleased() { }
+
+ /**
+ * Get the hot key
+ */
+ int getHotKey() const { return mHotKey; }
+
+ /**
+ * Set the hot key
+ */
+ void setHotKey(const int key) { mHotKey = key; }
+
protected:
/**
* Distributes an action event to all action listeners
@@ -1261,6 +1281,8 @@ namespace gcn
* Holds all children of the widget.
*/
std::list<Widget*> mChildren;
+
+ int mHotKey = 0;
};
}
diff --git a/guichan/include/guichan/widgets/button.hpp b/guichan/include/guichan/widgets/button.hpp
index cfe0973..06c8667 100644
--- a/guichan/include/guichan/widgets/button.hpp
+++ b/guichan/include/guichan/widgets/button.hpp
@@ -145,6 +145,8 @@ namespace gcn
virtual void draw(Graphics* graphics);
+ virtual void hotKeyPressed();
+ virtual void hotKeyReleased();
// Inherited from FocusListener
@@ -208,6 +210,8 @@ namespace gcn
* Holds the spacing between the border and the caption.
*/
unsigned int mSpacing;
+
+ bool mHotKeyPressed = false;
};
}
diff --git a/guichan/include/guichan/widgets/dropdown.hpp b/guichan/include/guichan/widgets/dropdown.hpp
index 21711c2..a238ae9 100644
--- a/guichan/include/guichan/widgets/dropdown.hpp
+++ b/guichan/include/guichan/widgets/dropdown.hpp
@@ -76,12 +76,12 @@ namespace gcn
* will be sent to all action listeners of the drop down.
*/
class GCN_CORE_DECLSPEC DropDown :
+ public Widget,
public ActionListener,
public KeyListener,
public MouseListener,
public FocusListener,
- public SelectionListener,
- public Widget
+ public SelectionListener
{
public:
/**
diff --git a/guichan/include/guichan/widgets/scrollarea.hpp b/guichan/include/guichan/widgets/scrollarea.hpp
index ad1d929..ef44a3a 100644
--- a/guichan/include/guichan/widgets/scrollarea.hpp
+++ b/guichan/include/guichan/widgets/scrollarea.hpp
@@ -58,8 +58,8 @@ namespace gcn
* necessary.
*/
class GCN_CORE_DECLSPEC ScrollArea:
- public MouseListener,
- public Widget
+ public Widget,
+ public MouseListener
{
public:
diff --git a/guichan/include/guichan/widgets/tab.hpp b/guichan/include/guichan/widgets/tab.hpp
index 459afbc..4a0daff 100644
--- a/guichan/include/guichan/widgets/tab.hpp
+++ b/guichan/include/guichan/widgets/tab.hpp
@@ -63,8 +63,8 @@ namespace gcn
* @since 0.8.0
*/
class GCN_CORE_DECLSPEC Tab:
- public MouseListener,
- public Widget
+ public Widget,
+ public MouseListener
{
public:
diff --git a/guichan/include/guichan/widgets/tabbedarea.hpp b/guichan/include/guichan/widgets/tabbedarea.hpp
index 39e875c..eb7f824 100644
--- a/guichan/include/guichan/widgets/tabbedarea.hpp
+++ b/guichan/include/guichan/widgets/tabbedarea.hpp
@@ -66,10 +66,10 @@ namespace gcn
* @since 0.8.0
*/
class GCN_CORE_DECLSPEC TabbedArea:
+ public Widget,
public ActionListener,
public KeyListener,
- public MouseListener,
- public Widget
+ public MouseListener
{
friend class Tab;
diff --git a/guichan/src/focushandler.cpp b/guichan/src/focushandler.cpp
index 069a728..08c8e82 100644
--- a/guichan/src/focushandler.cpp
+++ b/guichan/src/focushandler.cpp
@@ -49,6 +49,7 @@
#include "guichan/focuslistener.hpp"
#include "guichan/exception.hpp"
+#include "guichan/keyinput.hpp"
#include "guichan/widget.hpp"
namespace gcn
@@ -584,4 +585,38 @@ namespace gcn
{
mLastWidgetPressed = lastWidgetPressed;
}
+
+ void FocusHandler::checkHotKey(const KeyInput& keyInput)
+ {
+ int keyin = keyInput.getKey().getValue();
+
+ for (int i = 0; i < (int)mWidgets.size(); ++i)
+ {
+ int hotKey = mWidgets[i]->getHotKey();
+
+ if (hotKey == 0)
+ {
+ continue;
+ }
+ hotKey = isascii(hotKey) ? tolower(hotKey) : hotKey;
+
+ if ((isascii(keyin) && tolower(keyin) == hotKey) || keyin == hotKey)
+ {
+ if (keyInput.getType() == KeyInput::Pressed)
+ {
+ mWidgets[i]->hotKeyPressed();
+ if (mWidgets[i]->isFocusable())
+ {
+ this->requestFocus(mWidgets[i]);
+ }
+ }
+ else
+ {
+ mWidgets[i]->hotKeyReleased();
+ }
+ break;
+ }
+ }
+ }
+
}
diff --git a/guichan/src/gui.cpp b/guichan/src/gui.cpp
index da440c7..d81b2d8 100644
--- a/guichan/src/gui.cpp
+++ b/guichan/src/gui.cpp
@@ -283,6 +283,10 @@ namespace gcn
keyEventConsumed = keyEvent.isConsumed();
}
+ if (!keyEventConsumed) {
+ mFocusHandler->checkHotKey(keyInput);
+ }
+
// If the key event hasn't been consumed and
// tabbing is enable check for tab press and
// change focus.
diff --git a/guichan/src/key.cpp b/guichan/src/key.cpp
index 70e9652..6c2c19f 100644
--- a/guichan/src/key.cpp
+++ b/guichan/src/key.cpp
@@ -74,6 +74,11 @@ namespace gcn
&& (mValue != 215) && (mValue != 247));
}
+ bool Key::isPrintable() const
+ {
+ return mValue < -1000 || 32 <= mValue;
+ }
+
int Key::getValue() const
{
return mValue;
diff --git a/guichan/src/sdl/sdlgraphics.cpp b/guichan/src/sdl/sdlgraphics.cpp
index 752f5be..cc9ffbb 100644
--- a/guichan/src/sdl/sdlgraphics.cpp
+++ b/guichan/src/sdl/sdlgraphics.cpp
@@ -107,7 +107,7 @@ namespace gcn
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
s = SDL_CreateRGBSurface(0, width, height, DEPTH, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
#else
- s = SDL_CreateRGBSurface(0, width, height, DEPTH, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
+ s = SDL_CreateRGBSurface(0, width, height, DEPTH, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
#endif
clearTransparentSurface(s);
@@ -125,7 +125,8 @@ namespace gcn
}
mTarget = createTransparentSurface(width, height);
- mTargetTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height);
+ mTargetTexture = SDL_CreateTextureFromSurface(renderer, mTarget);
+ //mTargetTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height);
mRenderer = renderer;
}
#else
diff --git a/guichan/src/sdl/sdlinput.cpp b/guichan/src/sdl/sdlinput.cpp
index 1bb426f..d18fc04 100644
--- a/guichan/src/sdl/sdlinput.cpp
+++ b/guichan/src/sdl/sdlinput.cpp
@@ -49,8 +49,47 @@
#include "guichan/exception.hpp"
+#include <cstdint>
+
namespace gcn
{
+ //--------------------------------------------------------------------------
+ static std::uint32_t utf8ToUnicode(const char* text)
+ {
+ const std::uint32_t c0 = static_cast<unsigned char>(text[0]);
+ if ((c0 & 0xF8) == 0xF0) {
+ if (((text[1] & 0xC0) != 0x80) && ((text[2] & 0xC0) != 0x80)
+ && ((text[3] & 0xC0) != 0x80)) {
+ throw GCN_EXCEPTION("invalid utf8");
+ }
+ const unsigned char c1 = text[1] & 0x3F;
+ const unsigned char c2 = text[2] & 0x3F;
+ const unsigned char c3 = text[3] & 0x3F;
+
+ return ((c0 & 0x07) << 18) | (c1 << 12) | (c2 << 6) | c3;
+ } else if ((c0 & 0xF0) == 0xE0) {
+ if (((text[1] & 0xC0) != 0x80) && ((text[2] & 0xC0) != 0x80)) {
+ throw GCN_EXCEPTION("invalid utf8");
+ }
+ const unsigned char c1 = text[1] & 0x3F;
+ const unsigned char c2 = text[2] & 0x3F;
+
+ return ((c0 & 0x0F) << 12) | (c1 << 6) | c2;
+ } else if ((c0 & 0xE0) == 0xC0) {
+ if (((text[1] & 0xC0) != 0x80)) {
+ throw GCN_EXCEPTION("invalid utf8");
+ }
+ const unsigned char c1 = text[1] & 0x3F;
+
+ return ((c0 & 0x1F) << 6) | c1;
+ } else {
+ if ((c0 & 0x80) != 0) {
+ throw GCN_EXCEPTION("invalid utf8");
+ }
+ return (c0 & 0x7F);
+ }
+ }
+
SDLInput::SDLInput()
{
mMouseInWindow = true;
@@ -104,6 +143,17 @@ namespace gcn
switch (event.type)
{
+ case SDL_TEXTINPUT:
+ keyInput.setKey(utf8ToUnicode(event.text.text));
+ keyInput.setType(KeyInput::Pressed);
+ keyInput.setShiftPressed(false);
+ keyInput.setControlPressed(false);
+ keyInput.setAltPressed(false);
+ keyInput.setMetaPressed(false);
+ keyInput.setNumericPad(false);
+
+ mKeyInputQueue.push(keyInput);
+ break;
case SDL_KEYDOWN:
{
int value = convertSDLEventToGuichanKeyValue(event);
@@ -129,7 +179,11 @@ namespace gcn
&& event.key.keysym.sym <= SDLK_KP_EQUALS);
- mKeyInputQueue.push(keyInput);
+ if (!keyInput.getKey().isPrintable() || keyInput.isAltPressed()
+ || keyInput.isControlPressed())
+ {
+ mKeyInputQueue.push(keyInput);
+ }
break;
}
diff --git a/guichan/src/widgets/button.cpp b/guichan/src/widgets/button.cpp
index b3349d9..ab963a4 100644
--- a/guichan/src/widgets/button.cpp
+++ b/guichan/src/widgets/button.cpp
@@ -205,12 +205,13 @@ namespace gcn
}
else
{
- return mKeyPressed;
+ return mKeyPressed || mHotKeyPressed;
}
}
void Button::mousePressed(MouseEvent& mouseEvent)
{
+ mHasMouse = gcn::Rectangle(0, 0, getWidth(), getHeight()).isContaining(mouseEvent.getX(), mouseEvent.getY());
if (mouseEvent.isConsumed())
{
return;
@@ -265,6 +266,7 @@ namespace gcn
mKeyPressed = true;
keyEvent.consume();
}
+ mHotKeyPressed = false;
}
void Button::keyReleased(KeyEvent& keyEvent)
@@ -281,6 +283,21 @@ namespace gcn
}
}
+ void Button::hotKeyPressed()
+ {
+ mHotKeyPressed = true;
+ mMousePressed = false;
+ }
+
+ void Button::hotKeyReleased()
+ {
+ if (mHotKeyPressed)
+ {
+ mHotKeyPressed = false;
+ distributeActionEvent();
+ }
+ }
+
void Button::focusLost(const Event& event)
{
mMousePressed = false;
diff --git a/guichan/src/widgets/textbox.cpp b/guichan/src/widgets/textbox.cpp
index 0a985cf..4c1df35 100644
--- a/guichan/src/widgets/textbox.cpp
+++ b/guichan/src/widgets/textbox.cpp
@@ -194,7 +194,8 @@ namespace gcn
mText->insert(' ');
}
- else if (key.isCharacter() && mEditable)
+ else if (key.isCharacter() && !keyEvent.isAltPressed()
+ && !keyEvent.isControlPressed() && mEditable)
mText->insert(key.getValue());
adjustSize();
diff --git a/guichan/src/widgets/textfield.cpp b/guichan/src/widgets/textfield.cpp
index 9ff1ba0..786d7a8 100644
--- a/guichan/src/widgets/textfield.cpp
+++ b/guichan/src/widgets/textfield.cpp
@@ -186,6 +186,8 @@ namespace gcn
else if (key.isCharacter()
&& key.getValue() != Key::Tab
+ && !keyEvent.isAltPressed()
+ && !keyEvent.isControlPressed()
&& mEditable)
mText->insert(key.getValue());