/// <summary> /// The WheelStopped method is called when the wheel stops spinning. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WheelStopped(object sender, EventArgs e) { OnWheelSpinning?.Invoke(false); // Publish the status of the wheel - the wheel is no longer spinning. }
/// <summary> /// The SpinWheel method is called to spin the wheel. /// </summary> public void SpinWheel() { try { if (_wheelStoryBoard == null) { // Initialize the story board. _wheelStoryBoard = new Storyboard(); _wheelStoryBoard.Completed += new EventHandler(WheelStopped); } else if (_wheelStoryBoard.GetCurrentState(_wheelControl) == ClockState.Stopped || _wheelStoryBoard.GetIsPaused(_wheelControl)) { return; // Wheel is already spinning or is paused - do not spin. } // Wheel spinning animation 1. if (_wheelSpinningAnimation1 == null) { // Initialize the animation. _wheelSpinningAnimation1 = new DoubleAnimation(); _wheelSpinningAnimation1.SpeedRatio = Constants.WheelSpeedRatio; _wheelSpinningAnimation1.FillBehavior = FillBehavior.HoldEnd; Storyboard.SetTargetName(_wheelSpinningAnimation1, Constants.WheelRotateTransformName); Storyboard.SetTargetProperty(_wheelSpinningAnimation1, new PropertyPath(RotateTransform.AngleProperty)); _wheelStoryBoard.Children.Add(_wheelSpinningAnimation1); // Add the animation to the story board. } // Number of rotations in total degrees - multiply by wheel speed ratio (negative degrees for counter clockwise). double wheelEndAngleDegrees = -1 * _randomNumberGenerator.Next(Constants.MinimumWheelSpinDistanceDegrees, Constants.MaximumWheelSpinDistanceDegrees); _wheelSpinningAnimation1.By = wheelEndAngleDegrees * Constants.WheelSpeedRatio; // Duration of spin seconds - multiply by wheel speed ratio. double spinDurationSeconds = Constants.WheelSpinDurationSeconds; _wheelSpinningAnimation1.Duration = TimeSpan.FromSeconds(spinDurationSeconds * Constants.WheelSpeedRatio); _wheelSpinningAnimation1EndTime = DateTime.Now.AddSeconds(spinDurationSeconds); // Wheel spinning animation 2. if (_wheelSpinningAnimation2 == null) { // Initialize the animation. _wheelSpinningAnimation2 = new DoubleAnimation(); _wheelSpinningAnimation2.SpeedRatio = Constants.WheelSpeedRatio; _wheelSpinningAnimation2.DecelerationRatio = Constants.StoppingDecelerationRatio; _wheelSpinningAnimation2.FillBehavior = FillBehavior.HoldEnd; _wheelSpinningAnimation2.By = -1 * Constants.StoppingSpinDistanceDegrees; _wheelSpinningAnimation2.Duration = TimeSpan.FromSeconds(spinDurationSeconds * Constants.WheelSpeedRatio); Storyboard.SetTargetName(_wheelSpinningAnimation2, Constants.WheelRotateTransformName); Storyboard.SetTargetProperty(_wheelSpinningAnimation2, new PropertyPath(RotateTransform.AngleProperty)); _wheelStoryBoard.Children.Add(_wheelSpinningAnimation2); // Add the animation to the story board. } // Begin time - begins after the first wheel spinning animation. _wheelSpinningAnimation2.BeginTime = TimeSpan.FromSeconds(spinDurationSeconds); // Select a winning number, and retrieve the corresponding winning pocket. int number = _randomNumberGenerator.Next(Constants.MinimumWinningNumber, Constants.MaximumWinningNumber); _winningNumber = Pockets.SingleOrDefault(x => x.Number == number); double winningNumberStartAngleDegrees = _winningNumber.AngularPositionDegrees + ((RotateTransform)_wheelControl.RenderTransform).Angle; _winningNumberEndAngleDegrees = CorrectAngleDegrees(winningNumberStartAngleDegrees + (double)_wheelSpinningAnimation1.By); _wheelStoryBoard.Begin(_wheelControl, true); // Start the story board. OnWheelSpinning?.Invoke(true); // Publish the status of the wheel. } catch (Exception ex) { throw new Exception("RouletteWheel.SpinWheel(): " + ex.ToString()); } }