-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcontext.go
136 lines (111 loc) · 2.73 KB
/
context.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
129
130
131
132
133
134
135
136
// Copyright ©2021 The go-p5 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p5
import (
"image/color"
"gioui.org/f32"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/text"
)
// stackOps holds a stack of Gio operations and state.
type stackOps struct {
ops *op.Ops
ctx []context
}
func newStackOps(ops *op.Ops) *stackOps {
return &stackOps{
ops: ops,
ctx: make([]context, 1),
}
}
// context holds the state of the graphics stack.
type context struct {
bkg color.Color
fill color.Color
stroke strokeStyle
text textStyle
tau float32 // Catmull-Rom tension, used for Curve.
state op.StateOp
}
type strokeStyle struct {
color color.Color
style clip.StrokeStyle
}
type textStyle struct {
color color.Color
align text.Alignment
size float32
font text.Font
}
func (stk *stackOps) cur() *context {
return &stk.ctx[len(stk.ctx)-1]
}
func (stk *stackOps) save() {
stk.ctx = append(stk.ctx, *stk.cur())
stk.cur().state = op.Save(stk.ops)
}
func (stk *stackOps) load() {
stk.cur().state.Load()
stk.ctx = stk.ctx[:len(stk.ctx)-1]
}
func (stk *stackOps) rotate(angle float64) {
ops := stk.ops
aff := f32.Affine2D{}.Rotate(f32.Pt(0, 0), float32(-angle))
op.Affine(aff).Add(ops)
}
func (stk *stackOps) scale(x, y float64) {
ops := stk.ops
aff := f32.Affine2D{}.Scale(
f32.Pt(0, 0),
f32.Pt(float32(x), float32(y)),
)
op.Affine(aff).Add(ops)
}
func (stk *stackOps) translate(x, y float64) {
op.Offset(f32.Pt(float32(x), float32(y))).Add(stk.ops)
}
func (stk *stackOps) shear(x, y float64) {
ops := stk.ops
aff := f32.Affine2D{}.Shear(
f32.Pt(0, 0),
float32(x), float32(y),
)
op.Affine(aff).Add(ops)
}
func (stk *stackOps) matrix(aff f32.Affine2D) {
op.Affine(aff).Add(stk.ops)
}
// Push saves the current drawing style settings and transformations.
func (p *Proc) Push() {
p.stk.save()
}
// Pop restores the previous drawing style settings and transformations.
func (p *Proc) Pop() {
p.stk.load()
}
// Rotate rotates the graphical context by angle radians.
// Positive angles rotate counter-clockwise.
func (p *Proc) Rotate(angle float64) {
p.stk.rotate(angle)
}
// Scale rescales the graphical context by x and y.
func (p *Proc) Scale(x, y float64) {
p.stk.scale(x, y)
}
// Translate applies a translation by x and y.
func (p *Proc) Translate(x, y float64) {
p.stk.translate(x, y)
}
// Shear shears the graphical context by the given x and y angles in radians.
func (p *Proc) Shear(x, y float64) {
p.stk.shear(x, y)
}
// Matrix sets the affine matrix transformation.
func (p *Proc) Matrix(a, b, c, d, e, f float64) {
p.stk.matrix(f32.NewAffine2D(
float32(a), float32(b), float32(c),
float32(d), float32(e), float32(f),
))
}