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

Add ALT NUM+ {hex code} character input support for LineEdit, TextEdit and CodeEdit. #55441

Merged
merged 1 commit into from
May 13, 2022
Merged
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
5 changes: 5 additions & 0 deletions scene/gui/code_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ void CodeEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
}

Ref<InputEventKey> k = p_gui_input;
if (TextEdit::alt_input(p_gui_input)) {
accept_event();
return;
}

bool update_code_completion = false;
if (!k.is_valid()) {
TextEdit::gui_input(p_gui_input);
Expand Down
36 changes: 36 additions & 0 deletions scene/gui/line_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,45 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {

if (k.is_valid()) {
if (!k->is_pressed()) {
if (alt_start && k->get_keycode() == Key::ALT) {
alt_start = false;
if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff && alt_code <= 0x10ffff)) {
char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
insert_text_at_caret(ucodestr);
}
accept_event();
return;
}
return;
}

// Alt+ Unicode input:
if (k->is_alt_pressed()) {
if (!alt_start) {
if (k->get_keycode() == Key::KP_ADD) {
alt_start = true;
alt_code = 0;
accept_event();
return;
}
} else {
if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) {
alt_code = alt_code << 4;
alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0);
}
if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) {
alt_code = alt_code << 4;
alt_code += (uint32_t)(k->get_keycode() - Key::KP_0);
}
if (k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) {
alt_code = alt_code << 4;
alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10;
}
accept_event();
return;
}
}

if (context_menu_enabled) {
if (k->is_action("ui_menu", true)) {
_ensure_menu();
Expand Down
3 changes: 3 additions & 0 deletions scene/gui/line_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class LineEdit : public Control {
bool pass = false;
bool text_changed_dirty = false;

bool alt_start = false;
uint32_t alt_code = 0;

String undo_text;
String text;
String placeholder;
Expand Down
45 changes: 45 additions & 0 deletions scene/gui/text_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,47 @@ void TextEdit::unhandled_key_input(const Ref<InputEvent> &p_event) {
}
}

bool TextEdit::alt_input(const Ref<InputEvent> &p_gui_input) {
Ref<InputEventKey> k = p_gui_input;
if (k.is_valid()) {
if (!k->is_pressed()) {
if (alt_start && k->get_keycode() == Key::ALT) {
alt_start = false;
if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff && alt_code <= 0x10ffff)) {
handle_unicode_input(alt_code);
}
return true;
}
return false;
}

if (k->is_alt_pressed()) {
if (!alt_start) {
if (k->get_keycode() == Key::KP_ADD) {
alt_start = true;
alt_code = 0;
return true;
}
} else {
if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) {
alt_code = alt_code << 4;
alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0);
}
if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) {
alt_code = alt_code << 4;
alt_code += (uint32_t)(k->get_keycode() - Key::KP_0);
}
if (k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) {
alt_code = alt_code << 4;
alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10;
}
return true;
}
}
}
return false;
}

void TextEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
ERR_FAIL_COND(p_gui_input.is_null());

Expand Down Expand Up @@ -1865,6 +1906,10 @@ void TextEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
Ref<InputEventKey> k = p_gui_input;

if (k.is_valid()) {
if (alt_input(p_gui_input)) {
accept_event();
return;
}
if (!k->is_pressed()) {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions scene/gui/text_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ class TextEdit : public Control {

bool setting_text = false;

bool alt_start = false;
uint32_t alt_code = 0;

// Text properties.
String ime_text = "";
Point2 ime_selection;
Expand Down Expand Up @@ -625,6 +628,7 @@ class TextEdit : public Control {
/* General overrides. */
virtual void unhandled_key_input(const Ref<InputEvent> &p_event) override;
virtual void gui_input(const Ref<InputEvent> &p_gui_input) override;
bool alt_input(const Ref<InputEvent> &p_gui_input);
virtual Size2 get_minimum_size() const override;
virtual bool is_text_field() const override;
virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
Expand Down