Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#

What can be simpler than graphical primitives? Part 3

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
8 Apr 2013CPOL43 min read 23.9K   560   32  
using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Drawing .Drawing2D;
using System .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace GraphicalPrimitives
{
    public partial class Form_ChatoyantPolygons : Form
    {
        int m_version = 620;
        const string strAddRegKey = "Form_ChatoyantPolygons";

        Mover mover;
        Point ptMouse_Down, ptMouse_Up;
        SolitaryControl scCovers, scAdd;
        Polygon_Chatoyant polyPressed;
        int iPolyPressed;
        List<Polygon_Chatoyant> polygons = new List<Polygon_Chatoyant> ();
        TextM info;
        string infotext = "Vertices and central points can be moved to change the configuration.\n" +
                          "Resizing can be started at any border point (left button).\n" +
                          "By any inner point a polygon can be moved (left button) or rotated (right button).\n" +
                          "The order of polygons can be changed via the context menu.\n" +
                          "Polygons can be added and deleted.";
        bool bShowCovers = false;

        bool bRestore = false;

        // -------------------------------------------------
        public Form_ChatoyantPolygons ()
        {
            InitializeComponent ();
            mover = new Mover (this);
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            RestoreFromRegistry ();
            if (!bRestore)
            {
                DefaultView ();
            }
            RenewMover ();
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveIntoRegistry ();
        }
        // -------------------------------------------------        DefaultView
        public void DefaultView ()
        {
            polygons .Clear ();
            ClientSize = new Size (870, 650);
            btnCovers .Location = new Point (20, 20);
            btnAdd .Location = new Point (60, 20);

            PointF ptC = new PointF (150, 150);
            int nVertices = 3;
            polygons .Add (new Polygon_Chatoyant (this, mover, ptC, Auxi_Geometry .RegularPolygon (ptC, 100, nVertices, 20),
                                                  Color .Blue, Auxi_Colours .SmoothChangedColors (nVertices, Color .Red, Color .Green)));
            ptC = new PointF (650, 260);
            nVertices = 7;
            polygons .Add (new Polygon_Chatoyant (this, mover, ptC, Auxi_Geometry .RegularPolygon (ptC, 200, nVertices, -30),
                                                  Color .Yellow, Auxi_Colours .SmoothChangedColors (nVertices, Color .Cyan, Color .Magenta)));
            ptC = new PointF (180, 450);
            nVertices = 6;
            polygons .Add (new Polygon_Chatoyant (this, mover, ptC, Auxi_Geometry .RegularPolygon (ptC, 160, nVertices, 60),
                                                  Color .LightGreen, Auxi_Colours .SmoothChangedColors (nVertices, Color .Yellow, Color .Blue)));
            ptC = new PointF (420, 310);
            nVertices = 14;
            int radius = 80;
            PointF [] pts = Auxi_Geometry .RegularPolygon (ptC, radius, nVertices, 0.15);
            double [] coef = new double [14] { 0.7, 4.0, 1, 2.8, 1, 3.3, 1.4, 4.6, 1.22, 3, 0.8, 2.5, 1, 4.5 };
            for (int i = 0; i < nVertices; i++)
            {
                pts [i] = Auxi_Geometry .EllipsePoint (ptC, radius, radius,
                                                       Auxi_Convert .RadianToDegree (Auxi_Geometry .Line_Angle (ptC, pts [i])), coef [i]);
            }
            polygons .Insert (0, new Polygon_Chatoyant (this, mover, ptC, pts, Color .Cyan, 
                                                        Auxi_Colours .SmoothChangedColors (nVertices, Color .Red, Color .Blue)));
            scCovers = new SolitaryControl (btnCovers);
            scAdd = new SolitaryControl (btnAdd);
            info = new TextM (this, new Point (320, 540), infotext);
            info .BackColor = Color .Cyan;
        }
        // -------------------------------------------------        RenewMover
        public void RenewMover ()
        {
            mover .Clear ();
            mover .Add (scCovers);
            mover .Add (scAdd);
            mover .Add (info);
            for (int i = 0; i < polygons .Count; i++)
            {
                mover .Add (polygons [i]);
            }
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;
            GraphicalObject grobj;

            for (int i = mover .Count - 1; i >= 0; i--)
            {
                grobj = mover [i] .Source;
                if (grobj is Polygon_Chatoyant)
                {
                    (grobj as Polygon_Chatoyant) .Draw (grfx);
                    if (bShowCovers)
                    {
                        mover [i] .DrawCover (grfx);
                    }
                }
            }
            info .Draw (grfx);
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            ptMouse_Down = e .Location;
            if (mover .Catch (e .Location, e .Button))
            {
                GraphicalObject grobj = mover .CaughtSource;
                if (grobj is Polygon_Chatoyant)
                {
                    Polygon_Chatoyant poly = grobj as Polygon_Chatoyant;
                    if (e .Button == MouseButtons .Left)
                    {
                        NodeShape shape = mover .CaughtNodeShape;
                        if (shape == NodeShape .Circle)
                        {
                            poly .StartReconfiguration (mover .CaughtNode);
                        }
                        else if (shape == NodeShape .Strip)
                        {
                            poly .StartResizing (e .Location, mover .CaughtNode);
                        }
                    }
                    else if (e .Button == MouseButtons .Right)
                    {
                        poly .StartRotation (e .Location);
                    }
                }
            }
            ContextMenuStrip = null;
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            ptMouse_Up = e .Location;
            double dist = Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up);

            if (mover .Release ())
            {
                if (e .Button == MouseButtons .Right && dist <= 3)
                {
                    GraphicalObject grobj = mover .WasCaughtSource;
                    if (grobj is Polygon_Chatoyant)
                    {
                        long idPoly = mover .WasCaughtSource .ID;
                        for (int i = polygons .Count - 1; i >= 0; i--)
                        {
                            if (idPoly == polygons [i] .ID)
                            {
                                iPolyPressed = i;
                                polyPressed = polygons [i];
                                break;
                            }
                        }
                        ContextMenuStrip = menuOnPolygons;
                    }
                    else if (grobj is TextM)
                    {
                        ContextMenuStrip = menuOnInfo;
                    }
                }
            }
            else
            {
                if (e .Button == MouseButtons .Right && dist <= 3)
                {
                    ContextMenuStrip = menuOnEmpty;
                }
            }
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            if (mover .Move (e .Location))
            {
                Invalidate ();
            }
        }
        // -------------------------------------------------        OnContextMenuChanged
        private void OnContextMenuChanged (object sender, EventArgs e)
        {
            if (ContextMenuStrip != null)
            {
                ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
            }
        }

        // *****   menuOnPolygons   *****
        // -------------------------------------------------        OnOpening_menuOnPolygons
        private void OnOpening_menuOnPolygons (object sender, CancelEventArgs e)
        {
            ToolStripItemCollection items = menuOnPolygons .Items;

            items ["miPutOnTop"] .Enabled = iPolyPressed > 0;
            items ["miOneLevelUp"] .Enabled = iPolyPressed > 0;
            items ["miOneLevelDown"] .Enabled = iPolyPressed < polygons .Count - 1;
            items ["miPutUnderneath"] .Enabled = iPolyPressed < polygons .Count - 1;
        }
        // -------------------------------------------------        Click_miPutOnTop
        private void Click_miPutOnTop (object sender, EventArgs e)
        {
            polygons .RemoveAt (iPolyPressed);
            polygons .Insert (0, polyPressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miOneLevelUp
        private void Click_miOneLevelUp (object sender, EventArgs e)
        {
            polygons .Reverse (iPolyPressed - 1, 2);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miOneLevelDown
        private void Click_miOneLevelDown (object sender, EventArgs e)
        {
            polygons .Reverse (iPolyPressed, 2);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miPutUnderneath
        private void Click_miPutUnderneath (object sender, EventArgs e)
        {
            polygons .RemoveAt (iPolyPressed);
            polygons .Add (polyPressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDuplicate
        private void Click_miDuplicate (object sender, EventArgs e)
        {
            int dx = 50;
            int dy = 50;
            int nVert = polyPressed .VerticesNum;
            PointF [] pts = new PointF [nVert];
            Color [] clrs = new Color [nVert];
            for (int i = 0; i < nVert; i++)
            {
                pts [i] = new PointF (polyPressed .Vertices [i] .X + dx, polyPressed .Vertices [i] .Y + dy);
                clrs [i] = polyPressed .GetVertexColor (i);
            }
            Polygon_Chatoyant polyNew = new Polygon_Chatoyant (this, mover, new PointF (polyPressed .Center .X + dx, polyPressed .Center .Y + dy), 
                                                               pts, polyPressed .CenterColor, clrs);
            polygons .Insert (0, polyNew);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDelete
        private void Click_miDelete (object sender, EventArgs e)
        {
            polygons .RemoveAt (iPolyPressed);
            RenewMover ();
            Invalidate ();
        }

        // *****   menuOnInfo   *****
        // -------------------------------------------------        Click_miInfoFont
        private void Click_miInfoFont (object sender, EventArgs e)
        {
            FontDialogWithTitle dlg = new FontDialogWithTitle ();

            dlg .Font = info .Font;
            dlg .Title = "Information font";
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                info .Font = dlg .Font;
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_miInfoTextColor
        private void Click_miInfoTextColor (object sender, EventArgs e)
        {
            ColorDialogWithTitle dlg = new ColorDialogWithTitle ();

            dlg .Color = info .Color;
            dlg .Title = "Information color";
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                info .Color = dlg .Color;
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_miInfoBackColor
        private void Click_miInfoBackColor (object sender, EventArgs e)
        {
            ColorDialogWithTitle dlg = new ColorDialogWithTitle ();

            dlg .Color = info .BackColor;
            dlg .Title = "Information background";
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                info .BackColor = dlg .Color;
                Invalidate ();
            }
        }

        // *****   menuOnEmpty   *****
        // -------------------------------------------------        Click_miDefaultView
        private void Click_miDefaultView (object sender, EventArgs e)
        {
            DefaultView ();
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_btnCovers
        private void Click_btnCovers (object sender, EventArgs e)
        {
            bShowCovers = !bShowCovers;
            Invalidate ();
        }
        // -------------------------------------------------        Click_btnAdd
        private void Click_btnAdd (object sender, EventArgs e)
        {
            Form_AddChatoyantPolygon form = new Form_AddChatoyantPolygon ();
            if (form .ShowDialog () == DialogResult .OK)
            {
                Color clrCenter = form .CenterColor;
                Color [] clrs = form .Colors;
                PointF [] pts = Auxi_Geometry .RegularPolygon (Auxi_Geometry .Middle (ClientRectangle), 100.0, clrs .Length, 0);
                polygons .Insert (0, new Polygon_Chatoyant (this, mover, Auxi_Geometry .Middle (ClientRectangle), pts, clrCenter, clrs));
                RenewMover ();
                Invalidate ();
            }
        }

        const string name = "Main";
        // -------------------------------------------------        SaveIntoRegistry
        private void SaveIntoRegistry ()
        {
            string strRegKey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
                if (regkey != null)
                {
                    regkey .SetValue (name, new string [] { m_version .ToString (),             // 0
                                                            ClientSize .Width .ToString (),     // 1
                                                            ClientSize .Height .ToString (),    // 2
                                                            bShowCovers .ToString (),           // 3
                                                            polygons .Count .ToString () },     // 4
                                      RegistryValueKind .MultiString);
                    scCovers .IntoRegistry (regkey, "scCovers");
                    scAdd .IntoRegistry (regkey, "scAdd");
                    info .IntoRegistry (regkey, "Info");
                    for (int i = 0; i < polygons .Count; i++)
                    {
                        polygons [i] .IntoRegistry (regkey, "polygon_" + i .ToString ());
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
        // -------------------------------------------------        RestoreFromRegistry
        private void RestoreFromRegistry ()
        {
            string namekey = Auxi_DLL_Internal .PathDefinedByExecutable ();
            namekey += strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .OpenSubKey (namekey);
                if (regkey != null)
                {
                    string [] strs = (string []) regkey .GetValue (name);
                    if (strs != null && strs .Length == 5 && Convert .ToInt32 (strs [0]) >= 620)
                    {
                        ClientSize = Auxi_Convert .ToSize (strs, 1);
                        bShowCovers = Convert .ToBoolean (strs [3]);
                        int nFigures = Convert .ToInt32 (strs [4]);
                        scCovers = SolitaryControl .FromRegistry (regkey, "scCovers", btnCovers);
                        scAdd = SolitaryControl .FromRegistry (regkey, "scAdd", btnAdd);
                        info = TextM .FromRegistry (this, regkey, "Info");
                        polygons .Clear ();
                        for (int i = 0; i < nFigures; i++)
                        {
                            Polygon_Chatoyant poly = Polygon_Chatoyant .FromRegistry (this, mover, regkey, "polygon_" + i .ToString ());
                            if (poly != null)
                            {
                                polygons .Add (poly);
                            }
                        }
                        if (scCovers == null || scAdd == null || polygons .Count < nFigures)
                        {
                            return;
                        }
                        bRestore = true;
                    }
                    regkey .Close ();
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions