WIP: Unicode Character Text Entry #116679

Draft
Harley Acheson wants to merge 1 commits from Harley/blender:UnicodeEntry into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
1 changed files with 57 additions and 0 deletions

View File

@ -3660,6 +3660,61 @@ static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonDa
}
}
/**
* Allow holding Alt, enter Unicode character code, release alt, to get special characters.
*/
static char unicode_input[10] = {0};
static int ui_handle_unicode_input(uiBut *but,
uiHandleButtonData *data,
const wmEvent *event,
bool *changed)
{
if (ELEM(event->type, EVT_LEFTALTKEY, EVT_RIGHTALTKEY)) {
if (event->val == KM_PRESS) {
/* Alt first held down, so clear any saved input. */
unicode_input[0] = 0;
return WM_UI_HANDLER_CONTINUE;
}
else if (event->val == KM_RELEASE && unicode_input[0]) {
/* Alt released so deal with any previous entry. */
uint val = strtoul(unicode_input, NULL, 16);
if (val > 31 && val < 0x10FFFF) {
char utf8[10] = {0};
char32_t utf32[2] = {val, 0};
const int utf8_buf_len = BLI_str_utf32_as_utf8(utf8, utf32, 5);
*changed = ui_textedit_insert_buf(but, data, utf8, utf8_buf_len);
return *changed ? WM_UI_HANDLER_BREAK : WM_UI_HANDLER_CONTINUE;
}
}
}
if (event->modifier & KM_ALT && event->val == KM_PRESS) {
/* Holding Alt while pressing a key. */
char ch = 0;
if ((event->type >= EVT_ZEROKEY && event->type <= EVT_NINEKEY) ||
(event->type >= EVT_AKEY && event->type <= EVT_FKEY))
{
ch = event->type;
}
else if (event->type >= EVT_PAD0 && event->type <= EVT_PAD9) {
ch = event->type - 102;
}
if (ch) {
int len = BLI_strnlen(unicode_input, ARRAY_SIZE(unicode_input));
if (len < ARRAY_SIZE(unicode_input) - 1) {
unicode_input[len] = ch;
unicode_input[len + 1] = 0;
return WM_UI_HANDLER_BREAK;
}
}
}
return WM_UI_HANDLER_CONTINUE;
}
/**
* Return the jump type used for cursor motion & back-space/delete actions.
*/
@ -3818,6 +3873,8 @@ static void ui_do_but_textedit(
}
}
retval = ui_handle_unicode_input(but, data, event, &changed);
if (event->val == KM_PRESS && !is_ime_composing) {
switch (event->type) {
case EVT_VKEY: