Skip to content

Commit 06fcd3c

Browse files
authored
Merge pull request #26 from hatoo/clock
Clock
2 parents b34ee0a + e04f644 commit 06fcd3c

File tree

1 file changed

+77
-17
lines changed

1 file changed

+77
-17
lines changed

src/main.rs

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@ struct LastTick(Instant);
2727
#[derive(Resource)]
2828
struct Division(u32);
2929

30+
struct Delta {
31+
delta: f64,
32+
division: usize,
33+
// 0 to 2pi
34+
theta: f64,
35+
}
36+
3037
#[derive(Resource)]
3138
// delta and nearest disvision
32-
struct TapDeltas(VecDeque<(f64, usize)>);
39+
struct TapDeltas(VecDeque<Delta>);
3340

3441
#[derive(Resource, Default)]
3542
struct Mute {
@@ -60,6 +67,14 @@ impl AudioHandles {
6067
}
6168
}
6269

70+
#[derive(Resource)]
71+
struct ClockResource {
72+
mesh_legend: Handle<Mesh>,
73+
material_legend: Handle<ColorMaterial>,
74+
mesh_delta: Handle<Mesh>,
75+
material_delta: Handle<ColorMaterial>,
76+
}
77+
6378
#[derive(Component)]
6479
enum Index {
6580
Tick,
@@ -116,6 +131,7 @@ fn main() {
116131
button_system,
117132
set_audio_indices,
118133
set_statistics,
134+
set_clock_delta,
119135
),
120136
)
121137
// Set tap sound before tap
@@ -181,6 +197,13 @@ fn setup(
181197
tick: 1,
182198
});
183199

