From 100d1b1a321b9e549bcbbfad2c5fa8acb34eab68 Mon Sep 17 00:00:00 2001 From: axel paulander Date: Thu, 20 Aug 2020 21:52:56 +0200 Subject: [PATCH] added back button for non android --- add.go | 82 ++++++++++++++++++++++++++++++++++++++-------------- viewRules.go | 27 +++++++++++++++-- 2 files changed, 85 insertions(+), 24 deletions(-) diff --git a/add.go b/add.go index 273a40f..4c591e5 100644 --- a/add.go +++ b/add.go @@ -1,31 +1,38 @@ package main import ( + "image/color" + "runtime" + "gioui.org/io/key" "gioui.org/layout" + "gioui.org/text" "gioui.org/unit" "gioui.org/widget" "gioui.org/widget/material" + "golang.org/x/image/colornames" ) type addRule struct { rules []Rule - ruleRadio *widget.Enum - d1 *diceButton - d2 *diceButton - nameEdit *widget.Editor - saveClick *widget.Clickable + ruleRadio *widget.Enum + d1 *diceButton + d2 *diceButton + nameEdit *widget.Editor + saveClick *widget.Clickable + cancelClick *widget.Clickable } func addRuleScreen(th *material.Theme, rules []Rule) Screen { a := &addRule{ - rules: rules, - ruleRadio: new(widget.Enum), - d1: newDiceButton(th), - d2: newDiceButton(th), - nameEdit: new(widget.Editor), - saveClick: new(widget.Clickable), + rules: rules, + ruleRadio: new(widget.Enum), + d1: newDiceButton(th), + d2: newDiceButton(th), + nameEdit: new(widget.Editor), + saveClick: new(widget.Clickable), + cancelClick: new(widget.Clickable), } a.ruleRadio.Value = "sum" @@ -37,17 +44,48 @@ func (a *addRule) Layout(gtx Ctx, th *material.Theme) (nextScreen Screen) { nextScreen = a radio := func(gtx Ctx) Dim { - in := layout.UniformInset(unit.Dp(0)) - in.Top = unit.Dp(64) - return in.Layout(gtx, func(gtx Ctx) Dim { - return layout.Flex{ - Spacing: layout.SpaceAround, - }.Layout(gtx, - layout.Rigid(material.RadioButton(th, a.ruleRadio, "sum", "Summa").Layout), - layout.Rigid(material.RadioButton(th, a.ruleRadio, "set", "Par").Layout), - layout.Rigid(material.RadioButton(th, a.ruleRadio, "single", "En tärning").Layout), - ) - }) + if runtime.GOOS == "android" { + in := layout.UniformInset(unit.Dp(0)) + in.Top = unit.Dp(64) + return in.Layout(gtx, func(gtx Ctx) Dim { + return layout.Flex{ + Spacing: layout.SpaceAround, + }.Layout(gtx, + layout.Rigid(material.RadioButton(th, a.ruleRadio, "sum", "Summa").Layout), + layout.Rigid(material.RadioButton(th, a.ruleRadio, "set", "Par").Layout), + layout.Rigid(material.RadioButton(th, a.ruleRadio, "single", "En tärning").Layout), + ) + }) + } + return layout.Flex{ + Axis: layout.Vertical, + }.Layout(gtx, + layout.Rigid(func(gtx Ctx) Dim { + return layout.Flex{}.Layout(gtx, + layout.Rigid(func(gtx Ctx) Dim { + bttn := material.Button(th, a.cancelClick, "←") + bttn.Color = colornames.Black + bttn.Background = color.RGBA{255, 255, 255, 255} + bttn.Font.Weight = text.Bold + + for a.cancelClick.Clicked() { + nextScreen = gameScreen(a.rules) + } + + return bttn.Layout(gtx) + }), + ) + }), + layout.Rigid(func(gtx Ctx) Dim { + return layout.Flex{ + Spacing: layout.SpaceAround, + }.Layout(gtx, + layout.Rigid(material.RadioButton(th, a.ruleRadio, "sum", "Summa").Layout), + layout.Rigid(material.RadioButton(th, a.ruleRadio, "set", "Par").Layout), + layout.Rigid(material.RadioButton(th, a.ruleRadio, "single", "En tärning").Layout), + ) + }), + ) } rolls := func(gtx Ctx) Dim { diff --git a/viewRules.go b/viewRules.go index 297cf33..f9b3a82 100644 --- a/viewRules.go +++ b/viewRules.go @@ -1,9 +1,14 @@ package main import ( + "image/color" + "runtime" + "gioui.org/layout" "gioui.org/unit" + "gioui.org/widget" "gioui.org/widget/material" + "golang.org/x/image/colornames" ) var baserules = `Först väljs en spelare till treman, detta är en titel som kommer förflytta sig under spelets gång. @@ -21,7 +26,8 @@ n+n klunkar betyder att n st klunkar får delas ut till två personer eller dubb type viewRules struct { rules []Rule - list *layout.List + list *layout.List + cancelClick *widget.Clickable } func viewRulesScreen(rules []Rule) Screen { @@ -30,6 +36,7 @@ func viewRulesScreen(rules []Rule) Screen { list: &layout.List{ Axis: layout.Vertical, }, + cancelClick: new(widget.Clickable), } } @@ -39,8 +46,24 @@ func (v *viewRules) Layout(gtx Ctx, th *material.Theme) (nextScreen Screen) { layout.UniformInset(unit.Dp(8)).Layout(gtx, func(gtx Ctx) Dim { return v.list.Layout(gtx, len(v.rules)+2, func(gtx Ctx, i int) Dim { return layout.UniformInset(unit.Dp(16)).Layout(gtx, func(gtx Ctx) Dim { - if i == 0 { + if i == 0 && runtime.GOOS == "android" { return layout.Inset{Top: unit.Dp(16)}.Layout(gtx, material.H6(th, "Regler").Layout) + } else if i == 0 { + bttn := material.Button(th, v.cancelClick, "← Regler"+runtime.GOOS) + bttn.Color = colornames.Black + bttn.Background = color.RGBA{255, 255, 255, 255} + bttn.TextSize = material.H6(th, "").TextSize + bttn.Inset.Left = unit.Dp(0) + + for v.cancelClick.Clicked() { + nextScreen = gameScreen(v.rules) + } + + return layout.Flex{ + Alignment: layout.Start, + }.Layout(gtx, + RigidInset(layout.Inset{Top: unit.Dp(16)}, bttn.Layout), + ) } else if i == 1 { body := material.Body1(th, baserules) body.TextSize = unit.Dp(24)