Merge pull request #23923 from bruvzg/ime_gdscript

Changes IME to make it possible to use it from gdscript/gdnative
This commit is contained in:
Rémi Verschelde 2018-12-11 18:00:48 +01:00 committed by GitHub
commit 4c41e29c8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 87 additions and 43 deletions

View File

@ -378,12 +378,20 @@ bool _OS::get_borderless_window() const {
void _OS::set_ime_active(const bool p_active) { void _OS::set_ime_active(const bool p_active) {
return OS::get_singleton()->set_ime_active(p_active); OS::get_singleton()->set_ime_active(p_active);
} }
void _OS::set_ime_position(const Point2 &p_pos) { void _OS::set_ime_position(const Point2 &p_pos) {
return OS::get_singleton()->set_ime_position(p_pos); OS::get_singleton()->set_ime_position(p_pos);
}
Point2 _OS::get_ime_selection() const {
return OS::get_singleton()->get_ime_selection();
}
String _OS::get_ime_text() const {
return OS::get_singleton()->get_ime_text();
} }
void _OS::set_use_file_access_save_and_swap(bool p_enable) { void _OS::set_use_file_access_save_and_swap(bool p_enable) {
@ -1134,7 +1142,10 @@ void _OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_window_per_pixel_transparency_enabled"), &_OS::get_window_per_pixel_transparency_enabled); ClassDB::bind_method(D_METHOD("get_window_per_pixel_transparency_enabled"), &_OS::get_window_per_pixel_transparency_enabled);
ClassDB::bind_method(D_METHOD("set_window_per_pixel_transparency_enabled", "enabled"), &_OS::set_window_per_pixel_transparency_enabled); ClassDB::bind_method(D_METHOD("set_window_per_pixel_transparency_enabled", "enabled"), &_OS::set_window_per_pixel_transparency_enabled);
ClassDB::bind_method(D_METHOD("set_ime_active", "active"), &_OS::set_ime_active);
ClassDB::bind_method(D_METHOD("set_ime_position", "position"), &_OS::set_ime_position); ClassDB::bind_method(D_METHOD("set_ime_position", "position"), &_OS::set_ime_position);
ClassDB::bind_method(D_METHOD("get_ime_selection"), &_OS::get_ime_selection);
ClassDB::bind_method(D_METHOD("get_ime_text"), &_OS::get_ime_text);
ClassDB::bind_method(D_METHOD("set_screen_orientation", "orientation"), &_OS::set_screen_orientation); 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); ClassDB::bind_method(D_METHOD("get_screen_orientation"), &_OS::get_screen_orientation);

View File

@ -195,6 +195,8 @@ public:
virtual void set_ime_active(const bool p_active); virtual void set_ime_active(const bool p_active);
virtual void set_ime_position(const Point2 &p_pos); virtual void set_ime_position(const Point2 &p_pos);
virtual Point2 get_ime_selection() const;
virtual String get_ime_text() const;
Error native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track); Error native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track);
bool native_video_is_playing(); bool native_video_is_playing();

View File

@ -60,6 +60,7 @@ void MainLoop::_bind_methods() {
BIND_CONSTANT(NOTIFICATION_TRANSLATION_CHANGED); BIND_CONSTANT(NOTIFICATION_TRANSLATION_CHANGED);
BIND_CONSTANT(NOTIFICATION_WM_ABOUT); BIND_CONSTANT(NOTIFICATION_WM_ABOUT);
BIND_CONSTANT(NOTIFICATION_CRASH); BIND_CONSTANT(NOTIFICATION_CRASH);
BIND_CONSTANT(NOTIFICATION_OS_IME_UPDATE);
}; };
void MainLoop::set_init_script(const Ref<Script> &p_init_script) { void MainLoop::set_init_script(const Ref<Script> &p_init_script) {

View File

@ -65,6 +65,7 @@ public:
NOTIFICATION_TRANSLATION_CHANGED = 90, NOTIFICATION_TRANSLATION_CHANGED = 90,
NOTIFICATION_WM_ABOUT = 91, NOTIFICATION_WM_ABOUT = 91,
NOTIFICATION_CRASH = 92, NOTIFICATION_CRASH = 92,
NOTIFICATION_OS_IME_UPDATE = 93,
}; };
virtual void input_event(const Ref<InputEvent> &p_event); virtual void input_event(const Ref<InputEvent> &p_event);

View File

