-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
71 lines (57 loc) · 1.44 KB
/
utils.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
package main
import (
"bytes"
"image"
"image/color"
_ "image/png"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
)
func loadPicture(imagePath string) (pixel.Picture, error) {
data, err := Asset(imagePath)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
buf.Write(data)
img, _, err := image.Decode(buf)
if err != nil {
return nil, err
}
return pixel.PictureDataFromImage(img), nil
}
func isInsideBoundingBox(x, y float64, box pixel.Rect, boxPosition pixel.Vec) bool {
x1, y1, x2, y2 := box.Min.X, box.Min.Y,
box.Max.X, box.Max.Y
x0, y0 := boxPosition.XY()
return x > x0+x1 && x < x0+x2 && y > y0+y1 && y < y0+y2
}
func (circle *Circle) isPositionInside(position pixel.Vec) bool {
return circle.position.To(position).Len() <= circle.radius
}
func (circle *Circle) draw(imd *imdraw.IMDraw, position pixel.Vec) {
imd.Color = color.RGBA{0, 0, 0, 30}
imd.Push(position)
imd.Circle(circle.radius, 0)
imd.Color = color.RGBA{0, 0, 0, 50}
imd.Push(position)
imd.Circle(circle.radius, 1)
}
func loadTTF(path string, size float64) (font.Face, error) {
file, err := Asset(path)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
buf.Write(file)
font, err := truetype.Parse(buf.Bytes())
if err != nil {
return nil, err
}
return truetype.NewFace(font, &truetype.Options{
Size: size,
GlyphCacheEntries: 1,
}), nil
}