Add downloading message to home screen.
Download files in the background and keep track of progress.
This commit is contained in:
+71
-37
@@ -153,47 +153,81 @@ func (p *Home) HandleEvent(event *sdl.Event) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update updates animations and changing textures and other changes using `dt` which represents a time delta in float32.
|
||||
func (p *Home) Update(dt float32) error {
|
||||
select {
|
||||
case progressUpdate, ok := <-p.Context.MessageChannel:
|
||||
if !ok {
|
||||
// Channel is closed, set message to READY
|
||||
newMsg, err := NewStringItem("Status: READY", p.font, colors.Green)
|
||||
if err != nil {
|
||||
return err
|
||||
var (
|
||||
progressUpdate download.DownloadStatus
|
||||
ok bool
|
||||
gotUpdate bool
|
||||
)
|
||||
|
||||
// Drain the channel, keeping only the newest update.
|
||||
for {
|
||||
select {
|
||||
case progressUpdate, ok = <-p.Context.MessageChannel:
|
||||
gotUpdate = true
|
||||
|
||||
if !ok {
|
||||
// Channel was closed while draining.
|
||||
newMsg, err := NewStringItem("Status: READY", p.font, colors.Green)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
messageScale := float32(28) / float32(p.baseFontSize)
|
||||
newMsg.W = int32(float32(newMsg.W) * messageScale)
|
||||
newMsg.H = int32(float32(newMsg.H) * messageScale)
|
||||
p.message = newMsg
|
||||
p.isDownloadComplete = true
|
||||
|
||||
return nil
|
||||
}
|
||||
messageScale := float32(28) / float32(p.baseFontSize)
|
||||
newMsg.W = int32(float32(newMsg.W) * messageScale)
|
||||
newMsg.H = int32(float32(newMsg.H) * messageScale)
|
||||
p.message = newMsg
|
||||
p.isDownloadComplete = true
|
||||
} else if progressUpdate.Err != nil {
|
||||
// Handle error
|
||||
newMsg, err := NewStringItem(fmt.Sprintf("Status: %s", progressUpdate.Err.Error), p.font, colors.Red)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
messageScale := float32(28) / float32(p.baseFontSize)
|
||||
newMsg.W = int32(float32(newMsg.W) * messageScale)
|
||||
newMsg.H = int32(float32(newMsg.H) * messageScale)
|
||||
p.message = newMsg
|
||||
} else {
|
||||
// Update progress message
|
||||
percentComplete := float64(progressUpdate.Cur) / float64(progressUpdate.Max) * 100
|
||||
msg := fmt.Sprintf("Status: Downloading assets %.1f%% Complete", percentComplete)
|
||||
newMsg, err := NewStringItem(msg, p.font, colors.White)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
messageScale := float32(28) / float32(p.baseFontSize)
|
||||
newMsg.W = int32(float32(newMsg.W) * messageScale)
|
||||
newMsg.H = int32(float32(newMsg.H) * messageScale)
|
||||
p.message = newMsg
|
||||
|
||||
default:
|
||||
goto drained
|
||||
}
|
||||
default:
|
||||
// No update, do nothing
|
||||
}
|
||||
|
||||
drained:
|
||||
|
||||
if !gotUpdate {
|
||||
return nil
|
||||
}
|
||||
|
||||
if progressUpdate.Err != nil {
|
||||
// Handle error
|
||||
newMsg, err := NewStringItem(
|
||||
fmt.Sprintf("Status: %s", progressUpdate.Err.Error()),
|
||||
p.font,
|
||||
colors.Red,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
messageScale := float32(28) / float32(p.baseFontSize)
|
||||
newMsg.W = int32(float32(newMsg.W) * messageScale)
|
||||
newMsg.H = int32(float32(newMsg.H) * messageScale)
|
||||
p.message = newMsg
|
||||
|
||||
} else {
|
||||
// Update progress message using ONLY the latest update.
|
||||
percentComplete := float64(progressUpdate.Cur) / float64(progressUpdate.Max) * 100
|
||||
|
||||
msg := fmt.Sprintf(
|
||||
"Status: Downloading assets %.1f%% Complete",
|
||||
percentComplete,
|
||||
)
|
||||
|
||||
newMsg, err := NewStringItem(msg, p.font, colors.White)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
messageScale := float32(28) / float32(p.baseFontSize)
|
||||
newMsg.W = int32(float32(newMsg.W) * messageScale)
|
||||
newMsg.H = int32(float32(newMsg.H) * messageScale)
|
||||
p.message = newMsg
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user