Fix download progress and prevent re-downloading existing audio.
This commit is contained in:
+35
-46
@@ -171,72 +171,61 @@ func (p *Home) Update(dt float32) error {
|
||||
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.setMessage("Status: READY", colors.Green)
|
||||
p.isDownloadComplete = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
default:
|
||||
goto drained
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
p.setMessage(fmt.Sprintf("Error: %s", progressUpdate.Err.Error()), colors.Red)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Build the display string based on what the download package tells us.
|
||||
var msg string
|
||||
if progressUpdate.Max <= 0 {
|
||||
// No total known yet — show the descriptive message from the package.
|
||||
// This covers: "Fetching file manifest", "Checking local files",
|
||||
// "Comparing checksums", "Building download list", etc.
|
||||
msg = fmt.Sprintf("Status: %s", progressUpdate.Msg)
|
||||
} else {
|
||||
// We have a known total — show percentage.
|
||||
pct := float64(progressUpdate.Cur) / float64(progressUpdate.Max) * 100
|
||||
msg = fmt.Sprintf("Status: Downloading Assets — %.1f%%", pct)
|
||||
}
|
||||
|
||||
p.setMessage(msg, colors.White)
|
||||
return nil
|
||||
}
|
||||
|
||||
// setMessage deletes the old message texture and builds a new one.
|
||||
func (p *Home) setMessage(text string, color sdl.Color) {
|
||||
if p.message.ID != 0 {
|
||||
gl.DeleteTextures(1, &p.message.ID)
|
||||
p.message.ID = 0
|
||||
}
|
||||
|
||||
newMsg, err := NewStringItem(text, p.font, color)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
messageScale := float32(28) / float32(p.baseFontSize)
|
||||
newMsg.W = int32(float32(newMsg.W) * messageScale)
|
||||
newMsg.H = int32(float32(newMsg.H) * messageScale)
|
||||
p.message = newMsg
|
||||
}
|
||||
|
||||
func (p *Home) Render() error {
|
||||
baseFontSize := p.baseFontSize
|
||||
sm := shaderManager.Get()
|
||||
|
||||
Reference in New Issue
Block a user