bedrocktool/ui/gui/pages/worlds/worlds.go

158 lines
3.5 KiB
Go
Raw Normal View History

2023-03-14 01:07:39 +00:00
package worlds
import (
2023-03-28 13:22:11 +00:00
"fmt"
"sync"
2023-03-14 01:07:39 +00:00
"gioui.org/layout"
"gioui.org/unit"
2023-03-28 13:22:11 +00:00
"gioui.org/widget"
2023-03-14 01:07:39 +00:00
"gioui.org/widget/material"
"gioui.org/x/component"
"github.com/bedrock-tool/bedrocktool/ui/gui/pages"
2023-03-18 11:12:54 +00:00
"github.com/bedrock-tool/bedrocktool/ui/messages"
2023-03-14 01:07:39 +00:00
)
type (
C = layout.Context
D = layout.Dimensions
)
type Page struct {
*pages.Router
worldMap *Map
2023-03-18 11:12:54 +00:00
State messages.UIState
2023-03-14 01:07:39 +00:00
chunkCount int
voidGen bool
worldName string
2023-03-28 13:22:11 +00:00
worldsList widget.List
2023-04-30 21:04:41 +00:00
worlds []*messages.SavedWorld
2023-03-28 13:22:11 +00:00
l sync.Mutex
2023-03-14 01:07:39 +00:00
}
func New(router *pages.Router) *Page {
return &Page{
Router: router,
worldMap: &Map{},
2023-03-28 13:22:11 +00:00
worldsList: widget.List{
List: layout.List{
Axis: layout.Vertical,
},
},
2023-03-14 01:07:39 +00:00
}
}
var _ pages.Page = &Page{}
func (p *Page) Actions() []component.AppBarAction {
return []component.AppBarAction{}
}
func (p *Page) Overflow() []component.OverflowAction {
return []component.OverflowAction{}
}
func (p *Page) NavItem() component.NavItem {
return component.NavItem{
2023-03-14 01:10:16 +00:00
Name: "World Downloader",
2023-03-14 01:07:39 +00:00
//Icon: icon.OtherIcon,
}
}
2023-04-30 21:04:41 +00:00
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),
)
}),
)
})
}
2023-03-14 01:07:39 +00:00
func (p *Page) Layout(gtx C, th *material.Theme) D {
margin := layout.Inset{
Top: unit.Dp(25),
Bottom: unit.Dp(25),
Right: unit.Dp(35),
Left: unit.Dp(35),
}
2023-04-30 21:04:41 +00:00
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)
2023-03-28 13:22:11 +00:00
})
2023-04-30 21:04:41 +00:00
}),
)
}
return D{}
})
2023-03-14 01:07:39 +00:00
2023-03-28 13:22:11 +00:00
return layout.Dimensions{}
2023-03-14 01:07:39 +00:00
}
2023-04-07 14:24:38 +00:00
func (u *Page) Handler(data any) messages.MessageResponse {
2023-03-18 11:12:54 +00:00
r := messages.MessageResponse{
2023-03-14 01:07:39 +00:00
Ok: false,
Data: nil,
}
2023-04-01 22:22:50 +00:00
switch m := data.(type) {
2023-03-18 11:12:54 +00:00
case messages.SetUIState:
2023-04-01 22:22:50 +00:00
u.State = m
2023-03-14 01:07:39 +00:00
u.Router.Invalidate()
r.Ok = true
2023-03-18 11:12:54 +00:00
case messages.UpdateMap:
2023-04-01 22:22:50 +00:00
u.chunkCount = m.ChunkCount
u.worldMap.Update(&m)
2023-03-14 01:07:39 +00:00
u.Router.Invalidate()
r.Ok = true
2023-03-18 11:12:54 +00:00
case messages.SetVoidGen:
2023-04-01 22:22:50 +00:00
u.voidGen = m.Value
2023-03-14 01:07:39 +00:00
u.Router.Invalidate()
r.Ok = true
2023-03-18 11:12:54 +00:00
case messages.SetWorldName:
2023-04-01 22:22:50 +00:00
u.worldName = m.WorldName
2023-03-14 01:07:39 +00:00
u.Router.Invalidate()
r.Ok = true
2023-03-28 13:22:11 +00:00
case messages.SavingWorld:
u.l.Lock()
2023-04-30 21:04:41 +00:00
u.worlds = append(u.worlds, m.World)
2023-03-28 13:22:11 +00:00
u.l.Unlock()
u.Router.Invalidate()
r.Ok = true
2023-03-14 01:07:39 +00:00
}
return r
}