show world download list

This commit is contained in:
olebeck 2023-04-30 23:04:41 +02:00
parent 2761cf094c
commit e13856a4ff
4 changed files with 70 additions and 47 deletions

View File

@ -331,6 +331,9 @@ func (w *worldsHandler) SaveAndReset() {
img = w.mapUI.ToImage()
}
folder := fmt.Sprintf("worlds/%s/%s", w.serverState.Name, worldStateCopy.Name)
filename := folder + ".mcworld"
w.serverState.worldCounter += 1
w.Reset()
w.wg.Add(1)
@ -338,12 +341,14 @@ func (w *worldsHandler) SaveAndReset() {
defer w.wg.Done()
logrus.Infof(locale.Loc("saving_world", locale.Strmap{"Name": worldStateCopy.Name, "Count": len(worldStateCopy.chunks)}))
w.gui.Message(messages.SavingWorld{
Name: w.worldState.Name,
Chunks: len(w.worldState.chunks),
World: &messages.SavedWorld{
Name: worldStateCopy.Name,
Path: filename,
Chunks: len(worldStateCopy.chunks),
},
})
// open world
folder := fmt.Sprintf("worlds/%s/%s", w.serverState.Name, worldStateCopy.Name)
os.RemoveAll(folder)
os.MkdirAll(folder, 0o777)
provider, err := worldStateCopy.Save(folder)
@ -469,7 +474,6 @@ func (w *worldsHandler) SaveAndReset() {
w.AddPacks(folder)
// zip it
filename := folder + ".mcworld"
err = utils.ZipFolder(filename, folder)
if err != nil {
logrus.Error(err)
@ -563,6 +567,9 @@ func (w *worldsHandler) AddPacks(folder string) {
}
func (w *worldsHandler) OnConnect(err error) bool {
w.gui.Message(messages.SetWorldName{
WorldName: w.worldState.Name,
})
w.gui.Message(messages.SetUIState(messages.UIStateMain))
if err != nil {
return false

View File

@ -111,7 +111,7 @@ func drawPackEntry(gtx C, th *material.Theme, entry *packEntry, button *widget.C
size = utils.SizeofFmt(float32(entry.Size))
} else {
size = fmt.Sprintf("%s / %s %.02f%%",
utils.SizeofFmt(float32(entry.Size)),
utils.SizeofFmt(float32(entry.Loaded)),
utils.SizeofFmt(float32(entry.Size)),
float32(entry.Loaded)/float32(entry.Size)*100,
)
@ -206,6 +206,8 @@ func (p *Page) Layout(gtx C, th *material.Theme) D {
return margin.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
switch p.State {
case messages.UIStateConnecting:
return layout.Center.Layout(gtx, material.Label(th, 100, "Connecting").Layout)
case messages.UIStateMain:
return p.layoutFinished(gtx, th)
}

View File

@ -2,7 +2,6 @@ package worlds
import (
"fmt"
"image"
"sync"
"gioui.org/layout"
@ -29,7 +28,7 @@ type Page struct {
worldName string
worldsList widget.List
worlds []messages.SavingWorld
worlds []*messages.SavedWorld
l sync.Mutex
}
@ -62,6 +61,19 @@ func (p *Page) NavItem() component.NavItem {
}
}
func displayWorldEntry(gtx C, th *material.Theme, entry *messages.SavedWorld) D {
return layout.UniformInset(5).Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(material.Label(th, th.TextSize, entry.Name).Layout),
layout.Rigid(material.Label(th, th.TextSize, fmt.Sprintf("%d Chunks", entry.Chunks)).Layout),
)
}),
)
})
}
func (p *Page) Layout(gtx C, th *material.Theme) D {
margin := layout.Inset{
Top: unit.Dp(25),
@ -70,46 +82,42 @@ func (p *Page) Layout(gtx C, th *material.Theme) D {
Left: unit.Dp(35),
}
switch p.State {
case messages.UIStateConnect:
// display login page
return margin.Layout(gtx, material.Label(th, 100, "connect Client").Layout)
case messages.UIStateConnecting:
// display connecting to server
return margin.Layout(gtx, material.Label(th, 100, "Connecting").Layout)
case messages.UIStateMain:
// show the main ui
return layout.Flex{
Axis: layout.Vertical,
}.Layout(gtx,
layout.Flexed(1, func(gtx C) D {
return layout.Center.Layout(gtx, p.worldMap.Layout)
}),
)
case messages.UIStateFinished:
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.UniformInset(20).
Layout(gtx, material.Label(th, 20, "Worlds Saved").Layout)
}),
layout.Flexed(1, func(gtx C) D {
p.l.Lock()
defer p.l.Unlock()
return material.List(th, &p.worldsList).Layout(gtx, len(p.worlds), func(gtx C, index int) D {
entry := p.worlds[len(p.worlds)-index-1]
return layout.UniformInset(25).Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(material.Label(th, th.TextSize, entry.Name).Layout),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Dimensions{Size: image.Pt(20, 20)}
}),
layout.Rigid(material.Label(th, th.TextSize, fmt.Sprintf("%d chunks", entry.Chunks)).Layout),
)
margin.Layout(gtx, func(gtx C) D {
switch p.State {
case messages.UIStateConnect:
// display login page
return layout.Center.Layout(gtx, material.Label(th, 100, "connect Client").Layout)
case messages.UIStateConnecting:
return layout.Center.Layout(gtx, material.Label(th, 100, "Connecting").Layout)
case messages.UIStateMain:
// show the main ui
return layout.Flex{
Axis: layout.Vertical,
}.Layout(gtx,
//layout.Rigid(material.Label(th, th.TextSize, p.worldName).Layout),
layout.Flexed(1, func(gtx C) D {
return layout.Center.Layout(gtx, p.worldMap.Layout)
}),
)
case messages.UIStateFinished:
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.UniformInset(15).
Layout(gtx, material.Label(th, 20, "Worlds Saved").Layout)
}),
layout.Flexed(1, func(gtx C) D {
p.l.Lock()
defer p.l.Unlock()
return material.List(th, &p.worldsList).Layout(gtx, len(p.worlds), func(gtx C, index int) D {
entry := p.worlds[len(p.worlds)-index-1]
return displayWorldEntry(gtx, th, entry)
})
})
}),
)
}
}),
)
}
return D{}
})
return layout.Dimensions{}
}
@ -140,7 +148,7 @@ func (u *Page) Handler(data any) messages.MessageResponse {
r.Ok = true
case messages.SavingWorld:
u.l.Lock()
u.worlds = append(u.worlds, m)
u.worlds = append(u.worlds, m.World)
u.l.Unlock()
u.Router.Invalidate()
r.Ok = true

View File

@ -63,8 +63,14 @@ type NewSkin struct {
}
type SavingWorld struct {
World *SavedWorld
}
type SavedWorld struct {
Name string
Path string
Chunks int
Image image.Image
}
type CanShowImages struct{}