-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbar.go
128 lines (110 loc) · 2.81 KB
/
bar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"image"
"image/color"
"strconv"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
)
var (
colorBarBackground = color.NRGBA{R: 211, G: 211, B: 211, A: 255}
colorBarTextWon = color.NRGBA{R: 8, G: 229, A: 255}
colorBarTextLost = color.NRGBA{R: 202, G: 44, A: 255}
)
type Bar struct {
th *material.Theme
restart widget.Clickable
}
func NewBar(th *material.Theme) *Bar {
return &Bar{
th: th,
}
}
func (b *Bar) Layout(gtx C) D {
// Handle events.
if b.restart.Clicked() {
ResetGame()
}
return layout.Stack{Alignment: layout.Center}.Layout(gtx,
layout.Expanded(func(gtx C) D {
gtx.Constraints.Max.Y = 80
const inset = 3
min := image.Pt(inset, inset)
max := gtx.Constraints.Max
max.X -= inset
max.Y -= inset
rect := clip.Rect{Min: min, Max: max}
paint.FillShape(gtx.Ops, colorBarBackground, rect.Op())
return D{Size: gtx.Constraints.Max}
}),
layout.Expanded(func(gtx C) D {
return layout.Flex{
Axis: layout.Horizontal,
}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Inset{
Left: unit.Dp(16),
}.Layout(gtx, func(gtx C) D {
return layout.Center.Layout(gtx, func(gtx C) D {
return material.H4(b.th, "⏲️"+strconv.FormatUint(uint64(state.SecondsLeft), 10)).Layout(gtx)
})
})
}),
layout.Flexed(1, func(gtx C) D {
return layout.Center.Layout(gtx, func(gtx C) D {
if state.Running || !state.Finished {
return D{}
}
margin := layout.Inset{Left: unit.Dp(40)}
color := colorBarTextWon
if state.Lost {
color = colorBarTextLost
}
label := material.H4(b.th, "You")
label.Alignment = text.End
label.Color = color
return margin.Layout(gtx, label.Layout)
})
}),
layout.Rigid(func(gtx C) D {
return layout.Center.Layout(gtx, func(gtx C) D {
return material.Button(b.th, &b.restart, "Reset").Layout(gtx)
})
}),
layout.Flexed(1, func(gtx C) D {
return layout.Center.Layout(gtx, func(gtx C) D {
if state.Running || !state.Finished {
return D{}
}
var (
t = "Won"
color = colorBarTextWon
)
if state.Lost {
t = "Lost"
color = colorBarTextLost
}
margin := layout.Inset{Right: unit.Dp(40)}
label := material.H4(b.th, t)
label.Color = color
return margin.Layout(gtx, label.Layout)
})
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{
Right: unit.Dp(16),
}.Layout(gtx, func(gtx C) D {
return layout.Center.Layout(gtx, func(gtx C) D {
return material.H4(b.th, strconv.FormatUint(uint64(state.selectFlagsLeft()), 10)+"💣").Layout(gtx)
})
})
}),
)
}),
)
}