Skip to content

Commit

Permalink
Merge pull request #9 from Strosel/dev
Browse files Browse the repository at this point in the history
add challenge screen
  • Loading branch information
Strosel authored Aug 20, 2020
2 parents cfcf0fb + 1ab25a4 commit 0b87853
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
128 changes: 128 additions & 0 deletions challenge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package main

import (
"fmt"
"image/color"

"gioui.org/layout"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"golang.org/x/image/colornames"
)

type challenge struct {
old Roll
dice Roll
rules []Rule

bttn *widget.Clickable
}

func challengeScreen(rules []Rule, old Roll) Screen {
return &challenge{
rules: rules,
old: old,
bttn: new(widget.Clickable),
}
}

func (c *challenge) Rules() []Rule {
return c.rules
}

func (c *challenge) Layout(gtx Ctx, th *material.Theme) (nextScreen Screen) {
nextScreen = c

title := func(gtx Ctx) Dim {
in := layout.UniformInset(unit.Dp(0))
in.Top = unit.Dp(32)
lbl := material.H4(th, "Utmaning")
lbl.Alignment = text.Middle
return in.Layout(gtx, lbl.Layout)
}

dice := func(gtx Ctx) Dim {
return layout.Flex{
Spacing: layout.SpaceEvenly,
}.Layout(gtx,
layout.Rigid(func(gtx Ctx) Dim {
die := material.H2(th, fmt.Sprint(c.dice[0]))
die.Font.Variant = "Dice"
die.Color = color.RGBA{230, 0, 0, 255}
if c.dice[0] > 6 {
die.Text = fmt.Sprint((c.dice[0] % 6) + 1)
}

return die.Layout(gtx)
}),
layout.Rigid(func(gtx Ctx) Dim {
die := material.H2(th, fmt.Sprint(c.dice[1]))
die.Font.Variant = "Dice"
die.Color = colornames.Royalblue
if c.dice[0] > 6 {
die.Text = fmt.Sprint((c.dice[1] % 6) + 1)
}

return die.Layout(gtx)
}),
)
}

text := func(gtx Ctx) Dim {
lbl := material.H5(th, "Välj vars en tärning")
if c.dice[0] > 6 {
lbl.Text = ""
} else if c.dice[0] > c.dice[1] {
lbl.Text = "Röd är ny treman"
} else if c.dice[0] < c.dice[1] {
lbl.Text = "Blå är ny treman"
}
lbl.Alignment = text.Middle
return lbl.Layout(gtx)
}

button := func(gtx Ctx) Dim {
bttn := material.Button(th, c.bttn, "\nKör\n")
if c.dice[0] != c.dice[1] && c.dice[0] < 7 {
bttn.Text = "\nOK\n"
}
bttn.Background = colornames.Mediumseagreen

for c.bttn.Clicked() && c.dice[0] < 7 {
if bttn.Text == "\nKör\n" {
go func() {
c.dice.AnimateRoll()
for c.dice[0] == c.dice[1] {
//Challange should never yield identical dice
c.dice.Roll()
}
win.Invalidate()
}()
} else {
nextScreen = gameScreen(c.rules)
}
}

return bttn.Layout(gtx)
}

layout.UniformInset(unit.Dp(8)).Layout(gtx, func(gtx Ctx) Dim {
return layout.Flex{
Axis: layout.Vertical,
Alignment: layout.End,
Spacing: layout.SpaceBetween,
}.Layout(gtx,
layout.Rigid(title),
layout.Rigid(dice),
layout.Rigid(text),
layout.Rigid(button),
)
})

if ns, ok := nextScreen.(*game); ok {
ns.dice = c.old
}
return nextScreen
}
6 changes: 6 additions & 0 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ func (g *game) Layout(gtx Ctx, th *material.Theme) (nextScreen Screen) {
nextScreen = addRuleScreen(th, g.rules)
}
return newBttn.Layout(gtx)
} else if (SetRule{Set: Roll{2, 1}}.Valid(g.dice)) {
newBttn.Text = "\nUtmaning\n"
for g.newClick.Clicked() {
nextScreen = challengeScreen(g.rules, g.dice)
}
return newBttn.Layout(gtx)
}
return Dim{}
}),
Expand Down

0 comments on commit 0b87853

Please sign in to comment.