/// <summary> /// The RetrieveBall method is called to remove the ball from the wheel. /// </summary> public void RetrieveBall() { _ballControl.Visibility = Visibility.Hidden; OnBallTossed?.Invoke(false); // Publish the status of the ball - the ball is no longer in the wheel. }
/// <summary> /// The TossBall method is called to toss the ball. /// </summary> public void TossBall() { try { if (_ballStoryBoard == null) { // Initialize the story board. _ballStoryBoard = new Storyboard(); _ballStoryBoard.Completed += new EventHandler(BallStopped); } else if (_ballStoryBoard.GetCurrentState(_ballControl) == ClockState.Stopped || _ballStoryBoard.GetIsPaused(_ballControl)) { return; // Ball is already moving or is paused - do not spin. } // Ball spinning animation 1. if (_ballSpinningAnimation1 == null) { // Initialize the ball spinning animation. _ballSpinningAnimation1 = new DoubleAnimation(); _ballSpinningAnimation1.SpeedRatio = Constants.BallSpeedRatio; Storyboard.SetTargetName(_ballSpinningAnimation1, Constants.BallRotateTransformName); Storyboard.SetTargetProperty(_ballSpinningAnimation1, new PropertyPath(RotateTransform.AngleProperty)); _ballStoryBoard.Children.Add(_ballSpinningAnimation1); // Add the animation to the story board. } // Number of rotations in total degrees - multiply by ball speed ratio (positive degrees for clockwise). _ballSpinningAnimation1.From = 0; _ballSpinningAnimation1.To = _winningNumberEndAngleDegrees + (Constants.FullCircleDegrees * Constants.BallSpeedRatio); // Duration of spin seconds - multiply by ball speed ratio. TimeSpan ballSpinningDuration = _wheelSpinningAnimation1EndTime.Subtract(DateTime.Now); _ballSpinningAnimation1.Duration = TimeSpan.FromSeconds(ballSpinningDuration.TotalSeconds * Constants.BallSpeedRatio); // Ball falling animation. if (_ballFallingAnimation == null) { // Initialize the ball falling animation. _ballFallingAnimation = new DoubleAnimation(); _ballFallingAnimation.SpeedRatio = Constants.BallSpeedRatio; Storyboard.SetTargetName(_ballFallingAnimation, Constants.BallTranslateTransformName); Storyboard.SetTargetProperty(_ballFallingAnimation, new PropertyPath(TranslateTransform.YProperty)); _ballStoryBoard.Children.Add(_ballFallingAnimation); // Add the animation to the story board. } // Distance for the ball to fall in pixels. _ballFallingAnimation.From = _ballCenterPositionYPixels - _ballYOffsetPixels; // Start at the top-edge of the wheel. _ballFallingAnimation.To = _ballCenterPositionYPixels - (_ballYOffsetPixels * Constants.BallFallPercent); // Duration of fall in seconds - same as rotation duration. _ballFallingAnimation.Duration = _ballSpinningAnimation1.Duration; // Ball spinning animation 2. if (_ballSpinningAnimation2 == null) { // Initialize the ball spinning animation - it is a clone of the second wheel spinning animation. _ballSpinningAnimation2 = _wheelSpinningAnimation2.Clone(); Storyboard.SetTargetName(_ballSpinningAnimation2, Constants.BallRotateTransformName); Storyboard.SetTargetProperty(_ballSpinningAnimation2, new PropertyPath(RotateTransform.AngleProperty)); _ballStoryBoard.Children.Add(_ballSpinningAnimation2); // Add the animation to the story board. } // Begin time - begins after the first ball spinning animation. _ballSpinningAnimation2.BeginTime = TimeSpan.FromSeconds(ballSpinningDuration.TotalSeconds); // Ball sound. if (_ballMediaTimeLine == null) { _ballMediaTimeLine = new MediaTimeline(); //_ballMediaTimeLine.SpeedRatio = Constants.BallSpeedRatio; _ballMediaTimeLine.FillBehavior = FillBehavior.Stop; _ballMediaTimeLine.Source = new Uri(Directory.GetCurrentDirectory() + Constants.BallAudioFile, UriKind.Relative); Storyboard.SetTarget(_ballMediaTimeLine, _ballMediaElement); _ballStoryBoard.Children.Add(_ballMediaTimeLine); } //_ballMediaTimeLine.Duration = TimeSpan.FromSeconds(ballSpinningDuration.TotalSeconds * Constants.BallSpeedRatio); _ballMediaTimeLine.Duration = TimeSpan.FromSeconds(ballSpinningDuration.TotalSeconds); _ballControl.Visibility = Visibility.Visible; _ballStoryBoard.Begin(_ballControl, true); // Start the story board. OnBallTossed?.Invoke(true); // Publish the status of the ball. } catch (Exception ex) { throw new Exception("RouletteBall.TossBall(): " + ex.ToString()); } }