From 9ccfcd14f2835c1230f6c70fa5299fcd0ad86ad2 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 8 Jul 2021 22:51:00 -0500 Subject: [PATCH] added "window" system --- src/build.onyx | 1 + src/main.onyx | 24 ++++++++++++++++--- src/ui/window.onyx | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/ui/window.onyx diff --git a/src/build.onyx b/src/build.onyx index 50c527a..5d96599 100644 --- a/src/build.onyx +++ b/src/build.onyx @@ -13,6 +13,7 @@ #load "modules/bmfont/module" #load "src/main" + #load "src/ui/window" } #if (package runtime).Runtime == (package runtime).Runtime_Wasi { diff --git a/src/main.onyx b/src/main.onyx index 0c2051c..f38c138 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -43,6 +43,12 @@ init :: () { gl.bindTexture(gl.TEXTURE_2D, -1); } + test_window.size = .{ 800, 800 }; + test_window.border_width = 8.0f; + test_window.border_color = config.Colors.primary; + test_window.background_color = config.Colors.dark_background; + test_window.active_color = config.Colors.background; + load_colors :: () { json :: package json @@ -169,8 +175,12 @@ poll_events :: () -> bool { } update :: (dt: f32) { + t += dt; } +t := 0.0f; +test_window : ui.Window_State; + draw :: () { bg_color := config.Colors.background; gl.clearColor(bg_color.r, bg_color.g, bg_color.b, bg_color.a); @@ -180,11 +190,19 @@ draw :: () { menu_bar, main_area := ui.Flow.split_horizontal(window_rectangle, top_height=32); ui.workspace_start(main_area); - draw_background_lines(~~window_width, ~~window_height, line_color=config.Colors.background); - if ui.button(.{ 200, 300, 500, 400 }, "Test Button") do search_buffer.count = 0; - ui.textbox(.{ 200, 400, 500, 450 }, ^search_buffer); + ui.draw_rect(0, 0, 20, 20, color=.{1,0,0}); + + { + test_window.position = .{ 200, 200 }; + + ui.window_start(^test_window); + defer ui.window_end(); + + if ui.button(.{ 0, 0, 400, 300 }, "Top-Left") do search_buffer.count = 0; + ui.textbox(.{ 0, 300, 400, 350 }, ^search_buffer); + } ui.workspace_end(); diff --git a/src/ui/window.onyx b/src/ui/window.onyx new file mode 100644 index 0000000..221cfbc --- /dev/null +++ b/src/ui/window.onyx @@ -0,0 +1,58 @@ +package ui +#private_file map :: package core.map + +// This will be stored by the end user's library. +Window_State :: struct { + position: gfx.Vector2; + size: gfx.Vector2; + + border_width := 4.0f; + border_color := gfx.Color4.{ 1, 0, 0 }; + background_color := gfx.Color4.{ 0.2, 0.2, 0.2 }; + active_color := gfx.Color4.{ 0.3, 0.3, 0.3 }; + + get_rectangle :: (use w: ^Window_State) -> Rectangle { + return .{ position.x, position.y, position.x + size.x, position.y + size.y }; + } +} + +// I don't know if this needs the site and increment parameters +window_start :: (use state: ^Window_State, site := #callsite, increment := 0) { + + hash := get_site_hash(site, increment); + animation_state := map.get(^animation_states, hash); + + mx, my := get_mouse_position(); + contained := false; + if state->get_rectangle() |> Rectangle.contains(mx, my) { + contained = true; + } + + if contained { + move_towards(^animation_state.hover_time, 1.0f, 0.06); + } else { + move_towards(^animation_state.hover_time, 0.0f, 0.06); + } + + x, y := position.x + border_width, position.y + border_width; + w, h := size.x - border_width * 2, size.y - border_width * 2; + + bg_color := color_lerp(animation_state.hover_time, background_color, active_color); + draw_rect(position.x, position.y, size.x, size.y, color=border_color); + draw_rect(x, y, w, h, bg_color); + + if animation_state.click_time > 0 || animation_state.hover_time > 0 { + map.put(^animation_states, hash, animation_state); + } else { + map.delete(^animation_states, hash); + } + + gfx.push_scissor(x, y, w, h); + gfx.push_matrix(); + gfx.apply_transform(.{ translation = position, scale = .{ 1, 1 } }); +} + +window_end :: () { + gfx.pop_scissor(); + gfx.pop_matrix(); +} \ No newline at end of file -- 2.25.1