-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistview.go
219 lines (169 loc) · 3.95 KB
/
listview.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
package rlyeh
import (
"time"
rl "github.com/gen2brain/raylib-go/raylib"
)
type ListView struct {
OnClick func(string)
OnDoubleClick func(string)
layout *HBox
items []string
visibles int
upper int
current int
clicked int
state State
time time.Duration
}
func NewListView(items []string, visibles int) *ListView {
self := &ListView{}
self.layout = NewHBox(Auto, Both)
self.visibles = visibles
vbox := NewVBox(Auto, Both)
for i := 0; i < visibles; i++ {
vbox.Add(NewLabel(Left, None, ""))
}
self.layout.Add(vbox)
self.layout.Add(NewScrollbar(Vertical, self))
self.SetItems(items)
return self
}
func (self *ListView) Scroll(times int) {
if times < 0 {
times = -times
if times > self.current {
times = self.current
}
if self.upper >= times {
self.upper -= times
self.current -= times
} else {
self.current -= times
}
} else if times > 0 {
left := len(self.items) - self.current - 1
if times > left {
times = left
}
if self.upper < (len(self.items) - self.visibles) {
self.current += times
self.upper += times
} else {
self.current += times
}
}
}
func (self *ListView) GetCount() int {
return len(self.items)
}
func (self *ListView) GetCurrent() int {
return self.current
}
func (self *ListView) SetCurrent(index int) {
if index < 0 {
index = 0
}
if index >= self.GetCount() {
index = self.GetCount() - 1
}
if index <= (self.GetCount() - self.visibles) {
self.upper = index
} else {
self.upper = self.GetCount() - self.visibles
}
self.current = index
}
func (self *ListView) RemoveScrollbar() {
self.layout.Widgets = self.layout.Widgets[:len(self.layout.Widgets)-1]
}
func (self *ListView) SetItems(items []string) {
self.upper = 0
self.current = 0
self.state = Normal
self.clicked = -1
self.items = items
labels := self.layout.GetWidgets()[0].(Layout).GetWidgets()
for i := len(items); i < self.visibles; i++ {
label := labels[i].(*Label)
label.Text = ""
}
}
func (self *ListView) GetCurrentItem() string {
return self.items[self.current]
}
func (self *ListView) GetId() int32 {
return self.layout.GetId()
}
func (self *ListView) SetId(id int32) {
self.layout.SetId(id)
}
func (self *ListView) GetParent() Widget {
return self.layout.GetParent()
}
func (self *ListView) SetParent(parent Widget) {
self.layout.SetParent(parent)
}
func (self *ListView) GetBounds() rl.Rectangle {
return self.layout.GetBounds()
}
func (self *ListView) SetBounds(bounds rl.Rectangle) {
self.layout.SetBounds(bounds)
}
func (self *ListView) GetAlign() Align {
return self.layout.GetAlign()
}
func (self *ListView) GetFill() Fill {
return self.layout.GetFill()
}
func (self *ListView) GetDataSize() Size {
return self.layout.GetDataSize()
}
func (self *ListView) Update(dt float32) {
vbox := self.layout.GetWidgets()[0].(*VBox)
labels := vbox.GetWidgets()
self.time += time.Duration(1e9 * dt)
for i := 0; i < self.visibles; i++ {
label := labels[i].(*Label)
j := self.upper + i
if j < len(self.items) {
text := self.items[j]
label.Text = text
switch GetState(label.GetBounds()) {
case Released:
if Pressed == self.state && j == self.current {
self.time = 0
self.clicked = j
if nil != self.OnClick {
self.OnClick(text)
}
}
if Released != self.state {
self.current = j
}
self.state = Released
break
case Pressed:
if j == self.clicked && j == self.current {
if (200*time.Millisecond) > self.time && nil != self.OnDoubleClick {
self.OnDoubleClick(text)
self.clicked = -1
}
}
self.current = j
self.state = Pressed
break
}
if self.current == j {
label.BackgroundColor = style.ListviewSelectedBackgroundColor
label.TextColor = style.ListviewSelectedTextColor
} else {
label.BackgroundColor = style.GlobalBackgroundColor
label.TextColor = style.ListviewTextColor
}
}
}
self.layout.Update(dt)
}
func (self *ListView) Draw() {
self.layout.Draw()
}