Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support IME #599

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion libs/sdl/sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ typedef struct {
int value;
int fingerId;
int joystick;
const char* inputChar;
} event_data;

HL_PRIM bool HL_NAME(init_once)() {
Expand All @@ -117,6 +118,7 @@ HL_PRIM bool HL_NAME(init_once)() {
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");

return true;
}
Expand Down Expand Up @@ -146,6 +148,7 @@ HL_PRIM bool HL_NAME(hint_value)( vbyte* name, vbyte* value) {
return SDL_SetHint((char*)name, (char*)value) == SDL_TRUE;
}

bool textediting = false;
HL_PRIM bool HL_NAME(event_loop)( event_data *event ) {
while (true) {
SDL_Event e;
Expand Down Expand Up @@ -261,12 +264,16 @@ HL_PRIM bool HL_NAME(event_loop)( event_data *event ) {
}
break;
case SDL_TEXTEDITING:
// skip
// support IME
textediting = true;
continue;
case SDL_TEXTINPUT:
event->type = TextInput;
event->keyCode = *(int*)e.text.text;
event->keyCode &= e.text.text[0] ? e.text.text[1] ? e.text.text[2] ? e.text.text[3] ? 0xFFFFFFFF : 0xFFFFFF : 0xFFFF : 0xFF : 0;
// back all chars.
event->value = 2;
event->inputChar = hl_to_utf16(e.text.text);
break;
case SDL_CONTROLLERDEVICEADDED:
event->type = GControllerAdded;
Expand Down Expand Up @@ -836,6 +843,20 @@ HL_PRIM char* HL_NAME(get_clipboard_text)() {
return bytes;
}

// SDL2.0.22 support, Used to improve ime input.
HL_PRIM bool HL_NAME(is_text_input_shown)() {
if (textediting)
{
textediting = false;
return true;
}
#if defined(_WIN32) || defined(HL_MAC)
return SDL_IsTextInputShown();
#else
return false;
#endif
}

HL_PRIM varray* HL_NAME(get_displays)() {
int n = SDL_GetNumVideoDisplays();
if (n < 0)
Expand Down Expand Up @@ -916,6 +937,7 @@ DEFINE_PRIM(_VOID, free_cursor, _CURSOR);
DEFINE_PRIM(_VOID, set_cursor, _CURSOR);
DEFINE_PRIM(_BOOL, set_clipboard_text, _BYTES);
DEFINE_PRIM(_BYTES, get_clipboard_text, _NO_ARG);
DEFINE_PRIM(_BOOL, is_text_input_shown, _NO_ARG);
DEFINE_PRIM(_ARR, get_displays, _NO_ARG);
DEFINE_PRIM(_ARR, get_display_modes, _I32);
DEFINE_PRIM(_DYN, get_current_display_mode, _I32 _BOOL);
Expand Down
1 change: 1 addition & 0 deletions libs/sdl/sdl/Event.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package sdl;
public var value : Int;
public var fingerId : Int;
public var joystick : Int;
public var inputChar : hl.Bytes;
public function new() {
}
}
Expand Down
13 changes: 13 additions & 0 deletions libs/sdl/sdl/Sdl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ class Sdl {
else
return @:privateAccess String.fromUTF8(t);
}

/**
* This is needed for software applications (and games) using SDL to interact with IME an appropriately. Without knowing if the IME is currently actively taking input from the user or displayed it's not possible for an application to handle certain input events from the mouse appropriately.
* @return Bool
*/
public static function isTextInputShown():Bool{
return _isTextInputShown();
}

@:hlNative("?sdl", "get_screen_width")
static function get_screen_width() : Int {
Expand Down Expand Up @@ -233,6 +241,11 @@ class Sdl {
private static function _getClipboardText() : hl.Bytes {
return null;
}

@:hlNative("?sdl", "is_text_input_shown")
private static function _isTextInputShown() : Bool {
return false;
}
}

@:enum
Expand Down