/
state_browse_list.go
137 lines (115 loc) · 3.28 KB
/
state_browse_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
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
package game_map
import (
"fmt"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"github.com/steelx/go-rpg-cgm/gui"
"github.com/steelx/go-rpg-cgm/utilz"
"github.com/steelx/go-rpg-cgm/world"
"reflect"
)
type BrowseListState struct {
Stack *gui.StateStack
X, Y float64
Width, Height float64
Title string
OnFocus func(item interface{})
OnExit func()
Selection *gui.SelectionMenu
Box gui.Panel
UpArrow, DownArrow *pixel.Sprite
UpArrowPosition, DownArrowPosition pixel.Vec
hide bool
}
func BrowseListStateCreate(
stack *gui.StateStack, x, y, width, height float64, title string, onFocus func(item interface{}), onExit func(), data interface{}, args ...interface{}) *BrowseListState {
//args sequence -> onSelection func(int, interface{}), renderFunc(a ...interface{})
s := &BrowseListState{
Stack: stack,
X: x,
Y: y,
Width: width,
Height: height,
Title: title,
OnExit: onExit,
OnFocus: onFocus,
UpArrow: world.IconsDB.Get(12),
DownArrow: world.IconsDB.Get(13),
hide: false,
}
var selectCallback func(*BrowseListState, int, interface{}) = nil
if len(args) >= 1 {
selectCallback = reflect.ValueOf(args[0]).Interface().(func(*BrowseListState, int, interface{}))
}
var renderFunc func(a ...interface{}) = nil
if len(args) >= 2 {
renderFunc = reflect.ValueOf(args[1]).Interface().(func(a ...interface{}))
}
menu := gui.SelectionMenuCreate(19, 132, width,
data,
false,
pixel.V(x, y),
func(index int, itemIdx interface{}) {
selectCallback(s, index, itemIdx)
},
renderFunc,
)
s.Selection = &menu
s.Selection.SetPosition(x-80, y+20)
s.Box = gui.PanelCreate(pixel.V(x, y), s.Selection.GetWidth(), height)
s.SetArrowPosition()
return s
}
func (s *BrowseListState) Enter() {
s.OnFocus(s.Selection.SelectedItem())
}
func (s *BrowseListState) Exit() {
s.OnExit()
}
func (s *BrowseListState) Update(dt float64) bool {
return false
}
func (s *BrowseListState) Render(renderer *pixelgl.Window) {
if s.hide {
return
}
s.Box.Draw(renderer)
if s.Selection.CanScrollUp() {
s.UpArrow.Draw(renderer, pixel.IM.Moved(s.UpArrowPosition))
}
if s.Selection.CanScrollDown() {
s.DownArrow.Draw(renderer, pixel.IM.Moved(s.DownArrowPosition))
}
pos := pixel.V(s.X, s.Y)
shadow := utilz.HexToColor("#000000")
textBase := text.New(pos, gui.BasicAtlasAscii)
textBase.Color = shadow
fmt.Fprintln(textBase, s.Title)
textBase.Draw(renderer, pixel.IM.Moved(pos))
pos = pixel.V(s.X+0.5, s.Y-0.5)
textBase = text.New(pos, gui.BasicAtlas12)
fmt.Fprintln(textBase, s.Title)
textBase.Draw(renderer, pixel.IM.Moved(pos))
s.Selection.Render(renderer)
}
func (s *BrowseListState) HandleInput(win *pixelgl.Window) {
if win.JustPressed(pixelgl.KeyEscape) {
s.Stack.Pop()
return
}
s.Selection.HandleInput(win)
s.OnFocus(s.Selection.SelectedItem())
}
func (s *BrowseListState) Hide() {
s.hide = true
}
func (s *BrowseListState) Show() {
s.hide = false
s.OnFocus(s.Selection.SelectedItem())
}
func (s *BrowseListState) SetArrowPosition() {
_, topRight, _, bottomRight := s.Box.GetCorners()
s.UpArrowPosition = topRight.Sub(pixel.V(7, 7))
s.DownArrowPosition = bottomRight.Sub(pixel.V(7, -7))
}