-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscroll_bar.go
208 lines (170 loc) · 4.85 KB
/
scroll_bar.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
package gwk
import (
"fmt"
"github.com/Luncher/gwk/pkg/image"
"github.com/Luncher/gwk/pkg/structs"
"honnef.co/go/js/dom"
"math"
)
type ScrollBar struct {
*Widget
dragging bool
scrollRange float64
currentPosition float64
draggerRect *structs.Rect
pointerDownPoint *structs.Point
lastPointerPoint *structs.Point
currentPositionSaved float64
scrolledHandler onScrolledHandler
}
type onScrolledHandler func(currentPosition, scrollRange float64)
func NewScrollBar(t string, parent *Widget, x, y, w, h float32) *ScrollBar {
bar := &ScrollBar{
Widget: NewWidget(t, parent, x, y, w, h),
scrollRange: 100,
}
bar.I = bar
return bar
}
func (bar *ScrollBar) onScrolled(currentPosition, scrollRange float64) {
fmt.Printf("Scroll: %f\b", currentPosition)
if bar.scrolledHandler != nil {
bar.scrolledHandler(currentPosition, scrollRange)
}
return
}
func (bar *ScrollBar) onPointerDown(point *structs.Point) {
bar.getWindow().grab(bar.Widget)
bar.pointerDownPoint.X = point.X
bar.pointerDownPoint.Y = point.Y
p := bar.translatePoint(point)
if bar.draggerRect != nil && isPointInRect(p, bar.draggerRect) {
bar.dragging = true
bar.setState(STATE_ACTIVE, false)
bar.currentPositionSaved = bar.currentPosition
} else {
bar.setState(STATE_NORMAL, false)
}
return
}
func (bar *ScrollBar) onPointerMove(point *structs.Point) {
if bar.dragging {
ww := float64(bar.rect.W)
hh := float64(bar.rect.H)
if ww > hh {
dx := float64(point.X - bar.pointerDownPoint.X)
bar.setCurrentPosition(bar.currentPositionSaved + (dx/ww)*bar.scrollRange)
} else {
dy := float64(point.Y - bar.pointerDownPoint.Y)
bar.setCurrentPosition(bar.currentPositionSaved + (dy/hh)*bar.scrollRange)
}
}
return
}
func (bar *ScrollBar) onPointerUp(point *structs.Point) {
ww := bar.rect.W
hh := bar.rect.H
if bar.draggerRect == nil {
return
}
if !bar.dragging {
r := bar.draggerRect
p := bar.translatePoint(point)
if ww > hh {
if p.X < r.X {
bar.addToCurrentPosition(float64(-ww))
} else if p.X > (r.X + r.W) {
bar.addToCurrentPosition(float64(ww))
}
} else {
if p.Y < r.Y {
bar.addToCurrentPosition(float64(-hh))
} else if p.Y > (r.Y + r.H) {
bar.addToCurrentPosition(float64(hh))
}
}
}
bar.dragging = false
bar.getWindow().ungrab()
bar.setState(STATE_NORMAL, false)
return
}
func (bar *ScrollBar) setScrollRange(val float64) {
bar.scrollRange = val
bar.updateDraggerSize()
return
}
func (bar *ScrollBar) getScrollRange() float64 {
return bar.scrollRange
}
func (bar *ScrollBar) addToCurrentPosition(delta float64) {
currentPosition := bar.currentPosition + delta
bar.setCurrentPosition(currentPosition)
return
}
func (bar *ScrollBar) getCurrentPosition() float64 {
return bar.currentPosition
}
func (bar *ScrollBar) setCurrentPosition(currentPosition float64) {
size := math.Max(float64(bar.getWidth()), float64(bar.getHeight()))
bar.currentPosition = math.Max(math.Min(bar.scrollRange-size, currentPosition), 0)
bar.PostRedraw()
bar.updateDraggerSize()
bar.onScrolled(bar.currentPosition, bar.scrollRange)
return
}
func (bar *ScrollBar) onSized(w, h int) {
bar.updateDraggerSize()
return
}
func (bar *ScrollBar) updateDraggerSize() {
var xx, yy, ww, hh, percent float64
rect := bar.rect
if rect.W > rect.H {
bar.scrollRange = math.Max(float64(rect.W), bar.scrollRange)
percent = bar.currentPosition / bar.scrollRange
yy = 2
hh = float64(rect.H) - yy - yy
ww = math.Max(20, math.Floor(float64(rect.W)*float64(rect.W)/bar.scrollRange))
xx = math.Floor(math.Min(math.Max(0, percent*float64(rect.W)), float64(rect.W)-ww))
} else {
bar.scrollRange = math.Max(float64(rect.H), bar.scrollRange)
percent = bar.currentPosition / bar.scrollRange
xx = 2
ww = float64(rect.W) - xx - xx
hh = math.Max(20, math.Floor(float64(rect.H)*float64(rect.H)/bar.scrollRange))
yy = math.Floor(math.Min(math.Max(0, percent*float64(rect.H)), float64(rect.H)-hh))
}
bar.draggerRect = structs.NewRect(int(xx), int(yy), int(ww), int(hh))
bar.currentPosition = bar.scrollRange * percent
return
}
func (bar *ScrollBar) paintSelf(context *dom.CanvasRenderingContext2D) {
if bar.draggerRect != nil {
r := bar.draggerRect
style := bar.getStyle("")
if style.FgImage != nil {
style.FgImage.Draw(context, image.DISPLAY_9PATCH, r.X, r.Y, r.W, r.H)
} else {
context.FillStyle = style.DragColor
context.FillRect(float64(r.X), float64(r.Y), float64(r.W), float64(r.H))
}
}
return
}
type VScrollBar struct {
*ScrollBar
}
func NewVScrollBar(parent *Widget, x, y, w, h float32) *VScrollBar {
return &VScrollBar{
ScrollBar: NewScrollBar(TYPE_VSCROLL_BAR, parent, x, y, w, h),
}
}
type HScrollBar struct {
*ScrollBar
}
func NewHScrollBar(parent *Widget, x, y, w, h float32) *HScrollBar {
return &HScrollBar{
ScrollBar: NewScrollBar(TYPE_HSCROLL_BAR, parent, x, y, w, h),
}
}