-
Notifications
You must be signed in to change notification settings - Fork 3
/
state.go
145 lines (125 loc) · 2.76 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package state
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/dialog"
"github.com/kiamev/moogle-mod-manager/config"
"github.com/kiamev/moogle-mod-manager/ui/state/gui"
"github.com/kiamev/moogle-mod-manager/ui/state/ui"
"github.com/kiamev/moogle-mod-manager/ui/util/working"
)
type GUI byte
const (
None GUI = iota
LocalMods
DiscoverMods
ModAuthor
ConfigInstaller
)
var (
CurrentGame config.GameDef
guiHistories []*guiHistory
mainMenu Screen
screens = make(map[GUI]Screen)
baseDir = binding.NewString()
)
type guiHistory struct {
gui GUI
baseDir string
}
func appendGuiHistory(gui GUI) {
guiHistories = append(guiHistories, &guiHistory{
gui: gui,
baseDir: GetBaseDir(),
})
SetBaseDir("")
}
type Screen interface {
PreDraw(w fyne.Window, args ...interface{}) error
Draw(w fyne.Window)
DrawAsDialog(window fyne.Window)
OnClose()
}
func GetCurrentGUI() GUI {
if len(guiHistories) > 0 {
return guiHistories[len(guiHistories)-1].gui
}
return None
}
func GetScreen(gui GUI) Screen {
return screens[gui]
}
func ShowScreen(g GUI, args ...interface{}) {
defer working.HideDialog()
working.ShowDialog()
if g == DiscoverMods {
if ui.PopupWindow == nil {
ui.PopupWindow = ui.App.NewWindow("Finder")
ui.PopupWindow.Resize(config.Get().Size())
ui.PopupWindow.SetOnClosed(func() { ui.PopupWindow = nil })
if err := screens[g].PreDraw(ui.PopupWindow, args); err != nil {
dialog.ShowError(err, ui.Window)
return
}
ui.PopupWindow.Show()
ui.ShowingPopup = true
screens[g].DrawAsDialog(ui.PopupWindow)
}
return
} else {
if err := screens[g].PreDraw(ui.Window, args); err != nil {
dialog.ShowError(err, ui.Window)
return
}
}
appendGuiHistory(g)
mainMenu.Draw(ui.Window)
screens[g].Draw(ui.Window)
gui.Current.Set(int(g))
}
func ClosePopupWindow() {
ui.PopupWindow.Close()
ui.ShowingPopup = false
}
func ShowPreviousScreen() {
var s Screen
if len(guiHistories) > 1 {
guiHistories = guiHistories[:len(guiHistories)-1]
h := guiHistories[len(guiHistories)-1]
s = screens[h.gui]
SetBaseDir(h.baseDir)
gui.Current.Set(int(h.gui))
} else {
s = screens[None]
SetBaseDir("")
gui.Current.Set(int(None))
}
ui.Window.MainMenu().Refresh()
mainMenu.Draw(ui.Window)
s.Draw(ui.Window)
}
func UpdateCurrentScreen() {
s := screens[GetCurrentGUI()]
s.Draw(ui.Window)
gui.Current.Set(int(GetCurrentGUI()))
}
func RegisterScreen(g GUI, screen Screen) {
screens[g] = screen
gui.Current.Set(int(g))
}
func RegisterMainMenu(m Screen) {
mainMenu = m
}
func RefreshMenu() {
ui.Window.MainMenu().Refresh()
}
func GetBaseDir() string {
s, _ := baseDir.Get()
return s
}
func GetBaseDirBinding() binding.String {
return baseDir
}
func SetBaseDir(dir string) {
_ = baseDir.Set(dir)
}