2024-01-25 15:57:10 +00:00
|
|
|
package main
|
2024-01-24 14:02:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
raylib "github.com/gen2brain/raylib-go/raylib"
|
|
|
|
)
|
|
|
|
|
2024-01-25 15:57:10 +00:00
|
|
|
const (
|
|
|
|
toastMaxAge = 1 * time.Second
|
|
|
|
)
|
|
|
|
|
2024-01-24 14:02:04 +00:00
|
|
|
var (
|
2024-01-25 15:57:10 +00:00
|
|
|
toasts = []toast{}
|
2024-01-24 14:02:04 +00:00
|
|
|
toastHeight = float32(0)
|
|
|
|
)
|
|
|
|
|
2024-01-25 15:57:10 +00:00
|
|
|
type toast struct {
|
2024-01-24 14:02:04 +00:00
|
|
|
Text string
|
|
|
|
Age time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddToast(text string) {
|
2024-01-26 00:55:21 +00:00
|
|
|
toasts = append(toasts, toast{Text: text, Age: time.Now()})
|
2024-01-25 15:57:10 +00:00
|
|
|
fmt.Printf("Added toast: '%s'\n", text)
|
2024-01-24 14:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-01-24 14:02:04 +00:00
|
|
|
if time.Since(toasts[i].Age) > toastMaxAge {
|
|
|
|
toasts = append(toasts[:i], toasts[i+1:]...)
|
2024-01-25 15:57:10 +00:00
|
|
|
i -= 1
|
2024-01-24 14:02:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-01-24 14:02:04 +00:00
|
|
|
raylib.DrawText(text, 10, int32(20*i)+10, 10, raylib.White)
|
|
|
|
}
|
|
|
|
raylib.EndScissorMode()
|
|
|
|
}
|