2019-09-06 12:16:08 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
|
2019-11-20 14:30:43 -05:00
|
|
|
"gioui.org/f32"
|
|
|
|
"gioui.org/gesture"
|
|
|
|
"gioui.org/io/pointer"
|
|
|
|
"gioui.org/layout"
|
|
|
|
"gioui.org/op/clip"
|
|
|
|
"gioui.org/op/paint"
|
|
|
|
"gioui.org/text"
|
|
|
|
"gioui.org/unit"
|
2019-09-06 12:16:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
black = color.RGBA{A: 0xff, R: 0, G: 0, B: 0}
|
|
|
|
white = color.RGBA{A: 0xff, R: 0xff, G: 0xff, B: 0xff}
|
|
|
|
gray = color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xf0}
|
|
|
|
darkgray = color.RGBA{A: 0xff, R: 0xa0, G: 0xa0, B: 0xa0}
|
|
|
|
)
|
|
|
|
|
|
|
|
type Overlay struct {
|
2019-11-20 14:30:43 -05:00
|
|
|
Size unit.Value
|
2019-09-06 12:16:08 -04:00
|
|
|
Text string
|
|
|
|
Click gesture.Click
|
|
|
|
Color color.RGBA
|
|
|
|
Background color.RGBA
|
|
|
|
Alignment text.Alignment
|
|
|
|
}
|
|
|
|
|
2019-11-20 14:30:43 -05:00
|
|
|
func (b *Overlay) Layout(gtx *layout.Context) {
|
|
|
|
ins := layout.UniformInset(unit.Dp(1))
|
|
|
|
ins.Layout(gtx, func() {
|
|
|
|
st := layout.Stack{}
|
|
|
|
c2 := st.Rigid(gtx, func() {
|
|
|
|
l := th.Label(b.Size, b.Text)
|
|
|
|
ins := layout.UniformInset(unit.Dp(4))
|
|
|
|
l.Color = b.Color
|
|
|
|
ins.Layout(gtx, func() {
|
|
|
|
l.Layout(gtx)
|
|
|
|
})
|
|
|
|
pointer.RectAreaOp{image.Rect(0, 0, gtx.Dimensions.Size.X, gtx.Dimensions.Size.Y)}.Add(gtx.Ops)
|
|
|
|
})
|
|
|
|
c1 := st.Expand(gtx, func() {
|
|
|
|
layoutRRect(b.Background, gtx)
|
|
|
|
})
|
|
|
|
st.Layout(gtx, c1, c2)
|
|
|
|
})
|
2019-09-06 12:16:08 -04:00
|
|
|
}
|
|
|
|
|
2019-10-01 18:22:38 -04:00
|
|
|
type SelButton struct {
|
|
|
|
Button
|
|
|
|
SelColor color.RGBA
|
|
|
|
Selected bool
|
|
|
|
}
|
|
|
|
|
2019-09-06 12:16:08 -04:00
|
|
|
type Button struct {
|
2019-11-20 14:30:43 -05:00
|
|
|
Size unit.Value
|
2019-09-06 12:16:08 -04:00
|
|
|
Label string
|
|
|
|
Click gesture.Click
|
|
|
|
Color color.RGBA
|
|
|
|
Background color.RGBA
|
|
|
|
Alignment text.Alignment
|
|
|
|
clicked bool
|
|
|
|
}
|
|
|
|
|
2019-11-20 14:30:43 -05:00
|
|
|
func layoutRRect(col color.RGBA, gtx *layout.Context) {
|
|
|
|
r := float32(gtx.Config.Px(unit.Dp(4)))
|
|
|
|
sz := image.Point{X: gtx.Constraints.Width.Min, Y: gtx.Constraints.Height.Min}
|
2019-09-06 12:16:08 -04:00
|
|
|
w, h := float32(sz.X), float32(sz.Y)
|
2019-11-20 14:30:43 -05:00
|
|
|
rect := f32.Rectangle{
|
|
|
|
f32.Point{0, 0},
|
|
|
|
f32.Point{w, h},
|
|
|
|
}
|
|
|
|
clip.RoundRect(gtx.Ops, rect, r, r, r, r)
|
|
|
|
paint.ColorOp{Color: col}.Add(gtx.Ops)
|
|
|
|
paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: w, Y: h}}}.Add(gtx.Ops)
|
|
|
|
gtx.Dimensions = layout.Dimensions{Size: sz}
|
2019-09-06 12:16:08 -04:00
|
|
|
}
|
|
|
|
|
2019-11-20 14:30:43 -05:00
|
|
|
func (b *Button) Layout(gtx *layout.Context) {
|
2019-09-06 12:16:08 -04:00
|
|
|
b.clicked = false
|
2019-11-20 14:30:43 -05:00
|
|
|
for _, ev := range b.Click.Events(gtx) {
|
2019-09-06 12:16:08 -04:00
|
|
|
if ev.Type == gesture.TypeClick {
|
|
|
|
b.clicked = true
|
|
|
|
}
|
|
|
|
}
|
2019-11-20 14:30:43 -05:00
|
|
|
ins := layout.UniformInset(unit.Dp(1))
|
|
|
|
ins.Layout(gtx, func() {
|
|
|
|
st := layout.Stack{}
|
|
|
|
c2 := st.Rigid(gtx, func() {
|
|
|
|
l := th.Label(b.Size, b.Label)
|
|
|
|
ins := layout.UniformInset(unit.Dp(4))
|
|
|
|
//paint.ColorOp{Color: b.Color}.Add(ops)
|
|
|
|
ins.Layout(gtx, func() {
|
|
|
|
l.Layout(gtx)
|
|
|
|
})
|
|
|
|
pointer.RectAreaOp{image.Rect(0, 0, gtx.Dimensions.Size.X, gtx.Dimensions.Size.Y)}.Add(gtx.Ops)
|
|
|
|
b.Click.Add(gtx.Ops)
|
|
|
|
})
|
|
|
|
c1 := st.Expand(gtx, func() {
|
|
|
|
layoutRRect(b.Background, gtx)
|
|
|
|
})
|
|
|
|
st.Layout(gtx, c1, c2)
|
|
|
|
})
|
2019-09-06 12:16:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Button) Clicked() bool {
|
|
|
|
return b.clicked
|
|
|
|
}
|
2019-10-01 18:22:38 -04:00
|
|
|
|
2019-11-20 14:30:43 -05:00
|
|
|
func (b *SelButton) Toggle() {
|
|
|
|
b.Selected = !b.Selected
|
2019-10-03 17:34:04 -04:00
|
|
|
b.SelColor, b.Background = b.Background, b.SelColor
|
|
|
|
}
|
|
|
|
|
2019-11-20 14:30:43 -05:00
|
|
|
func (b *SelButton) Select() {
|
2019-10-03 17:34:04 -04:00
|
|
|
if !b.Selected {
|
2019-11-20 14:30:43 -05:00
|
|
|
b.Toggle()
|
2019-10-03 17:34:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 14:30:43 -05:00
|
|
|
func (b *SelButton) Deselect() {
|
2019-10-03 17:34:04 -04:00
|
|
|
if b.Selected {
|
2019-11-20 14:30:43 -05:00
|
|
|
b.Toggle()
|
2019-10-03 17:34:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 18:22:38 -04:00
|
|
|
func (b *SelButton) Clicked() bool {
|
|
|
|
if b.clicked {
|
2019-10-03 17:34:04 -04:00
|
|
|
b.Toggle()
|
2019-10-01 18:22:38 -04:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|