-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
243 lines (192 loc) · 4.26 KB
/
select.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package glui
import (
"github.com/veandco/go-sdl2/sdl"
)
//go:generate ./gen_element Select "appendChild CalcDepth On Size Padding"
//go:generate ./gen_element SelectWrapper "CalcDepth"
type SelectWrapper struct { // given to focusrect instead of actual wrapper
ElementData
sel *Select
}
type Select struct {
ElementData
options []string
text *Text
arrow *Icon
wrapper *SelectWrapper
value string
onChange func(i int, value string)
}
func NewSelect(options []string) *Select {
e := &Select{
NewElementData(9*2, 0),
options,
NewSans("Choose animal", 10),
NewIcon("arrow-down-drop", 10),
nil,
"",
nil,
}
e.wrapper = &SelectWrapper{NewElementData(0, 0), e}
e.Size(200, 50)
e.Padding(e.Root.P1.Skin.ButtonBorderThickness())
e.appendChild(NewHor(STRETCH, CENTER, 0).H(-1).Padding(0, 10).A(e.text, e.arrow))
e.Show()
e.On("mousedown", e.onMouseDown)
e.On("mousebuttonoutsidemenu", e.onMouseButtonOutsideMenu)
e.On("focus", e.onFocus)
e.On("blur", e.onBlur)
e.On("keydown", e.onKeyDown)
e.On("keypress", e.onKeyPress)
return e
}
func (e *Select) OnChange(fn func(i int, value string)) *Select {
e.onChange = fn
return e
}
func (e *Select) Cursor(x, y int) int {
if e.enabled {
return sdl.SYSTEM_CURSOR_HAND
} else {
return -1
}
}
func (e *Select) onMouseButtonOutsideMenu(evt *Event) {
if !e.IsHit(evt.X, evt.Y) {
e.Root.Menu.Hide()
}
}
func (e *Select) menuVisible() bool {
return e.Root.Menu.IsOwnedBy(e)
}
func (e *Select) onMouseDown(evt *Event) {
if e.menuVisible() {
e.Root.Menu.Hide()
} else {
e.onShowMenu()
}
}
func (e *Select) focused() bool {
return e.Root.FocusRect.IsOwnedBy(e.wrapper)
}
func (e *Select) onFocus(evt *Event) {
if evt.IsKeyboardEvent() {
e.Root.FocusRect.Show(e.wrapper)
}
}
func (e *Select) onBlur(evt *Event) {
if e.focused() {
e.Root.FocusRect.Hide()
}
}
func (e *Select) onKeyDown(evt *Event) {
if evt.IsReturnOrSpace() {
if e.menuVisible() {
e.Root.Menu.ClickSelected()
} else {
e.onShowMenu()
}
} else if e.menuVisible() {
if evt.Key == "down" {
e.Root.Menu.SelectNext()
} else if evt.Key == "up" {
e.Root.Menu.SelectPrev()
} else {
e.Root.Menu.Hide()
}
} else if evt.Key == "down" {
i := e.Index()
if i == len(e.options) - 1 {
e.SetValue(e.options[0])
} else {
e.SetValue(e.options[i+1])
}
} else if evt.Key == "up" {
i := e.Index()
if i <= 0 {
e.SetValue(e.options[len(e.options)-1])
} else {
e.SetValue(e.options[i-1])
}
}
}
func (e *Select) onKeyPress(evt *Event) {
// TODO
}
func (e *Select) CalcPos(maxWidth, maxHeight, maxZIndex int) (int, int) {
return e.SetButtonPos(maxWidth, maxHeight, maxZIndex)
}
func (e *SelectWrapper) CalcPos(maxWidth, maxHeight, maxZIndex int) (int, int) {
panic("shouldn't be called")
}
func (e *SelectWrapper) ZIndex() int {
return e.sel.ZIndex()
}
func (e *SelectWrapper) Visible() bool {
return e.sel.Visible()
}
func (e *SelectWrapper) Deleted() bool {
return e.sel.Deleted()
}
func (e *SelectWrapper) Rect() Rect {
ddr := e.sel.Rect()
if e.sel.menuVisible() {
return Rect{
ddr.X,
ddr.Y,
ddr.W,
ddr.H*(1 + len(e.sel.options)) + 2*e.Root.P1.Skin.ButtonBorderThickness(),
}
} else {
return ddr
}
}
func (e *Select) Hide() {
if e.menuVisible() {
e.Root.Menu.Hide()
}
e.ElementData.Hide()
}
func (e *Select) Show() {
e.SetButtonStyle()
e.ElementData.Show()
}
func (e *Select) SetValue(v string) {
if v == "" {
e.text.SetContent("Choose animal")
} else {
e.text.SetContent(v)
}
e.value = v
if e.onChange != nil {
e.onChange(e.Index(), e.Value())
}
}
func (e *Select) Value() string {
return e.value
}
func (e *Select) Index() int {
for i, opt := range e.options {
if opt == e.value {
return i
}
}
return -1
}
func (e *Select) onShowMenu() {
menu := e.Root.Menu
menu.ClearChildren()
for _, option := range e.options {
option_ := option
item := NewMenuItem(option_, func() {
e.SetValue(option_)
}).H(e.height)
menu.AddItem(item, true, option_ == e.value)
}
e.Root.Menu.ShowAt(
e,
0.0,
1.0,
e.rect.W,
)
}