~ WIP input IME

This commit is contained in:
REMqb
2019-12-19 19:59:43 +01:00
parent 350a1cf09f
commit ef791e2f3c
18 changed files with 207 additions and 23 deletions

View File

@@ -38,6 +38,16 @@ namespace Nz
return EventImpl::IsKeyPressed(key);
}
void Keyboard::StartTextInput()
{
EventImpl::StartTextInput();
}
void Keyboard::StopTextInput()
{
EventImpl::StopTextInput();
}
Keyboard::Scancode Keyboard::ToScanCode(VKey key)
{
return EventImpl::ToScanCode(key);

View File

@@ -100,6 +100,16 @@ namespace Nz
NazaraError("Invalid window handle");
}
void EventImpl::StartTextInput()
{
SDL_StartTextInput();
}
void EventImpl::StopTextInput()
{
SDL_StopTextInput();
}
Keyboard::Scancode EventImpl::ToScanCode(Keyboard::VKey key)
{
return SDLHelper::FromSDL(SDL_GetScancodeFromKey(SDLHelper::ToSDL(key)));

View File

@@ -26,6 +26,8 @@ namespace Nz
static bool IsMouseButtonPressed(Mouse::Button button);
static void SetMousePosition(int x, int y);
static void SetMousePosition(int x, int y, const Window& relativeTo);
static void StartTextInput();
static void StopTextInput();
static Keyboard::Scancode ToScanCode(Keyboard::VKey key);
static Keyboard::VKey ToVirtualKey(Keyboard::Scancode scancode);
};

View File

@@ -278,10 +278,10 @@ namespace Nz
case SDL_WINDOWEVENT_RESIZED:
evt.type = Nz::WindowEventType::WindowEventType_Resized;
evt.size.width = event->window.data1;
evt.size.height = event->window.data2;
evt.size.width = static_cast<unsigned int>(std::max(0, event->window.data1));
evt.size.height = static_cast<unsigned int>(std::max(0, event->window.data2));
window->m_size.Set(event->window.data1, event->window.data2);
window->m_size.Set(evt.size.width, evt.size.height);
break;
case SDL_WINDOWEVENT_MOVED:
@@ -387,6 +387,39 @@ namespace Nz
evt.key.shift = (event->key.keysym.mod & KMOD_SHIFT) != 0;
evt.key.system = (event->key.keysym.mod & KMOD_GUI) != 0;
// implements X11/Win32 APIs behavior for Enter and Backspace
switch (evt.key.virtualKey) {
case Nz::Keyboard::VKey::NumpadReturn:
case Nz::Keyboard::VKey::Return:
if (window->m_lastEditEventLength != 0)
{
break;
}
window->m_parent->PushEvent(evt);
evt.type = WindowEventType_TextEntered;
evt.text.character = U'\n';
evt.text.repeated = event->key.repeat != 0;
window->m_parent->PushEvent(evt);
break;
case Nz::Keyboard::VKey::Backspace:
window->m_parent->PushEvent(evt);
evt.type = WindowEventType_TextEntered;
evt.text.character = U'\b';
evt.text.repeated = event->key.repeat != 0;
window->m_parent->PushEvent(evt);
break;
default:
break;
}
break;
case SDL_KEYUP:
@@ -410,26 +443,42 @@ namespace Nz
return 0;
evt.type = WindowEventType_TextEntered;
evt.text.repeated = false;
for (decltype(evt.text.character)codepoint : String::Unicode(event->text.text).GetUtf32String())
for (decltype(evt.text.character)codepoint : String::Unicode(event->text.text).Simplify().GetUtf32String())
{
evt.text.character = codepoint;
evt.text.character = codepoint;
window->m_parent->PushEvent(evt);
}
// prevent post switch event
evt.type = WindowEventType::WindowEventType_Max;
evt.type = WindowEventType::WindowEventType_Max;
break;
break;
case SDL_TEXTEDITING:
if (SDL_GetWindowID(window->m_handle) != event->edit.windowID)
return 0;
evt.type = WindowEventType_TextEdited;
evt.edit.length = event->edit.length;
window->m_lastEditEventLength = evt.edit.length;
for (std::size_t i = 0; i < 32; i++)
{
evt.edit.text[i] = event->edit.text[i];
}
break;
}
if (evt.type != WindowEventType::WindowEventType_Max)
window->m_parent->PushEvent(evt);
}
catch (std::exception e)
{
NazaraError(e.what());
{
NazaraError(e.what());
}
catch (...) // Don't let any exceptions go thru C calls
{
@@ -511,11 +560,11 @@ namespace Nz
}
bool WindowImpl::Initialize()
{
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
NazaraError(SDL_GetError());
return false;
return false;
}
if (SDL_GL_LoadLibrary(nullptr) < 0)
{
@@ -523,7 +572,7 @@ namespace Nz
SDL_Quit();
return false;
}
}
if (SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, true) < 0)
NazaraError("Couldn't set share OpenGL contexes");

View File

@@ -87,6 +87,7 @@ namespace Nz
//static void WindowThread(SDL_Window* handle, /*DWORD styleEx,*/ const String& title, /*DWORD style,*/ bool fullscreen, const Rectui& dimensions, WindowImpl* window, Mutex* mutex, ConditionVariable* condition);
int m_lastEditEventLength = 0;
SDL_Cursor* m_cursor;
SDL_Window* m_handle;
WindowStyleFlags m_style;

View File

@@ -744,7 +744,10 @@ namespace Nz
#if defined(NAZARA_PLATFORM_SDL2)
if (SDL_VideoInit(NULL) != 0)
{
NazaraError(SDL_GetError());
}
#elif defined(NAZARA_PLATFORM_GLX)
Initializer<X11> display;
if (!display)