From ee8b68ac3e7439e6aa7d33a2ee6496b2535f77f4 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Thu, 14 Mar 2024 10:11:04 -0400 Subject: [PATCH] Fixed text auto-layout --- code/api.odin | 2 +- code/grime_hashmap_zpl.odin | 2 ++ code/grime_string_interning.odin | 2 +- code/tick_render.odin | 2 +- code/tick_update.odin | 9 ++++---- code/ui_layout.odin | 36 +++++++++++--------------------- code/ui_tests.odin | 9 ++++++-- 7 files changed, 29 insertions(+), 33 deletions(-) diff --git a/code/api.odin b/code/api.odin index 0ce1237..3fb9239 100644 --- a/code/api.odin +++ b/code/api.odin @@ -145,7 +145,7 @@ startup :: proc( prof : ^SpallProfiler, persistent_mem, frame_mem, transient_mem rl.SetConfigFlags( { rl.ConfigFlag.WINDOW_RESIZABLE, - rl.ConfigFlag.WINDOW_TOPMOST, + // rl.ConfigFlag.WINDOW_TOPMOST, }) // Rough setup of window with rl stuff diff --git a/code/grime_hashmap_zpl.odin b/code/grime_hashmap_zpl.odin index d8f860c..4e24eb1 100644 --- a/code/grime_hashmap_zpl.odin +++ b/code/grime_hashmap_zpl.odin @@ -247,6 +247,8 @@ zpl_hmap_find :: proc( using self : ^ HMapZPL( $ Type), key : u64 ) -> HMapZPL_F result.hash_index = cast(i64)( key % hashes.num ) result.entry_index = hashes.data[ result.hash_index ] + verify( result.entry_index < i64(entries.num), "Entry index is larger than the number of entries" ) + for ; result.entry_index >= 0; { entry := & entries.data[ result.entry_index ] if entry.key == key { diff --git a/code/grime_string_interning.odin b/code/grime_string_interning.odin index 5c452f4..b4205d3 100644 --- a/code/grime_string_interning.odin +++ b/code/grime_string_interning.odin @@ -53,7 +53,7 @@ str_cache_init :: proc( /*allocator : Allocator*/ ) -> ( cache : StringCache ) { cache.slab, alloc_error = slab_init( & policy, allocator = persistent_allocator() ) verify(alloc_error == .None, "Failed to initialize the string cache" ) - cache.table, alloc_error = zpl_hmap_init_reserve( StringCached, persistent_slab_allocator(), 2 * Kilobyte ) + cache.table, alloc_error = zpl_hmap_init_reserve( StringCached, persistent_slab_allocator(), 64 * Kilobyte ) return } diff --git a/code/tick_render.odin b/code/tick_render.odin index f812312..5d49644 100644 --- a/code/tick_render.odin +++ b/code/tick_render.odin @@ -150,7 +150,7 @@ render_mode_2d :: proc() computed_size := computed.bounds.p1 - computed.bounds.p0 if ! within_range2( view_bounds, computed.bounds ) { - // continue + continue } // TODO(Ed) : Render Borders diff --git a/code/tick_update.odin b/code/tick_update.odin index 2b8f1fd..4117d94 100644 --- a/code/tick_update.odin +++ b/code/tick_update.odin @@ -240,10 +240,10 @@ update :: proc( delta_time : f64 ) -> b32 // test_text_box() // test_parenting() - if false + if true { - frame := ui_widget( "Frame", {} ) - ui_parent(frame) + // frame := ui_widget( "Frame", {} ) + // ui_parent(frame) parent_layout := default_layout parent_layout.size = range2( { 300, 300 }, {} ) @@ -310,12 +310,13 @@ update :: proc( delta_time : f64 ) -> b32 } // Whitespace AST test - if true + if false { profile("Whitespace AST test") text_style := frame_style_default text_style.flags = { + .Size_To_Text, .Fixed_Position_X, .Fixed_Position_Y, // .Fixed_Width, .Fixed_Height, } diff --git a/code/ui_layout.odin b/code/ui_layout.odin index 95a9489..89a3652 100644 --- a/code/ui_layout.odin +++ b/code/ui_layout.odin @@ -70,17 +70,9 @@ ui_compute_layout :: proc() // 1. Anchors anchor := & layout.anchor - // anchored_pos := parent_content.min + parent_content_size * anchor.min - // anchored_size := parent_content_size * linalg.abs( anchor.p1 - anchor.p0 ) anchored_bounds := range2( - { - parent_content.min.x + parent_content_size.x * anchor.min.x, - parent_content.min.y + parent_content_size.y * anchor.min.y, - }, - { - parent_content.max.x - parent_content_size.x * anchor.max.x, - parent_content.max.y - parent_content_size.y * anchor.max.y, - } + parent_content.min + parent_content_size * anchor.min, + parent_content.max - parent_content_size * anchor.max, ) anchored_bounds_origin := (anchored_bounds.min + anchored_bounds.max) * 0.5 @@ -120,11 +112,8 @@ ui_compute_layout :: proc() text_size = computed.text_size } - // 4. Adjust Alignment of pivot position - alignment := layout.alignment - alignment_offset := Vec2 { - // adjusted_size.x * (alignment.x - 0.5), - // adjusted_size.y * (alignment.y - 0.5), + if size_to_text { + adjusted_size = text_size } // 5. Determine relative position @@ -142,7 +131,9 @@ ui_compute_layout :: proc() vec2_one := Vec2 { 1, 1 } - // Determine the box bounds + // 6. Determine the box bounds + // Adjust Alignment of pivot position + alignment := layout.alignment bounds := range2( rel_pos - adjusted_size * alignment, rel_pos + adjusted_size * (vec2_one - alignment), @@ -169,17 +160,14 @@ ui_compute_layout :: proc() computed.content = content_bounds // TODO(Ed): Needs a rework based on changes to rest of layout above being changed - // Text + // Text if len(current.text.str) > 0 { - bottom_left := content_bounds.min - top_right := content_bounds.max - - content_size := Vec2 { top_right.x - bottom_left.x, top_right.y - bottom_left.y } + content_size := content_bounds.max - content_bounds.min text_pos : Vec2 - text_pos = top_right - text_pos.x += (-content_size.x - text_size.x) * layout.text_alignment.x - text_pos.y += (-content_size.y + text_size.y) * layout.text_alignment.y + text_pos = content_bounds.min + { 0, text_size.y } + text_pos.x += ( content_size.x - text_size.x ) * layout.text_alignment.x + text_pos.y += ( content_size.y - text_size.y ) * layout.text_alignment.y computed.text_size = text_size computed.text_pos = { text_pos.x, -text_pos.y } diff --git a/code/ui_tests.odin b/code/ui_tests.odin index a12f728..a19a052 100644 --- a/code/ui_tests.odin +++ b/code/ui_tests.odin @@ -26,7 +26,7 @@ test_draggable :: proc() draggable_layout := UI_Layout { anchor = {}, // alignment = { 0.0, 0.5 }, - alignment = { 0.5, 0.5 }, + alignment = { 0.5, 0.5 }, text_alignment = { 0.0, 0.0 }, // alignment = { 1.0, 1.0 }, // corner_radii = { 0.3, 0.3, 0.3, 0.3 }, @@ -74,10 +74,13 @@ test_text_box :: proc() @static pos : Vec2 style := ui_style_peek( .Default ) + style.text_alignment = { 1.0, 1.0 } + // style.flags = { .Size_To_Text } + style.padding = { 10, 10, 10, 10 } + style.font_size = 32 ui_style_theme( { styles = { style, style, style, style, }} ) text := str_intern( "Lorem ipsum dolor sit amet") - font_size := 30 text_box := ui_text("TEXT BOX!", text, flags = { .Mouse_Clickable }) if text_box.first_frame { @@ -89,4 +92,6 @@ test_text_box :: proc() } text_box.style.layout.pos = pos + + text_box.style.size.min = { text_box.computed.text_size.x * 1.5, text_box.computed.text_size.y * 3 } }