~eliasnaur/gio-example

ref: c36baf973e18e174a4ebe926754bf5f365c65d8a gio-example/gio-extras/outlay/fan/widget/boring/cards.go -rw-r--r-- 3.5 KiB
c36baf97Chris Waldon gio-extras/materials: add materials example 4 months ago
                                                                                
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package boring

import (
	"image/color"
	"math"

	"gioui.org/f32"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/unit"
	"gioui.org/widget/material"
	"gioui.org/example/gio-extras/outlay/fan/playing"
	xwidget "gioui.org/example/gio-extras/outlay/fan/widget"
)

type (
	C = layout.Context
	D = layout.Dimensions
)

type CardPalette struct {
	RedSuit, BlackSuit color.NRGBA
	Border, Background color.NRGBA
}

func (p CardPalette) ColorFor(s playing.Suit) color.NRGBA {
	if s.Color() == playing.Red {
		return p.RedSuit
	}
	return p.BlackSuit
}

var DefaultPalette = &CardPalette{
	RedSuit:    color.NRGBA{R: 0xa0, B: 0x20, A: 0xff},
	BlackSuit:  color.NRGBA{A: 0xff},
	Border:     color.NRGBA{R: 0x80, G: 0x80, B: 0x80, A: 0xff},
	Background: color.NRGBA{R: 0xf0, G: 0xf0, B: 0xf0, A: 0xff},
}

type CardStyle struct {
	*material.Theme
	playing.Card
	Height unit.Value
	*CardPalette
}

const cardHeightToWidth = 14.0 / 9.0
const cardRadiusToWidth = 1.0 / 16.0
const borderWidth = 0.005

func (c *CardStyle) Palette() *CardPalette {
	if c.CardPalette == nil {
		return DefaultPalette
	}
	return c.CardPalette
}

func (c *CardStyle) Layout(gtx C) D {
	gtx.Constraints.Max.Y = gtx.Px(c.Height)
	gtx.Constraints.Max.X = int(float32(gtx.Constraints.Max.Y) / cardHeightToWidth)
	outerRadius := float32(gtx.Constraints.Max.X) * cardRadiusToWidth
	innerRadius := (1 - borderWidth) * outerRadius

	borderWidth := c.Height.Scale(borderWidth)
	return layout.Stack{}.Layout(gtx,
		layout.Expanded(func(gtx C) D {
			return Rect{
				Color: c.Palette().Border,
				Size:  layout.FPt(gtx.Constraints.Max),
				Radii: outerRadius,
			}.Layout(gtx)
		}),
		layout.Stacked(func(gtx C) D {
			return layout.UniformInset(borderWidth).Layout(gtx, func(gtx C) D {
				return layout.Stack{}.Layout(gtx,
					layout.Expanded(func(gtx C) D {
						return Rect{
							Color: c.Palette().Background,
							Size:  layout.FPt(gtx.Constraints.Max),
							Radii: innerRadius,
						}.Layout(gtx)
					}),
					layout.Stacked(func(gtx C) D {
						return layout.UniformInset(unit.Dp(2)).Layout(gtx, func(gtx C) D {
							defer op.Push(gtx.Ops).Pop()
							gtx.Constraints.Min = gtx.Constraints.Max
							origin := f32.Point{
								X: float32(gtx.Constraints.Max.X / 2),
								Y: float32(gtx.Constraints.Max.Y / 2),
							}
							layout.Center.Layout(gtx, func(gtx C) D {
								face := material.H1(c.Theme, c.Rank.String())
								face.Color = c.Palette().ColorFor(c.Suit)
								return face.Layout(gtx)
							})
							c.layoutCorner(gtx)
							op.Affine(f32.Affine2D{}.Rotate(origin, math.Pi)).Add(gtx.Ops)
							c.layoutCorner(gtx)

							return D{Size: gtx.Constraints.Max}
						})
					}),
				)
			})
		}),
	)
}

func (c *CardStyle) layoutCorner(gtx layout.Context) layout.Dimensions {
	col := c.Palette().ColorFor(c.Suit)
	return layout.NW.Layout(gtx, func(gtx C) D {
		return layout.UniformInset(unit.Dp(4)).Layout(gtx, func(gtx C) D {
			return layout.Flex{
				Axis:      layout.Vertical,
				Alignment: layout.Middle,
			}.Layout(gtx,
				layout.Rigid(func(gtx C) D {
					label := material.H6(c.Theme, c.Rank.String())
					label.Color = col
					return label.Layout(gtx)
				}),
				layout.Rigid(func(gtx C) D {
					label := material.H6(c.Theme, c.Suit.String())
					label.Color = col
					return label.Layout(gtx)
				}),
			)
		})
	})
}

type HoverCard struct {
	CardStyle
	*xwidget.HoverState
}

func (h HoverCard) Layout(gtx C) D {
	dims := h.CardStyle.Layout(gtx)
	gtx.Constraints.Max = dims.Size
	gtx.Constraints.Min = dims.Size
	h.HoverState.Layout(gtx)
	return dims
}