diff --git a/lib/game/components/backbox/backbox.dart b/lib/game/components/backbox/backbox.dart index 2102007b..10f1cbb8 100644 --- a/lib/game/components/backbox/backbox.dart +++ b/lib/game/components/backbox/backbox.dart @@ -53,14 +53,6 @@ class Backbox extends PositionComponent with ZIndex { if (state is LoadingState) { _display.add(LoadingDisplay()); } else if (state is InitialsFormState) { - _display.add( - InfoDisplay( - onShared: () { - //_bloc.add(ScoreShared()); - }, - ), - ); - /* _display.add( InitialsInputDisplay( score: state.score, @@ -76,9 +68,20 @@ class Backbox extends PositionComponent with ZIndex { }, ), ); - */ } else if (state is InitialsSuccessState) { - _display.add(InitialsSubmissionSuccessDisplay()); + _display.add( + InfoDisplay( + onShare: () { + _bloc.add( + ScoreShareRequested( + score: state.score, + initials: state.initials, + character: state.character, + ), + ); + }, + ), + ); } else if (state is InitialsFailureState) { _display.add(InitialsSubmissionFailureDisplay()); } diff --git a/lib/game/components/backbox/bloc/backbox_bloc.dart b/lib/game/components/backbox/bloc/backbox_bloc.dart index f315189e..d073434b 100644 --- a/lib/game/components/backbox/bloc/backbox_bloc.dart +++ b/lib/game/components/backbox/bloc/backbox_bloc.dart @@ -18,6 +18,7 @@ class BackboxBloc extends Bloc { super(LoadingState()) { on(_onPlayerInitialsRequested); on(_onPlayerInitialsSubmitted); + on(_onScoreShareRequested); } final LeaderboardRepository _leaderboardRepository; @@ -47,10 +48,29 @@ class BackboxBloc extends Bloc { character: event.character.toType, ), ); - emit(InitialsSuccessState()); + emit( + InitialsSuccessState( + initials: event.initials, + score: event.score, + character: event.character, + ), + ); } catch (error, stackTrace) { addError(error, stackTrace); emit(InitialsFailureState()); } } + + Future _onScoreShareRequested( + ScoreShareRequested event, + Emitter emit, + ) async { + emit( + ShareState( + initials: event.initials, + score: event.score, + character: event.character, + ), + ); + } } diff --git a/lib/game/components/backbox/bloc/backbox_event.dart b/lib/game/components/backbox/bloc/backbox_event.dart index 42203cdc..12347118 100644 --- a/lib/game/components/backbox/bloc/backbox_event.dart +++ b/lib/game/components/backbox/bloc/backbox_event.dart @@ -51,3 +51,27 @@ class PlayerInitialsSubmitted extends BackboxEvent { @override List get props => [score, initials, character]; } + +/// {@template score_share_requested} +/// Event that request the user to share score and initials. +/// {@endtemplate} +class ScoreShareRequested extends BackboxEvent { + /// {@macro score_share_requested} + const ScoreShareRequested({ + required this.score, + required this.initials, + required this.character, + }); + + /// Player's score. + final int score; + + /// Player's initials. + final String initials; + + /// Player's character. + final CharacterTheme character; + + @override + List get props => [score, initials, character]; +} diff --git a/lib/game/components/backbox/bloc/backbox_state.dart b/lib/game/components/backbox/bloc/backbox_state.dart index e1f2c801..361099a2 100644 --- a/lib/game/components/backbox/bloc/backbox_state.dart +++ b/lib/game/components/backbox/bloc/backbox_state.dart @@ -46,10 +46,28 @@ class InitialsFormState extends BackboxState { List get props => [score, character]; } +/// {@template initials_form_state} /// State when the leaderboard was successfully loaded. +/// {@endtemplate} class InitialsSuccessState extends BackboxState { + /// {@macro initials_success_state} + const InitialsSuccessState({ + required this.score, + required this.initials, + required this.character, + }) : super(); + + /// Player's score. + final int score; + + /// Player's initials. + final String initials; + + /// Player's character. + final CharacterTheme character; + @override - List get props => []; + List get props => [score, initials, character]; } /// State when the initials submission failed. @@ -57,3 +75,27 @@ class InitialsFailureState extends BackboxState { @override List get props => []; } + +/// {@template initials_form_state} +/// State when the user is sharing the score. +/// {@endtemplate} +class ShareState extends BackboxState { + /// {@macro share_state} + const ShareState({ + required this.score, + required this.initials, + required this.character, + }) : super(); + + /// Player's score. + final int score; + + /// Player's initials. + final String initials; + + /// Player's character. + final CharacterTheme character; + + @override + List get props => [score, initials, character]; +} diff --git a/lib/game/components/backbox/displays/info_display.dart b/lib/game/components/backbox/displays/info_display.dart index 34e06658..99b7ae56 100644 --- a/lib/game/components/backbox/displays/info_display.dart +++ b/lib/game/components/backbox/displays/info_display.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:flame/components.dart'; +import 'package:flame/input.dart'; import 'package:flutter/material.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball/l10n/l10n.dart'; @@ -8,9 +9,13 @@ import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_flame/pinball_flame.dart'; import 'package:pinball_ui/pinball_ui.dart'; -/// Signature for the callback called when the used has -/// shared score on the [InfoDisplay]. -typedef ScoreOnShared = void Function(); +/// Signature for the callback called when the used tries to share the score +/// on the [InfoDisplay]. +typedef OnShareTap = void Function(); + +/// Signature for the callback called when the used tries to navigate to the +/// Google IO site on the [InfoDisplay]. +typedef OnNavigateTap = void Function(); final _titleTextPaint = TextPaint( style: const TextStyle( @@ -35,7 +40,6 @@ final _linkTextPaint = TextPaint( color: PinballColors.orange, fontFamily: PinballFonts.pixeloidSans, fontWeight: FontWeight.bold, - decoration: TextDecoration.underline, decorationThickness: 1, ), ); @@ -54,21 +58,17 @@ final _descriptionTextPaint = TextPaint( class InfoDisplay extends Component with HasGameRef { /// {@macro info_display} InfoDisplay({ - ScoreOnShared? onShared, - }) : _onShared = onShared, - super( + OnShareTap? onShare, + OnNavigateTap? onNavigate, + }) : super( children: [ - _InstructionsComponent(), + _InstructionsComponent( + onShare: onShare, + onNavigate: onNavigate, + ), ], ); - final ScoreOnShared? _onShared; - - bool _share() { - _onShared?.call(); - return true; - } - @override Future onLoad() async { await super.onLoad(); @@ -77,19 +77,24 @@ class InfoDisplay extends Component with HasGameRef { } class _InstructionsComponent extends PositionComponent with HasGameRef { - _InstructionsComponent() - : super( + _InstructionsComponent({ + OnShareTap? onShare, + OnNavigateTap? onNavigate, + }) : super( anchor: Anchor.center, position: Vector2(0, -25), children: [ _TitleComponent(), - _LinksComponent(), + _LinksComponent( + onShare: onShare, + onNavigate: onNavigate, + ), _DescriptionComponent(), ], ); } -class _TitleComponent extends PositionComponent with HasGameRef { +class _TitleComponent extends PositionComponent with HasGameRef { _TitleComponent() : super( anchor: Anchor.center, @@ -102,8 +107,7 @@ class _TitleComponent extends PositionComponent with HasGameRef { ); } -class _ShareScoreTextComponent extends TextComponent - with HasGameRef { +class _ShareScoreTextComponent extends TextComponent with HasGameRef { _ShareScoreTextComponent() : super( anchor: Anchor.center, @@ -118,8 +122,7 @@ class _ShareScoreTextComponent extends TextComponent } } -class _ChallengeFriendsTextComponent extends TextComponent - with HasGameRef { +class _ChallengeFriendsTextComponent extends TextComponent with HasGameRef { _ChallengeFriendsTextComponent() : super( anchor: Anchor.center, @@ -152,26 +155,42 @@ class _TitleBackgroundSpriteComponent extends SpriteComponent with HasGameRef { } } -class _LinksComponent extends PositionComponent with HasGameRef { - _LinksComponent() - : super( +class _LinksComponent extends PositionComponent with HasGameRef { + _LinksComponent({ + OnShareTap? onShare, + OnNavigateTap? onNavigate, + }) : super( anchor: Anchor.center, position: Vector2(0, 9.2), children: [ - _ShareLinkComponent(), - _GotoIOLinkComponent(), + ShareLinkComponent(onTap: onShare), + GotoIOLinkComponent(onTap: onNavigate), ], ); } -class _ShareLinkComponent extends TextComponent with HasGameRef { - _ShareLinkComponent() - : super( +/// {@template share_link_component} +/// Link button for navigate to sharing score screen. +/// {@endtemplate} +class ShareLinkComponent extends TextComponent with HasGameRef, Tappable { + /// {@macro share_link_component} + ShareLinkComponent({ + OnShareTap? onTap, + }) : _onTap = onTap, + super( anchor: Anchor.center, position: Vector2(-7, 0), textRenderer: _linkTextPaint, ); + final OnShareTap? _onTap; + + @override + bool onTapDown(TapDownInfo info) { + _onTap?.call(); + return true; + } + @override Future onLoad() async { await super.onLoad(); @@ -179,14 +198,28 @@ class _ShareLinkComponent extends TextComponent with HasGameRef { } } -class _GotoIOLinkComponent extends TextComponent with HasGameRef { - _GotoIOLinkComponent() - : super( +/// {@template goto_io_link_component} +/// Link button for navigate to Google I/O site. +/// {@endtemplate} +class GotoIOLinkComponent extends TextComponent with HasGameRef, Tappable { + /// {@macro goto_io_link_component} + GotoIOLinkComponent({ + OnNavigateTap? onTap, + }) : _onTap = onTap, + super( anchor: Anchor.center, position: Vector2(6, 0), textRenderer: _linkTextPaint, ); + final OnNavigateTap? _onTap; + + @override + bool onTapDown(TapDownInfo info) { + _onTap?.call(); + return true; + } + @override Future onLoad() async { await super.onLoad(); @@ -194,8 +227,7 @@ class _GotoIOLinkComponent extends TextComponent with HasGameRef { } } -class _DescriptionComponent extends PositionComponent - with HasGameRef { +class _DescriptionComponent extends PositionComponent with HasGameRef { _DescriptionComponent() : super( anchor: Anchor.center, @@ -207,8 +239,7 @@ class _DescriptionComponent extends PositionComponent ); } -class _LearnMoreTextComponent extends TextComponent - with HasGameRef { +class _LearnMoreTextComponent extends TextComponent with HasGameRef { _LearnMoreTextComponent() : super( anchor: Anchor.center, @@ -223,8 +254,7 @@ class _LearnMoreTextComponent extends TextComponent } } -class _LearnMore2TextComponent extends TextComponent - with HasGameRef { +class _LearnMore2TextComponent extends TextComponent with HasGameRef { _LearnMore2TextComponent() : super( anchor: Anchor.center,