Input system more DRY, add Tab (`\t`) support.

This commit is contained in:
KikooDX 2021-02-26 01:03:41 +01:00
parent 25335e94a6
commit eec14bc99c
2 changed files with 14 additions and 16 deletions

BIN
level.kble Normal file

Binary file not shown.

View File

@ -24,6 +24,7 @@ const ActionCat = actions.ActionCat;
const Mode = @import("modes.zig").Mode;
const char_range = 255;
const input_buffer_len = 255;
pub fn main() void {
const level_path: [*:0]const u8 = if (std.os.argv.len > 1)
@ -120,7 +121,6 @@ pub fn main() void {
}
// Create input buffer.
const input_buffer_len = 255;
var input_buffer: [input_buffer_len]u32 = undefined;
comptime {
var i: u8 = 0;
@ -132,28 +132,18 @@ pub fn main() void {
while (!ray.WindowShouldClose()) {
{
// TODO: apply DRY
// Get keyboard input.
var key = ray.GetCharPressed();
// Check if more characters have been pressed.
while (key != 0) {
// Add key to buffer.
input_buffer[input_cursor] = @intCast(u32, key);
input_cursor += 1;
// Avoid writing out of memory.
if (input_cursor >= input_buffer_len)
input_cursor = input_buffer_len - 1;
add_key_to_buffer(&input_buffer, &input_cursor, key);
key = ray.GetCharPressed();
}
// Check for special keys, not detected by GetCharPressed.
if (ray.IsKeyDown(ray.KEY_ENTER)) {
input_buffer[input_cursor] = '\n';
input_cursor += 1;
// Avoid writing out of memory.
if (input_cursor >= input_buffer_len)
input_cursor = input_buffer_len - 1;
}
if (ray.IsKeyPressed(ray.KEY_ENTER))
add_key_to_buffer(&input_buffer, &input_cursor, '\n');
if (ray.IsKeyPressed(ray.KEY_TAB))
add_key_to_buffer(&input_buffer, &input_cursor, '\t');
}
// Process buffer content.
@ -274,3 +264,11 @@ pub fn main() void {
mouse.draw(scale, camera);
}
}
fn add_key_to_buffer(input_buffer: *[input_buffer_len]u32, input_cursor: *u8, key: c_int) void {
input_buffer[input_cursor.*] = @intCast(u32, key);
input_cursor.* += 1;
// Avoid writing out of memory.
if (input_cursor.* >= input_buffer_len)
input_cursor.* = input_buffer_len - 1;
}