-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomComponents.go
More file actions
52 lines (46 loc) · 1.04 KB
/
customComponents.go
File metadata and controls
52 lines (46 loc) · 1.04 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
package main
import (
"math"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/widget"
)
type cube struct {
widget.Icon
x, y float32
size float32
}
func newCube(textureUrl string) *cube {
texture, err := fyne.LoadResourceFromURLString(textureUrl)
if err != nil {
panic(err)
}
cubeImage := &cube{
x: 400,
y: 300,
size: 150,
}
cubeImage.Resource = texture
cubeImage.Move(fyne.NewPos(cubeImage.x, cubeImage.y))
cubeImage.Resize(fyne.NewSize(cubeImage.size, cubeImage.size))
return cubeImage
}
func (d *cube) Dragged(e *fyne.DragEvent) {
dx := d.x - (d.Position().X + e.Dragged.DX)
dy := d.y - (d.Position().Y + e.Dragged.DY)
distance := math.Sqrt(float64(dx*dx + dy*dy))
scale := float32(1 / (1 + distance/50))
d.Move(fyne.NewPos(d.Position().X+e.Dragged.DX*scale, d.Position().Y+e.Dragged.DY*scale))
d.Refresh()
}
func (d *cube) DragEnd() {
canvas.NewPositionAnimation(
d.Position(),
fyne.NewPos(d.x, d.y),
time.Second/2,
func(pos fyne.Position) {
d.Move(pos)
d.Refresh()
}).Start()
}