diff options
author | San Jacobs | 2025-09-27 18:40:35 +0200 |
---|---|---|
committer | San Jacobs | 2025-09-27 18:40:35 +0200 |
commit | 1f77cd05fdbed146afac1020954e4910fa01875f (patch) | |
tree | abc1dfa194019fa7acc58ad1a9004da9c1309b12 | |
parent | 58bb80100d3d74aeaf13a89360caddbc1e84fd97 (diff) | |
download | tafl-1f77cd05fdbed146afac1020954e4910fa01875f.tar.gz tafl-1f77cd05fdbed146afac1020954e4910fa01875f.tar.bz2 tafl-1f77cd05fdbed146afac1020954e4910fa01875f.zip |
Coms have hovering and pressing data! Buttons give feedback!
-rw-r--r-- | src/main.odin | 5 | ||||
-rw-r--r-- | src/tafl/tafl.odin | 86 |
2 files changed, 76 insertions, 15 deletions
diff --git a/src/main.odin b/src/main.odin index a621812..ead0e83 100644 --- a/src/main.odin +++ b/src/main.odin @@ -120,9 +120,12 @@ button :: proc(text : string, id : string) -> t.Com { padding={2,2,2,2}, flags=t.BUTTON, id=id) + color : t.Color = {.1,.1,.1, 1} + if com.hover do color={.2,.2,.2, 1} + if com.clicked || com.is_down do color={.05,.05,.05, 1} t.tafl(sizing_width=t.GROW, sizing_height=t.GROW, - color={.1,.1,.1, 1}, + color=color, position_horizontal=.MIDDLE, position_vertical=.MIDDLE,) t.tafl(text=text) diff --git a/src/tafl/tafl.odin b/src/tafl/tafl.odin index b92651d..993a231 100644 --- a/src/tafl/tafl.odin +++ b/src/tafl/tafl.odin @@ -27,9 +27,11 @@ mouse_position : [2]int mouse_left_pressed := false mouse_left_is_down := false mouse_left_released := false +mouse_press_position : [2]int = {-1, -1} clicked_pressed_id : string clicked_release_id : string +hovered_id : string DEFAULT_FONT : rl.Font FONT_SIZE : int : 24 @@ -159,13 +161,23 @@ tafl_open :: proc( output_com.__tafl_index = this_tafl.own_index if .CLICKABLE in flags { - if clicked_pressed_id != "" && clicked_release_id != "" { - if clicked_pressed_id == this_tafl.id && clicked_release_id == this_tafl.id { - output_com.clicked = true - delete(clicked_pressed_id) - delete(clicked_release_id) - clicked_pressed_id = "" - clicked_release_id = "" + if mouse_left_released { + if clicked_pressed_id != "" && clicked_release_id != "" { + if clicked_pressed_id == this_tafl.id && clicked_release_id == this_tafl.id { + output_com.clicked = true + delete(clicked_pressed_id) + delete(clicked_release_id) + clicked_pressed_id = "" + clicked_release_id = "" + } + } + } + } + if .HOVERABLE in flags { + if hovered_id == this_tafl.id { + output_com.hover = true + if mouse_left_is_down && clicked_pressed_id == this_tafl.id { + output_com.is_down = true } } } @@ -362,7 +374,6 @@ position_children :: proc(parent: ^Tafl) { } top_offset += child.height + parent.child_gap } - } } @@ -387,6 +398,18 @@ render :: proc() { rl.EndDrawing() + if mouse_left_released { + mouse_press_position = {-1, -1} + if clicked_pressed_id != "" { + delete(clicked_pressed_id) + clicked_pressed_id = "" + } + if clicked_release_id != "" { + delete(clicked_release_id) + clicked_release_id = "" + } + } + 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) @@ -394,6 +417,8 @@ render :: proc() { temp_mouse_position := rl.GetMousePosition() mouse_position = {int(temp_mouse_position.x), int(temp_mouse_position.y)} + if mouse_left_pressed do mouse_press_position = mouse_position + process_features() clear_layout() @@ -480,27 +505,45 @@ process_features :: proc() { clicked_release_id = "" } + something_is_hovered := false + for tafl in tafl_elements { if .CLICKABLE in tafl.flags { if mouse_left_pressed { - // Collision check - 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 collide(mouse_position, tafl) { if clicked_pressed_id != "" do delete(clicked_pressed_id) clicked_pressed_id = strings.clone(tafl.id) fmt.printfln("Pressed! {}", tafl.id) } } if mouse_left_released { - // Collision check - 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 collide(mouse_position, tafl) { if clicked_release_id != "" do delete(clicked_release_id) clicked_release_id = strings.clone(tafl.id) fmt.printfln("Released! {}", tafl.id) } } } + + if .HOVERABLE in tafl.flags { + if collide(mouse_position, tafl) { + something_is_hovered = true + if hovered_id != tafl.id { + if hovered_id != "" do delete(hovered_id) + hovered_id = strings.clone(tafl.id) + fmt.printfln("Hovering over: {}", tafl.id) + } + + } + } + } + + if !something_is_hovered { + if hovered_id != "" { + delete(hovered_id) + hovered_id = "" + fmt.printfln("Hovering over: {}", "nil") + } } } @@ -574,6 +617,13 @@ Feature_Flags :: bit_set[Feature_Flag] Com :: struct { __tafl_index : int, clicked : bool, + hover : bool, + is_down : bool, + press_root : [2]int, + drag_delta : [2]int, + + hover_t : f32, + press_t : f32, } Box :: struct { @@ -588,6 +638,14 @@ box_clamp :: proc(parent, child : Box) -> (output : Box) { return } +collision_check_coords :: proc(point : [2]int, x, y, w, h : int) -> bool { + return x <= point.x && point.x <= (x+w) && y <= point.y && point.y <= (y+h) +} +collision_check_tafl :: proc(point : [2]int, tafl : Tafl) -> bool { + return collision_check_coords(point, tafl.x, tafl.y, tafl.width, tafl.height) +} +collide :: proc{collision_check_tafl, collision_check_coords} + __child_iterator_index : int = 0 // TODO: NOT THIS children_of :: proc(tafl: ^Tafl) -> (child: ^Tafl, ok: bool) { if __child_iterator_index >= tafl.children.len { |