Download Cyotek.ArcadeExplosionMaker.zip version 1.0.0.1, last updated 05/06/2012 (123.69 KB)

Download
  • md5: bd412009442f7923a555ba5b0f508fe0
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Xml;

namespace Cyotek.ArcadeExplosionMaker
{
  // Cyotek Arcade Explosion Maker
  // Copyright (c) 2012 Cyotek. All Rights Reserved.
  // http://cyotek.com

  // http://cyotek.com/blog/arcade-explosion-generator

  // If you use this code or output in your applications, attribution or donations are welcome.

  public class ExplosionParameters : INotifyPropertyChanged, IXmlPersistence
  {
    #region  Private Member Declarations

    private bool _antiAlias;
    private Color _backColor;
    private Color _blackoutColor;
    private bool _blackOutExpiredExplosions;
    private float _borderSize;
    private Color _color1;
    private Color _color2;
    private Color _color3;
    private ExplosionMaskMode _explosionMaskMode;
    private bool _fitToBounds;
    private int _frames;
    private Size _frameSize;
    private bool _growBorders;
    private int _grownIncrement;
    private int _growthStages;
    private int _maximumExplosionRadius;
    private int _maximumExplosions;
    private int _minimumExplosionRadius;
    private bool _randomOrder;
    private bool _removeExpiredExplosions;
    private int _seed;
    private int _spriteSheetColumns;

    #endregion  Private Member Declarations

    #region  Public Constructors

    public ExplosionParameters()
    {
      this.Frames = 8;
      this.FrameSize = new Size(32, 32);
      this.MinimumExplosionRadius = 4;
      this.MaximumExplosionRadius = 12;
      this.FitToBounds = true;
      this.Color1 = Color.FromArgb(255, 198, 0);
      this.Color2 = Color.FromArgb(255, 226, 66);
      this.Color3 = Color.FromArgb(255, 250, 188);
      this.BorderSize = 1F;
      this.BackColor = Color.Transparent;
      this.AntiAlias = false;
      this.GrowthStages = 3;
      this.GrowthIncrement = 10;
      this.MaximumExplosions = 4;
      this.RemoveExpiredExplosions = false;
      this.RandomOrder = true;
      this.Seed = -1;
      this.BlackOutExpiredExplosions = false;
      this.BlackoutColor = Color.Black;
      this.SpriteSheetColumns = 0;
      this.ExplosionMaskMode = ExplosionMaskMode.None;
    }

    #endregion  Public Constructors

    #region  Events

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion  Events

    #region  Public Properties

    [DefaultValue(false), Category("Appearance"), Description("Whether or not rendered graphics are anti aliased")]
    public virtual bool AntiAlias
    {
      get { return _antiAlias; }
      set { this.UpdateAssignment<bool>(ref _antiAlias, value, "SmoothingMode"); }
    }

    [DefaultValue(typeof(Color), "Transparent"), Category("Appearance"), Description("The default color of each animation frame")]
    public virtual Color BackColor
    {
      get { return _backColor; }
      set { this.UpdateAssignment<Color>(ref _backColor, value, "BackColor"); }
    }

    [DefaultValue(typeof(Color), "Black"), Category("Appearance"), Description("The color of blacked out explosions")]
    public virtual Color BlackoutColor
    {
      get { return _blackoutColor; }
      set { this.UpdateAssignment<Color>(ref _blackoutColor, value, "BlackoutColor"); }
    }

    [DefaultValue(false), Category("Appearance"), Description("Determines whether or not expired explosions are blacked out")]
    public virtual bool BlackOutExpiredExplosions
    {
      get { return _blackOutExpiredExplosions; }
      set { this.UpdateAssignment<bool>(ref _blackOutExpiredExplosions, value, "BlackOutExpiredExplosions"); }
    }

    [DefaultValue(1F), Category("Appearance"), Description("The border size of explosion rings")]
    public virtual float BorderSize
    {
      get { return _borderSize; }
      set { this.UpdateAssignment<float>(ref _borderSize, value, "BorderSize"); }
    }

