ableos/sysdata/programs/render_example/src/examples/text.hb

133 lines
4 KiB
Plaintext
Raw Normal View History

.{memory, log, string} := @use("../../../../libraries/stn/src/lib.hb")
2024-10-26 03:23:28 -05:00
render := @use("../../../../libraries/render/src/lib.hb")
2024-11-07 19:48:17 -06:00
/* expected result: almost-not-trash notepad app
very jank
-----------------
features:
- basic keys
- holding support with DAS
- visible cursor
- shift key support
*/
2024-10-26 03:23:28 -05:00
psf := @embed("../../../../consolefonts/tamsyn/10x20r.psf")
2024-11-07 19:48:17 -06:00
handle_char := fn(char: u8, cursor: ^u8, buf: ^u8): ^u8 {
if char == 0 {
return cursor
}
if char != 0x8 {
*cursor = char
return cursor + 1
} else if char == 0xA {
*cursor = 32
cursor += 1;
*cursor = 92
return cursor + 1
} else if cursor > buf {
cursor -= 1;
*cursor = 32
return cursor
}
return cursor
}
2024-10-26 03:23:28 -05:00
example := fn(): void {
screen := render.init(true)
2024-11-07 19:48:17 -06:00
window := render.new_surface(480, 340)
font := render.text.font_from_psf2(@bitcast(&psf), true)
msg := "ableboard (tm) (patent pending):\n\0"
msg_len := string.length(msg)
buf := memory.alloc(u8, 4096)
@inline(memory.copy, u8, msg, buf, msg_len)
cursor := buf + msg_len
if font == null {
return
2024-11-07 19:48:17 -06:00
};
2024-11-07 19:48:17 -06:00
*cursor = 95
draw(screen, window, font, buf);
*cursor = 0
2024-11-07 19:48:17 -06:00
memory.outb(96, 238)
memory.outb(96, 238)
memory.outb(96, 244)
prev_input := @as(u8, 0xFF)
current_key := @as(u8, 0)
holding_timer := @as(u32, 0)
is_shift_pressed := @as(bool, false)
initial_delay := @as(u32, 50)
repeat_delay := @as(u32, 7)
loop {
input := memory.inb(96)
2024-11-07 19:48:17 -06:00
is_release := (input & 0x80) != 0
key_code := input & 0x7F
if input != prev_input {
2024-11-07 19:48:17 -06:00
if is_release {
if key_code == current_key {
current_key = 0
holding_timer = 0
} else if input == 0xAA | input == 0xB6 {
is_shift_pressed = false
}
} else {
if input == 0x2A | input == 0x36 {
is_shift_pressed = true
} else {
current_key = input
holding_timer = 1
cursor = handle_char(map_keys(current_key, is_shift_pressed), cursor, buf)
}
}
2024-11-07 19:48:17 -06:00
prev_input = input
}
2024-11-05 22:16:08 -06:00
2024-11-07 19:48:17 -06:00
if current_key != 0 & holding_timer > 0 {
holding_timer += 1
if holding_timer >= initial_delay {
cursor = handle_char(map_keys(current_key, is_shift_pressed), cursor, buf)
holding_timer = initial_delay - repeat_delay
}
};
*cursor = 95
draw(screen, window, font, buf);
*cursor = 0
if holding_timer > 0 & current_key != 0 {
if (memory.inb(96) & 0x80) != 0 {
current_key = 0
holding_timer = 0
}
}
}
2024-10-26 03:23:28 -05:00
return
}
2024-11-07 19:48:17 -06:00
draw := fn(screen: render.Surface, window: render.Surface, font: render.text.Font, buf: ^u8): void {
render.clear(screen, render.black)
render.clear(window, render.black)
render.put_rect(window, .(0, 0), .(window.width - 1, window.height - 1), render.white)
render.put_text(window, font, .(7, 7), render.gray, buf)
render.put_surface(screen, window, .(100, 100), false)
render.sync(screen)
}
ps2_table := [u8].(0x0, 0x1B, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x2D, 0x3D, 0x8, 0x9, 0x71, 0x77, 0x65, 0x72, 0x74, 0x79, 0x75, 0x69, 0x6F, 0x70, 0x5B, 0x5D, 0xA, 0x0, 0x61, 0x73, 0x64, 0x66, 0x67, 0x68, 0x6A, 0x6B, 0x6C, 0x3B, 0x27, 0x60, 0x0, 0x5C, 0x7A, 0x78, 0x63, 0x76, 0x62, 0x6E, 0x6D, 0x2C, 0x2E, 0x2F, 0x0, 0x2A, 0x0, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1B, 0x21, 0x40, 0x23, 0x24, 0x25, 0x5E, 0x26, 0x2A, 0x28, 0x29, 0x5F, 0x2B, 0x8, 0x9, 0x51, 0x57, 0x45, 0x52, 0x54, 0x59, 0x55, 0x49, 0x4F, 0x50, 0x7B, 0x7D, 0xA, 0x0, 0x41, 0x53, 0x44, 0x46, 0x47, 0x48, 0x4A, 0x4B, 0x4C, 0x3A, 0x22, 0x7E, 0x0, 0x7C, 0x5A, 0x58, 0x43, 0x56, 0x42, 0x4E, 0x4D, 0x3C, 0x3E, 0x3F, 0x0, 0x0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
map_keys := fn(scancode: u8, shift: bool): u8 {
if shift {
return ps2_table[scancode + 0x40]
}
2024-11-07 19:48:17 -06:00
return ps2_table[scancode]
2024-10-26 03:23:28 -05:00
}