From 8da1321bddeca1fa11c3827a5282cd20a0303b75 Mon Sep 17 00:00:00 2001 From: Stefan Stefanov Date: Fri, 19 Apr 2024 21:30:09 +0300 Subject: [PATCH] Added working text input box --- .vscode/settings.json | 2 +- src/game.odin | 57 +++++++++++++++++++++++++++-------------- src/input_box.odin | 15 +++++++++++ src/symbol_exports.odin | 2 ++ 4 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 src/input_box.odin diff --git a/.vscode/settings.json b/.vscode/settings.json index 08e7ac8..91653d1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,5 @@ "activityBar.background": "#322C2D", "titleBar.activeBackground": "#463E3F", "titleBar.activeForeground": "#FAFAFA" - } +} } \ No newline at end of file diff --git a/src/game.odin b/src/game.odin index c257fa7..35ca8a3 100644 --- a/src/game.odin +++ b/src/game.odin @@ -17,42 +17,61 @@ import rl "vendor:raylib" PixelWindowHeight :: 180 -GameMemory :: struct {} +FILE_DIALOG_SIZE :: 1000 +GameMemory :: struct { + file_dialog_text_buffer: [FILE_DIALOG_SIZE + 1]u8, +} g_mem: ^GameMemory +w, h: f32 + game_camera :: proc() -> rl.Camera2D { - w := f32(rl.GetScreenWidth()) - h := f32(rl.GetScreenHeight()) + w = f32(rl.GetScreenWidth()) + h = f32(rl.GetScreenHeight()) return {zoom = h / PixelWindowHeight, target = {}, offset = {w / 2, h / 2}} } +scaling: f32 = 2 ui_camera :: proc() -> rl.Camera2D { - return {zoom = f32(rl.GetScreenHeight()) / PixelWindowHeight} + // return {zoom = f32(rl.GetScreenHeight()) / PixelWindowHeight} + return {zoom = scaling} } +input_box_loc: rl.Vector2 = {} +moving_input_box: bool update :: proc() { + // Update the width/height + w = f32(rl.GetScreenWidth()) + h = f32(rl.GetScreenHeight()) + rl.SetMouseScale(1 / scaling, 1 / scaling) + + if rl.IsMouseButtonDown(.RIGHT) { + input_box_loc = rl.GetMousePosition() + } } draw :: proc() { rl.BeginDrawing() + defer rl.EndDrawing() + rl.ClearBackground(rl.BLACK) - rl.BeginMode2D(game_camera()) - // rl.DrawRectangleV(g_mem.player_pos, {4, 8}, rl.WHITE) - rl.DrawRectangleV({20, 20}, {10, 20}, rl.RED) - rl.EndMode2D() - rl.BeginMode2D(ui_camera()) - // rl.DrawText( - // fmt.ctprintf("some_number: %v\nplayer_pos: %v", g_mem.some_number, g_mem.player_pos), - // 5, - // 5, - // 8, - // rl.WHITE, - // ) + rl.GuiTextInputBox( + rl.Rectangle { + x = input_box_loc.x, + y = input_box_loc.y, + width = (w / scaling) / 2, + height = (h / scaling) / 2, + }, + "Files", + "File input box", + "Button", + cstring(rawptr(&g_mem.file_dialog_text_buffer)), + FILE_DIALOG_SIZE, + nil, + ) rl.EndMode2D() - - rl.EndDrawing() -} \ No newline at end of file +} diff --git a/src/input_box.odin b/src/input_box.odin new file mode 100644 index 0000000..f7984c1 --- /dev/null +++ b/src/input_box.odin @@ -0,0 +1,15 @@ +package game + +import rl "vendor:raylib" + +// Check if any key is pressed +// NOTE: We limit keys check to keys between 32 (KEY_SPACE) and 126 +IsAnyKeyPressed :: proc() -> (keyPressed: bool) { + key := rl.GetKeyPressed() + + if (i32(key) >= 32) && (i32(key) <= 126) { + keyPressed = true + } + + return +} diff --git a/src/symbol_exports.odin b/src/symbol_exports.odin index c82fd83..492956c 100644 --- a/src/symbol_exports.odin +++ b/src/symbol_exports.odin @@ -1,5 +1,7 @@ package game +import rl "vendor:raylib" + @(export) game_update :: proc() -> bool { update()