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
|
package layers
import (
"container/list"
"errors"
"image"
"image/draw"
)
type ImageFlusher interface {
Image() *image.RGBA
Flush(r image.Rectangle)
}
type Layers struct {
dst ImageFlusher
layers list.List
}
func (l *Layers) Dst(dst ImageFlusher) {
l.dst = dst
for e := l.layers.Front(); e != nil; e = e.Next() {
layer := e.Value.(*Layer)
rgba := image.NewRGBA(dst.Image().Bounds())
draw.Draw(rgba, layer.rgba.Bounds(), layer.rgba, layer.rgba.Bounds().Min, draw.Src)
layer.rgba = rgba
}
}
func (l *Layers) Push() *Layer {
layer := &Layer{
l: l,
rgba: image.NewRGBA(l.dst.Image().Bounds()),
}
layer.e = l.layers.PushBack(layer)
return layer
}
func (l *Layers) Flush(r image.Rectangle) {
if l.dst == nil {
panic(errors.New("layers: Flush: no destination"))
}
draw.Draw(l.dst.Image(), r, image.Transparent, r.Min, draw.Src)
for e := l.layers.Front(); e != nil; e = e.Next() {
layer := e.Value.(*Layer)
draw.Draw(l.dst.Image(), r, layer.rgba, r.Min, draw.Over)
}
l.dst.Flush(r)
}
type Layer struct {
l *Layers
e *list.Element
rgba *image.RGBA
}
func (l *Layer) Remove() {
l.l.layers.Remove(l.e)
}
func (l *Layer) Front() {
l.l.layers.MoveToBack(l.e)
}
func (l *Layer) Image() *image.RGBA {
return l.rgba
}
func (l *Layer) Flush(r image.Rectangle) {
l.l.Flush(r)
}
|