-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.go
140 lines (121 loc) · 2.86 KB
/
draw.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package egriden
import (
"github.com/greenthepear/imggg"
"github.com/hajimehoshi/ebiten/v2"
)
type Layer interface {
DrawSprite(o Gobject, on *ebiten.Image)
Static() bool
anchor() imggg.Point[float64]
}
// Returns ebiten.DrawImageOptions of a Gobject's SpritePack applied
// to the layer
func appliedDrawOptionsForPosition(o Gobject, layer Layer, x, y float64) *ebiten.DrawImageOptions {
copy := *o.SpritePack().DrawOptions
r := ©
drawX, drawY := o.ScreenPos(layer).XY()
// In static layers, the layer anchor offsets are handled in the draw
// function itself, so they need to be subtracted here
if layer.Static() {
xRealign, yRealign := layer.anchor().XY()
r.GeoM.Translate(
drawX-xRealign, drawY-yRealign)
} else {
r.GeoM.Translate(
drawX, drawY)
}
return r
}
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)
}
}
}
// Refresh image of a static grid layer
func (l *GridLayer) RefreshImage() {
if l.mode != Static {
return
}
img := ebiten.NewImage(
l.layerDimensions.Width*l.cellDimensions.Width,
l.layerDimensions.Height*l.cellDimensions.Height)
l.drawFromSliceMat(img)
l.staticImage = img
}
func (l GridLayer) DrawSprite(o Gobject, on *ebiten.Image) {
x, y := o.GridPos().XY()
on.DrawImage(o.Sprite(),
appliedDrawOptionsForPosition(o, &l, float64(x), float64(y)))
}
func (fl FreeLayer) DrawSprite(o Gobject, on *ebiten.Image) {
x, y := o.GridPos().XY()
on.DrawImage(o.Sprite(),
appliedDrawOptionsForPosition(o, &fl, float64(x), float64(y)))
}
// 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)
case Static:
if l.staticImage == nil {
l.RefreshImage()
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(l.Anchor.X), float64(l.Anchor.Y))
on.DrawImage(l.staticImage, op)
}
}
func (fl FreeLayer) internalDraw(on *ebiten.Image) {
for _, k := range fl.gobjects.keys {
if k.OnDraw() != nil {
k.OnDraw()(k, on, &fl)
continue
}
k.DrawSprite(on, &fl)
}
}
// Draw the layer
func (fl FreeLayer) Draw(on *ebiten.Image) {
if !fl.Visible {
return
}
if fl.static {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(fl.Anchor.X), float64(fl.Anchor.Y))
on.DrawImage(fl.staticImage, op)
return
}
fl.internalDraw(on)
}
// Refresh/create image of a static free layer
func (fl *FreeLayer) RefreshImage() {
if !fl.static {
return
}
fl.internalDraw(fl.staticImage)
}