private void bezierContextMenuNewSurface(object sender, System.EventArgs e)
 {
     bezierSurface = new BezierSurface("new surface", "");
     bezierSurface.Triangulate((float)triang_Slider.Value);
     bezierUndo = new BezierUndoStack();
     RenderBezier();
 }
        // Koniec sekcji
        ////////////////////////////////////////////////////////////////////////////////////
        private void objectToScene_Drop(object sender, System.Windows.DragEventArgs e)
        {
            // Wciąganie obiektów do sceny
            if (tabWidok.SelectedIndex == 0)
            {
                if (e.Data.GetDataPresent("Object"))
                {
                    object data = e.Data.GetData("Object");
                    //Console.WriteLine(data.GetType());
                    //ViewportInfo coords = GetViewCoords(tabWidok.SelectedIndex);
                    Vector3 translation;
                    if (data is Shape_)
                    {
                        Shape_ shape = (Shape_)data;
                        undo.Save(currScene);
                        // przekształcenie współrzędnych
                        float x = (float)e.GetPosition(this).X - xOffset;
                        float y = (float)e.GetPosition(this).Y - yOffset;

                        translation = CalculateTranslation(x, y);
                        currScene.AddObject(shape.Triangulate((float)triang_Slider.Value), shape.Name, translation);
                        currScene.hierarchyChange = false;
                        initializeTreeView();

                        //sceneChange = true;
                    }
                    else if (data is PreparedElement)
                    {
                        undo.Save(currScene);
                        PreparedElement element = (PreparedElement)data;

                        // przekształcenie współrzędnych
                        float x = (float)e.GetPosition(this).X - xOffset;
                        float y = (float)e.GetPosition(this).Y - yOffset;

                        translation = CalculateTranslation(x, y);
                        //for (int i = 0; i < element.Scene.cams.Count; i++)
                        //{
                        //    cameraPan.comboBox1.Items.Add("Kamera " + (currScene.cams.Count() + 1 + i));
                        //}
                        currScene.AddPreparedElement(element, translation);
                        currScene.hierarchyChange = false;
                        initializeTreeView();

                        //sceneChange = true;
                    }
                    else if (data is Surface)
                    {
                        Surface surface = (Surface)data;
                        undo.Save(currScene);
                        bool zazn = true;

                        if (currScene.selTriangles.Count == 0)
                        {
                            zazn = false;
                            ViewportInfo views = GetViewCoords(tabWidok.SelectedIndex);
                            int x = (int)e.GetPosition(this).X - xOffset;
                            int y = (int)e.GetPosition(this).Y - yOffset;

                            ViewportOrientation viewport = GetViewportType(x, y);

                            if (viewport != ViewportOrientation.None)
                            {
                                ViewportType viewportType = viewport == ViewportOrientation.Perspective ? ViewportType.Perspective : ViewportType.Orto;
                                int rect = (int)viewport;
                                int orthoRect = rect == 3 ? 0 : rect;

                                SelectingElems.SelectElems(currScene, renderer.GetCamsPoints(), renderer.GetLightsPoints(), viewportType, new System.Drawing.Point(x - views.posX[rect], y - views.posY[rect]),
                                    new System.Drawing.Point(views.sizeX[rect], views.sizeY[rect]), new Vector2(renderer.OrthoWidth[orthoRect], (float)views.sizeY[rect] / views.sizeX[rect] * renderer.OrthoWidth[orthoRect]),
                                    renderer.OrthoPos[orthoRect], renderer.OrthoLookAt[orthoRect], false);
                            }
                        }

                        if (String.Compare(surface.Material.Name, "default", true) == 0)
                        {
                            Modeler.DialogBoxes.NameDialog newName = new NameDialog();
                            bool res = (bool)newName.Show("Podaj nową nazwę materiału", "");
                            if (!res)
                                return;
                            surface.Material.name = newName.Result;

                        }
                        bool replace = false;
                        while (currScene.ContainsMaterialName(surface.Material.Name) && !replace)
                        {
                            Modeler.DialogBoxes.NameDialog dialog = new NameDialog();
                            dialog.Owner = this;

                            bool result = (bool)dialog.Show(materialSceneMessage, surface.Material.Name);
                            string name = dialog.Result;
                            if (!result)
                                return;
                            if (surface.Material.Name == name)
                                replace = true;
                            surface.Material.name = name;
                            dialog.Close();
                        }

                        currScene.AddMaterial(surface.Material);
                        currScene.hierarchyChange = false;

                        if (!zazn) currScene.ClearSelectedTriangles();
                        //Console.WriteLine("Przypisanie atrybutów powierzchniowych");

                        //sceneChange = true;
                    }
                    else if (data is Material_)
                    {
                        Material_ material = new Material_((Material_)data);
                        material.colorR = material.colorR / 255;
                        material.colorG = material.colorG / 255;
                        material.colorB = material.colorB / 255;
                        undo.Save(currScene);
                        bool zazn = true;

                        if (currScene.selTriangles.Count == 0)
                        {
                            zazn = false;
                            ViewportInfo views = GetViewCoords(tabWidok.SelectedIndex);
                            int x = (int)e.GetPosition(this).X - xOffset;
                            int y = (int)e.GetPosition(this).Y - yOffset;

                            ViewportOrientation viewport = GetViewportType(x, y);

                            if (viewport != ViewportOrientation.None)
                            {
                                ViewportType viewportType = viewport == ViewportOrientation.Perspective ? ViewportType.Perspective : ViewportType.Orto;
                                int rect = (int)viewport;
                                int orthoRect = rect == 3 ? 0 : rect;

                                SelectingElems.SelectElems(currScene, renderer.GetCamsPoints(), renderer.GetLightsPoints(), viewportType, new System.Drawing.Point(x - views.posX[rect], y - views.posY[rect]),
                                    new System.Drawing.Point(views.sizeX[rect], views.sizeY[rect]), new Vector2(renderer.OrthoWidth[orthoRect], (float)views.sizeY[rect] / views.sizeX[rect] * renderer.OrthoWidth[orthoRect]),
                                    renderer.OrthoPos[orthoRect], renderer.OrthoLookAt[orthoRect], false);
                            }
                        }

                        if (String.Compare(material.Name, "default", true) == 0)
                        {
                            Modeler.DialogBoxes.NameDialog newName = new NameDialog();
                            bool res = (bool)newName.Show("Podaj nową nazwę materiału", "");
                            if (!res)
                                return;
                            material.name = newName.Result;

                        }
                        bool replace = false;
                        while (currScene.ContainsMaterialName(material.Name) && !replace)
                        {
                            Modeler.DialogBoxes.NameDialog dialog = new NameDialog();
                            dialog.Owner = this;

                            bool result = (bool)dialog.Show(materialSceneMessage, material.Name);
                            string name = dialog.Result;
                            if (!result)
                                return;
                            if (material.Name == name)
                                replace = true;
                            material.name = name;
                            dialog.Close();
                        }

                        currScene.AddMaterial(material);

                        if (!zazn) currScene.ClearSelectedTriangles();
                        //Console.WriteLine("Przypisanie atrybutów powierzchniowych");

                        //sceneChange = true;
                    }
                    else if (data is LightObj)
                    {
                        if (currScene.selLights.Count > 0)  //objętnie ile ich jest zaznaczonych zmienia tylko 1 sprawdzająć nazwe
                        {
                            undo.Save(currScene);
                            Light_ sceneLight = currScene.lights[currScene.selLights[0]];
                            Light_ dataLight = ((LightObj)data).Light;
                            sceneLight.colorR = dataLight.colorR;
                            sceneLight.colorG = dataLight.colorG;
                            sceneLight.colorB = dataLight.colorB;
                            sceneLight.enabled = dataLight.enabled;
                            sceneLight.goniometric = dataLight.goniometric;
                            sceneLight.innerAngle = dataLight.innerAngle;
                            sceneLight.name = dataLight.name;
                            sceneLight.outerAngle = dataLight.outerAngle;
                            sceneLight.power = dataLight.power;
                            sceneLight.type = dataLight.type;

                            //sceneChange = true;
                        }
                        else
                        {
                            LightObj light = (LightObj)data;
                            undo.Save(currScene);
                            Light_ sceneLight = new Light_(light.Light);

                            // przekształcenie współrzędnych
                            float x = (float)e.GetPosition(this).X - xOffset;
                            float y = (float)e.GetPosition(this).Y - yOffset;

                            translation = CalculateTranslation(x, y);

                            currScene.AddLight(sceneLight, translation);
                            currScene.hierarchyChange = false;
                            initializeTreeView();

                            //sceneChange = true;
                        }
                    }

                    else if (data is Light_)
                    {
                        if (currScene.selLights.Count > 0)
                        {
                            undo.Save(currScene);
                            Light_ sceneLight = currScene.lights[currScene.selLights[0]];
                            Light_ dataLight = (Light_)data;
                            sceneLight.colorR = dataLight.colorR;
                            sceneLight.colorG = dataLight.colorG;
                            sceneLight.colorB = dataLight.colorB;
                            sceneLight.enabled = dataLight.enabled;
                            sceneLight.goniometric = dataLight.goniometric;
                            sceneLight.innerAngle = dataLight.innerAngle;
                            sceneLight.name = dataLight.name;
                            sceneLight.outerAngle = dataLight.outerAngle;
                            sceneLight.power = dataLight.power;
                            sceneLight.type = dataLight.type;

                            //currScene.hierarchyChange = true;
                            currScene.hierarchyChange = false;
                            initializeTreeView();

                            //sceneChange = true;
                        }
                        else
                        {
                            Light_ light = (Light_)data;
                            undo.Save(currScene);
                            Light_ sceneLight = new Light_(light);

                            // przekształcenie współrzędnych
                            float x = (float)e.GetPosition(this).X - xOffset;
                            float y = (float)e.GetPosition(this).Y - yOffset;

                            translation = CalculateTranslation(x, y);

                            currScene.AddLight(sceneLight, translation);
                            //TO DO sprawdzenie czy nazwa jest unikatowa
                            //currScene.hierarchyChange = true;
                            currScene.hierarchyChange = false;
                            initializeTreeView();

                            //sceneChange = true;
                        }

                    }
                    else if (data is Camera)
                    {
                        Camera cam = (Camera)data;
                        undo.Save(currScene);

                        float x = (float)e.GetPosition(this).X - xOffset;
                        float y = (float)e.GetPosition(this).Y - yOffset;

                        translation = CalculateTranslation(x, y);

                        currScene.AddCamera(cam, translation);
                        cameraPan.comboBox1.Items.Add("Kamera " + (currScene.cams.Count()));
                        cameraPan.comboBox1.SelectedIndex = currScene.activeCamera;

                        //sceneChange = true;
                    }
                }
            }
            // Wciąganie obiektów do edytora powierzchni beziera.
            else if (tabWidok.SelectedIndex == 1)
            {
                if (e.Data.GetDataPresent("Object"))
                {
                    object data = e.Data.GetData("Object");

                    if (data is BezierSurface)
                    {
                        bezierSurface = new BezierSurface((BezierSurface)data);
                        bezierSurface.Triangulate((float)triang_Slider.Value);
                        RenderBezier();
                    }
                }
            }

            e.Handled = true;

            trNumTBG1.Text = currScene.triangles.Count().ToString();
            trNumTBG2.Text = currScene.triangles.Count().ToString();
            trNumTBG3.Text = currScene.triangles.Count().ToString();
            trNumTBG4.Text = currScene.triangles.Count().ToString();

            Renderer.RecalculateData(currScene);

            RenderViews();
            currScene.selectedHierObj = null;
        }
        private void podstawoweTab_Drop(object sender, System.Windows.DragEventArgs e)
        {
            if (e.Data.GetDataPresent("Bezier"))
            {
                //Modeler.DialogBoxes.BezierNameDialog dialog = new BezierNameDialog();
                object data = e.Data.GetData("Bezier");
                //Console.WriteLine(data.GetType());
                bool replace = false;
                if (data is BezierSurface)
                {
                    BezierSurface bezier = new BezierSurface((BezierSurface)data);
                    //bezier.Name = "Def";
                    if (String.Compare(bezier.Name, "default", true) == 0 ||
                        String.Compare(bezier.Name, "new surface", true) == 0)
                    {
                        Modeler.DialogBoxes.NameDialog newName = new NameDialog();
                        bool res = (bool)newName.Show("Podaj nową nazwę powierzchni", "");
                        if (!res)
                            return;
                        bezier.Name = newName.Result;

                    }
                    while (_shapesGallery.GetNames().Contains(bezier.Name, StringComparer.OrdinalIgnoreCase) && !replace)
                    {
                        bool result;
                        Modeler.DialogBoxes.NameDialog dialog = new NameDialog();
                        string name;
                        if (ShapeGallery.ReservedNames.Contains(bezier.Name, StringComparer.OrdinalIgnoreCase))
                        {
                            dialog.Owner = this;
                            result = (bool)dialog.Show("Nazwa powierzchni jest zarezerwowana dla podstawowego kształtu.", bezier.Name);
                            name = dialog.Result;
                            if (!result)
                                return;
                            bezier.Name = name;
                            dialog.Close();
                        }
                        else
                        {
                            dialog.Owner = this;
                            result = (bool) dialog.Show(bezierMessage, bezier.Name);
                            name = dialog.Result;
                            if (!result)
                                return;
                            if (bezier.Name == name)
                                replace = true;
                            bezier.Name = name;
                            dialog.Close();
                        }
                    }

                    bezier.Triangulate(1);
                    if (replace)
                    {
                        int idx = _shapesGallery.GetNameIndex(bezier.Name);
                        _shapesGallery.RemoveAt(idx);
                        _shapesGallery.SaveBezierToGallery(bezier, renderer.GetBezierImage(bezier));
                        _shapesGallery.Insert(idx, bezier);
                    }
                    else
                    {
                        _shapesGallery.SaveBezierToGallery(bezier, renderer.GetBezierImage(bezier));
                        _shapesGallery.Add(bezier);
                    }
                }
            }
        }
        public MainWindow()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception e)
            {
            }

            bezierSurface = new BezierSurface("new surface", "");
            bezierSurface.Triangulate(0.5f);

            renderer = new Renderer(Views.Handle, ViewsBezier.Handle);
            maxViewport = ViewportOrientation.None;
            mouseDownHandled = false;
            shiftingViewport = ViewportOrientation.None;
            shiftingBezierViewport = ViewportOrientation.None;
            //currScene = Scene.GetExampleScene();
            currScene = new Scene();

            // Tworzenie kolekcji obiektów i dodawanie jej do kontrolki ItemsControl
            // galerii ksztaltow.
            _shapesGallery = new ShapeGallery();
            // Wczytywanie powierzchni beziera
            ksztalty_ListView.ItemsSource = _shapesGallery;

            // Tworzenie kolekcji gotowych obiektów i dodawanie jej do kontrolki ItemsControl
            // galerii gotowych obiektów.
            _elementsGallery = new PreparedObjectsGallery();
            gotowe_ListView.ItemsSource = _elementsGallery;

            _surfaceGallery = new SurfaceGallery();
            materialy_ListView.ItemsSource = _surfaceGallery;

            _lightGallery = new LightGallery();
            swiatla_ListView.ItemsSource = _lightGallery;

            //Menu kontekstowe
            contextMenu = new System.Windows.Forms.ContextMenu();
            contextMenu.MenuItems.Add("Przenieś", contextMenuClick);
            contextMenu.MenuItems.Add("Obróć", contextMenuClick);
            contextMenu.MenuItems.Add("Skaluj", contextMenuClick);
            contextMenu.MenuItems.Add("Skaluj wzdłuż osi", contextMenuClick);
            contextMenu.MenuItems.Add("Powiększ widok", contextViewport);
            contextMenu.MenuItems.Add("Zapisz gotowy element", contextSave);
            System.Windows.Forms.MenuItem[] subMenu = new System.Windows.Forms.MenuItem[4];
            System.Windows.Forms.MenuItem subLeft = new System.Windows.Forms.MenuItem("Lewej", subLeftClick);
            System.Windows.Forms.MenuItem subRight = new System.Windows.Forms.MenuItem("Prawej", subRightClick);
            System.Windows.Forms.MenuItem subUp = new System.Windows.Forms.MenuItem("Góry", subUpClick);
            System.Windows.Forms.MenuItem subDown = new System.Windows.Forms.MenuItem("Dołu", subDownClick);
            subMenu[0] = subLeft;
            subMenu[1] = subRight;
            subMenu[2] = subUp;
            subMenu[3] = subDown;
            contextMenu.MenuItems.Add("Dosuń do", subMenu);
            contextMenu.MenuItems.Add("Kopiuj", contextCopy);
            contextMenu.MenuItems.Add("Wklej", contextPaste);
            contextMenu.MenuItems.Add("Usuń", contextDelete);
            contextMenu.MenuItems[0].Checked = true;
            Views.ContextMenu = contextMenu;

            //Menu kontekstowe dla płatów beziera
            bezierContextMenu = new ContextMenu();
            bezierContextMenu.MenuItems.Add("Zresetuj punkty", bezierContextMenuClick);
            bezierContextMenu.MenuItems.Add("Nowa powierzchnia", bezierContextMenuNewSurface);
            ViewsBezier.ContextMenu = bezierContextMenu;

            copyPaste = new CopyPaste.CopyPaste();
        }