Skip to content

Commit 922dc37

Browse files
committed
Add add-/edit form for games
with that as well add the options to set environment variables and custom parameters to pass to the doom engine
1 parent a5bc09a commit 922dc37

File tree

8 files changed

+173
-146
lines changed

8 files changed

+173
-146
lines changed

games/game.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func NewGame(name, sourceport, iwad string) Game {
6666
// AddMod adds mod
6767
func (g *Game) AddMod(modFile string) {
6868
g.Mods = append(g.Mods, modFile)
69-
informChangeListeners()
69+
InformChangeListeners()
7070
Persist()
7171
}
7272

games/games.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ func GetInstance() GameList {
4646
// }
4747
//}
4848

49-
func informChangeListeners() {
49+
// InformChangeListeners triggers the given function for each registered listener
50+
func InformChangeListeners() {
5051
for _, f := range changeListeners {
5152
f()
5253
}
@@ -62,14 +63,14 @@ func RegisterChangeListener(f func()) {
6263
// this triggers the list to be written to disk as well
6364
func AddGame(g Game) {
6465
instance = append(instance, g)
65-
informChangeListeners()
66+
InformChangeListeners()
6667
Persist() // TODO: Could be done in a goroutine; Maybe queue via channel
6768
}
6869

6970
// RemoveGameAt removes the game at the given index
7071
func RemoveGameAt(i int) {
7172
instance = append(instance[:i], instance[i+1:]...)
72-
informChangeListeners()
73+
InformChangeListeners()
7374
Persist()
7475
}
7576

tui/addEditGame.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package tui
2+
3+
import (
4+
"strings"
5+
6+
"github.com/rivo/tview"
7+
"github.com/zmnpl/twad/cfg"
8+
"github.com/zmnpl/twad/games"
9+
)
10+
11+
const (
12+
addGame = "Add New Game"
13+
editGame = "Edit Game"
14+
15+
aeName = "Name"
16+
aeSourcePort = "Source Port"
17+
aeIWAD = "IWAD"
18+
19+
aeEnvironment = "Environment Variables"
20+
aeEnvironmentDetail = `Provide environment variables here; To turn VSync off entirely for example:
21+
"vblank_mode=1"`
22+
aeOtherParams = "Others"
23+
aeOtherParamsDetail = "Other parameters you want to pass to your ZDoom port for this game"
24+
)
25+
26+
func splitParams(params string) []string {
27+
result := strings.Split(params, " ")
28+
for i := range result {
29+
result[i] = strings.TrimSpace(result[i])
30+
}
31+
return result
32+
}
33+
34+
func indexOfItemIn(item string, list []string) (int, bool) {
35+
for i, val := range list {
36+
if val == item {
37+
return i, true
38+
}
39+
}
40+
return -1, false
41+
}
42+
43+
func makeAddEditGame(g *games.Game) *tview.Flex {
44+
gWasNil := false
45+
if g == nil {
46+
foo := games.NewGame("", "", "")
47+
g = &foo
48+
gWasNil = true
49+
}
50+
51+
inputName := tview.NewInputField().SetText(g.Name)
52+
53+
inputSourcePort := tview.NewDropDown().SetFieldWidth(20).SetOptions([]string{"NA"}, nil)
54+
if cfg.GetInstance().SourcePorts != nil && len(cfg.GetInstance().SourcePorts) > 0 {
55+
inputSourcePort.SetOptions(cfg.GetInstance().SourcePorts, nil)
56+
if i, isIn := indexOfItemIn(g.SourcePort, cfg.GetInstance().SourcePorts); isIn {
57+
inputSourcePort.SetCurrentOption(i)
58+
} else {
59+
inputSourcePort.SetCurrentOption(0)
60+
}
61+
}
62+
63+
inputIwad := tview.NewDropDown().SetFieldWidth(20).SetOptions([]string{"NA"}, nil)
64+
if cfg.GetInstance().IWADs != nil && len(cfg.GetInstance().IWADs) > 0 {
65+
inputIwad.SetOptions(cfg.GetInstance().IWADs, nil)
66+
if i, isIn := indexOfItemIn(g.Iwad, cfg.GetInstance().IWADs); isIn {
67+
inputIwad.SetCurrentOption(i)
68+
} else {
69+
inputIwad.SetCurrentOption(0)
70+
}
71+
}
72+
73+
inputEnvVars := tview.NewInputField().SetText(g.EnvironmentString())
74+
75+
inputCustomParams := tview.NewInputField().SetText(g.ParamsString())
76+
77+
okButton := tview.NewButton(optionsOkButtonLabel).SetBackgroundColorActivated(tview.Styles.PrimaryTextColor).SetLabelColorActivated(tview.Styles.ContrastBackgroundColor)
78+
okButton.SetSelectedFunc(func() {
79+
g.Name = strings.TrimSpace(inputName.GetText())
80+
_, g.SourcePort = inputSourcePort.GetCurrentOption()
81+
_, g.Iwad = inputIwad.GetCurrentOption()
82+
g.Environment = splitParams(inputEnvVars.GetText())
83+
g.Parameters = splitParams(inputCustomParams.GetText())
84+
85+
if gWasNil {
86+
games.AddGame(*g)
87+
}
88+
89+
games.Persist()
90+
games.InformChangeListeners()
91+
appModeNormal()
92+
})
93+
94+
// build form
95+
addEditGameForm := tview.NewFlex().SetDirection(tview.FlexRow).
96+
AddItem(tview.NewTextView().SetText(aeName).SetTextColor(tview.Styles.SecondaryTextColor), 1, 0, false).
97+
AddItem(inputName, 1, 0, true).
98+
AddItem(nil, 1, 0, false).
99+
AddItem(tview.NewTextView().SetText(aeSourcePort).SetTextColor(tview.Styles.SecondaryTextColor), 1, 0, false).
100+
AddItem(inputSourcePort, 1, 0, false).
101+
AddItem(nil, 1, 0, false).
102+
AddItem(tview.NewTextView().SetText(aeIWAD).SetTextColor(tview.Styles.SecondaryTextColor), 1, 0, false).
103+
AddItem(inputIwad, 1, 0, false).
104+
AddItem(nil, 1, 0, false).
105+
AddItem(tview.NewTextView().SetText(aeEnvironment).SetTextColor(tview.Styles.SecondaryTextColor), 1, 0, false).
106+
AddItem(tview.NewTextView().SetText(aeEnvironmentDetail), 2, 0, false).
107+
AddItem(inputEnvVars, 1, 0, false).
108+
AddItem(nil, 1, 0, false).
109+
AddItem(tview.NewTextView().SetText(aeOtherParams).SetTextColor(tview.Styles.SecondaryTextColor), 1, 0, false).
110+
AddItem(tview.NewTextView().SetText(aeOtherParamsDetail), 1, 0, false).
111+
AddItem(inputCustomParams, 1, 0, false).
112+
AddItem(nil, 1, 0, false).
113+
AddItem(nil, 1, 0, false).
114+
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).AddItem(okButton, 20, 0, false), 1, 0, false)
115+
addEditGameForm.SetBorder(true)
116+
addEditGameForm.SetTitle(addGame)
117+
addEditGameForm.SetBorderPadding(1, 1, 1, 1)
118+
119+
// navigation path
120+
inputName.SetInputCapture(tabNavigate(okButton, inputSourcePort))
121+
inputSourcePort.SetInputCapture(tabNavigate(inputName, inputIwad))
122+
inputIwad.SetInputCapture(tabNavigate(inputSourcePort, inputEnvVars))
123+
inputEnvVars.SetInputCapture(tabNavigate(inputIwad, inputCustomParams))
124+
inputCustomParams.SetInputCapture(tabNavigate(inputEnvVars, okButton))
125+
okButton.SetInputCapture(tabNavigate(inputCustomParams, inputName))
126+
127+
return addEditGameForm
128+
}

tui/gamesTable.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ func populateGamesTable() {
8282
return makeModTree(selectedGame)
8383
}
8484
}
85-
modTreeMaker := makeModTreeMaker(&games.Game{})
8685

8786
//makeCellPulser := func(c *tview.TableCell) func() {
8887
// return func() {
@@ -112,7 +111,6 @@ func populateGamesTable() {
112111
//cell = gamesTable.GetCell(r, len(g.Mods)+3)
113112
}
114113
selectedGameChanged(g)
115-
modTreeMaker = makeModTreeMaker(g)
116114
//cellPulser = makeCellPulser(cell)
117115
})
118116

