-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayer.go
More file actions
207 lines (175 loc) · 5.13 KB
/
layer.go
File metadata and controls
207 lines (175 loc) · 5.13 KB
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
package touch
import (
"image"
"image/color"
)
type Layer interface {
Children() []Layer
AddChild(...Layer)
InsertChild(Layer, int)
RemoveChild(Layer)
Frame() image.Rectangle
SetFrame(frame image.Rectangle)
Invalidate()
InvalidateRect(rect image.Rectangle)
// Render should render this layer and its subtree into ctx
Render(ctx DrawingContext)
// DrawIn should draw the parts of this layer that are visible in ctx
DrawIn(ctx DrawingContext)
HitTest(TouchEvent) LayerTouchDelegate
Parent() Layer
SetParent(Layer)
RemoveFromParent()
// TODO/WIP: Add IsOpaque()
OpaqueRect() image.Rectangle
// In a pre-rendering phase, collect the UNION of all child
// frames that need to be displayed with DrawingMode draw.Copy;
// In the rendering phase, draw dirty views in the global context,
// and (if the union rect is nonempty) draw all nondirty views
// that overlap the union rect in a clipped context.
// We may also choose to build a drawlist in which views that
// would be completely covered by another, opaque view are filtered
// out.
}
type LayerTouchDelegate interface {
StartTouch(TouchEvent)
UpdateTouch(TouchEvent)
EndTouch(TouchEvent)
CancelTouch()
}
type touchBlocker struct{}
func (tb touchBlocker) StartTouch(TouchEvent) {}
func (tb touchBlocker) UpdateTouch(TouchEvent) {}
func (tb touchBlocker) EndTouch(TouchEvent) {}
func (tb touchBlocker) CancelTouch() {}
type BasicLayer struct {
image.Rectangle
Radius int
Background color.Color
Self Layer
parent Layer
children []Layer
}
// Layer returns a layer interface to the outermost struct associated with this layer.
func (layer *BasicLayer) Layer() Layer {
if layer.Self != nil {
return layer.Self
}
return layer
}
func (layer *BasicLayer) Frame() image.Rectangle {
return layer.Rectangle
}
func (layer *BasicLayer) SetFrame(frame image.Rectangle) {
if layer.Eq(frame) {
return
}
leafClass := layer.Layer()
if !layer.Rectangle.Empty() {
leafClass.InvalidateRect(layer.Rectangle)
}
leafClass.InvalidateRect(frame)
layer.Rectangle = frame
}
func (layer *BasicLayer) AddChild(layers ...Layer) {
layer.children = append(layer.children, layers...)
self := layer.Layer()
for _, child := range layers {
child.SetParent(self)
}
}
func (layer *BasicLayer) InsertChild(child Layer, index int) {
child.SetParent(layer.Layer())
if index < len(layer.children) {
// Splice array into itself, duplicating element at index.
layer.children = append(layer.children[:index+1], layer.children[index:]...)
layer.children[index] = child
} else {
layer.children = append(layer.children, child)
}
}
func (layer *BasicLayer) RemoveChild(child Layer) {
for idx := range layer.children {
if layer.children[idx] == child {
layer.children = append(layer.children[:idx], layer.children[idx+1:]...)
layer.Layer().InvalidateRect(child.Frame())
return
}
}
}
func (layer *BasicLayer) HitTest(event TouchEvent) LayerTouchDelegate {
for idx := len(layer.children); idx > 0; idx-- {
if target := layer.children[idx-1].HitTest(event); target != nil {
return target
}
}
if event.Pressed && event.In(layer.Rectangle) {
if interactor, ok := layer.Self.(LayerTouchDelegate); ok {
return interactor
}
// Visible views block touches by default
// TODO: More-explicit method of blocking or allowing touches
if layer.Background != nil {
return touchBlocker{}
}
}
return nil
}
func (layer *BasicLayer) OpaqueRect() image.Rectangle {
if layer.Background != nil {
if _, _, _, a := layer.Background.RGBA(); a == 0xFFFF {
return layer.Rectangle.Inset((layer.Radius + 1) / 2)
}
}
return image.Rectangle{}
}
func (layer *BasicLayer) Parent() Layer {
return layer.parent
}
func (layer *BasicLayer) SetParent(parent Layer) {
layer.parent = parent
layer.Invalidate()
}
func (layer *BasicLayer) RemoveFromParent() {
if layer.parent != nil {
layer.parent.RemoveChild(layer.Layer())
}
}
func (layer *BasicLayer) Children() []Layer {
return layer.children
}
func (layer *BasicLayer) Invalidate() {
layer.Layer().InvalidateRect(layer.Rectangle)
}
func (layer *BasicLayer) InvalidateRect(rect image.Rectangle) {
if p := layer.Parent(); p != nil {
p.InvalidateRect(rect)
}
}
// DrawChildren draws child layers IFF they are visible in ctx, and (need display or overlap rect)
func (layer *BasicLayer) Render(ctx DrawingContext) {
// Draw the smallest rect of this layer that is not occluded by opaque children
rect := ctx.Bounds()
for _, child := range layer.children {
if opaque := child.OpaqueRect(); !opaque.Empty() {
rect = Winnow(rect, opaque)
}
}
// Draw this layer IFF there are pixels to be drawn.
if clipped := ctx.Clip(rect); !clipped.Bounds().Empty() {
layer.Layer().DrawIn(clipped)
}
// Render children (separate phase)
// TODO: Clip overlapping children of lower Z-order
for _, child := range layer.children {
if clipped := ctx.Clip(child.Frame()); !clipped.Bounds().Empty() {
child.Render(clipped)
}
}
}
// For delegation of default drawing behavior (Background / roundrect)
func (layer *BasicLayer) DrawIn(ctx DrawingContext) {
if layer.Background != nil {
ctx.Fill(layer.Rectangle, layer.Background, layer.Radius)
}
}