bedrocktool/ui/gui.go

216 lines
5.0 KiB
Go
Raw Normal View History

2023-03-08 11:46:38 +00:00
//go:build gui || android
2023-03-06 14:50:36 +00:00
package ui
2023-03-05 21:50:58 +00:00
import (
"context"
"sync"
2023-03-05 21:50:58 +00:00
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/widget"
2023-03-06 01:03:31 +00:00
"github.com/bedrock-tool/bedrocktool/subcommands"
"github.com/bedrock-tool/bedrocktool/subcommands/skins"
"github.com/bedrock-tool/bedrocktool/subcommands/world"
"github.com/bedrock-tool/bedrocktool/ui/gui"
2023-03-05 21:50:58 +00:00
"github.com/bedrock-tool/bedrocktool/utils"
2023-03-08 11:46:16 +00:00
"golang.org/x/exp/slices"
2023-03-05 21:50:58 +00:00
)
2023-03-06 01:03:31 +00:00
var settings = map[string]func(utils.Command) *widget.Form{
"worlds": func(cc utils.Command) *widget.Form {
2023-03-06 01:03:31 +00:00
c := cc.(*world.WorldCMD)
return widget.NewForm(
widget.NewFormItem(
"serverAddress", widget.NewEntryWithData(binding.BindString(&c.ServerAddress)),
), widget.NewFormItem(
"", widget.NewCheckWithData("packs", binding.BindBool(&c.Packs)),
), widget.NewFormItem(
"", widget.NewCheckWithData("void", binding.BindBool(&c.EnableVoid)),
), widget.NewFormItem(
"", widget.NewCheckWithData("saveImage", binding.BindBool(&c.SaveImage)),
), widget.NewFormItem(
"", widget.NewCheckWithData("experimentInventory", binding.BindBool(&c.ExperimentInventory)),
),
)
},
"skins": func(cc utils.Command) *widget.Form {
c := cc.(*skins.SkinCMD)
return widget.NewForm(
widget.NewFormItem(
"serverAddress", widget.NewEntryWithData(binding.BindString(&c.ServerAddress)),
), widget.NewFormItem(
"filter", widget.NewEntryWithData(binding.BindString(&c.Filter)),
),
)
},
"capture": func(cc utils.Command) *widget.Form {
c := cc.(*subcommands.CaptureCMD)
return widget.NewForm(
widget.NewFormItem(
"serverAddress", widget.NewEntryWithData(binding.BindString(&c.ServerAddress)),
),
)
},
"chat-log": func(cc utils.Command) *widget.Form {
c := cc.(*subcommands.ChatLogCMD)
return widget.NewForm(
widget.NewFormItem(
"serverAddress", widget.NewEntryWithData(binding.BindString(&c.ServerAddress)),
),
widget.NewFormItem(
"", widget.NewCheckWithData("Verbose", binding.BindBool(&c.Verbose)),
),
)
},
"debug-proxy": func(cc utils.Command) *widget.Form {
c := cc.(*subcommands.DebugProxyCMD)
return widget.NewForm(
widget.NewFormItem(
"serverAddress", widget.NewEntryWithData(binding.BindString(&c.ServerAddress)),
), widget.NewFormItem(
"filter", widget.NewEntryWithData(binding.BindString(&c.Filter)),
),
)
},
"packs": func(cc utils.Command) *widget.Form {
c := cc.(*subcommands.ResourcePackCMD)
return widget.NewForm(
widget.NewFormItem(
"serverAddress", widget.NewEntryWithData(binding.BindString(&c.ServerAddress)),
), widget.NewFormItem(
"", widget.NewCheckWithData("saveEncrypted", binding.BindBool(&c.SaveEncrypted)),
), widget.NewFormItem(
"", widget.NewCheckWithData("only-keys", binding.BindBool(&c.OnlyKeys)),
),
)
},
2023-03-06 01:03:31 +00:00
}
2023-03-05 21:50:58 +00:00
type GUI struct {
utils.BaseUI
2023-03-05 21:50:58 +00:00
commandUI gui.CommandUI
2023-03-05 21:50:58 +00:00
}
func (g *GUI) Init() bool {
return true
}
2023-03-08 11:46:16 +00:00
func (g *GUI) Start(ctx context.Context, cancel context.CancelFunc) error {
2023-03-05 21:50:58 +00:00
a := app.New()
w := a.NewWindow("Bedrocktool")
2023-03-08 11:46:16 +00:00
debug := binding.BindBool(&utils.Options.Debug)
extra_debug := binding.BindBool(&utils.Options.ExtraDebug)
2023-03-05 21:50:58 +00:00
entries := []string{}
forms := make(map[string]*widget.Form)
for k, c := range utils.ValidCMDs {
entries = append(entries, k)
2023-03-06 01:03:31 +00:00
f := settings[k]
if f != nil {
2023-03-08 11:46:16 +00:00
forms[k] = f(c)
2023-03-05 21:50:58 +00:00
}
}
2023-03-08 11:46:16 +00:00
slices.Sort(entries)
2023-03-05 21:50:58 +00:00
selected := binding.NewString()
2023-03-05 21:50:58 +00:00
forms_box := container.NewVBox()
start_button := widget.NewButton("Start", nil)
l := sync.Mutex{}
2023-03-06 01:03:31 +00:00
2023-03-05 21:50:58 +00:00
w.SetContent(container.NewVBox(
widget.NewRichTextFromMarkdown("## Settings"),
container.NewHBox(
widget.NewCheckWithData("Debug", debug),
widget.NewCheckWithData("extra-debug", extra_debug),
),
2023-03-06 01:03:31 +00:00
container.NewHBox(
widget.NewRichTextFromMarkdown("Custom Userdata:"),
widget.NewEntryWithData(binding.BindString(&utils.Options.PathCustomUserData)),
),
2023-03-05 21:50:58 +00:00
widget.NewRichTextFromMarkdown("# Commands"),
widget.NewSelect(entries, func(s string) {
l.Lock()
selected.Set(s)
forms_box.RemoveAll()
2023-03-08 11:46:16 +00:00
f := forms[s]
if f != nil {
forms_box.Add(forms[s])
}
l.Unlock()
2023-03-05 21:50:58 +00:00
}),
forms_box,
start_button,
2023-03-05 21:50:58 +00:00
))
2023-03-08 11:46:16 +00:00
wg := sync.WaitGroup{}
start_button.OnTapped = func() {
sub, _ := selected.Get()
cmd := utils.ValidCMDs[sub]
2023-03-08 11:46:16 +00:00
if cmd == nil {
return
}
u := gui.CommandUIs[sub]
if u != nil {
g.commandUI = u
2023-03-08 11:46:16 +00:00
u.Layout(w)
} else {
w.SetContent(container.NewCenter(
widget.NewRichTextFromMarkdown("# No UI yet, Look at the console."),
))
2023-03-05 21:50:58 +00:00
}
utils.InitDNS()
2023-03-09 13:58:54 +00:00
utils.InitExtraDebug(ctx)
2023-03-08 11:46:16 +00:00
wg.Add(1)
go func() {
defer wg.Done()
cmd.Execute(ctx, g)
}()
}
2023-03-05 21:50:58 +00:00
2023-03-08 11:46:16 +00:00
w.SetCloseIntercept(func() {
cancel()
wg.Wait()
w.Close()
})
2023-03-05 21:50:58 +00:00
w.ShowAndRun()
return nil
2023-03-05 21:50:58 +00:00
}
func (g *GUI) Message(name string, data interface{}) utils.MessageResponse {
h := g.commandUI.Handler()
if h != nil {
r := h(name, data)
if r.Ok {
return r
}
}
2023-03-05 21:50:58 +00:00
r := utils.MessageResponse{
Ok: false,
Data: nil,
2023-03-05 21:50:58 +00:00
}
switch name {
case "can_show_images":
r.Ok = true
}
return r
2023-03-05 21:50:58 +00:00
}
2023-03-06 14:50:36 +00:00
func init() {
utils.MakeGui = func() utils.UI {
return &GUI{}
}
}