-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
93 lines (81 loc) · 1.72 KB
/
list.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
package ui
import "github.com/hajimehoshi/ebiten/v2"
type UIDirection int
const (
Horizontal UIDirection = iota
Vertical
)
// Draw a list of UI
type List struct {
uis []Drawable
PosX int
PosY int
direction UIDirection
spacing int // pixel between each UI
}
func NewList(x, y int, dir UIDirection) *List {
return &List{uis: []Drawable{}, direction: dir,
PosX: x, PosY: y, spacing: 10}
}
func (s *List) AddUI(ui Drawable) {
count := len(s.uis)
s.uis = append(s.uis, ui)
if s.direction == Vertical {
if count == 0 {
ui.SetPos(s.PosX, s.PosY)
} else {
_, previousY := s.uis[count-1].GetPos()
_, previousH := s.uis[count-1].GetSize()
ui.SetPos(s.PosX, previousY+previousH+s.spacing)
}
} else if s.direction == Horizontal {
if count == 0 {
ui.SetPos(s.PosX, s.PosY)
} else {
previousX, _ := s.uis[count-1].GetPos()
previousW, _ := s.uis[count-1].GetSize()
ui.SetPos(previousX+previousW+s.spacing, s.PosY)
}
}
}
func (s *List) Draw(screen *ebiten.Image) {
for _, ui := range s.uis {
ui.Draw(screen)
}
}
// return index of hover UI, return -1 when not hovering
func (s *List) IsHover() int {
for i, ui := range s.uis {
if ui.IsHover() {
return i
}
}
return -1
}
// return index of mouse down UI, -1 if none
func (s *List) IsDown() int {
for i, ui := range s.uis {
if ui.IsDown() {
return i
}
}
return -1
}
// return index of mouse just pressed UI, -1 if none
func (s *List) IsJustPressed() int {
for i, ui := range s.uis {
if ui.IsJustPressed() {
return i
}
}
return -1
}
// return index of mouse just released UI, -1 if none
func (s *List) IsJustReleased() int {
for i, ui := range s.uis {
if ui.IsJustReleased() {
return i
}
}
return -1
}