-
Notifications
You must be signed in to change notification settings - Fork 103
/
widget.go
48 lines (39 loc) · 1000 Bytes
/
widget.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
package render
import (
"image"
"github.com/tidbyt/gg"
)
// A Widget is a self-contained object that can render itself as an image.
type Widget interface {
// PaintBounds Returns the bounds of the area that will actually be drawn to when Paint() is called
PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle
Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int)
FrameCount() int
}
// Widgets can require initialization
type WidgetWithInit interface {
Init() error
}
// WidgetStaticSize has inherent size and width known before painting.
type WidgetStaticSize interface {
Size() (int, int)
}
// Computes a (mod m). Useful for handling frameIdx > num available
// frames in Widget.Paint()
func ModInt(a, m int) int {
a = a % m
if a < 0 {
a += m
}
return a
}
// Computes the maximum frame count of a slice of widgets.
func MaxFrameCount(widgets []Widget) int {
m := 1
for _, w := range widgets {
if c := w.FrameCount(); c > m {
m = c
}
}
return m
}