<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FontEffectsLib.SpriteTypes;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using FontEffectsLib.FontTypes;
using FontEffectsLib.CoreTypes;

namespace FontEffectsLibSamples
{
    public class AchievementPanel : ComplexSprite, IStateful
    {
        public delegate bool ConditionChecker(GameTime currentGameTime);

        public static bool AreFiveCoinsInsert(GameTime currentGameTime)
        {
            return FontEffectsLibSamples.CoinsInserted == 5;
        }

        public event EventHandler AchievementEarned;

        public static bool IsNoCoinsInsertedAtOneMinute(GameTime currentGameTime)
        {
            return FontEffectsLibSamples.CoinsInserted == 0 && currentGameTime.TotalGameTime >= TimeSpan.FromMinutes(1);
        }

        public static bool AreHundredDollarsDonated(GameTime currentGameTime)
        {
            return FontEffectsLibSamples.CoinsInserted == 400;
        }

        public ConditionChecker Condition;

        public AchievementPanel(float startX, SpriteFont title, SpriteFont desc, string achievementDesc, GraphicsDevice dev)
            : base(Vector2.Zero)
        {
            GameFont titleObj = new GameFont(title, "Achievement!", new Vector2(5), Color.Yellow) { LayerDepth = 0.5f };
            GameFont descObj = new GameFont(desc, achievementDesc, new Vector2(2, titleObj.Size.Y + 5 + 4.5f), Color.White) { LayerDepth = 0.5f };
            Subsprites.Add(titleObj);
            Subsprites.Add(descObj);

            int achievePanelWidth = -1;
            int achievePanelHeight = -1;

            foreach (var spr in Subsprites)
            {
                Vector2 size = Vector2.Zero;

                if (!(spr is GameFont) && !(spr is GameSprite))
                {
                    continue;
                }
                if (spr is GameSprite)
                {
                    size = new Vector2((spr as GameSprite).Texture.Width, (spr as GameSprite).Texture.Height);
                }
                else
                {
                    size = (spr as GameFont).Size;
                }

                if (size.Y + spr.Position.Y > achievePanelHeight)
                {
                    achievePanelHeight = Convert.ToInt32(Math.Ceiling(size.Y + spr.Position.Y));
                }

                if (size.X + spr.Position.X > achievePanelWidth)
                {
                    achievePanelWidth = Convert.ToInt32(Math.Ceiling(size.X + spr.Position.X));
                }
            }

            titleObj.Position = new Vector2(achievePanelWidth / 2 - (titleObj.Size.X / 2), titleObj.Position.Y);
            descObj.Position = new Vector2(achievePanelWidth / 2 - (descObj.Size.X / 2), descObj.Position.Y);

            Subsprites.Add(GameSprite.CreateSolidColor(Vector2.Zero, achievePanelWidth + 5, achievePanelHeight + 10, Color.Black, dev));
            Subsprites[Subsprites.Count - 1].LayerDepth = 1;
            Subsprites.Insert(0, Subsprites[Subsprites.Count - 1]);
            Subsprites.RemoveAt(Subsprites.Count - 1);

            Position = new Vector2(startX, -achievePanelHeight);
            IsVisible = false;

            Width = achievePanelWidth;
            Height = achievePanelHeight;

            ChangeState(PanelState.Waiting);
        }

        public readonly int Height;
        public readonly int Width;

        protected Vector2 _velocity = Vector2.Zero;

        /// <summary>
        /// Gets or sets the velocity of this <see cref="AchievementPanel"/>, as in pixels moved per frame.
        /// </summary>
        public Vector2 Velocity
        {
            get { return _velocity; }
            set { _velocity = value; }
        }

        private float _colorDeteriorationfactor = 255;

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            _position += _velocity;

            if (_panelState == PanelState.Waiting && Condition != null && Condition(gameTime))
            {
                IsVisible = true;
                Velocity = new Vector2(0, 0.75f);
                ChangeState(PanelState.Sliding);
            }

            if (_panelState == PanelState.Sliding && Position.Y > 0)
            {
                Velocity = Vector2.Zero;
                if (AchievementEarned != null)
                {
                    AchievementEarned(this, EventArgs.Empty);
                }
                ChangeState(PanelState.Fading);
            }

            if (_panelState == PanelState.Fading)
            {
                TintColor = new Color(TintColor.ToVector4() - (Vector4.One / _colorDeteriorationfactor));
                _colorDeteriorationfactor *= 0.98f;

                if (TintColor.R <= 0 && TintColor.G <= 0 && TintColor.B <= 0 && TintColor.A <= 0)
                {
                    IsVisible = false;
                    TintColor = Color.White;
                    ChangeState(PanelState.Done);
                }
            }


        }

        private PanelState _panelState = PanelState.Waiting;

        public enum PanelState
        {
            Waiting,
            Sliding,
            Fading,
            Done
        }

        private void ChangeState(PanelState newState)
        {
            if (newState == _panelState)
            {
                return;
            }
            if (StateChanged != null)
            {
                StateChanged(this, new StateEventArgs(typeof(AchievementPanel.PanelState), newState));
            }
            _panelState = newState;
        }

        public event EventHandler<StateEventArgs> StateChanged;
    }
}
<commit_msg>Better documentation of sample AchievementPanel<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FontEffectsLib.SpriteTypes;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using FontEffectsLib.FontTypes;
using FontEffectsLib.CoreTypes;

