public MainWindow()
 {
     InitializeComponent();
     HasJar = PieceImage.Init();
     if (HasJar)
     {
         PreviewLabel.Text  = "";
         PreviewLabel.Image = SpellImage.BlankImage;
     }
     //buttonDownloadPsi.Text = "Download psi-" + PsiJarHelper.GetLatestPsiVersion("1.9") + ".jar";
     LoadAllSpells();
 }
        /// <summary>
        /// Render a spell preview
        /// </summary>
        /// <param name="spell">The spell to render</param>
        /// <returns>An Image of the spell's grid</returns>
        public static Image RenderSpell(Spell spell)
        {
            NbtCompound spellNbt = null;

            try
            {
                spellNbt = (NbtCompound)TextNbtParser.Parse(spell.Source);
            }
            catch (Exception)
            {
                //Console.WriteLine(e);
            }
            if (spellNbt == null)
            {
                return(BlankImage);
            }

            NbtList piecesNbt = (NbtList)spellNbt["spellList"];

            Piece[] pieces = new Piece[piecesNbt.Count];
            int     index  = 0;

            foreach (NbtCompound p in piecesNbt)
            {
                pieces[index++] = new Piece(p);
            }

            Bitmap   image = new Bitmap(ImageWidth, ImageHeight);
            Graphics g     = Graphics.FromImage(image);

            g.Clear(BackgroundColor);

            //Piece pass
            foreach (Piece piece in pieces)
            {
                int    x   = piece.X;
                int    y   = piece.Y;
                string key = piece.Key;
                x *= (TileWidth + Spacing);
                y *= (TileHeight + Spacing);

                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                g.DrawImage(PieceImage.Get(key), x, y, TileWidth, TileHeight);

                if (key == "constantNumber")
                {
                    string value = piece.ConstantValue;
                    g.DrawString(value,
                                 new Font(FontFamily.GenericMonospace, 40 / value.Length),
                                 new SolidBrush(Color.White),
                                 new RectangleF(x, y, TileWidth, TileHeight));
                }
            }

            //Connector pass
            foreach (Piece piece in pieces)
            {
                int    x    = piece.X;
                int    y    = piece.Y;
                string key  = piece.Key;
                int    pixx = x * (TileWidth + Spacing);
                int    pixy = y * (TileHeight + Spacing);
                if (key == "connector")
                {
                    g.DrawImage(PieceImage.ConnectorImages[(int)piece.Parameters[0]], pixx, pixy, TileWidth, TileHeight);

                    foreach (Piece.Side i in Enum.GetValues(typeof(Piece.Side)))
                    {
                        bool con = piece.HasConnection(pieces, i);
                        if (con)
                        {
                            g.DrawImage(PieceImage.ConnectorImages[(int)i], pixx, pixy, TileWidth, TileHeight);
                        }
                    }
                }
            }

            //Parameter pass
            foreach (Piece piece in pieces)
            {
                int    x   = piece.X;
                int    y   = piece.Y;
                string key = piece.Key;
                x *= (TileWidth + Spacing);
                y *= (TileHeight + Spacing);
                Piece.Side[] par = piece.Parameters;
                if (par != null)
                {
                    foreach (int param in par)
                    {
                        if (param > 0 && param <= 4)
                        {
                            Point p = new Point(x, y);
                            p.Offset(ParameterOffset[param]);
                            g.DrawImage(PieceImage.ParameterImages[param], new Rectangle(p, new Size(TileWidth / 2, TileHeight / 2)));
                        }
                    }
                }
            }
            return(image);
        }