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
use blit::slice::Slice;
use taffy::prelude::Node;
use vek::{Rect, Vec2};
use crate::input::Input;
/// A simple slider widget.
#[derive(Debug)]
pub struct Slider {
/// Top-left position of the widget in pixels.
pub offset: Vec2<f64>,
/// Length of the slider in pixels.
pub length: f64,
/// Minimum value of the slider.
pub min: f64,
/// Maximum value of the slider.
pub max: f64,
/// How many steps the slider should snap to.
pub steps: Option<f64>,
/// Current positition of the slider as a fraction.
pub pos: f64,
/// Whether the slider state is being captured by the mouse.
pub dragged: bool,
/// A custom label with the value.
pub value_label: Option<String>,
/// Taffy layout node.
pub node: Node,
}
impl Slider {
/// Handle the input.
///
/// Returns whether the value changed.
pub fn update(&mut self, input: &Input) -> bool {
if !self.dragged {
// Detect whether the mouse is being pressed on the handle
let handle = crate::sprite("slider-handle");
let handle_rect = Rect::new(
self.offset.x - handle.width() as f64 / 2.0,
self.offset.y - handle.height() as f64 / 2.0,
handle.width() as f64 * 2.0 + self.length,
handle.height() as f64 * 2.0,
);
if !self.dragged
&& input.left_mouse.is_pressed()
&& handle_rect.contains_point(input.mouse_pos.as_())
{
self.dragged = true;
}
false
} else if input.left_mouse.is_released() {
// Always release the slider when the mouse is released
self.dragged = false;
false
} else if input.left_mouse.is_down() {
let prev = self.pos;
// Drag the slider
self.pos = ((input.mouse_pos.x as f64 - self.offset.x) / self.length).clamp(0.0, 1.0);
// Clamp to steps
let steps = self.steps.unwrap_or(self.length);
self.pos = (self.pos * steps).round() / steps;
self.pos != prev
} else {
false
}
}
/// Render the slider.
pub fn render(&self, canvas: &mut [u32]) {
let handle = crate::sprite("slider-handle");
let bar = crate::sprite("slider-bar");
bar.render_vertical_slice(
canvas,
self.offset
+ (
0.0,
handle.height() as f64 / 2.0 - bar.height() as f64 / 2.0,
),
self.length,
Slice::Ternary {
split_first: 2,
split_last: 3,
},
);
handle.render(
canvas,
self.offset
+ (
self.pos.clamp(0.0, 1.0) * self.length - handle.width() as f64 / 2.0,
0.0,
),
);
// Draw the optional label
if let Some(value_label) = &self.value_label {
crate::font().render(
&format!("{value_label}: {}", self.value().round()),
self.offset + (self.length + 12.0, 2.0),
canvas,
);
}
}
/// Update from layout changes.
pub fn update_layout(&mut self, location: Vec2<f64>) {
self.offset = location;
}
/// Actual value of the slider.
pub fn value(&self) -> f64 {
(self.max - self.min) * self.pos + self.min
}
}
impl Default for Slider {
fn default() -> Self {
Self {
offset: Vec2::zero(),
length: 100.0,
min: 0.0,
max: 1.0,
steps: None,
pos: 0.0,
dragged: false,
value_label: None,
node: Node::default(),
}
}
}