-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalert.go
127 lines (92 loc) · 2.47 KB
/
alert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package fnf
import (
"time"
rl "github.com/gen2brain/raylib-go/raylib"
)
type Alert struct {
Message string
Age time.Duration
}
var TheAlertManager struct {
Alerts Queue[Alert]
AlertLifetime time.Duration
}
func InitAlert() {
am := &TheAlertManager
am.AlertLifetime = time.Millisecond * 2000
}
func DisplayAlert(msg string) {
am := &TheAlertManager
alert := Alert{
Message: msg,
}
am.Alerts.Enqueue(alert)
}
func UpdateAlert(deltaTime time.Duration) {
am := &TheAlertManager
if am.Alerts.IsEmpty() {
return
}
// don't update alerts while transition is on
if IsTransitionOn() {
return
}
for i := range am.Alerts.Length() {
alert := am.Alerts.At(i)
alert.Age += deltaTime
am.Alerts.Set(i, alert)
}
for !am.Alerts.IsEmpty() {
first := am.Alerts.PeekFirst()
if first.Age > am.AlertLifetime {
am.Alerts.Dequeue()
} else {
break
}
}
}
func DrawAlert() {
am := &TheAlertManager
// NOTE : resized font looks very ugly
// so we have to use whatever size font is loaded in
// if you want to resize the alerts, modify it in assets.go
var fontSize = float32(FontClear.BaseSize())
const vertMargin = 10
const hozMargin = 20
const msgInterval = 7
const animDuration = 0.1
offsetY := float32(10)
for i := range am.Alerts.Length() {
alert := am.Alerts.At(i)
scale := float32(1.0)
ageF32 := float32(alert.Age)
lifeTimeF32 := float32(am.AlertLifetime)
// calculate scale
if ageF32 < lifeTimeF32*animDuration {
t := ageF32 / (lifeTimeF32 * animDuration)
scale = EaseIn(t)
} else if ageF32 > lifeTimeF32*(1-animDuration) {
t := (ageF32 - lifeTimeF32*(1-animDuration)) / (lifeTimeF32 * animDuration)
scale = 1.0 - EaseOut(t)
}
// =====
fontSizeScaled := fontSize * scale
vertMarginScaled := vertMargin * scale
hozMarginScaled := hozMargin * scale
rl.SetTextLineSpacing(int(fontSizeScaled))
textSize := MeasureText(FontClear, alert.Message, fontSizeScaled, 0)
bgRect := rl.Rectangle{
Width: textSize.X + hozMarginScaled*2,
Height: textSize.Y + vertMarginScaled*2,
}
bgRect.X = SCREEN_WIDTH*0.5 - bgRect.Width*0.5
bgRect.Y = offsetY
rl.DrawRectangleRounded(bgRect, 0.2, 10, ToRlColor(FnfColor{0, 0, 0, 200}))
textPos := rl.Vector2{bgRect.X + hozMarginScaled, bgRect.Y + vertMarginScaled}
rl.SetTextLineSpacing(int(fontSizeScaled))
DrawText(FontClear,
alert.Message, textPos, fontSizeScaled, 0,
ToRlColor(FnfColor{255, 255, 255, 255}))
offsetY += bgRect.Height + msgInterval
}
}