-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.go
179 lines (154 loc) · 4.56 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
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
package ebitenlg
import (
"fmt"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
)
type RangeInterpolator[T float32 | float64 | int] struct {
Min T
Max T
Value T
}
func (r *RangeInterpolator[T]) SetRatio(ratio float64) {
v64 := float64(r.Min) + (float64(r.Max)-float64(r.Min))*(ratio)
r.Value = T(v64)
}
func (r *RangeInterpolator[T]) Ratio() float64 {
return float64(r.Value-r.Min) / float64(r.Max-r.Min)
}
func (r *RangeInterpolator[T]) String() string {
switch any(r.Value).(type) {
case float32:
v := any(r.Value).(float32)
return fmt.Sprintf("%.3f", v)
case float64:
v := any(r.Value).(float64)
return fmt.Sprintf("%.3f", v)
default:
v := any(r.Value).(int)
return fmt.Sprintf("%d", v)
}
}
type slider[T float32 | float64 | int] struct {
label string
hovered bool
callback func(v T)
Interpolator RangeInterpolator[T]
}
func UpdateSlider[T float32 | float64 | int](s *slider[T], x, y, width, height, scale float32) {
width *= scale
height *= scale
clicked := ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft)
if x >= width/2 && x <= width && y > 0 && y <= height {
s.hovered = true
} else {
s.hovered = false
}
if s.hovered && clicked {
ratio := float64((x - width/2) / width * 2)
s.Interpolator.SetRatio(ratio)
s.callback(s.Interpolator.Value)
}
}
func DrawSliderShape[T float32 | float64 | int](s *slider[T], image *ebiten.Image, whiteImage *ebiten.Image, left, top, width, height, scale float32) {
padding := 2.0 * scale
width *= scale
height *= scale
x := left + width/2 + padding
y := top + padding
w := width/2 - padding*2
h := height - padding*2
c := color.NRGBA{0x66, 0x66, 0x66, 0xff}
if s.hovered {
c = color.NRGBA{0x79, 0x79, 0x79, 0xff}
}
drawRect(image, whiteImage, x, y, w, h, c)
textPadding := 5.0 * scale
fontSize := height - textPadding*2
drawText(image, s.Interpolator.String(), x+textPadding, top+textPadding, fontSize, color.NRGBA{R: 0xeb, G: 0xeb, B: 0xeb, A: 0xff})
ratio := s.Interpolator.Ratio()
drawRange := w
drawWidth := drawRange * float32(ratio)
drawLine(
image,
whiteImage,
x+drawWidth,
y,
x+drawWidth,
y+h,
2.0*scale,
color.NRGBA{0x2c, 0xc9, 0xff, 0xff},
)
}
// SliderFloat64
type sliderFloat64 struct {
slider slider[float64]
}
func (g *GUI) AddSliderFloat64(label string, value, min, max float64, callback func(v float64)) {
s := slider[float64]{
label: label,
Interpolator: RangeInterpolator[float64]{Min: min, Max: max, Value: value},
callback: callback,
}
slider := &sliderFloat64{slider: s}
g.components = append(g.components, slider)
}
func (s *sliderFloat64) Label() string {
return s.slider.label
}
func (s *sliderFloat64) Update(x, y, width, height, scale float32) {
slider := &s.slider
UpdateSlider(slider, x, y, width, height, scale)
}
func (s *sliderFloat64) Draw(image *ebiten.Image, whiteImage *ebiten.Image, top, left, width, height, scale float32) {
slider := &s.slider
DrawSliderShape(slider, image, whiteImage, top, left, width, height, scale)
}
// SliderFloat32
type sliderFloat32 struct {
slider slider[float32]
}
func (g *GUI) AddSliderFloat32(label string, value, min, max float32, callback func(v float32)) {
s := slider[float32]{
label: label,
Interpolator: RangeInterpolator[float32]{Min: min, Max: max, Value: value},
callback: callback,
}
slider := &sliderFloat32{slider: s}
g.components = append(g.components, slider)
}
func (s *sliderFloat32) Label() string {
return s.slider.label
}
func (s *sliderFloat32) Update(x, y, width, height, scale float32) {
slider := &s.slider
UpdateSlider(slider, x, y, width, height, scale)
}
func (s *sliderFloat32) Draw(image *ebiten.Image, whiteImage *ebiten.Image, top, left, width, height, scale float32) {
slider := &s.slider
DrawSliderShape(slider, image, whiteImage, top, left, width, height, scale)
}
// SliderInt
type sliderInt struct {
slider slider[int]
}
func (g *GUI) AddSliderInt(label string, value, min, max int, callback func(v int)) {
s := slider[int]{
label: label,
Interpolator: RangeInterpolator[int]{Min: min, Max: max, Value: value},
callback: callback,
}
slider := &sliderInt{slider: s}
g.components = append(g.components, slider)
}
func (s *sliderInt) Label() string {
return s.slider.label
}
func (s *sliderInt) Update(x, y, width, height, scale float32) {
slider := &s.slider
UpdateSlider(slider, x, y, width, height, scale)
}
func (s *sliderInt) Draw(image *ebiten.Image, whiteImage *ebiten.Image, top, left, width, height, scale float32) {
slider := &s.slider
DrawSliderShape(slider, image, whiteImage, top, left, width, height, scale)
}