diff options
author | San Jacobs | 2025-09-25 22:53:48 +0200 |
---|---|---|
committer | San Jacobs | 2025-09-25 22:53:48 +0200 |
commit | 88020820193ef01e0ee7e6e15efe55901250c911 (patch) | |
tree | 045f5fd5554dfb16f3be4c65d15210c247e45ab0 /src | |
parent | 0c09982fbbc554188ad4a048a136dced6981bacf (diff) | |
download | tafl-88020820193ef01e0ee7e6e15efe55901250c911.tar.gz tafl-88020820193ef01e0ee7e6e15efe55901250c911.tar.bz2 tafl-88020820193ef01e0ee7e6e15efe55901250c911.zip |
Buttons are being clicked
Diffstat (limited to 'src')
-rw-r--r-- | src/main.odin | 36 | ||||
-rw-r--r-- | src/tafl/tafl.odin | 93 |
2 files changed, 108 insertions, 21 deletions
diff --git a/src/main.odin b/src/main.odin index f28fab4..a621812 100644 --- a/src/main.odin +++ b/src/main.odin @@ -1,5 +1,6 @@ package main +import "core:fmt" import t "tafl" main :: proc() { @@ -28,17 +29,21 @@ main :: proc() { sizing_height=t.GROW, sizing_width=t.FIT, layout=.TOP_TO_BOTTOM) - {t.tafl(color={0,0,0,0}, - sizing_height=t.GROW, - sizing_width=t.GROW)} - {t.tafl( - padding={8,8,8,8}, - child_gap=8, - color={.0, .0, .0, 0.4}, - ) - button("Yeet") - button("Test") - button("Render") + { + t.tafl(color={0,0,0,0}, + sizing_height=t.GROW, + sizing_width=t.GROW)} + { + t.tafl( + padding={8,8,8,8}, + child_gap=8, + color={.0, .0, .0, 0.4}, + ) + { + if button("Yeet", "yeet").clicked do fmt.println("YEEET!") + if button("Test", "test").clicked do fmt.println("Test!") + if button("Render", "render").clicked do fmt.println("Render!") + } } } @@ -108,17 +113,20 @@ main :: proc() { } -button :: proc(text : string) { - t.tafl(sizing_width=t.FIXED(120), +button :: proc(text : string, id : string) -> t.Com { + com := t.tafl(sizing_width=t.FIXED(120), sizing_height=t.FIXED(40), color=colors.button_outline, - padding={2,2,2,2}) + padding={2,2,2,2}, + flags=t.BUTTON, + id=id) t.tafl(sizing_width=t.GROW, sizing_height=t.GROW, color={.1,.1,.1, 1}, position_horizontal=.MIDDLE, position_vertical=.MIDDLE,) t.tafl(text=text) + return com } Color_Scheme :: struct { diff --git a/src/tafl/tafl.odin b/src/tafl/tafl.odin index 369485b..8fb9902 100644 --- a/src/tafl/tafl.odin +++ b/src/tafl/tafl.odin @@ -18,10 +18,18 @@ child_index_buffer : [4096]int child_index_buffer_len : int temp_child_buffer : [1024]int -temp_child_buffer_len : int // May be inferred by child count, and not be needed +temp_child_buffer_len : int scissor_stack : [1024]Box +mouse_position : [2]int + +mouse_left_pressed := false +mouse_left_is_down := false +mouse_left_released := false + +clicked_id : string + DEFAULT_FONT : rl.Font FONT_SIZE : int : 24 @@ -46,8 +54,10 @@ tafl :: proc( padding : Sides = {0,0,0,0}, child_gap : int = 0, color : Color = {0,0,0,0}, - text : = "", - ) -> ^Tafl { + text := "", + id := "", + flags : Feature_Flags = {}, + ) -> Com { return tafl_open( /*width , @@ -62,7 +72,9 @@ tafl :: proc( padding, child_gap, color, - text) + text, + id, + flags,) } tafl_open :: proc( @@ -79,7 +91,9 @@ tafl_open :: proc( child_gap : int = 0, color : Color = {0,0,0,0}, text : = "", - ) -> ^Tafl{ + id := "", + flags : Feature_Flags = {}, + ) -> Com { sizing_height := sizing_height sizing_width := sizing_width @@ -112,9 +126,13 @@ tafl_open :: proc( parent = parent_ptr, text = text, + id = id, + flags = flags, } this_tafl : ^Tafl = &tafl_elements[tafl_elements_count] + if id == "" do this_tafl.id = text + if this_tafl.sizing.width.type == .FIXED { this_tafl.width = this_tafl.sizing.width.max } @@ -135,11 +153,25 @@ tafl_open :: proc( tafl_elements_count += 1 tafl_stack_depth += 1 - return this_tafl + + output_com : Com + output_com.__tafl_index = this_tafl.own_index + + if .CLICKABLE in flags { + if clicked_id != "" && clicked_id == this_tafl.id { + output_com.clicked = true + delete(clicked_id) + clicked_id = "" + } + } + + return output_com } -tafl_close :: proc(tafl : ^Tafl) { +tafl_close :: proc(com : Com) { + + tafl : ^Tafl = &tafl_elements[com.__tafl_index] total_child_gap := max(tafl.children.len - 1, 0) * tafl.child_gap switch tafl.layout { @@ -348,6 +380,17 @@ render :: proc() { rl.DrawFPS(5, 5) rl.EndDrawing() + + + mouse_left_pressed = rl.IsMouseButtonPressed(rl.MouseButton.LEFT) + mouse_left_is_down = rl.IsMouseButtonDown(rl.MouseButton.LEFT) + mouse_left_released = rl.IsMouseButtonReleased(rl.MouseButton.LEFT) + + temp_mouse_position := rl.GetMousePosition() + mouse_position = {int(temp_mouse_position.x), int(temp_mouse_position.y)} + + process_features() + clear_layout() } @@ -423,6 +466,21 @@ get_window_size :: proc() -> (i32, i32) { return rl.GetScreenWidth(), rl.GetScreenHeight() } +process_features :: proc() { + for tafl in tafl_elements { + if .CLICKABLE in tafl.flags { + if mouse_left_released { + if (tafl.x <= mouse_position.x && mouse_position.x <= (tafl.x+tafl.width) && + tafl.y <= mouse_position.y && mouse_position.y <= (tafl.y+tafl.height)) { + if clicked_id != "" do delete(clicked_id) + clicked_id = strings.clone(tafl.id) + fmt.printfln("Clicked! {}", tafl.id) + } + } + } + } +} + Sizing_Dimension :: struct { type : enum{ FIT, @@ -475,6 +533,23 @@ Tafl :: struct { child_gap : int, color : Color, text : string, + id : string, + + flags : Feature_Flags, + + // transitions + hover_t : f32, + press_t : f32, +} + +Feature_Flag :: enum { + CLICKABLE, +} +Feature_Flags :: bit_set[Feature_Flag] + +Com :: struct { + __tafl_index : int, + clicked : bool, } Box :: struct { @@ -520,6 +595,10 @@ FIXED :: proc(x: int) -> Sizing_Dimension { return {.FRACTIONAL, x, x} }*/ +BUTTON : Feature_Flags : { + .CLICKABLE +} + u8_clamp :: proc(input: f32) -> u8 { mult := input*255 output := u8(mult) |