ableos/sysdata/programs/horizon/src/main.hb
2024-11-10 06:18:23 -06:00

153 lines
3.9 KiB
Plaintext

stn := @use("../../../libraries/stn/src/lib.hb");
.{string, memory, buffer, random, log} := stn;
.{Vec2} := stn.math
horizon_api := @use("../../../libraries/horizon_api/src/lib.hb");
.{new_label, render_label_to_surface, set_label_text} := horizon_api.widgets.label;
.{sexpr_parser, render_ui} := horizon_api.ui
render := @use("../../../libraries/render/src/lib.hb");
.{Surface} := render;
.{Font} := render.text
intouch := @use("../../../libraries/intouch/src/lib.hb")
Window := struct {
// TODO: Replace this with widgets
implicit_framebuffer: render.Surface,
width: int,
height: int,
x: int,
y: int,
}
psf := @embed("../../../assets/consolefonts/tamsyn/10x20r.psf")
img := @embed("../../../assets/wallpaper.qoi")
main := fn(): int {
win_buff := buffer.create("XHorizon\0")
screen := render.init(true)
// Clear the screen to black.
render.clear(screen, render.black)
wallpaper := render.image.from(@bitcast(&img))
if wallpaper == null {
return 1
}
window := render.new_surface(screen.width / 3, screen.height / 3)
mem_buf := memory.request_page(1)
color := random.any(render.Color)
side := window.width / 8
// really we should null check but it is a bit broked
font := @unwrap(render.text.font_from_psf2(@bitcast(&psf), false))
mouse_x := @as(i16, 0)
mouse_y := @as(i16, 0)
text_label := new_label("Hi\0")
// widgets := "()\0"
// ui := sexpr_parser(widgets)
loop {
// Clear the screen
render.clear(screen, render.black)
render.put_surface(screen, wallpaper, .(0, 0), false)
// TODO: Read the window buffer here
{
// ret := buffer.recv([u8; 4096], win_buff, mem_buf)
// for some reason this null check causes the compiler to spin forever
// if ret == null {
// log.info("No messages\0")
// } else {
// log.info("Handle Messages\0")
// }
}
// get input events from drivers via intouch
// key_event := intouch.recieve_key_event();
// log.info("before mouse event check\0");
if false {
// Scroll bar :ThumbsUp:
render.put_rect(screen, .(100, 100), .(100, 10), render.white)
render.put_filled_rect(screen, .(110, 100), .(20, 10), render.white)
render.put_rect(screen, .(90, 110), .(10, 100), render.white)
render.put_filled_rect(screen, .(90, 120), .(10, 20), render.white)
}
{
pos := Vec2(uint).(1, screen.height - 21)
render_label_to_surface(screen, text_label, font, pos)
render.put_rect(screen, .(0, screen.height - 21), .(screen.width - 1, 20), render.white)
}
// Screen border
render.put_rect(screen, .(0, 0), .(screen.width - 1, screen.height - 1), render.white)
{
mouse_event := intouch.recieve_mouse_event()
//
if mouse_event != null {
// log.warn("Mouse event recieved\0")
change_x := @as(i16, mouse_event.x_change)
change_x = change_x << 8
change_x = change_x >> 8
mouse_x += change_x
if mouse_x < 0 {
mouse_x = 0
}
if mouse_x >= screen.width - 20 {
mouse_x = @intcast(screen.width - 21)
}
change_y := @as(i16, mouse_event.y_change)
change_y = change_y << 8
change_y = change_y >> 8
if mouse_y < 0 {
mouse_y = 0
}
if mouse_y >= screen.height - 20 {
mouse_y = @intcast(screen.height - 21)
}
mouse_y -= change_y
if mouse_event.left {
set_label_text(text_label, "LEFT CLICK\0")
}
if mouse_event.middle {
set_label_text(text_label, "MIDDLE CLICK\0")
}
if mouse_event.right {
set_label_text(text_label, "RIGHT CLICK\0")
}
}
// render mouse
lum := render.indexptr(screen, mouse_x, mouse_y)
if lum.r / 3 + lum.g / 3 + lum.b / 3 < 128 {
render.put_rect(screen, .(mouse_x, mouse_y), .(20, 20), render.white)
} else {
render.put_rect(screen, .(mouse_x, mouse_y), .(20, 20), render.black)
}
// Send events to focused window
}
// TODO: Get windows out of a collection and iter through
// Sync the screen
render.sync(screen)
}
return 0
}