@ -242,7 +242,8 @@ public:
virtual void set_ime_active(const bool p_active) {} virtual void set_ime_active(const bool p_active) {}
virtual void set_ime_position(const Point2 &p_pos) {} virtual void set_ime_position(const Point2 &p_pos) {}
virtual void set_ime_intermediate_text_callback(ImeCallback p_callback, void *p_inp) {} virtual Point2 get_ime_selection() const { return Point2(); }
virtual String get_ime_text() const { return String(); }
virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false) { return ERR_UNAVAILABLE; } virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false) { return ERR_UNAVAILABLE; }
virtual Error close_dynamic_library(void *p_library_handle) { return ERR_UNAVAILABLE; } virtual Error close_dynamic_library(void *p_library_handle) { return ERR_UNAVAILABLE; }

View File

@ -136,5 +136,7 @@
</constant> </constant>
<constant name="NOTIFICATION_CRASH" value="92"> <constant name="NOTIFICATION_CRASH" value="92">
</constant> </constant>
<constant name="NOTIFICATION_OS_IME_UPDATE" value="93">
</constant>
</constants> </constants>
</class> </class>

View File

@ -210,6 +210,20 @@
Returns the path to the current engine executable. Returns the path to the current engine executable.
</description> </description>
</method> </method>
<method name="get_ime_text" qualifiers="const">
<return type="String">
</return>
<description>
Returns IME intermediate text.
</description>
</method>
<method name="get_ime_selection" qualifiers="const">
<return type="Vector2">
</return>
<description>
Returns IME selection range.
</description>
</method>
<method name="get_latin_keyboard_variant" qualifiers="const"> <method name="get_latin_keyboard_variant" qualifiers="const">
<return type="String"> <return type="String">
</return> </return>
@ -663,12 +677,22 @@
Sets the game's icon. Sets the game's icon.
</description> </description>
</method> </method>
<method name="set_ime_active">
<return type="void">
</return>
<argument index="0" name="active" type="bool">
</argument>
<description>
Sets whether IME input mode should be enabled.
</description>
</method>
<method name="set_ime_position"> <method name="set_ime_position">
<return type="void"> <return type="void">
</return> </return>
<argument index="0" name="position" type="Vector2"> <argument index="0" name="position" type="Vector2">
</argument> </argument>
<description> <description>
Sets position of IME suggestion list popup (in window coordinates).
</description> </description>
</method> </method>
<method name="set_thread_name"> <method name="set_thread_name">

View File

@ -124,8 +124,8 @@ public:
Point2 im_position; Point2 im_position;
bool im_active; bool im_active;
ImeCallback im_callback; String im_text;
void *im_target; Point2 im_selection;
power_osx *power_manager; power_osx *power_manager;
@ -245,7 +245,8 @@ public:
virtual void set_ime_active(const bool p_active); virtual void set_ime_active(const bool p_active);
virtual void set_ime_position(const Point2 &p_pos); virtual void set_ime_position(const Point2 &p_pos);
virtual void set_ime_intermediate_text_callback(ImeCallback p_callback, void *p_inp); virtual Point2 get_ime_selection() const;
virtual String get_ime_text() const;
virtual String get_unique_id() const; virtual String get_unique_id() const;

View File

