-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwidget.go
102 lines (84 loc) · 2.14 KB
/
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
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
package main
import (
"fmt"
"image"
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
// Background Holds the background image and implements a thin wrapper to implement image.Image
type Background struct {
stack *SpriteStack
textLayer *TextLayer
}
func (b *Background) ColorModel() color.Model {
return b.stack.sprites[0].ColorModel()
}
func (b *Background) Bounds() image.Rectangle {
return b.stack.sprites[0].Bounds()
}
func (b *Background) At(x, y int) color.Color {
if colorAt := b.textLayer.DrawAtPosition(x, y); colorAt != nil {
return colorAt
}
if colorAt := b.stack.DrawAtPosition(x, y); colorAt != nil {
return colorAt
}
return b.stack.sprites[0].At(x, y)
}
type bgWidget struct {
widget.BaseWidget
ci *canvas.Image
bg *Background
x, y float32
w fyne.Window
rdr fyne.WidgetRenderer
}
func (item *bgWidget) MouseDown(event *desktop.MouseEvent) {
x := int(event.Position.X / scaleFactor)
y := int(event.Position.Y / scaleFactor)
fmt.Printf("MouseDown: %d, %d\n", x, y)
item.bg.stack.MouseDown(x, y)
item.ci.Refresh()
item.rdr.Refresh()
}
func (item *bgWidget) MouseUp(event *desktop.MouseEvent) {
x := int(event.Position.X / scaleFactor)
y := int(event.Position.Y / scaleFactor)
fmt.Printf("MouseUp: %d, %d\n", x, y)
item.bg.stack.DoAtPosition(x, y)
item.ci.Refresh()
item.rdr.Refresh()
}
func (item *bgWidget) Dragged(event *fyne.DragEvent) {
item.bg.stack.Dragged(event)
item.ci.Refresh()
item.rdr.Refresh()
}
func (item *bgWidget) DragEnd() {
item.bg.stack.DragEnd()
}
var _ desktop.Mouseable = (*bgWidget)(nil)
var _ fyne.Draggable = (*bgWidget)(nil)
func newBgWidget(rawImg *Background) *bgWidget {
img := canvas.NewImageFromImage(rawImg)
img.ScaleMode = canvas.ImageScalePixels
item := &bgWidget{
ci: img,
bg: rawImg,
}
item.ExtendBaseWidget(item)
item.Resize(item.ci.Size())
return item
}
func (item *bgWidget) CreateRenderer() fyne.WidgetRenderer {
cnt := container.New(layout.NewStackLayout(),
item.ci,
)
item.rdr = widget.NewSimpleRenderer(cnt)
return item.rdr
}