Skip to content

Commit

Permalink
Use radio buttons for displaying game options
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben9922 committed Jun 3, 2023
1 parent cc58e9a commit 592a31f
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,14 @@ func createTitleView(maxWidth int, r rules, pm playerMode) string {

textStrings := []string{
"",
fmt.Sprintf("Press P to toggle between 1-player and 2-player modes \n(currently %s)", pm),
fmt.Sprintf("Press R to toggle between Othello and Reversi rules\n(currently %s)", r),
createRadioButton([]playerMode{OnePlayer, TwoPlayer}, pm, "Player mode", "P"),
createRadioButton([]rules{OthelloRules, ReversiRules}, r, "Rules", "R"),
"",
"Press any other key to start...",
"",
lipgloss.NewStyle().
Foreground(lipgloss.Color("241")).
Render("r: toggle rules • p: toggle player mode • any other key: continue"),
Render("p: toggle player mode • r: toggle rules • any other key: continue"),
}
text := lipgloss.NewStyle().
Width(maxWidth).
Expand Down Expand Up @@ -688,6 +688,38 @@ func createPointConfirmationView(m model, scores map[player]int, maxWidth int) s
Render(lipgloss.JoinVertical(lipgloss.Left, textStrings...))
}

type radioButtonItem interface {
comparable
String() string
}

func createRadioButton[T radioButtonItem](options []T, selected T, label string, key string) string {
var builder strings.Builder
builder.WriteString(label)
builder.WriteString(": ")
for i, option := range options {
if option == selected {
builder.WriteString(lipgloss.NewStyle().
Foreground(lipgloss.Color("105")).
Render(option.String() + " [▪]"))
} else {
builder.WriteString(option.String() + " [ ]")
}

if i != len(options)-1 {
builder.WriteString(";")
}

builder.WriteString(" ")
}
builder.WriteString(
lipgloss.NewStyle().
Foreground(lipgloss.Color("241")).
Render(fmt.Sprintf("(press %s)", strings.ToUpper(key))))

return builder.String()
}

func createPassView(m model, maxWidth int) string {
textStrings := make([]string, 0, 6)
textStrings = []string{
Expand Down

0 comments on commit 592a31f

Please sign in to comment.