forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
painter.go
63 lines (54 loc) · 1.77 KB
/
painter.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
package software
import (
"image"
"github.com/williambrode/fyne/v2"
"github.com/williambrode/fyne/v2/canvas"
"github.com/williambrode/fyne/v2/internal/driver"
"github.com/williambrode/fyne/v2/internal/scale"
)
// Painter is a simple software painter that can paint a canvas in memory.
type Painter struct {
}
// NewPainter creates a new Painter.
func NewPainter() *Painter {
return &Painter{}
}
// Paint is the main entry point for a simple software painter.
// The canvas to be drawn is passed in as a parameter and the return is an
// image containing the result of rendering.
func (*Painter) Paint(c fyne.Canvas) image.Image {
bounds := image.Rect(0, 0, scale.ToScreenCoordinate(c, c.Size().Width), scale.ToScreenCoordinate(c, c.Size().Height))
base := image.NewNRGBA(bounds)
paint := func(obj fyne.CanvasObject, pos, clipPos fyne.Position, clipSize fyne.Size) bool {
w := fyne.Min(clipPos.X+clipSize.Width, c.Size().Width)
h := fyne.Min(clipPos.Y+clipSize.Height, c.Size().Height)
clip := image.Rect(
scale.ToScreenCoordinate(c, clipPos.X),
scale.ToScreenCoordinate(c, clipPos.Y),
scale.ToScreenCoordinate(c, w),
scale.ToScreenCoordinate(c, h),
)
switch o := obj.(type) {
case *canvas.Image:
drawImage(c, o, pos, base, clip)
case *canvas.Text:
drawText(c, o, pos, base, clip)
case gradient:
drawGradient(c, o, pos, base, clip)
case *canvas.Circle:
drawCircle(c, o, pos, base, clip)
case *canvas.Line:
drawLine(c, o, pos, base, clip)
case *canvas.Raster:
drawRaster(c, o, pos, base, clip)
case *canvas.Rectangle:
drawRectangle(c, o, pos, base, clip)
}
return false
}
driver.WalkVisibleObjectTree(c.Content(), paint, nil)
for _, o := range c.Overlays().List() {
driver.WalkVisibleObjectTree(o, paint, nil)
}
return base
}