Skip to content

Commit

Permalink
Merge pull request godotengine#9366 from GodotExplorer/pr-ime-positio…
Browse files Browse the repository at this point in the history
…n-flow-cursor

IME window follow the input cursor.
  • Loading branch information
akien-mga authored Jul 11, 2017
2 parents faae28c + 7358766 commit a5bb77d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
7 changes: 7 additions & 0 deletions core/bind/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ bool _OS::get_borderless_window() const {
return OS::get_singleton()->get_borderless_window();
}

void _OS::set_ime_position(const Point2 &p_pos) {

return OS::get_singleton()->set_ime_position(p_pos);
}

void _OS::set_use_file_access_save_and_swap(bool p_enable) {

FileAccess::set_backup_save(p_enable);
Expand Down Expand Up @@ -993,6 +998,8 @@ void _OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_borderless_window", "borderless"), &_OS::set_borderless_window);
ClassDB::bind_method(D_METHOD("get_borderless_window"), &_OS::get_borderless_window);

ClassDB::bind_method(D_METHOD("set_ime_position"), &_OS::set_ime_position);

ClassDB::bind_method(D_METHOD("set_screen_orientation", "orientation"), &_OS::set_screen_orientation);
ClassDB::bind_method(D_METHOD("get_screen_orientation"), &_OS::get_screen_orientation);

Expand Down
2 changes: 2 additions & 0 deletions core/bind/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ class _OS : public Object {
virtual void set_borderless_window(bool p_borderless);
virtual bool get_borderless_window() const;

virtual void set_ime_position(const Point2 &p_pos);

Error native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track);
bool native_video_is_playing();
void native_video_pause();
Expand Down
2 changes: 2 additions & 0 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ class OS {
virtual void set_borderless_window(int p_borderless) {}
virtual bool get_borderless_window() { return 0; }

virtual void set_ime_position(const Point2 &p_pos) {}

virtual Error open_dynamic_library(const String p_path, void *&p_library_handle) { return ERR_UNAVAILABLE; };
virtual Error close_dynamic_library(void *p_library_handle) { return ERR_UNAVAILABLE; };
virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle) { return ERR_UNAVAILABLE; };
Expand Down
21 changes: 8 additions & 13 deletions platform/x11/os_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,22 +508,17 @@ void OS_X11::xim_destroy_callback(::XIM im, ::XPointer client_data,
os->xic = NULL;
}

void OS_X11::set_ime_position(short x, short y) {
void OS_X11::set_ime_position(const Point2 &p_pos) {

if (!xic) {
if (!xic)
return;
}

::XPoint spot;
spot.x = x;
spot.y = y;
XVaNestedList preedit_attr = XVaCreateNestedList(0,
XNSpotLocation, &spot,
NULL);
XSetICValues(xic,
XNPreeditAttributes, preedit_attr,
NULL);
spot.x = short(p_pos.x);
spot.y = short(p_pos.y);
XVaNestedList preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL);
XSetICValues(xic, XNPreeditAttributes, preedit_attr, NULL);
XFree(preedit_attr);
return;
}

void OS_X11::finalize() {
Expand Down Expand Up @@ -1489,7 +1484,7 @@ void OS_X11::process_xevents() {
case ConfigureNotify:
if (xic) {
// Not portable.
set_ime_position(0, 1);
set_ime_position(Point2(0, 1));
}
/* call resizeGLScene only if our window-size changed */

Expand Down
2 changes: 1 addition & 1 deletion platform/x11/os_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ class OS_X11 : public OS_Unix {
::XIMStyle xim_style;
static void xim_destroy_callback(::XIM im, ::XPointer client_data,
::XPointer call_data);
void set_ime_position(short x, short y);

Point2i last_mouse_pos;
bool last_mouse_pos_valid;
Expand Down Expand Up @@ -253,6 +252,7 @@ class OS_X11 : public OS_Unix {

virtual void set_borderless_window(int p_borderless);
virtual bool get_borderless_window();
virtual void set_ime_position(const Point2 &p_pos);

virtual void move_window_to_foreground();
virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
Expand Down
10 changes: 10 additions & 0 deletions scene/gui/line_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,19 +660,29 @@ void LineEdit::_notification(int p_what) {
Point2(x_ofs, y_ofs), Size2(1, caret_height)),
cursor_color);
}

if (has_focus()) {

OS::get_singleton()->set_ime_position(get_global_position() + Point2(x_ofs, y_ofs + caret_height));
}
} break;
case NOTIFICATION_FOCUS_ENTER: {

if (!caret_blink_enabled) {
draw_caret = true;
}

Point2 cursor_pos = Point2(get_cursor_pos(), 1) * get_minimum_size().height;
OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos);

if (OS::get_singleton()->has_virtual_keyboard())
OS::get_singleton()->show_virtual_keyboard(text, get_global_rect());

} break;
case NOTIFICATION_FOCUS_EXIT: {

OS::get_singleton()->set_ime_position(Point2());

if (OS::get_singleton()->has_virtual_keyboard())
OS::get_singleton()->hide_virtual_keyboard();

Expand Down
9 changes: 9 additions & 0 deletions scene/gui/text_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1191,12 +1191,19 @@ void TextEdit::_notification(int p_what) {
}
}

if (has_focus()) {
OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos + Point2(0, get_row_height()));
}
} break;
case NOTIFICATION_FOCUS_ENTER: {

if (!caret_blink_enabled) {
draw_caret = true;
}

Point2 cursor_pos = Point2(cursor_get_column(), cursor_get_line()) * get_row_height();
OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos);

if (OS::get_singleton()->has_virtual_keyboard())
OS::get_singleton()->show_virtual_keyboard(get_text(), get_global_rect());
if (raised_from_completion) {
Expand All @@ -1206,6 +1213,8 @@ void TextEdit::_notification(int p_what) {
} break;
case NOTIFICATION_FOCUS_EXIT: {

OS::get_singleton()->set_ime_position(Point2());

if (OS::get_singleton()->has_virtual_keyboard())
OS::get_singleton()->hide_virtual_keyboard();
if (raised_from_completion) {
Expand Down

0 comments on commit a5bb77d

Please sign in to comment.