@@ -136,11 +134,14 @@ func populateGamesTable() {
136134
app.SetFocus(optionsDiag)
137135
// open dialog to add mod to game
138136
case 'a':
139-
modTree := modTreeMaker()
140-
actionPager.AddPage(pageModSelector, modTree, true, false)
141-
actionPager.SwitchToPage(pageModSelector)
142-
app.SetFocus(modTree)
143-
return nil
137+
if r > 0 {
138+
mtm := makeModTreeMaker(&allGames[r-fixRows])
139+
modTree := mtm()
140+
actionPager.AddPage(pageModSelector, modTree, true, false)
141+
actionPager.SwitchToPage(pageModSelector)
142+
app.SetFocus(modTree)
143+
return nil
144+
}
144145

145146
// remove last mod from game
146147
case 'r':
@@ -166,7 +167,7 @@ func populateGamesTable() {
166167

167168
// open dialog to insert new game
168169
case 'i':
169-
newForm = makeNewGameForm()
170+
newForm := makeAddEditGame(nil)
170171
actionPager.AddPage(pageNewForm, newForm, true, false)
171172
actionPager.SwitchToPage(pageNewForm)
172173
app.SetFocus(newForm)
@@ -184,11 +185,13 @@ func populateGamesTable() {
184185
return nil
185186

186187
case 'p':
187-
customParameters := makeParamsEditor(&allGames[r-fixRows])
188-
actionPager.AddPage(pageParamsEdit, customParameters, true, false)
189-
actionPager.SwitchToPage(pageParamsEdit)
190-
app.SetFocus(customParameters)
191-
return nil
188+
if r > 0 {
189+
customParameters := makeAddEditGame(&allGames[r-fixRows])
190+
actionPager.AddPage(pageParamsEdit, customParameters, true, false)
191+
actionPager.SwitchToPage(pageParamsEdit)
192+
app.SetFocus(customParameters)
193+
return nil
194+
}
192195
}
193196
}
194197

tui/newGameForm.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

tui/options.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package tui
33
import (
44
"strings"
55

6-
"github.com/gdamore/tcell"
76
"github.com/rivo/tview"
87
"github.com/zmnpl/twad/cfg"
98
)
@@ -20,18 +19,6 @@ const (
2019
optionsMaxLabelLength = 35
2120
)
2221

23-
func optionMoveTo(next tview.Primitive) func(event *tcell.EventKey) *tcell.EventKey {
24-
return func(event *tcell.EventKey) *tcell.EventKey {
25-
k := event.Key()
26-
switch k {
27-
case tcell.KeyTab:
28-
app.SetFocus(next)
29-
return nil
30-
}
31-
return event
32-
}
33-
}
34-
3522
func makeOptions() *tview.Flex {
3623
leftColSize := optionsMaxLabelLength + 1
3724
rigthColSize := 1
@@ -89,7 +76,7 @@ func makeOptions() *tview.Flex {
8976
cfg.GetInstance().SourcePorts = sps
9077

9178
iwds := strings.Split(iwads.GetText(), ",")
92-
for i := range sps {
79+
for i := range iwds {
9380
iwds[i] = strings.TrimSpace(iwds[i])
9481
}
9582
cfg.GetInstance().IWADs = iwds
@@ -102,13 +89,13 @@ func makeOptions() *tview.Flex {
10289
})
10390

10491
// navigation path
105-
path.SetInputCapture(optionMoveTo(sourcePorts))
106-
sourcePorts.SetInputCapture(optionMoveTo(iwads))
107-
iwads.SetInputCapture(optionMoveTo(warn))
108-
warn.SetInputCapture(optionMoveTo(saveDirs))
109-
saveDirs.SetInputCapture(optionMoveTo(firstStart))
110-
firstStart.SetInputCapture(optionMoveTo(okButton))
111-
okButton.SetInputCapture(optionMoveTo(path))
92+
path.SetInputCapture(tabNavigate(okButton, sourcePorts))
93+
sourcePorts.SetInputCapture(tabNavigate(path, iwads))
94+
iwads.SetInputCapture(tabNavigate(sourcePorts, warn))
95+
warn.SetInputCapture(tabNavigate(iwads, saveDirs))
96+
saveDirs.SetInputCapture(tabNavigate(warn, firstStart))
97+
firstStart.SetInputCapture(tabNavigate(saveDirs, okButton))
98+
okButton.SetInputCapture(tabNavigate(firstStart, path))
11299

113100
options := tview.NewFlex().SetDirection(tview.FlexRow).
114101
AddItem(pathRow, 1, 0, true).

tui/paramsEditor.go

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)