200+
commands.insert_resource(ClockResource {
201+
mesh_legend: meshes.add(Mesh::from(Circle { radius: 16.0 })),
202+
material_legend: materials.add(Color::linear_rgb(0.1, 0.3, 0.1)),
203+
mesh_delta: meshes.add(Mesh::from(Circle { radius: 8.0 })),
204+
material_delta: materials.add(Color::linear_rgb(0.1, 0.1, 0.3)),
205+
});
206+
184207
commands.spawn((
185208
Camera2d,
186209
Projection::Orthographic(OrthographicProjection {
@@ -640,7 +663,7 @@ fn tap(
640663
let delta_from_last = from_last % time_step_div.as_secs_f64();
641664
let delta_from_next = from_next % time_step_div.as_secs_f64();
642665

643-
let delta = if delta_from_last < delta_from_next {
666+
let (delta, division) = if delta_from_last < delta_from_next {
644667
let division = (from_last / time_step_div.as_secs_f64()) as usize;
645668
(delta_from_last, division)
646669
} else {
@@ -650,7 +673,11 @@ fn tap(
650673
(-delta_from_next, division)
651674
};
652675

653-
tap_deltas.0.push_front(delta);
676+
tap_deltas.0.push_front(Delta {
677+
delta,
678+
division,
679+
theta: from_last / time_step.as_secs_f64() * 2.0 * std::f64::consts::PI,
680+
});
654681
while tap_deltas.0.len() > BINS {
655682
tap_deltas.0.pop_back();
656683
}
@@ -761,8 +788,10 @@ fn clock(
761788

762789
let angle = 2.0 * std::f32::consts::PI * delta as f32;
763790

764-
let mut transform = query.single_mut();
765-
transform.translation = Vec3::new(angle.sin() * CIRCLE_SIZE, angle.cos() * CIRCLE_SIZE, 1.0);
791+
for mut transform in &mut query {
792+
transform.translation =
793+
Vec3::new(angle.sin() * CIRCLE_SIZE, angle.cos() * CIRCLE_SIZE, 1.0);
794+
}
766795
}
767796

768797
#[derive(Component)]
@@ -781,7 +810,7 @@ fn set_bins(
781810
) {
782811
if tap_deltas.is_changed() {
783812
for (BinIndex(index), mut node, mut color, mut visibility) in &mut query_bar {
784-
if let Some((delta, _)) = tap_deltas.0.get(*index) {
813+
if let Some(Delta { delta, .. }) = tap_deltas.0.get(*index) {
785814
let height = delta.abs() as f32 * BAR_HEIGHT_MULTIPLIER;
786815
node.height = Val::Px(height);
787816
node.position_type = PositionType::Absolute;
@@ -803,8 +832,11 @@ fn set_bins(
803832
}
804833

805834
for (BinIndex(index), mut text) in &mut query_text {
806-
if let Some((delta, index)) = tap_deltas.0.get(*index) {
807-
text.0 = format!("[{}]{:+.1}", index, delta * 1000.0);
835+
if let Some(Delta {
836+
delta, division, ..
837+
}) = tap_deltas.0.get(*index)
838+
{
839+
text.0 = format!("[{}]{:+.1}", division, delta * 1000.0);
808840
} else {
809841
text.0 = "".to_string();
810842
}
@@ -832,8 +864,7 @@ fn set_clock_legend(
832864
query: Query<Entity, With<ClockLegend>>,
833865
parent: Query<Entity, With<Clock>>,
834866
division: Res<Division>,
835-
mut meshes: ResMut<Assets<Mesh>>,
836-
mut materials: ResMut<Assets<ColorMaterial>>,
867+
clock_resource: Res<ClockResource>,
837868
) {
838869
if division.is_changed() {
839870
for e in query.iter() {
@@ -843,10 +874,6 @@ fn set_clock_legend(
843874
for parent in &parent {
844875
let division = division.0;
845876

846-
// TODO: reuse mesh and material handles
847-
let mesh = Mesh2d(meshes.add(Mesh::from(Circle { radius: 16.0 })));
848-
let material = MeshMaterial2d(materials.add(Color::linear_rgb(0.1, 0.3, 0.1)));
849-
850877
commands.entity(parent).with_children(|commands| {
851878
for bundle in (0..division).map(|i| {
852879
let angle = 2.0 * std::f32::consts::PI * (i as f32 / division as f32);
@@ -855,8 +882,8 @@ fn set_clock_legend(
855882

856883
(
857884
ClockLegend,
858-
mesh.clone(),
859-
material.clone(),
885+
Mesh2d(clock_resource.mesh_legend.clone()),
886+
MeshMaterial2d(clock_resource.material_legend.clone()),
860887
Transform::from_xyz(x, y, 3.0),
861888
)
862889
}) {
@@ -867,6 +894,39 @@ fn set_clock_legend(
867894
}
868895
}
869896

897+
#[derive(Component)]
898+
struct ClockDelta;
899+
900+
fn set_clock_delta(
901+
mut commands: Commands,
902+
query: Query<Entity, With<ClockDelta>>,
903+
tap_deltas: Res<TapDeltas>,
904+
parent: Query<Entity, With<Clock>>,
905+
clock_resource: Res<ClockResource>,
906+
) {
907+
if tap_deltas.is_changed() {
908+
for e in query.iter() {
909+
commands.entity(e).despawn_recursive();
910+
}
911+
912+
for parent in &parent {
913+
commands.entity(parent).with_children(|commands| {
914+
for Delta { theta, .. } in tap_deltas.0.iter() {
915+
let x = theta.sin() as f32 * CIRCLE_SIZE;
916+
let y = theta.cos() as f32 * CIRCLE_SIZE;
917+
918+
commands.spawn((
919+
ClockDelta,
920+
Mesh2d(clock_resource.mesh_delta.clone()),
921+
MeshMaterial2d(clock_resource.material_delta.clone()),
922+
Transform::from_xyz(x, y, 4.0),
923+
));
924+
}
925+
});
926+
}
927+
}
928+
}
929+
870930
#[derive(Component)]
871931
struct DiagnosticsText;
872932

@@ -1032,7 +1092,7 @@ fn index_button_system(
10321092
}
10331093

10341094
fn set_statistics(tap_deltas: Res<TapDeltas>, mut query: Query<&mut Text, With<Statistics>>) {
1035-
let mean = tap_deltas.0.iter().map(|d| d.0.abs()).sum::<f64>() / tap_deltas.0.len() as f64;
1095+
let mean = tap_deltas.0.iter().map(|d| d.delta.abs()).sum::<f64>() / tap_deltas.0.len() as f64;
10361096

10371097
for mut text in &mut query {
10381098
text.0 = format!("|avg(ms)|: {:.1}", mean * 1000.0);

0 commit comments

Comments
 (0)