From b52a35c75318bcc4a8b6599bb926f2ac3a869955 Mon Sep 17 00:00:00 2001 From: Martin Fouilleul Date: Sun, 5 Mar 2023 17:03:00 +0100 Subject: [PATCH] [ui, textbox] Fixed selection background rectangle --- examples/ui/main.c | 5 ----- src/ui.c | 25 +++++++++++++------------ 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/examples/ui/main.c b/examples/ui/main.c index 93bbe25..7905d22 100644 --- a/examples/ui/main.c +++ b/examples/ui/main.c @@ -111,11 +111,6 @@ int main() } } - if(mp_key_pressed(MP_KEY_C)) - { - printf("pressed C!\n"); - } - mg_surface_prepare(surface); mp_rect frame = mp_window_get_content_rect(window); diff --git a/src/ui.c b/src/ui.c index f706ca4..cad8d4b 100644 --- a/src/ui.c +++ b/src/ui.c @@ -2026,7 +2026,7 @@ void ui_text_box_render(ui_box* box, void* data) str32 before = str32_slice(codepoints, 0, firstDisplayedChar); mp_rect beforeBox = mg_text_bounding_box_utf32(style->font, style->fontSize, before); - f32 textMargin = 5; + f32 textMargin = 5; //TODO: make that configurable f32 textX = textMargin + box->rect.x - beforeBox.w; f32 textTop = box->rect.y + 0.5*(box->rect.h - lineHeight); @@ -2039,7 +2039,7 @@ void ui_text_box_render(ui_box* box, void* data) str32 beforeSelect = str32_slice(codepoints, 0, selectStart); mp_rect beforeSelectBox = mg_text_bounding_box_utf32(style->font, style->fontSize, beforeSelect); - beforeSelectBox.x += textX + beforeBox.x + beforeBox.w; + beforeSelectBox.x += textX; beforeSelectBox.y += textY; if(selectStart != selectEnd) @@ -2116,6 +2116,8 @@ ui_text_box_result ui_text_box(const char* name, mem_arena* arena, str8 text) ui_box* frame = ui_box_make(name, frameFlags); ui_style* style = &frame->computedStyle; + f32 textMargin = 5; //TODO parameterize this margin! must be the same as in ui_text_box_render + mg_font_extents extents = mg_font_get_scaled_extents(style->font, style->fontSize); ui_box_set_size(frame, UI_AXIS_Y, UI_SIZE_PIXELS, extents.ascent+extents.descent+10, 1); @@ -2144,7 +2146,6 @@ ui_text_box_result ui_text_box(const char* name, mem_arena* arena, str8 text) { //NOTE: set cursor/extend selection on mouse press or drag vec2 pos = ui_mouse_position(); - f32 textMargin = 5; //TODO parameterize this margin! must be the same as in ui_text_box_render f32 cursorX = pos.x - frame->rect.x - textMargin; str32 codepoints = utf8_push_to_codepoints(&ui->frameArena, text); @@ -2222,20 +2223,21 @@ ui_text_box_result ui_text_box(const char* name, mem_arena* arena, str8 text) } } - //NOTE(martin): text box focus shortcuts - if(mp_key_pressed(MP_KEY_ENTER)) - { - //TODO(martin): extract in gui_edit_complete() (and use below) - ui_box_deactivate(frame); - ui->focus = 0; - } - + //NOTE(martin): check changed/accepted if(oldCodepoints.ptr != codepoints.ptr) { result.changed = true; result.text = utf8_push_from_codepoints(arena, codepoints); } + if(mp_key_pressed(MP_KEY_ENTER)) + { + //TODO(martin): extract in gui_edit_complete() (and use below) + result.accepted = true; + ui_box_deactivate(frame); + ui->focus = 0; + } + //NOTE slide contents { if(ui->editCursor < ui->editFirstDisplayedChar) @@ -2244,7 +2246,6 @@ ui_text_box_result ui_text_box(const char* name, mem_arena* arena, str8 text) } else { - f32 textMargin = 5.; //TODO: pull this out and make it configurable i32 firstDisplayedChar = ui->editFirstDisplayedChar; str32 firstToCursor = str32_slice(codepoints, firstDisplayedChar, ui->editCursor); mp_rect firstToCursorBox = mg_text_bounding_box_utf32(style->font, style->fontSize, firstToCursor);