context: /*  ----------------------------------------------------------------------------
   *  Copyright (C) 2011 XfsGames <http://www.xfsgames.com.ar/>
   *  ----------------------------------------------------------------------------
   *  Img Tools
   *  ----------------------------------------------------------------------------
   *  File:       AnimationBox.cs
   *  Author:     CARLOSX
   *  ----------------------------------------------------------------------------
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   */
  using System;
  using System.ComponentModel;
  using System.Drawing;
  using System.Drawing.Imaging;
  using System.IO;
  using System.Windows.Forms;
  using System.Runtime.InteropServices;
  
  namespace ImgTools
  {
  
      public class AnimationBox : UserControl
      {
  
          private ContextMenu cmOptions;
          private IContainer components;
          private Animation[] m_Animations;
          private DateTime m_Begin;
          private string m_FileName;
          private Label m_Label;
          private string m_Title;
          private int m_xCenter;
          private int m_yCenter;
          private MenuItem miOptionsCenter;
          private MenuItem miOptionsLoop;
          private MenuItem miOptionsPlay;
          private MenuItem miOptionsSave;
          private MenuItem miOptionsSep;
          private MenuItem miOptionsStop;
          private Timer timRedraw;
  
          private static TimeSpan Interval;
  
          public Animation[] Animations
          {
              get
              {
                  return m_Animations;
              }
              set
              {
                  m_Animations = value;
              }
          }
  
          public string FileName
          {
              get
              {
                  return m_FileName;
              }
              set
              {
                  m_FileName = value;
              }
          }
  
          public string Title
          {
              get
              {
                  return m_Title;
              }
              set
              {
                  if (m_Title == value)
                  {
                  }
                  else
                  {
                      m_Title = value;
                      if (m_Title != null)
                      {
                          if (m_Label == null)
                          {
                              m_Label = new Label();
                              Size size = ClientSize;
                              m_Label.SetBounds(4, 4, size.Width - 8, 0x10);
                              m_Label.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                              m_Label.TextAlign = ContentAlignment.MiddleCenter;
                              Controls.Add(m_Label);
                          }
                          m_Label.Text = m_Title;
                      }
                      else if (m_Label != null)
                      {
                          Controls.Remove(m_Label);
                          m_Label.Dispose();
                          m_Label = null;
                      }
                  }
              }
          }
  
          public AnimationBox()
          {
              InitializeComponent();
              SetStyle(ControlStyles.UserPaint | ControlStyles.Opaque | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
              m_Begin = DateTime.Now;
          }
  
          static AnimationBox()
          {
              AnimationBox.Interval = TimeSpan.FromMilliseconds(50.0);
          }
  
          private void InitializeComponent()
          {
              this.components = new System.ComponentModel.Container();
              this.timRedraw = new System.Windows.Forms.Timer(this.components);
              this.cmOptions = new System.Windows.Forms.ContextMenu();
              this.miOptionsPlay = new System.Windows.Forms.MenuItem();
              this.miOptionsStop = new System.Windows.Forms.MenuItem();
              this.miOptionsLoop = new System.Windows.Forms.MenuItem();
              this.miOptionsCenter = new System.Windows.Forms.MenuItem();
              this.miOptionsSep = new System.Windows.Forms.MenuItem();
              this.miOptionsSave = new System.Windows.Forms.MenuItem();
              this.SuspendLayout();
              // 
              // timRedraw
              // 
              this.timRedraw.Interval = 30;
              this.timRedraw.Tick += new System.EventHandler(this.timRedraw_Tick);
              // 
              // cmOptions
              // 
              this.cmOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
              this.miOptionsPlay,
              this.miOptionsStop,
              this.miOptionsLoop,
              this.miOptionsCenter,
              this.miOptionsSep,
              this.miOptionsSave});
              // 
              // miOptionsPlay
              // 
              this.miOptionsPlay.Enabled = false;
              this.miOptionsPlay.Index = 0;
              this.miOptionsPlay.Text = "&Play";
              this.miOptionsPlay.Click += new System.EventHandler(this.miOptionsPlay_Click);
              // 
              // miOptionsStop
              // 
              this.miOptionsStop.Index = 1;
              this.miOptionsStop.Text = "&Stop";
              this.miOptionsStop.Click += new System.EventHandler(this.miOptionsStop_Click);
              // 
              // miOptionsLoop
              // 
              this.miOptionsLoop.Checked = true;
              this.miOptionsLoop.Index = 2;
              this.miOptionsLoop.Text = "&Loop";
              this.miOptionsLoop.Click += new System.EventHandler(this.miOptionsLoop_Click);
              // 
              // miOptionsCenter
              // 
              this.miOptionsCenter.Index = 3;
              this.miOptionsCenter.Text = "Show &Center";
              this.miOptionsCenter.Click += new System.EventHandler(this.miOptionsCenter_Click);
              // 
              // miOptionsSep
              // 
              this.miOptionsSep.Index = 4;
              this.miOptionsSep.Text = "-";
              // 
              // miOptionsSave
              // 
              this.miOptionsSave.Index = 5;
              this.miOptionsSave.Text = "&Save As";
              this.miOptionsSave.Click += new System.EventHandler(this.miOptionsSave_Click);
              // 
              // AnimationBox
              // 
              this.ContextMenu = this.cmOptions;
              this.Name = "AnimationBox";
              this.Size = new System.Drawing.Size(484, 452);
              this.ResumeLayout(false);
  
          }
  
          private void miOptionsCenter_Click(object sender, EventArgs e)
          {
              miOptionsCenter.Checked = !miOptionsCenter.Checked;
              Invalidate();
          }
  
          private void miOptionsLoop_Click(object sender, EventArgs e)
          {
              miOptionsLoop.Checked = !miOptionsLoop.Checked;
          }
  
          private void miOptionsPlay_Click(object sender, EventArgs e)
          {
              miOptionsPlay.Enabled = false;
              miOptionsStop.Enabled = true;
              m_Begin = DateTime.Now;
          }
  
          private void miOptionsSave_Click(object sender, EventArgs e)
          {
              SaveFileDialog dialog;
              ImageFormat png;
              int num;
              if (this.m_Animations != null)
              {
                  dialog = new SaveFileDialog
                  {
                      OverwritePrompt = true,
                      Filter = "All Supported Files|*.png;*.bmp;*.gif;*.jpg|PNG Files (*.png)|*.png|BMP Files (*.bmp)|*.bmp|GIF Files (*.gif)|*.gif|JPEG Files (*.jpg)|*.jpg",
                      FileName = (this.m_FileName == null) ? "Image" : this.m_FileName,
                      Title = "Save Image"
                  };
                  try
                  {
                      if (dialog.ShowDialog() == DialogResult.OK)
                      {
                          switch (Path.GetExtension(dialog.FileName).ToLower())
                          {
                              case ".png":
                                  png = ImageFormat.Png;
                                  goto Label_00E1;
  
                              case ".bmp":
                                  png = ImageFormat.Bmp;
                                  goto Label_00E1;
  
                              case ".gif":
                                  png = ImageFormat.Gif;
                                  goto Label_00E1;
  
                              case ".jpg":
                                  png = ImageFormat.Jpeg;
                                  goto Label_00E1;
                          }
                          MessageBox.Show("Unsupported image extension.");
                      }
                  }
                  catch (Exception ex)
                  {
                      Log.WriteLine(ex.Message);
                  }
              }
              return;
          Label_00E1:
              num = 0;
              int length = 0;
              int num3 = 0x7fffffff;
              int num4 = 0x7fffffff;
              int num5 = -2147483648;
              int num6 = -2147483648;
              for (int i = 0; i < this.m_Animations.Length; i++)
              {
                  Animation animation = this.m_Animations[i];
                  
                  Frame[] frames = animation.Frames;
                  num += frames.Length;
                  if (frames.Length > length)
                  {
                      length = frames.Length;
                  }
                  for (int j = 0; j < frames.Length; j++)
                  {
                      Frame frame = frames[j];
                      int centerX = frame.CenterX;
                      int centerY = frame.CenterY;
                      int num11 = centerX + frame.Image.Width;
                      int num12 = centerY + frame.Image.Height;
                      if (centerX < num3)
                      {
                          num3 = centerX;
                      }
  
gt:                     if (centerY < num4)