    [DefaultValue(typeof(Color), "255, 198, 0"), Category("Appearance"), Description("The first explosion color")]
    public virtual Color Color1
    {
      get { return _color1; }
      set { this.UpdateAssignment<Color>(ref _color1, value, "Color1"); }
    }

    [DefaultValue(typeof(Color), "255, 226, 66"), Category("Appearance"), Description("The second explosion color")]
    public virtual Color Color2
    {
      get { return _color2; }
      set { this.UpdateAssignment<Color>(ref _color2, value, "Color2"); }
    }

    [DefaultValue(typeof(Color), "255, 250, 188"), Category("Appearance"), Description("The third explosion color")]
    public virtual Color Color3
    {
      get { return _color3; }
      set { this.UpdateAssignment<Color>(ref _color3, value, "Color3"); }
    }

    [DefaultValue(typeof(ExplosionMaskMode), "None"), Category("Appearance"), Description("Specifies how overlapping explosions will be drawn")]
    public virtual ExplosionMaskMode ExplosionMaskMode
    {
      get { return _explosionMaskMode; }
      set { this.UpdateAssignment<ExplosionMaskMode>(ref _explosionMaskMode, value, "ExplosionMaskMode"); }
    }

    [DefaultValue(true), Category("Appearance"), Description("whether or not explosions are constrained to stay within the bounds of the animation")]
    public virtual bool FitToBounds
    {
      get { return _fitToBounds; }
      set { this.UpdateAssignment<bool>(ref _fitToBounds, value, "FitToBounds"); }
    }

    [DefaultValue(8), Category("Behavior"), Description("The number of frames in the animation")]
    public virtual int Frames
    {
      get { return _frames; }
      set { this.UpdateAssignment<int>(ref _frames, value, "Frames"); }
    }

    [DefaultValue(typeof(Size), "32,32"), Category("Appearance"), Description("The size of each frame in the animation")]
    public virtual Size FrameSize
    {
      get { return _frameSize; }
      set { this.UpdateAssignment<Size>(ref _frameSize, value, "FrameSize"); }
    }

    [DefaultValue(false), Category("Behavior"), Description("Determines whether or not explosion borders scale")]
    public virtual bool GrowBorders
    {
      get { return _growBorders; }
      set { this.UpdateAssignment<bool>(ref _growBorders, value, "GrowBorders"); }
    }

    [DefaultValue(10), Category("Behavior"), Description("The percentage of the original size that each explosion grows by")]
    public virtual int GrowthIncrement
    {
      get { return _grownIncrement; }
      set { this.UpdateAssignment<int>(ref _grownIncrement, value, "GrowthIncrement"); }
    }

    [DefaultValue(3), Category("Behavior"), Description("The number of times an explosion grows and shrinks")]
    public virtual int GrowthStages
    {
      get { return _growthStages; }
      set { this.UpdateAssignment<int>(ref _growthStages, value, "GrowthStages"); }
    }

    [DefaultValue(12), Category("Appearance"), Description("The maxium radius of explosions")]
    public virtual int MaximumExplosionRadius
    {
      get { return _maximumExplosionRadius; }
      set { this.UpdateAssignment<int>(ref _maximumExplosionRadius, value, "MaximumExplosionRadius"); }
    }

    [DefaultValue(4), Category("Appearance"), Description("The maximum number of explosions visible at once")]
    public virtual int MaximumExplosions
    {
      get { return _maximumExplosions; }
      set { this.UpdateAssignment<int>(ref _maximumExplosions, value, "MaximumExplosions"); }
    }

    [DefaultValue(4), Category("Appearance"), Description("The minimum radius of explosions")]
    public virtual int MinimumExplosionRadius
    {
      get { return _minimumExplosionRadius; }
      set { this.UpdateAssignment<int>(ref _minimumExplosionRadius, value, "MinimumExplosionRadius"); }
    }

