From 976f6e0df372ccced14f7baca903ec9e483c89ae Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Tue, 4 Feb 2020 15:21:28 +0100 Subject: improve textbox --- src/input.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- src/ui.c | 12 ++++++------ 2 files changed, 61 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/input.c b/src/input.c index 4ee795b..cd68ba5 100644 --- a/src/input.c +++ b/src/input.c @@ -199,9 +199,46 @@ void keyboard_handle_input_string(platform_window *window, keyboard_input *keybo } } + if (keyboard_is_key_pressed(keyboard, KEY_X)) + { + bool is_lctrl_down = keyboard->keys[KEY_LEFT_CONTROL]; + + if (is_lctrl_down) + { + // copy selection + if (keyboard->has_selection) + { + char buffer[MAX_INPUT_LENGTH]; + utf8_str_copy_range(keyboard->input_text, + keyboard->selection_begin_offset, + keyboard->selection_begin_offset+keyboard->selection_length, + buffer); + + if (!string_equals(buffer, "")) + platform_set_clipboard(window, buffer); + } + + // delete selection + if (keyboard->has_selection) + { + utf8_str_remove_range(keyboard->input_text, keyboard->selection_begin_offset, + keyboard->selection_begin_offset + keyboard->selection_length); + keyboard->has_selection = false; + keyboard->text_changed = true; + + if (keyboard->selection_begin_offset < keyboard->cursor) + { + keyboard->cursor -= keyboard->selection_length; + } + } + + keyboard->input_text_len = utf8len(keyboard->input_text); + } + } + if (keyboard_is_key_down(keyboard, KEY_BACKSPACE)) { - current_keyboard_to_handle->keys[KEY_BACKSPACE] = false; + keyboard->keys[KEY_BACKSPACE] = false; bool is_lctrl_down = keyboard->keys[KEY_LEFT_CONTROL]; @@ -214,16 +251,29 @@ void keyboard_handle_input_string(platform_window *window, keyboard_input *keybo if (keyboard->selection_begin_offset < keyboard->cursor) { - keyboard->cursor -= keyboard->selection_length-1; + keyboard->cursor -= keyboard->selection_length; } } else if (is_lctrl_down) { - for (s32 i = 0; i < keyboard->cursor; i++) + char *iter = keyboard->input_text; + s32 index = 0; + s32 found_index = 0; + utf8_int32_t br; + while((iter = utf8codepoint(iter, &br)) && index < keyboard->cursor && br) + { + index++; + if (br == ' ' && index+1 < keyboard->cursor) + { + found_index = index; + } + } + + for (s32 i = found_index; i < keyboard->cursor; i++) { - utf8_str_remove_at(keyboard->input_text, 0); + utf8_str_remove_at(keyboard->input_text, found_index); } - keyboard->cursor = 0; + keyboard->cursor = found_index; keyboard->text_changed = true; } else if (keyboard->cursor > 0) diff --git a/src/ui.c b/src/ui.c index ef71340..a06a4aa 100644 --- a/src/ui.c +++ b/src/ui.c @@ -839,21 +839,21 @@ bool ui_push_textbox(textbox_state *state, char *placeholder) if (is_selecting) { // move text offset x when selecting so we can select more text than available on screen. - if (global_ui_context.mouse->x < x + 20) + if (global_ui_context.mouse->x < x + 10) { s32 text_w = calculate_text_width(global_ui_context.font_small, state->buffer); - if (text_w > TEXTBOX_WIDTH-20) + if (text_w > TEXTBOX_WIDTH-10) { state->diff -= TEXTBOX_SCROLL_X_SPEED; if (state->diff < 0) state->diff = 0; } } - if (global_ui_context.mouse->x > x + TEXTBOX_WIDTH - 25) + if (global_ui_context.mouse->x > x + TEXTBOX_WIDTH - 10) { s32 text_w = calculate_text_width(global_ui_context.font_small, state->buffer); - s32 diff = text_w - TEXTBOX_WIDTH + 25; + s32 diff = text_w - TEXTBOX_WIDTH + 10; - if (text_w > TEXTBOX_WIDTH-25) + if (text_w > TEXTBOX_WIDTH-10) { state->diff += TEXTBOX_SCROLL_X_SPEED; if (state->diff > diff) @@ -1440,7 +1440,7 @@ inline void ui_end_menu_bar() global_ui_context.layout.offset_x = 0; global_ui_context.layout.offset_y += MENU_BAR_HEIGHT; - ui_push_separator(); + //ui_push_separator(); } inline void ui_destroy() -- cgit v1.2.3-70-g09d2