Repository navigation
Expand file tree
/
Copy pathdraggable_slider.rs
More file actions
379 lines (343 loc) · 12.8 KB
/
Copy pathdraggable_slider.rs
File metadata and controls
379 lines (343 loc) · 12.8 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//! Demonstrates why pointer capture matters during drag operations.
//!
//! A slider sits above a row of buttons. Without [`PointerCaptureMap`], dragging the slider thumb
//! over the buttons causes them to show a hover highlight — misleading the user into thinking the
//! button is about to be activated. With capture enabled the slider owns the pointer for the
//! duration of the drag, so no other widget receives hover state.
//!
//! Toggle capture on/off with the button at the bottom to see the difference.
//!
//! Additional UI elements showcased:
//! - A **fill bar** that grows with the slider value.
//! - **Min / max labels** flanking the track.
//! - A **Reset button** that snaps the value back to the default.
use bevy::prelude::*;
const TRACK_WIDTH: f32 = 500.0;
const TRACK_HEIGHT: f32 = 14.0;
const THUMB_SIZE: f32 = 28.0;
const THUMB_HALF: f32 = THUMB_SIZE / 2.0;
const FILL_HEIGHT: f32 = 8.0;
const SLIDER_DEFAULT_VALUE: f32 = 0.5;
const COLOR_TRACK: Color = Color::srgb(0.2, 0.2, 0.2);
const COLOR_FILL: Color = Color::srgb(0.35, 0.65, 0.35);
const COLOR_FILL_DRAGGING: Color = Color::srgb(0.45, 0.85, 0.45);
const COLOR_THUMB_IDLE: Color = Color::srgb(0.85, 0.55, 0.1);
const COLOR_THUMB_HOVER: Color = Color::srgb(1.0, 0.75, 0.2);
const COLOR_DECOY_IDLE: Color = Color::srgb(0.2, 0.45, 0.8);
const COLOR_DECOY_HOVER: Color = Color::srgb(0.9, 0.2, 0.2);
const COLOR_TOGGLE_ON: Color = Color::srgb(0.15, 0.65, 0.3);
const COLOR_TOGGLE_OFF: Color = Color::srgb(0.55, 0.15, 0.15);
const COLOR_RESET_IDLE: Color = Color::srgb(0.3, 0.3, 0.55);
const COLOR_RESET_HOVER: Color = Color::srgb(0.5, 0.5, 0.75);
/// The normalized (0 – 1) position of the slider thumb.
#[derive(Resource)]
struct SliderValue(f32);
impl Default for SliderValue {
fn default() -> Self {
Self(SLIDER_DEFAULT_VALUE)
}
}
/// Whether [`PointerCaptureMap`] is used while dragging the thumb.
#[derive(Resource)]
struct CaptureEnabled(bool);
impl Default for CaptureEnabled {
fn default() -> Self {
Self(true)
}
}
#[derive(Component)]
struct SliderThumb;
#[derive(Component)]
struct SliderFill;
#[derive(Component)]
struct SliderLabel;
#[derive(Component)]
struct ToggleLabel;
#[derive(Component)]
struct ToggleButton;
#[derive(Component)]
struct ResetButton;
#[derive(Component)]
struct DecoyButton;
/// X offset of the left edge of the thumb node.
fn thumb_left(v: f32) -> f32 {
v.clamp(0.0, 1.0) * (TRACK_WIDTH - THUMB_SIZE)
}
/// Width of the fill bar (reaches to the center of the thumb).
fn fill_width(v: f32) -> f32 {
v.clamp(0.0, 1.0) * (TRACK_WIDTH - THUMB_SIZE) + THUMB_HALF
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.init_resource::<SliderValue>()
.init_resource::<CaptureEnabled>()
.add_systems(Startup, setup)
.add_systems(Update, (sync_thumb_and_label, sync_fill))
.run();
}
fn setup(mut commands: Commands) {
commands.spawn(Camera2d);
// Root column – centered, full-screen, non-pickable container.
commands
.spawn((
Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
row_gap: Val::Px(28.0),
..default()
},
Pickable::IGNORE,
))
.with_children(|root| {
root.spawn((
Text::new(
"Drag the slider over the blue buttons.\n\
With capture ON they stay blue. With capture OFF they incorrectly turn red.",
),
TextFont::from_font_size(15.0),
TextColor(Color::srgb(0.75, 0.75, 0.75)),
));
root.spawn((
Node {
flex_direction: FlexDirection::Row,
align_items: AlignItems::Center,
column_gap: Val::Px(10.0),
..default()
},
Pickable::IGNORE,
))
.with_children(|row| {
row.spawn((
Text::new("0.0"),
TextFont::from_font_size(13.0),
TextColor(Color::srgb(0.55, 0.55, 0.55)),
));
row.spawn((
Node {
width: Val::Px(TRACK_WIDTH),
height: Val::Px(TRACK_HEIGHT),
position_type: PositionType::Relative,
align_items: AlignItems::Center,
..default()
},
BackgroundColor(COLOR_TRACK),
Pickable::IGNORE,
))
.with_children(|track| {
track.spawn((
SliderFill,
Node {
width: Val::Px(fill_width(SLIDER_DEFAULT_VALUE)),
height: Val::Px(FILL_HEIGHT),
position_type: PositionType::Absolute,
left: Val::Px(0.0),
..default()
},
BackgroundColor(COLOR_FILL),
Pickable::IGNORE,
));
track
.spawn((
SliderThumb,
Node {
width: Val::Px(THUMB_SIZE),
height: Val::Px(THUMB_SIZE),
position_type: PositionType::Absolute,
left: Val::Px(thumb_left(SLIDER_DEFAULT_VALUE)),
border_radius: BorderRadius::all(Val::Px(4.0)),
..default()
},
BackgroundColor(COLOR_THUMB_IDLE),
))
.observe(on_drag_start)
.observe(on_drag)
.observe(on_drag_end)
.observe(
|_: On<PointerOver>,
mut q: Single<&mut BackgroundColor, With<SliderThumb>>| {
q.0 = COLOR_THUMB_HOVER;
},
)
.observe(
|_: On<PointerOut>,
mut q: Single<&mut BackgroundColor, With<SliderThumb>>| {
q.0 = COLOR_THUMB_IDLE;
},
);
});
row.spawn((
Text::new("1.0"),
TextFont::from_font_size(13.0),
TextColor(Color::srgb(0.55, 0.55, 0.55)),
));
});
root.spawn((
SliderLabel,
Text::new(format!("Value: {:.2}", SLIDER_DEFAULT_VALUE)),
TextFont::from_font_size(18.0),
));
root.spawn((
Node {
flex_direction: FlexDirection::Row,
column_gap: Val::Px(16.0),
..default()
},
Pickable::IGNORE,
))
.with_children(|row| {
for label in ["Button A", "Button B", "Button C", "Button D", "Button E"] {
spawn_decoy_button(row, label);
}
});
root.spawn((
Node {
flex_direction: FlexDirection::Row,
column_gap: Val::Px(20.0),
align_items: AlignItems::Center,
..default()
},
Pickable::IGNORE,
))
.with_children(|row| {
row.spawn((
ToggleButton,
Node {
padding: UiRect::axes(Val::Px(20.0), Val::Px(10.0)),
border_radius: BorderRadius::all(Val::Px(6.0)),
..default()
},
BackgroundColor(COLOR_TOGGLE_ON),
))
.with_children(|btn| {
btn.spawn((
ToggleLabel,
Text::new("Capture: ON (click to toggle)"),
TextFont::from_font_size(16.0),
TextColor(Color::WHITE),
Pickable::IGNORE,
));
})
.observe(on_toggle_click);
row.spawn((
ResetButton,
Node {
padding: UiRect::axes(Val::Px(20.0), Val::Px(10.0)),
border_radius: BorderRadius::all(Val::Px(6.0)),
..default()
},
BackgroundColor(COLOR_RESET_IDLE),
))
.with_children(|btn| {
btn.spawn((
Text::new("Reset"),
TextFont::from_font_size(16.0),
TextColor(Color::WHITE),
Pickable::IGNORE,
));
})
.observe(
|_: On<PointerOver>, mut q: Single<&mut BackgroundColor, With<ResetButton>>| {
q.0 = COLOR_RESET_HOVER;
},
)
.observe(
|_: On<PointerOut>, mut q: Single<&mut BackgroundColor, With<ResetButton>>| {
q.0 = COLOR_RESET_IDLE;
},
)
.observe(on_reset_click);
});
});
}
fn spawn_decoy_button(parent: &mut ChildSpawnerCommands, label: &str) {
parent
.spawn((
DecoyButton,
Node {
padding: UiRect::axes(Val::Px(24.0), Val::Px(14.0)),
border_radius: BorderRadius::all(Val::Px(6.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
BackgroundColor(COLOR_DECOY_IDLE),
))
.with_child((
Text::new(label),
TextFont::from_font_size(15.0),
TextColor(Color::WHITE),
Pickable::IGNORE,
))
.observe(
|event: On<PointerOver>, mut colors: Query<&mut BackgroundColor, With<DecoyButton>>| {
if let Ok(mut color) = colors.get_mut(event.entity) {
color.0 = COLOR_DECOY_HOVER;
}
},
)
.observe(
|event: On<PointerOut>, mut colors: Query<&mut BackgroundColor, With<DecoyButton>>| {
if let Ok(mut color) = colors.get_mut(event.entity) {
color.0 = COLOR_DECOY_IDLE;
}
},
);
}
fn on_drag_start(
event: On<PointerDragStart>,
capture_enabled: Res<CaptureEnabled>,
mut capture_map: ResMut<PointerCaptureMap>,
mut fill_color: Single<&mut BackgroundColor, With<SliderFill>>,
) {
if capture_enabled.0 {
capture_map.capture(event.pointer.id, event.entity, event.hit.clone());
}
// Signal active drag with a brighter fill color.
fill_color.0 = COLOR_FILL_DRAGGING;
}
fn on_drag(event: On<PointerDrag>, mut value: ResMut<SliderValue>) {
value.0 = (value.0 + event.delta.x / (TRACK_WIDTH - THUMB_SIZE)).clamp(0.0, 1.0);
}
fn on_drag_end(
event: On<PointerDragEnd>,
mut capture_map: ResMut<PointerCaptureMap>,
mut fill_color: Single<&mut BackgroundColor, With<SliderFill>>,
) {
capture_map.release(event.pointer.id);
fill_color.0 = COLOR_FILL;
}
fn on_toggle_click(
_: On<PointerClick>,
mut capture_enabled: ResMut<CaptureEnabled>,
mut label: Single<&mut Text, With<ToggleLabel>>,
mut toggle_bg: Single<&mut BackgroundColor, With<ToggleButton>>,
) {
capture_enabled.0.toggle();
if capture_enabled.0 {
label.0 = "Capture: ON (click to toggle)".into();
toggle_bg.0 = COLOR_TOGGLE_ON;
} else {
label.0 = "Capture: OFF (click to toggle)".into();
toggle_bg.0 = COLOR_TOGGLE_OFF;
}
}
fn on_reset_click(_: On<PointerClick>, mut value: ResMut<SliderValue>) {
value.0 = SLIDER_DEFAULT_VALUE;
}
/// Moves the thumb and updates the numeric readout.
fn sync_thumb_and_label(
value: Res<SliderValue>,
mut thumb: Single<&mut Node, With<SliderThumb>>,
mut value_label: Single<&mut Text, With<SliderLabel>>,
) {
thumb.left = Val::Px(thumb_left(value.0));
value_label.0 = format!("Value: {:.2}", value.0);
}
/// Resizes the fill bar to match the current slider value.
fn sync_fill(value: Res<SliderValue>, mut fill: Single<&mut Node, With<SliderFill>>) {
fill.width = Val::Px(fill_width(value.0));
}