Skip to content

Commit

Permalink
extract game menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Steinke committed Feb 11, 2024
1 parent c4f90b2 commit db5fe38
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 52 deletions.
2 changes: 0 additions & 2 deletions logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ func NewLogic() *logic {
}

type logic struct {
app.Compo

sequence []int64
clicks int
stage int
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func main() {
flag.Parse()

l := NewLogic()
// TODO: make game settings its own component
// TODO: keep scores in local storage
// TODO: listen to app updates
// TODO: improve overall styling
Expand Down
67 changes: 67 additions & 0 deletions menu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"fmt"
"strconv"

"github.com/maxence-charriere/go-app/v9/pkg/app"
)

type menu struct {
app.Compo
selectedDifficulty difficulty
}

func NewMenu() *menu {
return &menu{}
}

func (g *menu) Render() app.UI {
return app.Div().Body(

app.Button().
Class("simon-button", "new-game").
Body(app.Span().Text("New Game")).
OnClick(func(ctx app.Context, _ app.Event) {
ctx.NewActionWithValue(eventNewGame, g.selectedDifficulty)
}),
app.Div().ID("difficulty").Body(
app.Label().Body(
app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", easy).Value(easy).Checked(true).OnClick(func(ctx app.Context, _ app.Event) {
val := ctx.JSSrc().Get("value").String()
d, err := strconv.Atoi(val)
if err != nil {
fmt.Println("failed parsing", val)
return
}
g.selectedDifficulty = difficulty(d)
}),
app.Text("easy"),
),
app.Label().Body(
app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", medium).Value(medium).OnClick(func(ctx app.Context, _ app.Event) {
val := ctx.JSSrc().Get("value").String()
d, err := strconv.Atoi(val)
if err != nil {
fmt.Println("failed parsing", val)
return
}
g.selectedDifficulty = difficulty(d)
}),
app.Text("medium"),
),
app.Label().Body(
app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", hard).Value(hard).OnClick(func(ctx app.Context, _ app.Event) {
val := ctx.JSSrc().Get("value").String()
d, err := strconv.Atoi(val)
if err != nil {
fmt.Println("failed parsing", val)
return
}
g.selectedDifficulty = difficulty(d)
}),
app.Text("hard"),
),
),
)
}
52 changes: 3 additions & 49 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"strconv"

"github.com/maxence-charriere/go-app/v9/pkg/app"
)
Expand All @@ -24,8 +23,7 @@ func NewUI() *ui {
type ui struct {
app.Compo

Text string
selectedDifficulty difficulty
Text string
}

func (g *ui) OnMount(ctx app.Context) {
Expand Down Expand Up @@ -53,54 +51,10 @@ func (g *ui) Render() app.UI {
gameStateText,
)

newGameButton := app.Button().
Class("simon-button", "new-game").
Body(app.Span().Text("New Game")).
OnClick(func(ctx app.Context, _ app.Event) {
ctx.NewActionWithValue(eventNewGame, g.selectedDifficulty)
})
difficultySetting := app.Div().ID("difficulty").Body(
app.Label().Body(
app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", easy).Value(easy).Checked(true).OnClick(func(ctx app.Context, _ app.Event) {
val := ctx.JSSrc().Get("value").String()
d, err := strconv.Atoi(val)
if err != nil {
fmt.Println("failed parsing", val)
return
}
g.selectedDifficulty = difficulty(d)
}),
app.Text("easy"),
),
app.Label().Body(
app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", medium).Value(medium).OnClick(func(ctx app.Context, _ app.Event) {
val := ctx.JSSrc().Get("value").String()
d, err := strconv.Atoi(val)
if err != nil {
fmt.Println("failed parsing", val)
return
}
g.selectedDifficulty = difficulty(d)
}),
app.Text("medium"),
),
app.Label().Body(
app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", hard).Value(hard).OnClick(func(ctx app.Context, _ app.Event) {
val := ctx.JSSrc().Get("value").String()
d, err := strconv.Atoi(val)
if err != nil {
fmt.Println("failed parsing", val)
return
}
g.selectedDifficulty = difficulty(d)
}),
app.Text("hard"),
),
)
menu := NewMenu()

return app.Div().Class("fill").Body(
newGameButton,
difficultySetting,
menu,
gameField,
)
}
Expand Down

0 comments on commit db5fe38

Please sign in to comment.