    [DefaultValue(true), Category("Behavior"), Description("Determines whether or not new insertions are randomly inserted")]
    public virtual bool RandomOrder
    {
      get { return _randomOrder; }
      set { this.UpdateAssignment<bool>(ref _randomOrder, value, "RandomOrder"); }
    }

    [DefaultValue(false), Category("Behavior"), Description("Determines whether expired explosions are removed allowing new explosions to be generated")]
    public virtual bool RemoveExpiredExplosions
    {
      get { return _removeExpiredExplosions; }
      set { this.UpdateAssignment<bool>(ref _removeExpiredExplosions, value, "RemoveExpiredExplosions"); }
    }

    [DefaultValue(-1), Category("Behavior"), Description("The seed used for the random generator")]
    public virtual int Seed
    {
      get { return _seed; }
      set { this.UpdateAssignment<int>(ref _seed, value, "Seed"); }
    }

    [DefaultValue(0), Category("Export"), Description("The number of columns in the generated sprite sheet")]
    public virtual int SpriteSheetColumns
    {
      get { return _spriteSheetColumns; }
      set { this.UpdateAssignment<int>(ref _spriteSheetColumns, value, "SpriteSheetColumns"); }
    }

    #endregion  Public Properties

    #region  Private Methods

    void IXmlPersistence.Read(string fileName, XmlNode reader)
    {
      this.Read(fileName, reader);
    }

    void IXmlPersistence.Write(string fileName, XmlWriter writer)
    {
      this.Write(fileName, writer);
    }

    #endregion  Private Methods

    #region  Protected Methods

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
      PropertyChangedEventHandler handler;

      handler = this.PropertyChanged;

      if (handler != null)
        handler(this, e);
    }

    protected virtual void Read(string fileName, XmlNode reader)
    {
      Type type;

      type = this.GetType();

      if (reader.Name != type.Name)
        throw new InvalidDataException("Unrecognized or unsupported file data.");

      foreach (XmlElement child in reader.ChildNodes)
      {
        PropertyInfo property;

        property = type.GetProperty(child.Name);

        if (property.PropertyType == typeof(Color))
          property.SetValue(this, ColorTranslator.FromHtml(child.InnerText), null);
        else if (property.PropertyType == typeof(Size))
          property.SetValue(this, new Size(Convert.ToInt32(child.Attributes["width"].Value), Convert.ToInt32(child.Attributes["height"].Value)), null);
        else if (property.PropertyType.IsEnum)
          property.SetValue(this, Enum.Parse(property.PropertyType, child.InnerText, true), null);
        else
          property.SetValue(this, Convert.ChangeType(child.InnerText, property.PropertyType), null);
      }
    }

    protected bool UpdateAssignment<T>(ref T value, T newValue, string propertyName)
    {
      bool result;

      if (!object.Equals(value, newValue))
      {
        value = newValue;

        this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        result = true;
      }
      else
        result = false;

      return result;
    }

    protected virtual void Write(string fileName, XmlWriter writer)
    {
      Type type;

      type = this.GetType();
      writer.WriteStartElement(type.Name);

      foreach (PropertyInfo property in type.GetProperties())
      {
        if (property.CanRead && property.CanWrite)
        {
          object rawValue;

          rawValue = property.GetValue(this, null);

          writer.WriteStartElement(property.Name);

          if (property.PropertyType == typeof(Color))
            writer.WriteValue(ColorTranslator.ToHtml((Color)rawValue));
          else if (property.PropertyType == typeof(Size))
          {
            writer.WriteAttributeString("width", ((Size)rawValue).Width.ToString());
            writer.WriteAttributeString("height", ((Size)rawValue).Height.ToString());
          }
          else
            writer.WriteValue(rawValue.ToString());

          writer.WriteEndElement();
        }
      }

      writer.WriteEndElement();
    }

    #endregion  Protected Methods
  }
}

Donate

Donate