-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathslider.go
149 lines (125 loc) · 3.31 KB
/
slider.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
package nanogui
import (
"fmt"
"github.com/shibukawa/glfw"
"github.com/shibukawa/nanovgo"
)
type Slider struct {
WidgetImplement
value float32
highlightColor nanovgo.Color
highlightedRange [2]float32
callback func(float32)
finalCallback func(float32)
}
func NewSlider(parent Widget) *Slider {
slider := &Slider{}
InitWidget(slider, parent)
return slider
}
func (s *Slider) Value() float32 {
return s.value
}
func (s *Slider) SetValue(v float32) {
s.value = v
}
func (s *Slider) HighlightColor() nanovgo.Color {
return s.highlightColor
}
func (s *Slider) SetHighlightColor(c nanovgo.Color) {
s.highlightColor = c
}
func (s *Slider) HighlightedRange() (float32, float32) {
return s.highlightedRange[0], s.highlightedRange[1]
}
func (s *Slider) SetHighlightedRange(l, h float32) {
s.highlightedRange[0] = l
s.highlightedRange[1] = h
}
func (s *Slider) SetCallback(callback func(float32)) {
s.callback = callback
}
func (s *Slider) SetFinalCallback(callback func(float32)) {
s.finalCallback = callback
}
func (s *Slider) MouseDragEvent(self Widget, x, y, relX, relY, button int, modifier glfw.ModifierKey) bool {
if !s.enabled {
return false
}
s.value = clampF(float32(x-s.x)/float32(s.w), 0.0, 1.0)
if s.callback != nil {
s.callback(s.value)
}
return true
}
func (s *Slider) MouseButtonEvent(self Widget, x, y int, button glfw.MouseButton, down bool, modifier glfw.ModifierKey) bool {
if !s.enabled {
return false
}
s.value = clampF(float32(x-s.x)/float32(s.w), 0.0, 1.0)
if s.callback != nil {
s.callback(s.value)
}
if s.finalCallback != nil {
s.finalCallback(s.value)
}
return true
}
func (s *Slider) PreferredSize(self Widget, ctx *nanovgo.Context) (int, int) {
return 70, 12
}
func (s *Slider) Draw(self Widget, ctx *nanovgo.Context) {
sx := float32(s.x)
sy := float32(s.y)
sw := float32(s.w)
sh := float32(s.h)
cy := sy + sh*0.5
kx := sx + s.value*sw
ky := cy + 0.5
kr := sh * 0.5
var a1, a2, a3 uint8
if s.enabled {
a1 = 32
a2 = 128
a3 = 255
} else {
a1 = 10
a2 = 210
a3 = 100
}
background := nanovgo.BoxGradient(sx, cy-3+1, sw, 6, 3, 3, nanovgo.MONO(0, a1), nanovgo.MONO(0, a2))
ctx.BeginPath()
ctx.RoundedRect(sx, cy-3+1, sw, 6, 2)
ctx.SetFillPaint(background)
ctx.Fill()
if s.highlightedRange[0] != s.highlightedRange[1] {
ctx.BeginPath()
ctx.RoundedRect(sx+s.highlightedRange[0]*sw, cy-3+1, sw*(s.highlightedRange[1]-s.highlightedRange[0]), 6, 2)
ctx.SetFillColor(s.highlightColor)
ctx.Fill()
}
knobShadow := nanovgo.RadialGradient(kx, ky, kr-3, kr+3, nanovgo.MONO(0, 64), s.theme.Transparent)
ctx.BeginPath()
ctx.Rect(kx-kr-5, ky-kr-5, kr*2+10, kr*2+10+3)
ctx.Circle(kx, ky, kr)
ctx.PathWinding(nanovgo.Hole)
ctx.SetFillPaint(knobShadow)
ctx.Fill()
knobPaint := nanovgo.LinearGradient(sx, cy-kr, sx, cy+kr, s.theme.BorderLight, s.theme.BorderMedium)
knobReversePaint := nanovgo.LinearGradient(sx, cy-kr, sx, cy+kr, s.theme.BorderMedium, s.theme.BorderLight)
ctx.BeginPath()
ctx.Circle(kx, ky, kr)
ctx.SetStrokeColor(s.theme.BorderDark)
ctx.SetFillPaint(knobPaint)
ctx.Stroke()
ctx.Fill()
ctx.BeginPath()
ctx.Circle(kx, ky, kr/2)
ctx.SetStrokePaint(knobReversePaint)
ctx.SetFillColor(nanovgo.MONO(150, a3))
ctx.Stroke()
ctx.Fill()
}
func (s *Slider) String() string {
return s.StringHelper("Slider", fmt.Sprintf("%f", s.value))
}