added "window" system
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 9 Jul 2021 03:51:00 +0000 (22:51 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 9 Jul 2021 03:51:00 +0000 (22:51 -0500)
src/build.onyx
src/main.onyx
src/ui/window.onyx [new file with mode: 0644]

index 50c527a1b8836b5c3d1e2e3b985d8f74f34f4450..5d96599c280aab72ba186b1507d150d4fa1c841c 100644 (file)
@@ -13,6 +13,7 @@
     #load "modules/bmfont/module"
 
     #load "src/main"
+    #load "src/ui/window"
 }
 
 #if (package runtime).Runtime == (package runtime).Runtime_Wasi {
index 0c2051cf5fe536559cb83904ab06aee51f5c7e79..f38c138200e6bdc6efb1be483b69e05ad8fa2619 100644 (file)
@@ -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 (file)
index 0000000..221cfbc
--- /dev/null
@@ -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