-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrating.go
62 lines (50 loc) · 1.4 KB
/
rating.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
package rating
import (
"image"
"github.com/hajimehoshi/ebiten/v2"
)
const interval = 5
type Rating struct {
originalImg image.Image
marks []*ebiten.Image
ops []*ebiten.DrawImageOptions
maskImg *ebiten.Image
maxVal int
fillCount int
}
func NewRating(img image.Image, x, y int, max int) *Rating {
width := img.Bounds().Dx()
height := img.Bounds().Dy()
maskImg := ebiten.NewImage(width, height)
marks := []*ebiten.Image{}
for i := 0; i < max; i++ {
marks = append(marks, ebiten.NewImageFromImage(img))
}
ops := []*ebiten.DrawImageOptions{}
for i := 0; i < max; i++ {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(x+i*(width+interval)), float64(y))
ops = append(ops, op)
}
return &Rating{originalImg: img, marks: marks, ops: ops, maskImg: maskImg, fillCount: max, maxVal: max}
}
func (r *Rating) SetValue(val float64) {
r.marks[r.fillCount-1] = ebiten.NewImageFromImage(r.originalImg)
r.fillCount = int(val)
if r.fillCount >= int(r.maxVal) {
return
}
op := &ebiten.DrawImageOptions{}
shiftRate := val - float64(int(val))
width := float64(r.originalImg.Bounds().Dx())
op.GeoM.Translate(width*shiftRate, 0.0)
op.Blend = ebiten.BlendClear
r.marks[r.fillCount-1].DrawImage(r.maskImg, op)
}
func (r *Rating) Draw(screen *ebiten.Image) {
for i := 0; i < len(r.marks); i++ {
if i <= r.fillCount-1 {
screen.DrawImage(r.marks[i], r.ops[i])
}
}
}