/
main.go
71 lines (56 loc) · 1.84 KB
/
main.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
// SPDX-License-Identifier: Unlicense OR MIT
package main
import (
"image"
"gioui.org/f32" // f32 contains float32 points.
"gioui.org/io/event" // event contains general event information.
"gioui.org/io/pointer" // pointer contains input/output for mouse and touch screens.
"gioui.org/op" // op is used for recording different operations.
"gioui.org/op/clip"
"gioui.org/op/paint" // paint contains operations for coloring.
"github.com/golangestonia/learn-gio/qapp" // qapp contains convenience funcs for this tutorial
"github.com/golangestonia/learn-gio/qasset" // qasset contains convenience assets for this tutorial
)
var imageOp = paint.NewImageOp(qasset.Neutral)
var location = f32.Pt(300, 300)
var targetLocation = location
func main() {
qapp.InputSize(func(ops *op.Ops, queue event.Queue, windowSize image.Point) {
// register area for input events
defer clip.Rect(image.Rectangle{Max: windowSize}).Push(ops).Pop()
// register the area for pointer events
pointer.InputOp{
Tag: &location,
Types: pointer.Press,
}.Add(ops)
// read events from input event queue
for _, ev := range queue.Events(&location) {
// figure out, which event it was
switch ev := ev.(type) {
case pointer.Event:
if ev.Type == pointer.Press {
targetLocation = ev.Position
}
}
}
// move slightly towards the target location
if delta := targetLocation.Sub(location); abs(delta.X) > 1 || abs(delta.Y) > 1 {
delta.X *= 0.1
delta.Y *= 0.1
location = location.Add(delta)
// ensure we animate this
op.InvalidateOp{}.Add(ops)
}
defer op.Affine(f32.Affine2D{}.Offset(location)).Push(ops).Pop()
imageSize := imageOp.Size().Div(-2)
defer op.Offset(imageSize).Push(ops).Pop()
imageOp.Add(ops)
paint.PaintOp{}.Add(ops)
})
}
func abs(s float32) float32 {
if s < 0 {
return -s
}
return s
}