@@ -10,9 +10,6 @@ use bevy::{
1010 render:: { camera:: ScalingMode , mesh:: CircleMeshBuilder } ,
1111} ;
1212
13- const TICK_AUDIO_PATH : & str = "sounds/c5.ogg" ;
14- const TAP_AUDIO_PATH : & str = "sounds/c4.ogg" ;
15-
1613const CIRCLE_SIZE : f32 = 400.0 ;
1714const BINS : usize = 16 ;
1815
@@ -47,8 +44,33 @@ struct Clock;
4744
4845#[ derive( Resource ) ]
4946struct AudioHandles {
50- tick : Handle < AudioSource > ,
51- tap : Handle < AudioSource > ,
47+ handles : Vec < Handle < AudioSource > > ,
48+ tick : usize ,
49+ tap : usize ,
50+ }
51+
52+ impl AudioHandles {
53+ fn tick ( & self ) -> & Handle < AudioSource > {
54+ & self . handles [ self . tick ]
55+ }
56+
57+ fn tap ( & self ) -> & Handle < AudioSource > {
58+ & self . handles [ self . tap ]
59+ }
60+ }
61+
62+ #[ derive( Component ) ]
63+ enum Index {
64+ Tick ,
65+ Tap ,
66+ }
67+
68+ #[ derive( Component ) ]
69+ enum IndexButton {
70+ TickIncrement ,
71+ TickDecrement ,
72+ TapIncrement ,
73+ TapDecrement ,
5274}
5375
5476fn main ( ) {
@@ -80,7 +102,6 @@ fn main() {
80102 . add_systems (
81103 Update ,
82104 (
83- tap,
84105 control,
85106 clock,
86107 set_status_text,
@@ -89,8 +110,11 @@ fn main() {
89110 diagnostics_text_update_system,
90111 hide_clock,
91112 button_system,
113+ set_audio_indices,
92114 ) ,
93115 )
116+ // Set tap sound before tap
117+ . add_systems ( Update , ( index_button_system, tap) . chain ( ) )
94118 . run ( ) ;
95119}
96120
@@ -141,8 +165,15 @@ fn setup(
141165 asset_server : Res < AssetServer > ,
142166) {
143167 commands. insert_resource ( AudioHandles {
144- tick : asset_server. load ( TICK_AUDIO_PATH ) ,
145- tap : asset_server. load ( TAP_AUDIO_PATH ) ,
168+ handles : vec ! [
169+ asset_server. load( "sounds/c4.ogg" ) ,
170+ asset_server. load( "sounds/c5.ogg" ) ,
171+ asset_server. load( "sounds/808sd.ogg" ) ,
172+ asset_server. load( "sounds/808cb.ogg" ) ,
173+ asset_server. load( "sounds/808cp.ogg" ) ,
174+ ] ,
175+ tap : 0 ,
176+ tick : 1 ,
146177 } ) ;
147178
148179 commands. spawn ( (
@@ -187,16 +218,140 @@ fn setup(
187218 } ,
188219 ) . with_children ( |commands| {
189220 commands. spawn ( (
190- StatusText ,
191- Text :: new ( "" ) ,
192221 Node {
222+ display : Display :: Flex ,
223+ flex_direction : FlexDirection :: Column ,
193224 margin : UiRect {
194225 left : Val :: Px ( 12.0 ) ,
195226 ..Default :: default ( )
196227 } ,
197228 ..Default :: default ( )
198229 } ,
199- ) ) ;
230+ ) ) . with_children ( |commands| {
231+ commands. spawn ( (
232+ StatusText ,
233+ Text :: new ( "" ) ,
234+ ) ) ;
235+ commands. spawn ( (
236+ Node {
237+ border : UiRect :: all ( Val :: Px ( 2.0 ) ) ,
238+ justify_content : JustifyContent :: Center ,
239+ align_items : AlignItems :: Center ,
240+ margin : UiRect {
241+ top : Val :: Px ( 4.0 ) ,
242+ ..Default :: default ( )
243+ } ,
244+ ..Default :: default ( )
245+ } ,
246+ BorderColor ( Color :: BLACK ) ,
247+ BackgroundColor ( NORMAL_BUTTON ) ,
248+ ) ) . with_children ( |commands| {
249+ commands. spawn ( (
250+ Button ,
251+ IndexButton :: TickDecrement ,
252+ Node {
253+ position_type : PositionType :: Absolute ,
254+ left : Val :: Px ( 0.0 ) ,
255+ height : Val :: Percent ( 100.0 ) ,
256+ width : Val :: Percent ( 50.0 ) ,
257+ ..default ( )
258+ } ,
259+ ) ) . with_children ( |commands| {
260+ commands. spawn ( (
261+ Text :: new ( "-" ) ,
262+ Node {
263+ position_type : PositionType :: Absolute ,
264+ left : Val :: Px ( 12.0 ) ,
265+ ..default ( )
266+ }
267+ ) ) ;
268+ } ) ;
269+ commands. spawn ( (
270+ Button ,
271+ IndexButton :: TickIncrement ,
272+ Node {
273+ position_type : PositionType :: Absolute ,
274+ right : Val :: Px ( 0.0 ) ,
275+ height : Val :: Percent ( 100.0 ) ,
276+ width : Val :: Percent ( 50.0 ) ,
277+ ..default ( )
278+ } ,
279+ ) ) . with_children ( |commands| {
280+ commands. spawn ( (
281+ Text :: new ( "+" ) ,
282+ Node {
283+ position_type : PositionType :: Absolute ,
284+ right : Val :: Px ( 12.0 ) ,
285+ ..default ( )
286+ }
287+ ) ) ;
288+ } ) ;
289+ commands. spawn ( (
290+ Index :: Tick ,
291+ Text :: new ( "" ) ,
292+ ) ) ;
293+ } ) ;
294+ commands. spawn ( (
295+ Node {
296+ border : UiRect :: all ( Val :: Px ( 2.0 ) ) ,
297+ justify_content : JustifyContent :: Center ,
298+ align_items : AlignItems :: Center ,
299+ margin : UiRect {
300+ top : Val :: Px ( 2.0 ) ,
301+ ..Default :: default ( )
302+ } ,
303+ ..Default :: default ( )
304+ } ,
305+ BorderColor ( Color :: BLACK ) ,
306+ BackgroundColor ( NORMAL_BUTTON ) ,
307+ ) ) . with_children ( |commands| {
308+ commands. spawn ( (
309+ Button ,
310+ IndexButton :: TapDecrement ,
311+ Node {
312+ position_type : PositionType :: Absolute ,
313+ left : Val :: Px ( 0.0 ) ,
314+ height : Val :: Percent ( 100.0 ) ,
315+ width : Val :: Percent ( 50.0 ) ,
316+ ..default ( )
317+ } ,
318+ ) ) . with_children ( |commands| {
319+ commands. spawn ( (
320+ Text :: new ( "-" ) ,
321+ Node {
322+ position_type : PositionType :: Absolute ,
323+ left : Val :: Px ( 12.0 ) ,
324+ ..default ( )
325+ }
326+ ) ) ;
327+ } ) ;
328+ commands. spawn ( (
329+ Button ,
330+ IndexButton :: TapIncrement ,
331+ Node {
332+ position_type : PositionType :: Absolute ,
333+ right : Val :: Px ( 0.0 ) ,
334+ height : Val :: Percent ( 100.0 ) ,
335+ width : Val :: Percent ( 50.0 ) ,
336+ ..default ( )
337+ } ,
338+ ) ) . with_children ( |commands| {
339+ commands. spawn ( (
340+ Text :: new ( "+" ) ,
341+ Node {
342+ position_type : PositionType :: Absolute ,
343+ right : Val :: Px ( 12.0 ) ,
344+ ..default ( )
345+ }
346+ ) ) ;
347+ } ) ;
348+ commands. spawn ( (
349+ Index :: Tap ,
350+ Text :: new ( "" ) ,
351+ ) ) ;
352+ } ) ;
353+ }
354+ ) ;
200355
201356 commands. spawn ( (
202357 Text :: new (
@@ -448,7 +603,7 @@ fn tap(
448603 {
449604 if !mute. tap_mute {
450605 commands. spawn ( (
451- AudioPlayer :: new ( audio_handles. tap . clone ( ) ) ,
606+ AudioPlayer :: new ( audio_handles. tap ( ) . clone ( ) ) ,
452607 PlaybackSettings :: DESPAWN ,
453608 ) ) ;
454609 }
@@ -484,7 +639,7 @@ fn metronome(
484639) {
485640 if !mute. tick_mute {
486641 commands. spawn ( (
487- AudioPlayer :: new ( audio_handles. tick . clone ( ) ) ,
642+ AudioPlayer :: new ( audio_handles. tick ( ) . clone ( ) ) ,
488643 PlaybackSettings :: DESPAWN ,
489644 ) ) ;
490645 }
@@ -799,3 +954,53 @@ fn button_system(
799954 }
800955 }
801956}
957+
958+ fn set_audio_indices (
959+ audio_handles : Res < AudioHandles > ,
960+ mut tick_text : Query < ( & mut Text , & Index ) > ,
961+ // mut tap_text: Query<&mut Text, With<TapIndex>>,
962+ ) {
963+ if audio_handles. is_changed ( ) {
964+ for ( mut text, index) in & mut tick_text {
965+ text. 0 = match index {
966+ Index :: Tick => {
967+ format ! ( "tick: {}" , audio_handles. tick)
968+ }
969+ Index :: Tap => {
970+ format ! ( "tap: {}" , audio_handles. tap)
971+ }
972+ } ;
973+ }
974+ }
975+ }
976+
977+ #[ allow( clippy:: type_complexity) ]
978+ fn index_button_system (
979+ mut interaction_query : Query <
980+ ( & Interaction , & IndexButton ) ,
981+ ( Changed < Interaction > , With < Button > ) ,
982+ > ,
983+ mut audio_handles : ResMut < AudioHandles > ,
984+ ) {
985+ for ( interaction, index_button) in & mut interaction_query {
986+ match * interaction {
987+ Interaction :: Pressed => match index_button {
988+ IndexButton :: TickIncrement => {
989+ audio_handles. tick = ( audio_handles. tick + 1 ) % audio_handles. handles . len ( ) ;
990+ }
991+ IndexButton :: TickDecrement => {
992+ audio_handles. tick = ( audio_handles. tick + audio_handles. handles . len ( ) - 1 )
993+ % audio_handles. handles . len ( ) ;
994+ }
995+ IndexButton :: TapIncrement => {
996+ audio_handles. tap = ( audio_handles. tap + 1 ) % audio_handles. handles . len ( ) ;
997+ }
998+ IndexButton :: TapDecrement => {
999+ audio_handles. tap = ( audio_handles. tap + audio_handles. handles . len ( ) - 1 )
1000+ % audio_handles. handles . len ( ) ;
1001+ }
1002+ } ,
1003+ Interaction :: None | Interaction :: Hovered => { }
1004+ }
1005+ }
1006+ }
0 commit comments