-
Notifications
You must be signed in to change notification settings - Fork 0
/
modemenu.go
109 lines (92 loc) · 2.25 KB
/
modemenu.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
package main
import (
"image/color"
"log"
"github.com/hajimehoshi/ebiten"
"github.com/tkido/mygen/font"
"github.com/tkido/mygen/mode"
"github.com/tkido/mygen/ui"
)
type ModeMenu struct {
*MenuBase
CursorBox *ui.Box
Data []string
}
func NewModeMenu(w, h, col, row int) *ModeMenu {
m := &ModeMenu{
NewMenuBase(w, h, col, row, color.White),
ui.NewBox(w, h, ui.Color("ff0a")),
[]string{},
}
m.Self = m
m.SetKeyCallback(ebiten.KeyLeft, func(el ui.Element) {
m.MoveCursor(-1, 0)
})
m.SetKeyCallback(ebiten.KeyRight, func(el ui.Element) {
m.MoveCursor(1, 0)
})
m.SetKeyCallback(ebiten.KeyUp, func(el ui.Element) {
m.MoveCursor(0, -1)
})
m.SetKeyCallback(ebiten.KeyDown, func(el ui.Element) {
m.MoveCursor(0, 1)
})
m.SetUiCallback(ui.GotFocus, func(el ui.Element) {
log.Printf("ModeMenu GotFocus")
m.CursorBox.SetBackgroundColor(ui.Color("f00a"))
})
m.SetUiCallback(ui.LostFocus, func(el ui.Element) {
log.Printf("ModeMenu LostFocus")
m.CursorBox.SetBackgroundColor(ui.Color("ff0a"))
})
return m
}
func (m *ModeMenu) MoveCursor(dX, dY int) (exit bool) {
log.Printf("ModeMenu MoveCursor")
exit = m.MenuBase.MoveCursor(dX, dY)
m.SetCursor(m.Cursor)
return exit
}
func (m *ModeMenu) SetCursor(index int) {
log.Printf("ModeMenu SetCursor index = %d", index)
m.MenuBase.SetCursor(index)
x, y := m.Cursor%m.Col, m.Cursor/m.Col
m.CursorBox.Move(x*m.W, y*m.H)
g.Sprites.Mode = mode.Type(index)
g.Sprites.Dirty()
}
func (m *ModeMenu) Update() {
m.Data = []string{}
for _, mt := range mode.Types {
m.Data = append(m.Data, mt.String())
}
m.Limit = len(m.Data) - 1
m.Clear()
for i, text := range m.Data {
bgColor := ui.Color("fff")
if i%2 == 1 {
bgColor = ui.Color("e3ebf1")
}
label := ui.NewLabel(m.W, m.H, text, font.Regular, font.Small, ui.Center, color.Black, bgColor)
caption := text
index := i
clicked := func(el ui.Element) {
log.Printf("%s clicked\n", caption)
m.SetFocus()
m.SetCursor(index)
}
label.SetMouseCallback(
ui.LeftClick,
clicked,
)
x, y := i%m.Col, i/m.Col
m.Add(x*m.W, y*m.H, label)
}
x, y := m.Cursor%m.Col, m.Cursor/m.Col
m.Add(x*m.W, y*m.H, m.CursorBox)
m.Dirty()
}
func (m *ModeMenu) Reflesh() {
log.Println("ModeMenu.Reflesh")
m.Image.Fill(ui.Color("aa0"))
}