@ -427,11 +427,13 @@ static const NSRange kEmptyRange = { NSNotFound, 0 };
} else { } else {
[markedText initWithString:aString]; [markedText initWithString:aString];
} }
if (OS_OSX::singleton->im_callback) { if (OS_OSX::singleton->im_active) {
imeMode = true; imeMode = true;
String ret; OS_OSX::singleton->im_text.parse_utf8([[markedText mutableString] UTF8String]);
ret.parse_utf8([[markedText mutableString] UTF8String]); OS_OSX::singleton->im_selection = Point2(selectedRange.location, selectedRange.length);
OS_OSX::singleton->im_callback(OS_OSX::singleton->im_target, ret, Point2(selectedRange.location, selectedRange.length));
if (OS_OSX::singleton->get_main_loop())
OS_OSX::singleton->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE);
} }
} }
@ -443,8 +445,13 @@ static const NSRange kEmptyRange = { NSNotFound, 0 };
- (void)unmarkText { - (void)unmarkText {
imeMode = false; imeMode = false;
[[markedText mutableString] setString:@""]; [[markedText mutableString] setString:@""];
if (OS_OSX::singleton->im_callback) if (OS_OSX::singleton->im_active) {
OS_OSX::singleton->im_callback(OS_OSX::singleton->im_target, "", Point2()); OS_OSX::singleton->im_text = String();
OS_OSX::singleton->im_selection = Point2();
if (OS_OSX::singleton->get_main_loop())
OS_OSX::singleton->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE);
}
} }
- (NSArray *)validAttributesForMarkedText { - (NSArray *)validAttributesForMarkedText {
@ -1136,12 +1143,14 @@ inline void sendPanEvent(double dx, double dy, int modifierFlags) {
@end @end
void OS_OSX::set_ime_intermediate_text_callback(ImeCallback p_callback, void *p_inp) { Point2 OS_OSX::get_ime_selection() const {
im_callback = p_callback;
im_target = p_inp; return im_selection;
if (!im_callback) { }
[window_view cancelComposition];
} String OS_OSX::get_ime_text() const {
return im_text;
} }
String OS_OSX::get_unique_id() const { String OS_OSX::get_unique_id() const {
@ -1169,10 +1178,14 @@ String OS_OSX::get_unique_id() const {
} }
void OS_OSX::set_ime_active(const bool p_active) { void OS_OSX::set_ime_active(const bool p_active) {
im_active = p_active; im_active = p_active;
if (!im_active)
[window_view cancelComposition];
} }
void OS_OSX::set_ime_position(const Point2 &p_pos) { void OS_OSX::set_ime_position(const Point2 &p_pos) {
im_position = p_pos; im_position = p_pos;
} }
@ -2637,8 +2650,6 @@ OS_OSX::OS_OSX() {
singleton = this; singleton = this;
im_active = false; im_active = false;
im_position = Point2(); im_position = Point2();
im_callback = NULL;
im_target = NULL;
layered_window = false; layered_window = false;
autoreleasePool = [[NSAutoreleasePool alloc] init]; autoreleasePool = [[NSAutoreleasePool alloc] init];

View File

@ -831,7 +831,6 @@ void LineEdit::_notification(int p_what) {
OS::get_singleton()->set_ime_active(true); OS::get_singleton()->set_ime_active(true);
OS::get_singleton()->set_ime_position(get_global_position() + Point2(using_placeholder ? 0 : x_ofs, y_ofs + caret_height)); OS::get_singleton()->set_ime_position(get_global_position() + Point2(using_placeholder ? 0 : x_ofs, y_ofs + caret_height));
OS::get_singleton()->set_ime_intermediate_text_callback(_ime_text_callback, this);
} }
} break; } break;
case NOTIFICATION_FOCUS_ENTER: { case NOTIFICATION_FOCUS_ENTER: {
@ -843,7 +842,6 @@ void LineEdit::_notification(int p_what) {
OS::get_singleton()->set_ime_active(true); OS::get_singleton()->set_ime_active(true);
Point2 cursor_pos = Point2(get_cursor_position(), 1) * get_minimum_size().height; Point2 cursor_pos = Point2(get_cursor_position(), 1) * get_minimum_size().height;
OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos); OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos);
OS::get_singleton()->set_ime_intermediate_text_callback(_ime_text_callback, this);
if (OS::get_singleton()->has_virtual_keyboard()) if (OS::get_singleton()->has_virtual_keyboard())
OS::get_singleton()->show_virtual_keyboard(text, get_global_rect()); OS::get_singleton()->show_virtual_keyboard(text, get_global_rect());
@ -852,7 +850,6 @@ void LineEdit::_notification(int p_what) {
case NOTIFICATION_FOCUS_EXIT: { case NOTIFICATION_FOCUS_EXIT: {
OS::get_singleton()->set_ime_position(Point2()); OS::get_singleton()->set_ime_position(Point2());
OS::get_singleton()->set_ime_intermediate_text_callback(NULL, NULL);
OS::get_singleton()->set_ime_active(false); OS::get_singleton()->set_ime_active(false);
ime_text = ""; ime_text = "";
ime_selection = Point2(); ime_selection = Point2();
@ -861,6 +858,12 @@ void LineEdit::_notification(int p_what) {
OS::get_singleton()->hide_virtual_keyboard(); OS::get_singleton()->hide_virtual_keyboard();
} break; } break;
case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
ime_text = OS::get_singleton()->get_ime_text();
ime_selection = OS::get_singleton()->get_ime_selection();
update();
} break;
} }
} }
@ -1461,13 +1464,6 @@ void LineEdit::set_right_icon(const Ref<Texture> &p_icon) {
update(); update();
} }
void LineEdit::_ime_text_callback(void *p_self, String p_text, Point2 p_selection) {
LineEdit *self = (LineEdit *)p_self;
self->ime_text = p_text;
self->ime_selection = p_selection;
self->update();
}
void LineEdit::_text_changed() { void LineEdit::_text_changed() {
if (expand_to_text_length) if (expand_to_text_length)

View File

@ -122,7 +122,6 @@ private:
Timer *caret_blink_timer; Timer *caret_blink_timer;
static void _ime_text_callback(void *p_self, String p_text, Point2 p_selection);
void _text_changed(); void _text_changed();
void _emit_text_change(); void _emit_text_change();
bool expand_to_text_length; bool expand_to_text_length;

View File

@ -1438,7 +1438,6 @@ void TextEdit::_notification(int p_what) {
if (has_focus()) { if (has_focus()) {
OS::get_singleton()->set_ime_active(true); OS::get_singleton()->set_ime_active(true);
OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos + Point2(0, get_row_height())); OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos + Point2(0, get_row_height()));
OS::get_singleton()->set_ime_intermediate_text_callback(_ime_text_callback, this);
} }
} break; } break;
@ -1451,7 +1450,6 @@ void TextEdit::_notification(int p_what) {
OS::get_singleton()->set_ime_active(true); OS::get_singleton()->set_ime_active(true);
Point2 cursor_pos = Point2(cursor_get_column(), cursor_get_line()) * get_row_height(); Point2 cursor_pos = Point2(cursor_get_column(), cursor_get_line()) * get_row_height();
OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos); OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos);
OS::get_singleton()->set_ime_intermediate_text_callback(_ime_text_callback, this);
if (OS::get_singleton()->has_virtual_keyboard()) if (OS::get_singleton()->has_virtual_keyboard())
OS::get_singleton()->show_virtual_keyboard(get_text(), get_global_rect()); OS::get_singleton()->show_virtual_keyboard(get_text(), get_global_rect());
@ -1459,7 +1457,6 @@ void TextEdit::_notification(int p_what) {
case NOTIFICATION_FOCUS_EXIT: { case NOTIFICATION_FOCUS_EXIT: {
OS::get_singleton()->set_ime_position(Point2()); OS::get_singleton()->set_ime_position(Point2());
OS::get_singleton()->set_ime_intermediate_text_callback(NULL, NULL);
OS::get_singleton()->set_ime_active(false); OS::get_singleton()->set_ime_active(false);
ime_text = ""; ime_text = "";
ime_selection = Point2(); ime_selection = Point2();
@ -1467,14 +1464,13 @@ void TextEdit::_notification(int p_what) {
if (OS::get_singleton()->has_virtual_keyboard()) if (OS::get_singleton()->has_virtual_keyboard())
OS::get_singleton()->hide_virtual_keyboard(); OS::get_singleton()->hide_virtual_keyboard();
} break; } break;
} case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
}
void TextEdit::_ime_text_callback(void *p_self, String p_text, Point2 p_selection) { ime_text = OS::get_singleton()->get_ime_text();
TextEdit *self = (TextEdit *)p_self; ime_selection = OS::get_singleton()->get_ime_selection();
self->ime_text = p_text; update();
self->ime_selection = p_selection; } break;
self->update(); }
} }
void TextEdit::_consume_pair_symbol(CharType ch) { void TextEdit::_consume_pair_symbol(CharType ch) {

View File

@ -382,8 +382,6 @@ private:
void _scroll_lines_up(); void _scroll_lines_up();
void _scroll_lines_down(); void _scroll_lines_down();
static void _ime_text_callback(void *p_self, String p_text, Point2 p_selection);
//void mouse_motion(const Point& p_pos, const Point& p_rel, int p_button_mask); //void mouse_motion(const Point& p_pos, const Point& p_rel, int p_button_mask);
Size2 get_minimum_size() const; Size2 get_minimum_size() const;

View File

@ -639,6 +639,7 @@ void SceneTree::_notification(int p_notification) {
} }
} break; } break;
case NOTIFICATION_OS_MEMORY_WARNING: case NOTIFICATION_OS_MEMORY_WARNING:
case NOTIFICATION_OS_IME_UPDATE:
case NOTIFICATION_WM_MOUSE_ENTER: case NOTIFICATION_WM_MOUSE_ENTER:
case NOTIFICATION_WM_MOUSE_EXIT: case NOTIFICATION_WM_MOUSE_EXIT:
case NOTIFICATION_WM_FOCUS_IN: case NOTIFICATION_WM_FOCUS_IN: