public void Init(LevelObjectPieceLayout pieceLayout)
 {
     PieceLayout = pieceLayout;
     Width = Size.X;
     Height = Size.Y;
     PieceLayoutMaker = new PieceLayoutMaker(pieceLayout);
 }
 public PieceLayoutMaker(LevelObjectPieceLayout pieceLayout)
 {
     PieceLayout = pieceLayout;
     LineWidth = 1;
     CurrentColor = 0;
     ShowOutline = true;
     ShowImages = false;
     SelectedPieceIndex = 0;
     DraggingIndex = -1;
     ZeroPosition = new Point(0, 0);
 }
        private void buildleftScroll(LevelObjectPieceLayout pieceLayout)
        {
            objectFrameworkArea.Data.ListOfPieces.Controls = new List<Element>();
            for (var i = 0; i < pieceLayout.Pieces.Count; i++) {
                ImageButton<LevelObjectPieceLayoutPiece> bd = new ImageButton<LevelObjectPieceLayoutPiece>(null, 0, 0, 0, 0);
                bd.Text = (Func<string>) ( () => { return objectFrameworkArea.Data.ObjectFramework.Pieces[bd.Data.PieceIndex].Name; } );
                bd.OnDraw = (canvas, x, y) => {
                                var pc = objectFrameworkArea.Data.ObjectFramework.Pieces[bd.Data.PieceIndex];
                                var ast = objectFrameworkArea.Data.ObjectFramework.Assets[pc.AssetIndex];
                                if (ast.Frames.Count == 0) return;
                                ast.Frames[0].DrawSimple(canvas, new Point(x, y), bd.Width, bd.Height - 15, pc.Xflip, pc.Yflip);
                            };
                bd.Click = (p) => {
                               for (var j = 0; j < objectFrameworkArea.Data.ListOfPieces.Controls.Count; j++) {
                                   if (bd == objectFrameworkArea.Data.ListOfPieces.Controls[j]) {
                                       ( (ImageButton) objectFrameworkArea.Data.ListOfPieces.Controls[j] ).Toggled = true;
                                       objectFrameworkArea.Data.MainPanel.Data.pe.PieceLayoutMaker.SelectedPieceIndex = j;
                                   } else
                                       ( (ImageButton) objectFrameworkArea.Data.ListOfPieces.Controls[j] ).Toggled = false;
                               }

                               for (var j = 0; j < objectFrameworkArea.Data.MainPanel.Data.SelectPieceScroll.Controls.Count; j++) {
                                   var fm = ( (ImageButton<ObjectFrameworkAreaPiece>) objectFrameworkArea.Data.MainPanel.Data.SelectPieceScroll.Controls[j] );
                                   fm.Data.piece = bd.Data;
                                   fm.Toggled = ( j == pieceLayout.Pieces[objectFrameworkArea.Data.MainPanel.Data.pe.PieceLayoutMaker.SelectedPieceIndex].PieceIndex );
                               }
                           };
                objectFrameworkArea.Data.ListOfPieces.AddControl(bd);
                bd.Toggle = true;
                bd.Data = pieceLayout.Pieces[i];
                if (i == objectFrameworkArea.Data.MainPanel.Data.pe.PieceLayoutMaker.SelectedPieceIndex) bd.Toggled = true;
            }
        }
        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;
                }


            };
*/
        }