<commit_before>﻿using Microsoft.Xna.Framework;
using System;

namespace MonoVarmint.Widgets
{
    //--------------------------------------------------------------------------------------
    /// <summary>
    /// VarmintAnimation -       simple class for providing animations.  This class handles
    ///                          thinking about the delta and then calls out to the subclass
    ///                          to actually perform the animation
    /// </summary>
    //--------------------------------------------------------------------------------------
    public class VarmintAnimation
    {
        private readonly double _animationDurationSeconds;
        private TimeSpan? _animationStartTime;

        /// <summary>
        /// Represents an action to take when animating
        /// </summary>
        /// <param name="delta">The number of seconds for infinite durations, the progress from 0 to 1 for finite durations</param>
        protected delegate void AnimateAction(double delta);

        protected AnimateAction Animate;
        
        /// <summary>
        /// IsComplete
        /// </summary>
        public bool IsComplete { get; internal set; }

        /// <summary>
        /// OnComplete
        /// </summary>
        public event Action OnComplete;

        //--------------------------------------------------------------------------------------
        /// <summary>
        /// Instantiates a new instance of the VarmintAnimation class with the given duration
        /// </summary>
        /// <param name="durationSeconds">The number of seconds the animation will last, with 0 meaning an infinite duration</param>
        //--------------------------------------------------------------------------------------
        public VarmintAnimation(double durationSeconds)
        {
            _animationDurationSeconds = durationSeconds;
        }

        //--------------------------------------------------------------------------------------
        /// <summary>
        /// Reset - put defaults back and clear any attached events
        /// </summary>
        //--------------------------------------------------------------------------------------
        public void Reset()
        {
            if(OnComplete != null)
            {
                foreach(Action handler in OnComplete.GetInvocationList())
                {
                    OnComplete -= handler;
                }
            }

            IsComplete = false;
            _animationStartTime = null;
        }

        //--------------------------------------------------------------------------------------
        /// <summary>
        /// Update - calculates the animation delta and calls the attached animation code
        /// </summary>
        //--------------------------------------------------------------------------------------
        internal void Update(GameTime gameTime)
        {
            if (IsComplete) return;
            if (_animationStartTime == null) _animationStartTime = gameTime.TotalGameTime;
            var animationProgressSeconds = (gameTime.TotalGameTime - _animationStartTime.Value).TotalSeconds;
            if (_animationDurationSeconds > 0)
            {
                Animate(animationProgressSeconds / _animationDurationSeconds);
                if (animationProgressSeconds >= _animationDurationSeconds)
                {
                    OnComplete?.Invoke();
                    IsComplete = true;
                }
            }
            else
            {
                Animate(animationProgressSeconds);
            }
        }

        //--------------------------------------------------------------------------------------
        /// <summary>
        /// Finish - call this to force an animation to complete
        /// </summary>
        //--------------------------------------------------------------------------------------
        internal void Finish(VarmintWidget widget)
        {
            if (_animationDurationSeconds > 0) Update(new GameTime(TimeSpan.MaxValue, TimeSpan.MaxValue));<commit_msg>Guarantee that VarmintAnimation deltas are between 0 and 1<commit_after>﻿using Microsoft.Xna.Framework;
using System;

namespace MonoVarmint.Widgets
{
    //--------------------------------------------------------------------------------------
    /// <summary>
    /// VarmintAnimation -       simple class for providing animations.  This class handles
    ///                          thinking about the delta and then calls out to the subclass
    ///                          to actually perform the animation
    /// </summary>
    //--------------------------------------------------------------------------------------
    public class VarmintAnimation
    {
        private readonly double _animationDurationSeconds;
        private TimeSpan? _animationStartTime;

        /// <summary>
        /// Represents an action to take when animating
        /// </summary>
        /// <param name="delta">The number of seconds for infinite durations, the progress from 0 to 1 for finite durations</param>
        protected delegate void AnimateAction(double delta);

        protected AnimateAction Animate;
        
        /// <summary>
        /// IsComplete
        /// </summary>
        public bool IsComplete { get; internal set; }

        /// <summary>
        /// OnComplete
        /// </summary>
        public event Action OnComplete;

        //--------------------------------------------------------------------------------------
        /// <summary>
        /// Instantiates a new instance of the VarmintAnimation class with the given duration
        /// </summary>
        /// <param name="durationSeconds">The number of seconds the animation will last, with 0 meaning an infinite duration</param>
        //--------------------------------------------------------------------------------------
        public VarmintAnimation(double durationSeconds)
        {
            _animationDurationSeconds = durationSeconds;
        }

        //--------------------------------------------------------------------------------------
        /// <summary>
        /// Reset - put defaults back and clear any attached events
        /// </summary>
        //--------------------------------------------------------------------------------------
        public void Reset()
        {
            if(OnComplete != null)
            {
                foreach(Action handler in OnComplete.GetInvocationList())
                {
                    OnComplete -= handler;
                }
            }

            IsComplete = false;
            _animationStartTime = null;
        }

        //--------------------------------------------------------------------------------------
        /// <summary>
        /// Update - calculates the animation delta and calls the attached animation code
        /// </summary>
        //--------------------------------------------------------------------------------------
        internal void Update(GameTime gameTime)
        {
            if (IsComplete) return;
            if (_animationStartTime == null) _animationStartTime = gameTime.TotalGameTime;
            var animationProgressSeconds = (gameTime.TotalGameTime - _animationStartTime.Value).TotalSeconds;
            if (_animationDurationSeconds > 0)
            {
                Animate(Math.Min(Math.Max(0, animationProgressSeconds / _animationDurationSeconds), 1));
                if (animationProgressSeconds >= _animationDurationSeconds)
                {
                    OnComplete?.Invoke();
                    IsComplete = true;
                }
            }
            else
            {
                Animate(animationProgressSeconds);
            }
        }

        //--------------------------------------------------------------------------------------
        /// <summary>
        /// Finish - call this to force an animation to complete
        /// </summary>
        //--------------------------------------------------------------------------------------
        internal void Finish(VarmintWidget widget)
        {
            if (_animationDurationSeconds > 0) Update(new GameTime(TimeSpan.MaxValue, TimeSpan.MaxValue));
{'fraction_non_alphanumeric': 0.27748881093504296, 'fraction_numerical': 0.001693480101608806, 'mean_word_length': 0.9081467805215786, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3613354', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1481, 'n_words': 441}