From 1907663cdc41d95a13ab03acada94c0e856479bd Mon Sep 17 00:00:00 2001 From: Martin Fouilleul Date: Tue, 29 Aug 2023 18:43:25 +0200 Subject: [PATCH] Skip drawing ui box when it's rect + boders are fully outside current clip --- src/graphics/graphics.h | 1 + src/graphics/graphics_common.c | 13 ++++++++++++ src/ui/ui.c | 36 ++++++++++++++++++++++++++++------ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/graphics/graphics.h b/src/graphics/graphics.h index 46761a8..f8dd612 100644 --- a/src/graphics/graphics.h +++ b/src/graphics/graphics.h @@ -283,6 +283,7 @@ ORCA_API void oc_matrix_pop(void); ORCA_API void oc_clip_push(f32 x, f32 y, f32 w, f32 h); ORCA_API void oc_clip_pop(void); +ORCA_API oc_rect oc_clip(); //------------------------------------------------------------------------------------------ //NOTE(martin): graphics attributes setting/getting diff --git a/src/graphics/graphics_common.c b/src/graphics/graphics_common.c index 06cb44b..ccbefa4 100644 --- a/src/graphics/graphics_common.c +++ b/src/graphics/graphics_common.c @@ -972,6 +972,19 @@ void oc_clip_pop() } } +oc_rect oc_clip() +{ + oc_rect clip = { -FLT_MAX / 2, -FLT_MAX / 2, FLT_MAX, FLT_MAX }; + + oc_canvas_data* canvas = __mgCurrentCanvas; + if(canvas) + { + clip = oc_clip_stack_top(canvas); + } + + return (clip); +} + //------------------------------------------------------------------------------------------ //NOTE(martin): graphics attributes setting/getting //------------------------------------------------------------------------------------------ diff --git a/src/ui/ui.c b/src/ui/ui.c index f66650d..b2bfac3 100644 --- a/src/ui/ui.c +++ b/src/ui/ui.c @@ -1300,18 +1300,40 @@ void oc_ui_draw_box(oc_ui_box* box) oc_ui_style* style = &box->style; + bool draw = true; + + { + oc_rect clip = oc_clip(); + oc_rect expRect = { + box->rect.x - 0.5 * style->borderSize, + box->rect.y - 0.5 * style->borderSize, + box->rect.w + style->borderSize, + box->rect.h + style->borderSize + }; + + if((expRect.x + expRect.w < clip.x) + || (expRect.y + expRect.h < clip.y) + || (expRect.x > clip.x + clip.w) + || (expRect.y > clip.y + clip.h)) + { + draw = false; + } + } + if(box->flags & OC_UI_FLAG_CLIP) { oc_clip_push(box->rect.x, box->rect.y, box->rect.w, box->rect.h); } - if(box->flags & OC_UI_FLAG_DRAW_BACKGROUND) + if(draw && (box->flags & OC_UI_FLAG_DRAW_BACKGROUND)) { oc_set_color(style->bgColor); oc_ui_rectangle_fill(box->rect, style->roundness); } - if((box->flags & OC_UI_FLAG_DRAW_PROC) && box->drawProc) + if(draw + && (box->flags & OC_UI_FLAG_DRAW_PROC) + && box->drawProc) { box->drawProc(box, box->drawData); } @@ -1321,7 +1343,7 @@ void oc_ui_draw_box(oc_ui_box* box) oc_ui_draw_box(child); } - if(box->flags & OC_UI_FLAG_DRAW_TEXT) + if(draw && (box->flags & OC_UI_FLAG_DRAW_TEXT)) { oc_rect textBox = oc_text_bounding_box(style->font, style->fontSize, box->string); @@ -1371,7 +1393,7 @@ void oc_ui_draw_box(oc_ui_box* box) oc_clip_pop(); } - if(box->flags & OC_UI_FLAG_DRAW_BORDER) + if(draw && (box->flags & OC_UI_FLAG_DRAW_BORDER)) { oc_set_width(style->borderSize); oc_set_color(style->borderColor); @@ -2675,7 +2697,9 @@ void oc_ui_edit_perform_move(oc_ui_context* ui, oc_ui_edit_move move, int direct { ui->editCursor--; } - } else { + } + else + { while(ui->editCursor > 0 && !oc_ui_edit_is_whitespace(codepoints.ptr[ui->editCursor - 1]) && !oc_ui_edit_is_word_separator(codepoints.ptr[ui->editCursor - 1])) @@ -2995,7 +3019,7 @@ oc_ui_text_box_result oc_ui_text_box(const char* name, oc_arena* arena, oc_str8 //NOTE handle shortcuts oc_keymod_flags mods = oc_key_mods(&ui->input); - const oc_ui_edit_command* editCommands; + const oc_ui_edit_command* editCommands; u32 editCommandCount; oc_host_platform hostPlatform = oc_get_host_platform(); switch(hostPlatform) -- 2.25.1