/
screenpositioncomponent.go
64 lines (57 loc) · 1.57 KB
/
screenpositioncomponent.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
package flat
import "github.com/hajimehoshi/ebiten/v2"
type PositionAnchor int
const (
UpperLeft PositionAnchor = iota
UpperCenter
UpperRight
CenterLeft
CenterCenter
CenterRight
LowerLeft
LowerCenter
LowerRight
)
type ScreenPositionComponent struct {
ComponentBase
Anchor PositionAnchor
XPercentAwayFromAnchor float32
YPercentAwayFromAnchor float32
XPixelsAwayFromAnchor int32
YPixelsAwayFromAnchor int32
}
func (s *ScreenPositionComponent) Draw(screen *ebiten.Image) {
bounds := screen.Bounds()
var startX, startY float32
dirX, dirY := float32(1), float32(1)
switch s.Anchor {
case UpperLeft:
startX, startY = 0, 0
case UpperCenter:
startX, startY = float32(bounds.Dx())/2, 0
case UpperRight:
startX, startY = float32(bounds.Dx()), 0
dirX = -1
case CenterLeft:
startX, startY = 0, float32(bounds.Dy())/2
case CenterCenter:
startX, startY = float32(bounds.Dx())/2, float32(bounds.Dy())/2
case CenterRight:
startX, startY = float32(bounds.Dx()), float32(bounds.Dy())/2
dirX = -1
case LowerLeft:
startX, startY = 0, float32(bounds.Dy())
dirY = -1
case LowerCenter:
startX, startY = float32(bounds.Dx())/2, float32(bounds.Dy())
dirY = -1
case LowerRight:
startX, startY = float32(bounds.Dx()), float32(bounds.Dy())
dirX, dirY = -1, -1
}
x := startX + dirX*(float32(s.XPixelsAwayFromAnchor)+float32(bounds.Dx())*s.XPercentAwayFromAnchor/100)
y := startY + dirY*(float32(s.YPixelsAwayFromAnchor)+float32(bounds.Dy())*s.YPercentAwayFromAnchor/100)
t := s.GetTransform()
t.Location.X = float64(x)
t.Location.Y = float64(y)
}