private void contextSave(object sender, System.EventArgs e)
        {
            if (currScene.selTriangles.Count > 0)
            {
                Scene scene = currScene.SceneFromSelection(out trash);
                System.Drawing.Image sceneImg = renderer.GetSceneImage(scene);
                PreparedElement elem = new PreparedElement("", "", scene);
                bool replace = false;

                Modeler.DialogBoxes.NameDialog newName = new NameDialog();
                bool res = (bool)newName.Show("Podaj nazwę obiektu", "");
                if (!res)
                    return;
                elem.Name = newName.Result;

                while (_elementsGallery.GetNames().Contains(elem.Name, StringComparer.OrdinalIgnoreCase) && !replace)
                {
                    Modeler.DialogBoxes.NameDialog dialog = new NameDialog();
                    dialog.Owner = this;

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

                if (replace)
                {
                    int idx = _elementsGallery.GetNameIdx(elem.Name);
                    _elementsGallery.RemoveAt(idx);
                    _elementsGallery.SaveObjectToGallery(elem, sceneImg);
                    _elementsGallery.Insert(idx, elem);
                }
                else
                {

                    _elementsGallery.SaveObjectToGallery(elem, sceneImg);
                    _elementsGallery.Add(elem);
                }
            }
        }
        private void swiatloGaleriaTab_Drop(object sender, System.Windows.DragEventArgs e)
        {
            if (e.Data.GetDataPresent("Object") || e.Data.GetDataPresent("Light"))
            {
                object data;
                if (e.Data.GetData("Object") is Light_)
                {
                    data = e.Data.GetData("Object");
                }
                else
                {
                    data = e.Data.GetData("Light");
                }
                //Console.WriteLine(data.GetType());
                if (data is Light_)
                {
                    Light_ light = new Light_((Light_)data);
                    Graphics.LightRaytracer.Render(light);
                    bool replace = false;

                    if (String.Compare(light.name, "Default", true) == 0)
                    {
                        Modeler.DialogBoxes.NameDialog newName = new NameDialog();
                        bool res = (bool)newName.Show("Podaj nową nazwę światła", "");
                        if (!res)
                            return;
                        light.name = newName.Result;

                    }
                    while (_lightGallery.GetNames().Contains(light.name, StringComparer.OrdinalIgnoreCase) && !replace)
                    {
                        Modeler.DialogBoxes.NameDialog dialog = new NameDialog();
                        dialog.Owner = this;

                        bool result = (bool)dialog.Show(lightMessage, light.name);
                        string name = dialog.Result;
                        if (!result)
                            return;
                        if (light.name == name)
                            replace = true;
                        light.name = name;
                        dialog.Close();
                    }
                    if (replace)
                    {
                        int idx = _lightGallery.GetNameIndex(light.name);
                        _lightGallery.RemoveAt(idx);
                        LightObj lgtObj = _lightGallery.SaveLightToGallery(light);
                        _lightGallery.Insert(idx, lgtObj);
                    }
                    else
                    {
                        LightObj lgtObj = _lightGallery.SaveLightToGallery(light);
                        _lightGallery.Add(lgtObj);
                    }
                }
            }
        }
        // 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);
                    }
                }
            }
        }
        private void materialy_ListView_Drop(object sender, System.Windows.DragEventArgs e)
        {
            if (e.Data.GetDataPresent("Object"))
            {
                object data = e.Data.GetData("Object");
                //Console.WriteLine(data.GetType());
                if (data is Material_)
                {
                    Material_ material = new Material_((Material_)data);
                    Graphics.SurfaceRaytracer.Render(material);
                    material.colorR = material.colorR / 255;
                    material.colorG = material.colorG / 255;
                    material.colorB = material.colorB / 255;
                    bool replace = 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;

                    }
                    while (_surfaceGallery.GetNames().Contains(material.Name, StringComparer.OrdinalIgnoreCase) && !replace)
                    {
                        Modeler.DialogBoxes.NameDialog dialog = new NameDialog();
                        dialog.Owner = this;

                        bool result = (bool)dialog.Show(materialMessage, material.Name);
                        string name = dialog.Result;
                        if (!result)
                            return;
                        if (material.Name == name)
                            replace = true;
                        material.name = name;
                        dialog.Close();
                    }
                    if (replace)
                    {
                        int idx = _surfaceGallery.GetNameIndex(material.name);
                        _surfaceGallery.RemoveAt(idx);
                        Surface surface = _surfaceGallery.SaveToGallery(material);
                        _surfaceGallery.Insert(idx, surface);
                    }
                    else
                    {
                        Surface surface = _surfaceGallery.SaveToGallery(material);
                        _surfaceGallery.Add(surface);
                    }
                }
            }
        }