labels now have bg/fg color

This commit is contained in:
Able 2024-11-18 09:19:04 -06:00
parent 0af4d142a4
commit 6b673bc7e6
2 changed files with 17 additions and 8 deletions

View file

@ -3,7 +3,7 @@ stn := @use("../../../../libraries/stn/src/lib.hb");
.{Vec2} := stn.math
render := @use("../../../../libraries/render/src/lib.hb");
.{Surface} := render;
.{Surface, Color} := render;
.{Font} := render.text
Label := struct {
@ -12,6 +12,8 @@ Label := struct {
surface: Surface,
text: ^u8,
text_length: uint,
bg: Color,
fg: Color,
}
set_label_text := fn(label: Label, text: ^u8): void {
@ -24,8 +26,8 @@ set_label_text := fn(label: Label, text: ^u8): void {
render_label_to_surface := fn(surface: Surface, label: Label, font: Font, pos: Vec2(uint)): void {
if label.is_dirty {
render.clear(label.surface, render.black)
render.put_text(label.surface, font, .(0, 0), render.white, label.text)
render.clear(label.surface, label.bg)
render.put_text(label.surface, font, .(0, 0), label.fg, label.text)
}
render.put_surface(surface, label.surface, pos, false)
}
@ -33,6 +35,12 @@ render_label_to_surface := fn(surface: Surface, label: Label, font: Font, pos: V
new_label := fn(text: ^u8): Label {
text_surface := render.new_surface(1024, 20)
text_length := string.length(text)
label := Label.(3, true, text_surface, text, text_length)
label := Label.(3, true, text_surface, text, text_length, render.black, render.white)
return label
}
$set_color := fn(label: Label, bg: Color, fg: Color): void {
label.bg = bg
label.fg = fg
label.is_dirty = true
}

View file

@ -3,7 +3,7 @@ render := @use("../../../libraries/render/src/lib.hb")
intouch := @use("../../../libraries/intouch/src/lib.hb")
horizon_api := @use("../../../libraries/horizon_api/src/lib.hb");
.{new_label, render_label_to_surface, set_label_text} := horizon_api.widgets.label
.{new_label, render_label_to_surface, set_label_text, set_color} := horizon_api.widgets.label
stn := @use("../../../libraries/stn/src/lib.hb");
.{Vec2} := stn.math
@ -29,6 +29,7 @@ main := fn(): int {
mouse_y := 100
text_label := new_label("Hi\0")
set_color(text_label, sunset.server.DECO_COLOUR, render.black)
loop {
mouse_event := intouch.recieve_mouse_event()
@ -38,7 +39,7 @@ main := fn(): int {
change_x = change_x >> 8
mouse_x += change_x
if mouse_x < 0 {
if mouse_x <= 0 {
mouse_x = 0
}
if mouse_x >= screen.width - 20 {
@ -49,13 +50,13 @@ main := fn(): int {
change_y = change_y << 8
change_y = change_y >> 8
mouse_y -= change_y
if mouse_y < 0 {
mouse_y = 0
mouse_y = 1
}
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")