forked from quasilyte/ge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyline.go
78 lines (63 loc) · 1.25 KB
/
polyline.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
package ge
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/quasilyte/gmath"
)
type PolyLine struct {
Points []LinePoint
Width float64
Visible bool
disposed bool
}
type LinePoint struct {
Pos gmath.Vec
ColorScale ColorScale
}
func NewPolyLine() *PolyLine {
return &PolyLine{
Visible: true,
Width: 1,
}
}
func (l *PolyLine) ResetPoints() {
l.Points = l.Points[:0]
}
func (l *PolyLine) PushColorPoint(pt gmath.Vec, c ColorScale) {
l.Points = append(l.Points, LinePoint{
ColorScale: c,
Pos: pt,
})
}
func (l *PolyLine) PushPoint(pt gmath.Vec) {
l.Points = append(l.Points, LinePoint{
ColorScale: defaultColorScale,
Pos: pt,
})
}
func (l *PolyLine) PopPoint() gmath.Vec {
pt := l.Points[len(l.Points)-1]
l.Points = l.Points[:len(l.Points)-1]
return pt.Pos
}
func (l *PolyLine) IsDisposed() bool {
return l.disposed
}
func (l *PolyLine) Dispose() {
l.disposed = true
}
func (l *PolyLine) Draw(screen *ebiten.Image) {
if !l.Visible {
return
}
if len(l.Points) < 2 {
return
}
points := l.Points
var colorM ebiten.ColorM
for i := 0; i < len(points)-1; i++ {
pt1 := points[i]
pt2 := points[i+1]
applyColorScale(pt2.ColorScale, &colorM)
drawLine(screen, pt1.Pos, pt2.Pos, l.Width, colorM)
}
}