ColouringApp/toast.go

56 lines
1.1 KiB
Go
Raw Normal View History

2024-01-25 15:57:10 +00:00
package main
import (
"fmt"
"time"
raylib "github.com/gen2brain/raylib-go/raylib"
)
2024-01-25 15:57:10 +00:00
const (
toastMaxAge = 1 * time.Second
)
var (
2024-01-25 15:57:10 +00:00
toasts = []toast{}
toastHeight = float32(0)
)
2024-01-25 15:57:10 +00:00
type toast struct {
Text string
Age time.Time
}
func AddToast(text string) {
2024-01-25 15:57:10 +00:00
toast := toast{Text: text, Age: time.Now()}
toasts = append(toasts, toast)
2024-01-24 18:02:19 +00:00
2024-01-25 15:57:10 +00:00
fmt.Printf("Added toast: '%s'\n", text)
}
func UpdateToasts() {
if len(toasts) != 0 {
toastHeight = raylib.Lerp(toastHeight, float32(20*len(toasts))+10, 0.1)
} else {
toastHeight = raylib.Lerp(toastHeight, 0, 0.1)
}
2024-01-25 15:57:10 +00:00
for i := 0; i < len(toasts); i += 1 {
if time.Since(toasts[i].Age) > toastMaxAge {
toasts = append(toasts[:i], toasts[i+1:]...)
2024-01-25 15:57:10 +00:00
i -= 1
}
}
}
func DrawToasts() {
raylib.BeginScissorMode(0, 0, WindowWidth, int32(toastHeight))
raylib.DrawRectangle(0, 0, WindowWidth, WindowHeight, raylib.Fade(raylib.Black, 0.5))
for i := 0; i < len(toasts); i++ {
2024-01-25 15:57:10 +00:00
//text := fmt.Sprintf("%s (%s)", toasts[i].Text, time.Since(toasts[i].Age).Round(time.Second))
text := toasts[i].Text
raylib.DrawText(text, 10, int32(20*i)+10, 10, raylib.White)
}
raylib.EndScissorMode()
}