-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslider.go
176 lines (163 loc) · 5.08 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2025 The Ebitengine Authors
package debugui
import (
"fmt"
"image"
"math"
"strconv"
"github.com/hajimehoshi/ebiten/v2"
)
// Slider cretes a slider widget with the given int value, range, and step.
//
// lo and hi specify the range of the slider.
//
// Slider returns an EventHandler to handle value change events.
// A returned EventHandler is never nil.
//
// A Slider widget is uniquely determined by its call location.
// Function calls made in different locations will create different widgets.
// If you want to generate different widgets with the same function call in a loop (such as a for loop), use [IDScope].
func (c *Context) Slider(value *int, lo, hi int, step int) EventHandler {
pc := caller()
id := c.idFromCaller(pc)
return c.wrapEventHandlerAndError(func() (EventHandler, error) {
return c.slider(value, lo, hi, step, id, optionAlignCenter)
})
}
// SliderF cretes a slider widget with the given float64 value, range, step, and number of digits.
//
// lo and hi specify the range of the slider.
// digits specifies the number of digits to display after the decimal point.
//
// SliderF returns an EventHandler to handle value change events.
// A returned EventHandler is never nil.
//
// A SliderF widget is uniquely determined by its call location.
// Function calls made in different locations will create different widgets.
// If you want to generate different widgets with the same function call in a loop (such as a for loop), use [IDScope].
func (c *Context) SliderF(value *float64, lo, hi float64, step float64, digits int) EventHandler {
pc := caller()
id := c.idFromCaller(pc)
return c.wrapEventHandlerAndError(func() (EventHandler, error) {
return c.sliderF(value, lo, hi, step, digits, id, optionAlignCenter)
})
}
func (c *Context) slider(value *int, low, high, step int, id widgetID, opt option) (EventHandler, error) {
last := *value
v := last
if err := c.numberTextField(&v, id); err != nil {
return nil, err
}
if c.numberEdit == id {
return nil, nil
}
*value = v
return c.widget(id, opt, nil, func(bounds image.Rectangle, wasFocused bool) EventHandler {
var e EventHandler
if c.focus == id && c.pointing.pressed() {
if w := bounds.Dx() - defaultStyle.thumbSize; w > 0 {
v = low + (c.pointingPosition().X-bounds.Min.X)*(high-low)/w
}
if step != 0 {
v = v / step * step
}
}
*value = clamp(v, low, high)
v = *value
if last != v {
e = &eventHandler{}
}
return e
}, func(bounds image.Rectangle) {
c.drawWidgetFrame(id, bounds, colorBase, opt)
w := c.style().thumbSize
x := int((v - low) * (bounds.Dx() - w) / (high - low))
thumb := image.Rect(bounds.Min.X+x, bounds.Min.Y, bounds.Min.X+x+w, bounds.Max.Y)
c.drawWidgetFrame(id, thumb, colorButton, opt)
text := fmt.Sprintf("%d", v)
c.drawWidgetText(text, bounds, colorText, opt)
})
}
func (c *Context) sliderF(value *float64, low, high, step float64, digits int, id widgetID, opt option) (EventHandler, error) {
last := *value
v := last
if err := c.numberTextFieldF(&v, id); err != nil {
return nil, err
}
if c.numberEdit == id {
return nil, nil
}
*value = v
return c.widget(id, opt, nil, func(bounds image.Rectangle, wasFocused bool) EventHandler {
var e EventHandler
if c.focus == id && c.pointing.pressed() {
if w := float64(bounds.Dx() - defaultStyle.thumbSize); w > 0 {
v = low + float64(c.pointingPosition().X-bounds.Min.X)*(high-low)/w
}
if step != 0 {
v = math.Round(v/step) * step
}
}
*value = clamp(v, low, high)
v = *value
if last != v {
e = &eventHandler{}
}
return e
}, func(bounds image.Rectangle) {
c.drawWidgetFrame(id, bounds, colorBase, opt)
w := c.style().thumbSize
x := int((v - low) * float64(bounds.Dx()-w) / (high - low))
thumb := image.Rect(bounds.Min.X+x, bounds.Min.Y, bounds.Min.X+x+w, bounds.Max.Y)
c.drawWidgetFrame(id, thumb, colorButton, opt)
text := formatNumber(v, digits)
c.drawWidgetText(text, bounds, colorText, opt)
})
}
func (c *Context) numberTextField(value *int, id widgetID) error {
if c.pointing.justPressed() && ebiten.IsKeyPressed(ebiten.KeyShift) && c.hover == id {
c.numberEdit = id
c.numberEditBuf = fmt.Sprintf("%d", *value)
}
if c.numberEdit == id {
e, err := c.textFieldRaw(&c.numberEditBuf, id, optionAlignRight)
if err != nil {
return err
}
if e != nil {
e.On(func() {
nval, err := strconv.ParseInt(c.numberEditBuf, 10, 64)
if err != nil {
nval = 0
}
*value = int(nval)
c.numberEdit = emptyWidgetID
})
}
}
return nil
}
func (c *Context) numberTextFieldF(value *float64, id widgetID) error {
if c.pointing.justPressed() && ebiten.IsKeyPressed(ebiten.KeyShift) && c.hover == id {
c.numberEdit = id
c.numberEditBuf = fmt.Sprintf(realFmt, *value)
}
if c.numberEdit == id {
e, err := c.textFieldRaw(&c.numberEditBuf, id, optionAlignRight)
if err != nil {
return err
}
if e != nil {
e.On(func() {
nval, err := strconv.ParseFloat(c.numberEditBuf, 64)
if err != nil {
nval = 0
}
*value = float64(nval)
c.numberEdit = emptyWidgetID
})
}
}
return nil
}