-
Notifications
You must be signed in to change notification settings - Fork 0
/
winop.go
297 lines (244 loc) · 7.04 KB
/
winop.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package draw
import (
"fmt"
"github.com/bcvery1/tilepix"
px "github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
"image/color"
"strings"
)
/*
Some general conventions on the Render(..) functions.
The parameters that are passed to Render are called
'context' and 'canvas'. Canvas represents the target
window/bitmap that will be rendered to. Context keeps
track of all resizes, mirrors, color changes and so
on that happens while traversing the WinOp tree.
An operation that is composite modifies
the canvas, and updates the context with this change,
renders it's child operation, and then restores the
canvas and context to the state they were in before.
A leaf operation does not modify the context,
but instead renders 'in the context it is', which
means no modification of either canvas nor context.
For example, an image will just render in the context
it is in; the canvas has already been setup for the
rendering, with all translations/scales/rotations,
color masks and so on, while a ColorOp will
1) set it's color as color mask in the canvas
2) render it's child element
3) restore canvas color mask from context
Context can thus be seen as an 'undo state'.
The reason we have a context at all is that the
matrix/color/etc state of the Canvas isn't public
in Pixel library, so we need to keep track of it
ourselves.
*/
type WinMoved struct {
translation px.Vec
winOp WinOp
}
func (winMoved WinMoved) DrawTo(canvas *pixelgl.Canvas, context Context) {
winMoved.Render(context.Transform, canvas)
}
func (winMoved WinMoved) String() string {
return strings.Join(winMoved.Lines(), "\n")
}
func (winMoved WinMoved) Lines() []string {
head := winMovedHeader(winMoved)
body := winMoved.winOp.Lines()
return headerWithIndentedBody(head, body)
}
func winMovedHeader(winMoved WinMoved) string {
xnum := winMoved.translation.X
xword := "right"
if xnum < 0 {
xnum *= -1
xword = "left"
}
ynum := winMoved.translation.Y
yword := "up"
if ynum < 0 {
ynum *= -1
yword = "down"
}
head := fmt.Sprintf("Moved %1.f pixels %v %1.f pixels %v:",
xnum,
xword,
ynum,
yword)
return head
}
func (winMoved WinMoved) Render(mx px.Matrix, canvas *pixelgl.Canvas) {
newMatrix := mx.Moved(winMoved.translation)
canvas.SetMatrix(newMatrix)
winMoved.winOp.Render(newMatrix, canvas)
canvas.SetMatrix(mx)
}
func Moved(translation px.Vec, winOp WinOp) WinOp {
return WinMoved{
translation: translation,
winOp: winOp,
}
}
type WinImdOp struct {
imdOp ImdOp
}
func (winImdOp WinImdOp) String() string {
return strings.Join(winImdOp.Lines(), "\n")
}
func (winImdOp WinImdOp) Lines() []string {
head := "WinOp from ImdOp:"
body := winImdOp.imdOp.Lines()
return headerWithIndentedBody(head, body)
}
func (winImdOp WinImdOp) Render(_ px.Matrix, canvas *pixelgl.Canvas) {
winImdOp.DrawTo(canvas, Context{px.IM})
}
func (winImdOp WinImdOp) DrawTo(canvas *pixelgl.Canvas, _ Context) {
imd := imdraw.New(nil)
winImdOp.imdOp.Render(imd)
imd.Draw(canvas)
}
func ToWinOp(imdOp ImdOp) WinOp {
return WinImdOp{imdOp: imdOp}
}
type TileLayerOp struct {
layerName string
tileMap *tilepix.Map
}
func (tileLayerOp TileLayerOp) String() string {
return fmt.Sprintf("TileLayer \"Foreground\"")
}
func (tileLayerOp TileLayerOp) Lines() []string {
return []string{tileLayerOp.String()}
}
func (tileLayerOp TileLayerOp) Render(_ px.Matrix, canvas *pixelgl.Canvas) {
tileLayerOp.DrawTo(canvas, Context{Transform: px.IM})
}
func (tileLayerOp TileLayerOp) DrawTo(canvas *pixelgl.Canvas, _ Context) {
_ = tileLayerOp.tileMap.GetTileLayerByName(tileLayerOp.layerName).Draw(canvas)
}
func TileLayer(tileMap *tilepix.Map, layerName string) WinOp {
return TileLayerOp{
layerName: layerName,
tileMap: tileMap,
}
}
type ImageOp struct {
image *px.Sprite
name string
}
func (imageOp ImageOp) String() string {
return fmt.Sprintf("Image \"%v\"", imageOp.name)
}
func (imageOp ImageOp) Lines() []string {
return []string{imageOp.String()}
}
func (imageOp ImageOp) Render(_ px.Matrix, canvas *pixelgl.Canvas) {
imageOp.DrawTo(canvas, Context{Transform: px.IM})
}
func (imageOp ImageOp) DrawTo(canvas *pixelgl.Canvas, _ Context) {
sprite := imageOp.image
sprite.Draw(canvas, px.IM)
}
func Image(image *px.Sprite, name string) WinOp {
return ImageOp{
image: image,
name: name,
}
}
type ColorOp struct {
color color.Color
winOp WinOp
}
func (colorOp ColorOp) DrawTo(canvas *pixelgl.Canvas, context Context) {
colorOp.Render(context.Transform, canvas)
}
func (colorOp ColorOp) String() string {
return strings.Join(colorOp.Lines(), "\n")
}
func (colorOp ColorOp) Lines() []string {
head := fmt.Sprintf("Color %v:", colorOp.color)
body := colorOp.winOp.Lines()
return headerWithIndentedBody(head, body)
}
func (colorOp ColorOp) Render(mx px.Matrix, canvas *pixelgl.Canvas) {
canvas.SetColorMask(colorOp.color)
colorOp.winOp.Render(mx, canvas)
// TODO: Color should be part of 'context' so that restore becomes saner
canvas.SetColorMask(colornames.White)
}
func Color(color color.RGBA, winOp WinOp) WinOp {
return ColorOp{
color: color,
winOp: winOp,
}
}
type WinOpSequence struct {
winOps []WinOp
}
func (sequence WinOpSequence) String() string {
head := "WinOp Sequence:"
body := make([]string, 0)
for _, op := range sequence.winOps {
for _, line := range op.Lines() {
body = append(body, line)
}
}
return strings.Join(headerWithIndentedBody(head, body), "\n")
}
func (sequence WinOpSequence) Lines() []string {
return strings.Split(sequence.String(), "\n")
}
func (sequence WinOpSequence) Render(mx px.Matrix, canvas *pixelgl.Canvas) {
sequence.DrawTo(canvas, Context{Transform: mx})
}
func (sequence WinOpSequence) DrawTo(canvas *pixelgl.Canvas, context Context) {
mx := context.Transform
for _, op := range sequence.winOps {
op.Render(mx, canvas)
}
}
func OpSequence(ops ...WinOp) WinOp {
return WinOpSequence{
winOps: ops,
}
}
type OpMirrored struct {
op WinOp
}
func (mirrored OpMirrored) String() string {
return strings.Join(mirrored.Lines(), "\n")
}
func (mirrored OpMirrored) Lines() []string {
head := "Mirrored around Y axis:"
body := make([]string, 0)
for _, line := range mirrored.op.Lines() {
body = append(body, line)
}
return headerWithIndentedBody(head, body)
}
func (mirrored OpMirrored) Render(mx px.Matrix, canvas *pixelgl.Canvas) {
mirrored.DrawTo(canvas, Context{Transform: mx})
}
func (mirrored OpMirrored) DrawTo(canvas *pixelgl.Canvas, context Context) {
// Pixel isn't following linear algebra convention of matrix multiplication;
// m.Moved(..).Scaled(..) really means:
// Scaled(..)*Moved(..)*m
// .. which means that if we want to mirror an image around the Y-axis,
// this has to be written with the scale to the left! :)
oldTransform := context.Transform
context.Transform = px.IM.ScaledXY(px.V(0, 1), px.V(-1, 1)).Chained(oldTransform)
canvas.SetMatrix(context.Transform)
mirrored.op.DrawTo(canvas, context)
canvas.SetMatrix(oldTransform)
}
func Mirrored(winOp WinOp) WinOp {
return OpMirrored{op: winOp}
}
type Context struct {
Transform px.Matrix
}