11use crate :: color:: { DARK_BLACK , WHITE_COLOR } ;
2+ use crate :: game:: Settings ;
23use crate :: loading:: { FontAssets , TextureAssets } ;
34use crate :: GameState ;
45use bevy:: prelude:: * ;
@@ -464,13 +465,14 @@ fn on_show_settings(
464465 font_assets : Res < FontAssets > ,
465466 texture_assets : Res < TextureAssets > ,
466467 q_setting : Query < Entity , With < SettingContainer > > ,
468+ setting : Res < Settings > ,
467469) {
468470 let ( entity, mut visibility) = q_dialog. into_inner ( ) ;
469471 if trigger. event ( ) . 0 {
470472 time. pause ( ) ;
471473 * visibility = Visibility :: Visible ;
472474 commands. entity ( entity) . with_children ( |builder| {
473- spawn_settings ( & font_assets, & texture_assets, builder) ;
475+ spawn_settings ( & font_assets, & texture_assets, builder, & setting ) ;
474476 } ) ;
475477 } else {
476478 time. unpause ( ) ;
@@ -486,6 +488,7 @@ fn spawn_settings(
486488 font_assets : & Res < FontAssets > ,
487489 texture_assets : & Res < TextureAssets > ,
488490 builder : & mut ChildBuilder ,
491+ settings : & Res < Settings > ,
489492) {
490493 builder
491494 . spawn ( (
@@ -524,7 +527,6 @@ fn spawn_settings(
524527 width : Val :: Percent ( 100.0 ) ,
525528 flex_direction : FlexDirection :: Column ,
526529 // margin: UiRect::all(Val::Px(16.0)),
527-
528530 ..default ( )
529531 } ,
530532 // BackgroundColor(*DARK_BLACK),
@@ -544,38 +546,59 @@ fn spawn_settings(
544546 ..default ( )
545547 } ,
546548 ..default ( )
547- }
549+ } ,
548550 ) ) ;
549551
550552 setting_item (
551553 font_assets,
552554 texture_assets,
553555 builder,
554556 "Check guesses when entered" ,
557+ settings. check_guesses_when_entered ,
558+ |_trigger, mut settings| {
559+ settings. check_guesses_when_entered =
560+ !settings. check_guesses_when_entered ;
561+ } ,
555562 ) ;
556563 setting_item (
557564 font_assets,
558565 texture_assets,
559566 builder,
560567 "Start in automatic mode" ,
568+ settings. start_in_automatic_mode ,
569+ |_trigger, mut settings| {
570+ settings. start_in_automatic_mode = !settings. start_in_automatic_mode ;
571+ } ,
561572 ) ;
562573 setting_item (
563574 font_assets,
564575 texture_assets,
565576 builder,
566577 "Highlight conflicts" ,
578+ settings. highlight_conflicts ,
579+ |_trigger, mut settings| {
580+ settings. highlight_conflicts = !settings. highlight_conflicts ;
581+ } ,
567582 ) ;
568583 setting_item (
569584 font_assets,
570585 texture_assets,
571586 builder,
572587 "Play sound on solve" ,
588+ settings. play_sound_on_solve ,
589+ |_trigger, mut settings| {
590+ settings. play_sound_on_solve = !settings. play_sound_on_solve ;
591+ } ,
573592 ) ;
574593 setting_item (
575594 font_assets,
576595 texture_assets,
577596 builder,
578597 "Show clock" ,
598+ settings. show_clock ,
599+ |_trigger, mut settings| {
600+ settings. show_clock = !settings. show_clock ;
601+ } ,
579602 ) ;
580603 } ) ;
581604 } ) ;
@@ -586,10 +609,12 @@ fn setting_item(
586609 texture_assets : & Res < TextureAssets > ,
587610 builder : & mut ChildBuilder ,
588611 text : & str ,
612+ checked : bool ,
613+ change_setting : fn ( Trigger < Pointer < Click > > , settings : ResMut < Settings > ) ,
589614) {
590615 builder
591616 . spawn ( (
592- Name :: new ( "auto " ) ,
617+ Name :: new ( "text " ) ,
593618 Node {
594619 margin : UiRect {
595620 top : Val :: Px ( 10.0 ) ,
@@ -599,29 +624,23 @@ fn setting_item(
599624 align_items : AlignItems :: Center ,
600625 ..default ( )
601626 } ,
627+ CheckOption ( checked) ,
602628 ) )
629+ . observe ( click_setting_option)
630+ . observe ( change_setting)
603631 . with_children ( |builder| {
604632 builder. spawn ( (
605- ImageNode :: new ( texture_assets. blank_check . clone ( ) ) ,
606- Node {
607- width : Val :: Px ( 13.0 ) ,
608- height : Val :: Px ( 13.0 ) ,
609- position_type : PositionType :: Absolute ,
610- ..default ( )
633+ if checked {
634+ ImageNode :: new ( texture_assets. check . clone ( ) )
635+ } else {
636+ ImageNode :: new ( texture_assets. blank_check . clone ( ) )
611637 } ,
612- Unchecked ,
613- ) ) ;
614-
615- builder. spawn ( (
616- Visibility :: Hidden ,
617- ImageNode :: new ( texture_assets. check . clone ( ) ) ,
618638 Node {
619- position_type : PositionType :: Absolute ,
620639 width : Val :: Px ( 13.0 ) ,
621640 height : Val :: Px ( 13.0 ) ,
641+ position_type : PositionType :: Absolute ,
622642 ..default ( )
623643 } ,
624- Checked ,
625644 ) ) ;
626645
627646 builder. spawn ( (
@@ -644,7 +663,24 @@ fn setting_item(
644663}
645664
646665#[ derive( Component ) ]
647- pub struct Checked ;
666+ pub struct CheckOption ( pub bool ) ;
648667
649- #[ derive( Component ) ]
650- pub struct Unchecked ;
668+ fn click_setting_option (
669+ trigger : Trigger < Pointer < Click > > ,
670+ mut q_option : Query < ( & mut CheckOption , & Children ) > ,
671+ mut q_image : Query < & mut ImageNode > ,
672+ texture_assets : Res < TextureAssets > ,
673+ ) {
674+ if let Ok ( ( mut checked, children) ) = q_option. get_mut ( trigger. entity ( ) ) {
675+ checked. 0 = !checked. 0 ;
676+ for child in children {
677+ if let Ok ( mut image) = q_image. get_mut ( * child) {
678+ if checked. 0 {
679+ image. image = texture_assets. check . clone ( ) ;
680+ } else {
681+ image. image = texture_assets. blank_check . clone ( ) ;
682+ }
683+ }
684+ }
685+ }
686+ }
0 commit comments