mirror of
https://github.com/love2d/love.git
synced 2026-08-20 12:40:20 +02:00
love.keypressed's second argument is now a unicode character string instead of a unicode codepoint number.
This commit is contained in:
@@ -23,6 +23,8 @@
|
|||||||
#include "keyboard/Keyboard.h"
|
#include "keyboard/Keyboard.h"
|
||||||
#include "mouse/Mouse.h"
|
#include "mouse/Mouse.h"
|
||||||
|
|
||||||
|
#include "libraries/utf8/utf8.h"
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
namespace love
|
namespace love
|
||||||
@@ -85,11 +87,11 @@ void Event::clear()
|
|||||||
|
|
||||||
Message *Event::convert(SDL_Event &e)
|
Message *Event::convert(SDL_Event &e)
|
||||||
{
|
{
|
||||||
Message *msg = NULL;
|
Message *msg = 0;
|
||||||
love::keyboard::Keyboard::Key key;
|
love::keyboard::Keyboard::Key key;
|
||||||
love::mouse::Mouse::Button button;
|
love::mouse::Mouse::Button button;
|
||||||
love::joystick::Joystick::Hat hat;
|
love::joystick::Joystick::Hat hat;
|
||||||
Variant *arg1, *arg2, *arg3;
|
Variant *arg1 = 0, *arg2 = 0, *arg3 = 0;
|
||||||
const char *txt;
|
const char *txt;
|
||||||
switch (e.type)
|
switch (e.type)
|
||||||
{
|
{
|
||||||
@@ -99,10 +101,16 @@ Message *Event::convert(SDL_Event &e)
|
|||||||
if (!love::keyboard::Keyboard::getConstant(key, txt))
|
if (!love::keyboard::Keyboard::getConstant(key, txt))
|
||||||
txt = "unknown";
|
txt = "unknown";
|
||||||
arg1 = new Variant(txt, strlen(txt));
|
arg1 = new Variant(txt, strlen(txt));
|
||||||
arg2 = new Variant((double) e.key.keysym.unicode);
|
txt = convertUnicode(e.key.keysym.unicode);
|
||||||
msg = new Message("keypressed", arg1, arg2);
|
if (txt)
|
||||||
|
{
|
||||||
|
arg2 = new Variant(txt, strlen(txt));
|
||||||
|
msg = new Message("keypressed", arg1, arg2);
|
||||||
|
arg2->release();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
msg = new Message("keypressed", arg1);
|
||||||
arg1->release();
|
arg1->release();
|
||||||
arg2->release();
|
|
||||||
break;
|
break;
|
||||||
case SDL_KEYUP:
|
case SDL_KEYUP:
|
||||||
if (!keys.find(e.key.keysym.sym, key))
|
if (!keys.find(e.key.keysym.sym, key))
|
||||||
@@ -190,6 +198,28 @@ Message *Event::convert(SDL_Event &e)
|
|||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *Event::convertUnicode(Uint16 codepoint) const
|
||||||
|
{
|
||||||
|
// A single unicode character can use up to 4 bytes.
|
||||||
|
static char str[5] = {'\0'};
|
||||||
|
ptrdiff_t length = 0;
|
||||||
|
|
||||||
|
if (codepoint == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
char *end = utf8::unchecked::append(codepoint, str);
|
||||||
|
length = end - str;
|
||||||
|
|
||||||
|
if (length <= 0 || !utf8::is_valid(str, str + length))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// Zero out the rest of the string.
|
||||||
|
for (ptrdiff_t i = length; i < 5; i++)
|
||||||
|
str[i] = '\0';
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
EnumMap<love::keyboard::Keyboard::Key, SDLKey, love::keyboard::Keyboard::KEY_MAX_ENUM>::Entry Event::keyEntries[] =
|
EnumMap<love::keyboard::Keyboard::Key, SDLKey, love::keyboard::Keyboard::KEY_MAX_ENUM>::Entry Event::keyEntries[] =
|
||||||
{
|
{
|
||||||
{ love::keyboard::Keyboard::KEY_BACKSPACE, SDLK_BACKSPACE},
|
{ love::keyboard::Keyboard::KEY_BACKSPACE, SDLK_BACKSPACE},
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
Message *convert(SDL_Event &e);
|
Message *convert(SDL_Event &e);
|
||||||
|
const char *convertUnicode(Uint16 codepoint) const;
|
||||||
|
|
||||||
static EnumMap<love::keyboard::Keyboard::Key, SDLKey, love::keyboard::Keyboard::KEY_MAX_ENUM>::Entry keyEntries[];
|
static EnumMap<love::keyboard::Keyboard::Key, SDLKey, love::keyboard::Keyboard::KEY_MAX_ENUM>::Entry keyEntries[];
|
||||||
static EnumMap<love::keyboard::Keyboard::Key, SDLKey, love::keyboard::Keyboard::KEY_MAX_ENUM> keys;
|
static EnumMap<love::keyboard::Keyboard::Key, SDLKey, love::keyboard::Keyboard::KEY_MAX_ENUM> keys;
|
||||||
|
|||||||
Reference in New Issue
Block a user