-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.go
More file actions
103 lines (85 loc) · 1.9 KB
/
draw.go
File metadata and controls
103 lines (85 loc) · 1.9 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package egriden
import (
"github.com/hajimehoshi/ebiten/v2"
)
// Returns ebiten.DrawImageOptions of a Gobject's SpritePack applied
// to the layer
func appliedDrawOptionsForPosition(
o Gobject, layer Layer) *ebiten.DrawImageOptions {
copy := *o.SpritePack().DrawOptions
copy.GeoM.Translate(o.ScreenPos(layer).XY())
return ©
}
func (l GridLayer) drawFromSliceMat(on *ebiten.Image) {
for y := range l.layerDimensions.Height {
for x := range l.layerDimensions.Width {
o := l.sliceMat[y][x]
if o == nil || !o.IsVisible() {
continue
}
if o.OnDraw() != nil {
o.OnDraw()(o, on, &l)
continue
}
o.DrawSprite(on, &l)
}
}
}
func (l GridLayer) DrawLikeSprite(
img *ebiten.Image, o Gobject, on *ebiten.Image) {
on.DrawImage(img,
appliedDrawOptionsForPosition(o, &l))
}
func (l GridLayer) DrawSprite(o Gobject, on *ebiten.Image) {
l.DrawLikeSprite(o.Sprite(), o, on)
}
func (fl FreeLayer) DrawLikeSprite(img *ebiten.Image, o Gobject, on *ebiten.Image) {
on.DrawImage(img,
appliedDrawOptionsForPosition(o, &fl))
}
func (fl FreeLayer) DrawSprite(o Gobject, on *ebiten.Image) {
fl.DrawLikeSprite(o.Sprite(), o, on)
}
// Draw the layer
func (l *GridLayer) Draw(on *ebiten.Image) {
if !l.Visible {
return
}
switch l.mode {
case Sparse:
for _, o := range l.mapMat {
if !o.IsVisible() {
continue
}
if o.OnDraw() != nil {
o.OnDraw()(o, on, l)
continue
}
o.DrawSprite(on, l)
}
case Dense:
l.drawFromSliceMat(on)
default:
panic("unknown DrawMode")
}
}
func (fl *FreeLayer) internalDraw(on *ebiten.Image) {
for e := fl.gobjects.Front(); e != nil; e = e.Next() {
o, ok := e.Value.(Gobject)
if !ok {
panic("list element isn't a Gobject")
}
if o.OnDraw() != nil {
o.OnDraw()(o, on, fl)
continue
}
o.DrawSprite(on, fl)
}
}
// Draw the layer
func (fl *FreeLayer) Draw(on *ebiten.Image) {
if !fl.Visible {
return
}
fl.internalDraw(on)
}