namespace FontEffectsLibSamples
{
    /// <summary>
    /// Example implementation of ComplexSprite.
    /// Uses 3 sprites: An "Achievement!" title, a description text field, and a black background.
    /// Also raises events upon state change.
    /// </summary>
    public class AchievementPanel : ComplexSprite, IStateful
    {
        public delegate bool ConditionChecker(GameTime currentGameTime);

        public static bool AreFiveCoinsInsert(GameTime currentGameTime)
        {
            return FontEffectsLibSamples.CoinsInserted == 5;
        }

        public event EventHandler AchievementEarned;

        public static bool IsNoCoinsInsertedAtOneMinute(GameTime currentGameTime)
        {
            return FontEffectsLibSamples.CoinsInserted == 0 && currentGameTime.TotalGameTime >= TimeSpan.FromMinutes(1);
        }

        public static bool AreHundredDollarsDonated(GameTime currentGameTime)
        {
            return FontEffectsLibSamples.CoinsInserted == 400;
        }

        public ConditionChecker Condition;

        public AchievementPanel(float startX, SpriteFont title, SpriteFont desc, string achievementDesc, GraphicsDevice dev)
            : base(Vector2.Zero)
        {
            // Create "Achievement!" and description GameFonts
            GameFont titleObj = new GameFont(title, "Achievement!", new Vector2(5), Color.Yellow) { LayerDepth = 0.5f };
            GameFont descObj = new GameFont(desc, achievementDesc, new Vector2(2, titleObj.Size.Y + 5 + 4.5f), Color.White) { LayerDepth = 0.5f };
            Subsprites.Add(titleObj);
            Subsprites.Add(descObj);

            int achievePanelWidth = -1;
            int achievePanelHeight = -1;


            // Positioning logic for text elements
            foreach (var spr in Subsprites)
            {
                Vector2 size = Vector2.Zero;

                if (!(spr is GameFont) && !(spr is GameSprite))
                {
                    continue;
                }
                if (spr is GameSprite)
                {
                    size = new Vector2((spr as GameSprite).Texture.Width, (spr as GameSprite).Texture.Height);
                }
                else
                {
                    size = (spr as GameFont).Size;
                }

                if (size.Y + spr.Position.Y > achievePanelHeight)
                {
                    achievePanelHeight = Convert.ToInt32(Math.Ceiling(size.Y + spr.Position.Y));
                }

                if (size.X + spr.Position.X > achievePanelWidth)
                {
                    achievePanelWidth = Convert.ToInt32(Math.Ceiling(size.X + spr.Position.X));
                }
            }

            titleObj.Position = new Vector2(achievePanelWidth / 2 - (titleObj.Size.X / 2), titleObj.Position.Y);
            descObj.Position = new Vector2(achievePanelWidth / 2 - (descObj.Size.X / 2), descObj.Position.Y);

            // Create a solid color background
            Subsprites.Insert(0, GameSprite.CreateSolidColor(Vector2.Zero, achievePanelWidth + 5, achievePanelHeight + 10, Color.Black, dev));
            Subsprites[0].LayerDepth = 1;

            Position = new Vector2(startX, -achievePanelHeight);
            IsVisible = false;

            Width = achievePanelWidth;
            Height = achievePanelHeight;

            ChangeState(PanelState.Waiting);
        }

        public readonly int Height;
        public readonly int Width;

        protected Vector2 _velocity = Vector2.Zero;

        /// <summary>
        /// Gets or sets the velocity of this <see cref="AchievementPanel"/>, as in pixels moved per frame.
        /// </summary>
        public Vector2 Velocity
        {
            get { return _velocity; }
            set { _velocity = value; }
        }

        private float _colorDeteriorationfactor = 255;

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            _position += _velocity;

            if (_panelState == PanelState.Waiting && Condition != null && Condition(gameTime))
            {
                IsVisible = true;
                Velocity = new Vector2(0, 0.75f);
                ChangeState(PanelState.Sliding);
            }

            if (_panelState == PanelState.Sliding && Position.Y > 0)
            {
                Velocity = Vector2.Zero;
                if (AchievementEarned != null)
                {
                    AchievementEarned(this, EventArgs.Empty);
                }
                ChangeState(PanelState.Fading);
            }

            if (_panelState == PanelState.Fading)
            {
                TintColor = new Color(TintColor.ToVector4() - (Vector4.One / _colorDeteriorationfactor));
                _colorDeteriorationfactor *= 0.98f;

                if (TintColor.R <= 0 && TintColor.G <= 0 && TintColor.B <= 0 && TintColor.A <= 0)
                {
                    IsVisible = false;
                    TintColor = Color.White;
                    ChangeState(PanelState.Done);
                }
            }


        }

        private PanelState _panelState = PanelState.Waiting;

        public enum PanelState
        {
            Waiting,
            Sliding,
            Fading,
            Done
        }

        private void ChangeState(PanelState newState)
        {
            if (newState == _panelState)
            {
                return;
            }
            if (StateChanged != null)
            {
                StateChanged(this, new StateEventArgs(typeof(AchievementPanel.PanelState), newState));
            }
            _panelState = newState;
        }

        public event EventHandler<StateEventArgs> StateChanged;
    }
}

{'fraction_non_alphanumeric': 0.08638942999915304, 'fraction_numerical': 0.01092572202930465, 'mean_word_length': 1.6714932126696833, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4229232', 'n_tokens_mistral': 3239, 'n_tokens_neox': 3007, 'n_words': 690}