Inheritance: Element
        private void loadPiece(LevelObjectPiece piece)
        {
            ClearMainArea();

            objectFrameworkArea.Data.MainPanel.AddControl(new TextArea(25, 25, "Name: ") {Color = "black"});
            TextBox textBox = null;
            objectFrameworkArea.Data.MainPanel.AddControl(textBox = new TextBox(100, 5, 290, 25, piece.Name) {Color = "rgb(50,150,50)", Click = (p) => { piece.Name = textBox.Text; }});

            Button b = null;
            objectFrameworkArea.Data.MainPanel.AddControl(b = new Button(40, 160, 70, 25, "XFlip") {
                                                                                                           Color = "rgb(50,150,50)",
                                                                                                           Click = (p) => { piece.Xflip = b.Toggled; }
                                                                                                   });
            b.Toggle = true;
            b.Toggled = piece.Xflip;

            Button c = null;
            objectFrameworkArea.Data.MainPanel.AddControl(c = new Button(115, 160, 70, 25, "YFlip") {
                                                                                                            Color = "rgb(50,150,50)",
                                                                                                            Click = (p) => { piece.Yflip = c.Toggled; }
                                                                                                    });
            c.Toggle = true;
            c.Toggled = piece.Yflip;

            HScrollBox jd;
            objectFrameworkArea.Data.MainPanel.AddControl(jd = new HScrollBox(20, 35, 70, 4, 112) {BackColor = "rgb(50,60,127)"});
            ImageButton<LevelObjectAsset> bd = null;
            jd.Controls = new List<Element>();

            for (var i = 0; i < objectFrameworkArea.Data.ObjectFramework.Assets.Count; i++) {
                bd = new ImageButton<LevelObjectAsset>(objectFrameworkArea.Data.ObjectFramework.Assets[i], 0, 0, 0, 0);
                ImageButton<LevelObjectAsset> bd1 = bd;
                bd.Text = ( (Func<string>) ( () => bd1.Data.Name ) );

                bd.OnDraw = (canvas, x, y) => {
                                if (bd1.Data.Frames.Count == 0) return;
                                bd1.Data.Frames[0].DrawSimple(canvas, new Point(x, y), bd1.Width, bd1.Height - 15, piece.Xflip, piece.Yflip);
                            };
                bd.Click = (p) => {
                               for (var j = 0; j < jd.Controls.Count; j++) {
                                   if (jd.Controls[j] == bd1) {
                                       if (piece.AssetIndex == j)
                                           bd1.Toggled = true;

                                       piece.AssetIndex = j;
                                       continue;
                                   }
                                   ( (ImageButton) jd.Controls[j] ).Toggled = false;
                               }
                           };

                jd.AddControl(bd);
                bd.Toggle = true;
                if (piece.AssetIndex == i)
                    bd.Toggled = true;
            }
        }
        private void loadAsset(LevelObjectAsset asset)
        {
            ClearMainArea();

            objectFrameworkArea.Data.MainPanel.AddControl(new TextArea(25, 25, "Name: ") {Color = "black"});
            TextBox tb = null;
            objectFrameworkArea.Data.MainPanel.AddControl(tb = new TextBox(100, 5, 290, 25, asset.Name) {Color = "rgb(50,150,50)", Click = (p) => { asset.Name = tb.Text; }});

            objectFrameworkArea.Data.MainPanel.AddControl(new Button(400, 5, 100, 25, "Add Frame") {
                                                                                                           Color = "rgb(50,150,50)",
                                                                                                           Click = (p) => {
                                                                                                                       LevelObjectAssetFrame vs;
                                                                                                                       asset.Frames.Add(vs = new LevelObjectAssetFrame("Frame " + ( asset.Frames.Count + 1 )));
                                                                                                                       vs.Palette = new string[] {"000", "111", "222", "333", "444", "555", "666", "777", "888", "999", "AAA", "BBB", "CCC", "DDD", "EEE", "FFF"};
                                                                                                                       vs.Width = (int) ( Math.Floor(Math.Random() * 40) + 20 );
                                                                                                                       vs.Height = (int) ( Math.Floor(Math.Random() * 40) + 20 );
                                                                                                                       vs.ColorMap = new int[vs.Width][];
                                                                                                                       for (var i = 0; i < vs.Width; i++) {
                                                                                                                           vs.ColorMap[i] = new int[vs.Height];
                                                                                                                           for (var j = 0; j < vs.Height; j++) {
                                                                                                                               vs.ColorMap[i][j] = (int) Math.Floor(Math.Random() * vs.Palette.Length);
                                                                                                                           }
                                                                                                                       }

                                                                                                                       objectFrameworkArea.Data.MainPanel.Data.AssetPopulate(asset);
                                                                                                                   }
                                                                                                   });

            HScrollBox jd;
            objectFrameworkArea.Data.MainPanel.AddControl(jd = new HScrollBox(20, 35, 70, 4, 112) {BackColor = "rgb(50,60,127)"});
            objectFrameworkArea.Data.MainPanel.Data.AssetPopulate = (ast) => {
                                                                        jd.Controls = new List<Element>();
                                                                        foreach (LevelObjectAssetFrame t in ast.Frames) {
                                                                            ImageButton<LevelObjectAssetFrame> bd = null;
                                                                            bd = new ImageButton<LevelObjectAssetFrame>(null, 0, 0, 0, 0);

                                                                            bd.Text = ( (Func<string>) ( () => { return bd.Data.Name; } ) );
                                                                            bd.OnDraw = (canvas, x, y) =>
                                                                                        bd.Data.DrawSimple(canvas, new Point(x, y), bd.Width, bd.Height - 15, false, false);

                                                                            bd.Click = (p) => objectFrameworkArea.Data.MainPanel.Data.LoadFrame(bd.Data);

                                                                            jd.AddControl(bd);
                                                                            bd.Data = t;
                                                                        }
                                                                    };

            objectFrameworkArea.Data.MainPanel.Data.AssetPopulate(asset);

            objectFrameworkArea.Data.MainPanel.AddControl(objectFrameworkArea.Data.MainPanel.Data.FrameArea = new Panel<FrameAreaData>(new FrameAreaData(), 7, 155, 480, 350));
            objectFrameworkArea.Data.MainPanel.Data.FrameArea.Outline = false;

            objectFrameworkArea.Data.MainPanel.Data.LoadFrame = (frame) => {
                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.Controls = new List<Element>();
                                                                    //Data.MainPanel.Data.FrameArea.currentFrame = frame;
                                                                    //var ce;
                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(new TextArea(15, 21, "Name: ") {Color = "black"});
                                                                    TextBox textBox = null;
                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(textBox=new TextBox(90, 0, 395, 25, frame.Name??"") {
                                                                                                                                                                                 Color = "rgb(50,150,50)",
                                                                                                                                                                                 Click = (p) => { frame.Name = textBox.Text; }
                                                                                                                                                                         });


                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(new TextArea(15, 100, "Transparent Color: ") { Color = "black" });
                                                                    TextBox tb2=null;
                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(tb2 = new TextBox(15, 120, 395, 25, frame.TransparentColor ?? "")
                                                                    {
                                                                        Color = "rgb(50,150,50)",
                                                                        TextChanged = () => { frame.TransparentColor = tb2.Text;
                                                                        
                                                                        frame.ClearCache();
                                                                        }
                                                                    });

                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(new TextArea(0, 275, (Func<string>) ( () => { return "Width:  " + frame.Width; } )) {Color = "Black"});

                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(new Button(75, 275 - 25, 14, 17, "^") {
                                                                                                                                                                               Color = "rgb(50,150,50)",
                                                                                                                                                                               Click = (p) => { frame.SetWidth(frame.Width + 1); }
                                                                                                                                                                       });
                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(new Button(75, 275 - 5, 14, 20, "v") {
                                                                                                                                                                              Color = "rgb(50,150,50)",
                                                                                                                                                                              Click = (p) => { frame.SetWidth(frame.Width - 1); }
                                                                                                                                                                      });

                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(new TextArea(0, 320, ( (Func<string>) ( () => { return "Height: " + frame.Height; } ) )) {Color = "Black"});

                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(new Button(75, 320 - 25, 14, 17, "^") {
                                                                                                                                                                               Color = "rgb(50,150,50)",
                                                                                                                                                                               Click = (p) => { frame.SetHeight(frame.Height + 1); }
                                                                                                                                                                       });
                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(new Button(75, 320 - 5, 14, 20, "v") {
                                                                                                                                                                              Color = "rgb(50,150,50)",
                                                                                                                                                                              Click = (p) => { frame.SetHeight(frame.Height - 1); }
                                                                                                                                                                      });

                                                                    Button bt;
                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(bt = new Button(230 - 55, 35, 150, 25, "Collide Map") {
                                                                                                                                                                                               Color = "rgb(50,150,50)",
                                                                                                                                                                                               Click = (p) => {
                                                                                                                                                                                                           //    ce.showCollideMap = this.toggled;
                                                                                                                                                                                                       }
                                                                                                                                                                                       });
                                                                    bt.Toggle = true;
                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(bt = new Button(390 - 55, 35, 150, 25, "Hurt Map") {
                                                                                                                                                                                            Color = "rgb(50,150,50)",
                                                                                                                                                                                            Click = (p) => {
                                                                                                                                                                                                        //    ce.showHurtMap = this.toggled;
                                                                                                                                                                                                    }
                                                                                                                                                                                    });
                                                                    bt.Toggle = true;

                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(objectFrameworkArea.Data.MainPanel.Data.FrameArea.Data.ColorEditor = new ColorEditingArea(230 - 55, 65, 310, 225) {ShowOffset = true});
                                                                    var ce = objectFrameworkArea.Data.MainPanel.Data.FrameArea.Data.ColorEditor;
                                                                    ce.Init(frame);
                                                                    ce.Editor.ShowOutline = false;
                                                                    ce.Editable = false;
                                                                    ce.Click = (p) => { frame.SetOffset(p.X, p.Y); };
                                                                    /*
                                Data.MainPanel.Data.FrameArea.AddControl(new HtmlBox(19, 64, 120, 31, () =>
                                {
                                    var sc = document.getElementById("picFieldUploader");

                                    sc.style.left = (objectFrameworkArea.x + 320 + 7 + 19) + "px";
                                    sc.style.top = (objectFrameworkArea.y + 150 + 155 + 64) + "px";
                                    sc.style.position = "absolute";
                                    sc.style.visibility = "visible";
                                }, (x, y) =>
                                {
                                    var sc = document.getElementById("picFieldUploader");
                                    if (sc)
                                    {
                                        if (sc.style.left == x + "px" && sc.style.top == y + "px")
                                            return;
                                        sc.style.left = x + "px";
                                        sc.style.top = y + "px";
                                    }
                                }, () =>
                                {
                                    var sc = document.getElementById("picFieldUploader");
                                    if (sc)
                                    {
                                        sc.style.visibility = "visible";
                                    }
                                }, () =>
                                {
                                    var sc = document.getElementById("picFieldUploader");
                                    if (sc)
                                    {
                                        sc.style.left = "-100px";
                                        sc.style.top = "-100px";
                                        sc.style.visibility = "hidden";
                                    }
                                }));
                */

                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(objectFrameworkArea.Data.MainPanel.Data.FrameArea.Data.PalatteArea = new PaletteArea(230 - 55, 300) {Scale = new Point(39, 11), ShowCurrent = false});
                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.Data.PalatteArea.Init(frame.Palette, true);

                                                                    objectFrameworkArea.Data.MainPanel.Data.FrameArea.AddControl(new Button(230 - 55, 305 + 11 * 2, 310, 25, "Edit Map") {
                                                                                                                                                                                                 Color = "rgb(50,150,50)",
                                                                                                                                                                                                 Click = (p) => {
                                                                                                                                                                                                             SonicManager.Instance.UIManager.UIManagerAreas.ColorEditorArea.Data.Init(frame);
                                                                                                                                                                                                             SonicManager.Instance.UIManager.UIManagerAreas.ColorEditorArea.Visible = true;
                                                                                                                                                                                                             SonicManager.Instance.UIManager.UIManagerAreas.ColorEditorArea.Depth = /*this.Depth + 1*/ 10;
                                                                                                                                                                                                             objectFrameworkArea.LoseFocus();
                                                                                                                                                                                                         }
                                                                                                                                                                                         });
                                                                };
        }
        private void loadPieceLayout(LevelObjectPieceLayout pieceLayout)
        {
            ClearMainArea();

            objectFrameworkArea.Data.MainPanel.AddControl(new TextArea(25, 25, "Name= ") {Color = "black"});
            TextBox textBox = null;
            textBox = new TextBox(100, 5, 390, 25, pieceLayout.Name) {Color = "rgb(50,150,50)", Click = (p) => { pieceLayout.Name = textBox.Text; }};
            objectFrameworkArea.Data.MainPanel.AddControl(textBox);

            objectFrameworkArea.Data.MainPanel.AddControl(objectFrameworkArea.Data.MainPanel.Data.pe = new PieceLayoutEditor(145, 105, new Point(350, 280)));
            objectFrameworkArea.Data.MainPanel.Data.pe.Init(pieceLayout);

            objectFrameworkArea.Data.MainPanel.AddControl(objectFrameworkArea.Data.ListOfPieces = new ScrollBox(10, 105, 70, 5, 112) {BackColor = "rgb(50,60,127)"});

            HScrollBox selectPieceScroll;
            objectFrameworkArea.Data.MainPanel.AddControl(objectFrameworkArea.Data.MainPanel.Data.SelectPieceScroll = selectPieceScroll = new HScrollBox(145, 390, 70, 3, 112) {BackColor = "rgb(50,60,127)"});
            selectPieceScroll.Controls = new List<Element>();
            ;

            objectFrameworkArea.Data.MainPanel.AddControl(objectFrameworkArea.Data.MainPanel.Data.PriorityDrawing = new Button(148, 38, 140, 25, "Foreground") {
                                                                                                                                                                       Color = "rgb(50,150,50)",
                                                                                                                                                                       Click =
                                                                                                                                                                               (p) => {
                                                                                                                                                                                   objectFrameworkArea.Data.MainPanel.Data.pe.PieceLayoutMaker.SetPriority(
                                                                                                                                                                                           objectFrameworkArea.Data.MainPanel.Data.PriorityDrawing.Toggled);
                                                                                                                                                                               }
                                                                                                                                                               });
            objectFrameworkArea.Data.MainPanel.Data.PriorityDrawing.Toggle = true;

            for (var i = 0; i < objectFrameworkArea.Data.ObjectFramework.Pieces.Count; i++) {
                ImageButton<ObjectFrameworkAreaPiece> bdc = new ImageButton<ObjectFrameworkAreaPiece>(null, 0, 0, 0, 0);
                bdc.Text = ( (Func<string>) ( () => objectFrameworkArea.Data.ObjectFramework.Pieces[bdc.Data.index].Name ) );
                bdc.OnDraw = (canvas, x, y) => {
                                 var d = objectFrameworkArea.Data.ObjectFramework.Pieces[bdc.Data.index];
                                 var ast = objectFrameworkArea.Data.ObjectFramework.Assets[d.AssetIndex];
                                 if (ast.Frames.Count == 0) return;
                                 ast.Frames[0].DrawSimple(canvas, new Point(x, y), bdc.Width, bdc.Height - 15, d.Xflip, d.Yflip);
                             };
                bdc.Click = (p) => {
                                ( (ImageButton<LevelObjectPieceLayoutPiece>) objectFrameworkArea.Data.ListOfPieces.Controls[objectFrameworkArea.Data.MainPanel.Data.pe.PieceLayoutMaker.SelectedPieceIndex] ).Data.PieceIndex = bdc.Data.index;

                                foreach (ImageButton<ObjectFrameworkAreaPiece> t in selectPieceScroll.Controls) {
                                    if (t == bdc)
                                        t.Toggled = true;
                                    else
                                        t.Toggled = false;
                                }
                            };

                selectPieceScroll.AddControl(bdc);

                bdc.Data = new ObjectFrameworkAreaPiece {piece = pieceLayout.Pieces[0], index = i};
                bdc.Toggle = true;
                if (pieceLayout.Pieces.Count > 0)
                    bdc.Toggled = pieceLayout.Pieces[0].PieceIndex == i;
            }

            Button showB = null;
            objectFrameworkArea.Data.MainPanel.AddControl(showB = new Button(348, 38, 140, 25, "Show Images") {
                                                                                                                      Color = "rgb(50,150,50)",
                                                                                                                      Click = (p) => { objectFrameworkArea.Data.MainPanel.Data.pe.PieceLayoutMaker.ShowImages = showB.Toggled; }
                                                                                                              });
            showB.Toggle = true;

            objectFrameworkArea.Data.MainPanel.AddControl(new Button(348, 68, 140, 25, "Add Branch") {
                                                                                                             Color = "rgb(50,150,50)",
                                                                                                             Click = (p) => {
                                                                                                                         LevelObjectPieceLayoutPiece pc;
                                                                                                                         objectFrameworkArea.Data.MainPanel.Data.pe.PieceLayoutMaker.PieceLayout.Pieces.Add(
                                                                                                                                 pc = new LevelObjectPieceLayoutPiece((int) ( objectFrameworkArea.Data.ObjectFramework.Pieces.Count * Math.Random() )));
                                                                                                                         pc.X = (int) ( Math.Random() * objectFrameworkArea.Data.MainPanel.Data.pe.PieceLayoutMaker.PieceLayout.Width );
                                                                                                                         pc.Y = (int) ( Math.Random() * objectFrameworkArea.Data.MainPanel.Data.pe.PieceLayoutMaker.PieceLayout.Height );

                                                                                                                         objectFrameworkArea.Data.MainPanel.Data.pe.PieceLayoutMaker.SelectedPieceIndex = objectFrameworkArea.Data.MainPanel.Data.pe.PieceLayoutMaker.PieceLayout.Pieces.Count - 1;

                                                                                                                         buildleftScroll(pieceLayout);
                                                                                                                     }
                                                                                                     });

            buildleftScroll(pieceLayout);

/*
            Data.MainPanel.Data.UpdatePieces = () =>
            {
                ImageButton<ObjectFrameworkAreaPiece> df=null;
                for (var j = 0; j < Data.ListOfPieces.Controls.Count; j++)
                {


                    if (j == Data.MainPanel.Data.pe.PieceLayoutMaker.SelectedPieceIndex)
                    {
                        ((ImageButton<ObjectFrameworkAreaPiece>)Data.MainPanel.Data.SelectPieceScroll.Controls[j]).Toggled = true;
                        df = ((ImageButton<ObjectFrameworkAreaPiece>)Data.MainPanel.Data.SelectPieceScroll.Controls[j]);
                    }
                    else
                    {
                        ((ImageButton<ObjectFrameworkAreaPiece>)Data.MainPanel.Data.SelectPieceScroll.Controls[j]).Toggled = false;
                    }
                }

                for (var j = 0; j < Data.MainPanel.Data.SelectPieceScroll.Controls.Count; j++)
                {
                    df.Data.piece = ??this??;
                    if (df.Data.piece.PieceIndex == j)
                        ((ImageButton<ObjectFrameworkAreaPiece>)Data.MainPanel.Data.SelectPieceScroll.Controls[j]).Toggled = true;
                    else
                        ((ImageButton<ObjectFrameworkAreaPiece>)Data.MainPanel.Data.SelectPieceScroll.Controls[j]).Toggled = false;
                }


            };
*/
        }