|
| 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 | +} |
0 commit comments