Click here to Skip to main content
15,885,366 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.8K   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_Circles_ClassicalCover : Form
    {
        int m_version = 620;
        const string strAddRegKey = "Form_Circles_ClassicalCover";

        Mover mover;
        Point ptMouse_Down, ptMouse_Up;
        SolitaryControl scCovers, scAdd;
        Circle circlePressed;
        int iCirclePressed, iNodePressed;
        List<Circle> circles = new List<Circle> ();
        TextM info;
        string infotext = "Resizing can be started at any border point (left button).\n" +
                          "By any inner point a circle can be moved (left button) or rotated (right button).\n" +
                          "The order of circles can be changed via the context menu.\n" +
                          "Circles can be added and deleted.";
        bool bShowCovers = false;
        Random rand;

        bool bRestore = false;

        // -------------------------------------------------
        public Form_Circles_ClassicalCover ()
        {
            InitializeComponent ();
            mover = new Mover (this);

            int nSeed;
            rand = Auxi_Common .RandomByCurTime (out nSeed);
        }
        // -------------------------------------------------        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 ()
        {
            ClientSize = new Size (740, 560);
            btnCovers .Location = new Point (20, 20);
            btnAdd .Location = new Point (60, 20);

            circles .Clear ();
            circles .Add (new Circle (this, mover, new PointF (260, 130), 90, 0, RandomValues (7)));
            circles .Add (new Circle (this, mover, new PointF (500, 180), 70, 30, RandomValues (4)));
            circles .Add (new Circle (this, mover, new PointF (220, 330), 130, 
                                      new double [] { 12, 6, 4, 1, 5, 7, 3, 6.5, 2.8, 10, 3.7 }, Color .Yellow, Color .Violet));
            circles .Add (new Circle (this, mover, new PointF (480, 350), 40, 130, RandomValues (1)));
            scCovers = new SolitaryControl (btnCovers);
            scAdd = new SolitaryControl (btnAdd);
            info = new TextM (this, new Point (260, 480), infotext);
            info .BackColor = Color .Cyan;
        }
        // -------------------------------------------------        RandomValues
        private double [] RandomValues (int nVals)
        {
            nVals = Math .Max (nVals, 1);
            double [] fVals = new double [nVals];
            for (int i = 0; i < fVals .Length; i++)
            {
                fVals [i] = rand .NextDouble ();
            }
            return (fVals);
        }
        // -------------------------------------------------        RenewMover
        public void RenewMover ()
        {
            mover .Clear ();
            mover .Add (scCovers);
            mover .Add (scAdd);
            mover .Add (info);
            for (int i = 0; i < circles .Count; i++)
            {
                mover .Add (circles [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 Circle)
                {
                    (grobj as Circle) .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 Circle)
                {
                    Circle circle = grobj as Circle;
                    if (e .Button == MouseButtons .Left)
                    {
                        if (mover .CaughtNode != circle .NodesCount - 1) 
                        {
                            circle .StartResizing (e .Location, mover .CaughtNode);
                        }
                    }
                    else if (e .Button == MouseButtons .Right)
                    {
                        circle .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);
            int iWasObject;

            if (mover .Release (out iWasObject, out iNodePressed))
            {
                GraphicalObject grobj = mover .WasCaughtSource;
                if (e .Button == MouseButtons .Left)
                {
                    if (grobj is Circle)
                    {
                        Circle circle = grobj as Circle;
                        if (iNodePressed != circle .NodesCount - 1)
                        {
                            circle .StopResizing ();
                            Invalidate ();
                        }
                    }
                }
                else if (e .Button == MouseButtons .Right && dist <= 3)
                {
                    if (grobj is Circle)
                    {
                        long idCircle = mover .WasCaughtSource .ID;
                        for (int i = circles .Count - 1; i >= 0; i--)
                        {
                            if (idCircle == circles [i] .ID)
                            {
                                iCirclePressed = i;
                                circlePressed = circles [i];
                                break;
                            }
                        }
                        ContextMenuStrip = menuOnCircles;
                    }
                    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));
            }
        }

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

            items ["miPutOnTop"] .Enabled = iCirclePressed > 0;
            items ["miOneLevelUp"] .Enabled = iCirclePressed > 0;
            items ["miOneLevelDown"] .Enabled = iCirclePressed < circles .Count - 1;
            items ["miPutUnderneath"] .Enabled = iCirclePressed < circles .Count - 1;
        }
        // -------------------------------------------------        Click_miPutOnTop
        private void Click_miPutOnTop (object sender, EventArgs e)
        {
            circles .RemoveAt (iCirclePressed);
            circles .Insert (0, circlePressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miOneLevelUp
        private void Click_miOneLevelUp (object sender, EventArgs e)
        {
            circles .Reverse (iCirclePressed - 1, 2);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miOneLevelDown
        private void Click_miOneLevelDown (object sender, EventArgs e)
        {
            circles .Reverse (iCirclePressed, 2);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miPutUnderneath
        private void Click_miPutUnderneath (object sender, EventArgs e)
        {
            circles .RemoveAt (iCirclePressed);
            circles .Add (circlePressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDuplicate
        private void Click_miDuplicate (object sender, EventArgs e)
        {
            int dx = 50;
            int dy = 50;
            int nValues = circlePressed .Values .Length;
            double [] vals = new double [nValues];
            Color [] clrs = new Color [nValues];
            for (int i = 0; i < nValues; i++)
            {
                vals [i] = circlePressed .Values [i];
                clrs [i] = circlePressed .Colors [i];
            }
            Circle circleNew = new Circle (this, mover, new PointF (circlePressed .Center .X + dx, circlePressed .Center .Y + dy),
                                           circlePressed .Radius, Auxi_Convert .RadianToDegree (circlePressed .Angle), vals);
            circleNew .SetColors (clrs);
            circleNew .DrawingDirection = circlePressed .DrawingDirection;
            circles .Insert (0, circleNew);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDelete
        private void Click_miDelete (object sender, EventArgs e)
        {
            circles .RemoveAt (iCirclePressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miColors
        private void Click_miColors (object sender, EventArgs e)
        {
            if (circlePressed .Colors .Count == 1)
            {
                ColorDialog dlg = new ColorDialog ();

                dlg .Color = circlePressed .Color;
                if (dlg .ShowDialog () == DialogResult .OK)
                {
                    circlePressed .Color = dlg .Color;
                    Invalidate ();
                }
            }
            else
            {
                Form_Colors_Circle form = new Form_Colors_Circle (circlePressed .Values, circlePressed .Colors,
                                                                  Auxi_Convert .RadianToDegree (circlePressed .Angle),
                                                                  circlePressed .DrawingDirection);
                if (DialogResult .OK == form .ShowDialog ())
                {
                    Circle_SlidingPartitions sample = form .Sample;
                    Circle circleNew = new Circle (this, mover, circlePressed .Center, circlePressed .Radius, 
                                                   Auxi_Convert .RadianToDegree (sample .Angle), sample .Values);
                    circleNew .SetColors (sample .Colors);
                    circles .RemoveAt (iCirclePressed);
                    circles .Insert (iCirclePressed, circleNew);
                    RenewMover ();
                    Invalidate ();
                }
            }
        }
        // -------------------------------------------------        Click_miChangeDrawingDirection
        private void Click_miChangeDrawingDirection (object sender, EventArgs e)
        {
            circlePressed .ChangeDrawingDirection ();
            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_Add_Circle form = new Form_Add_Circle ();
            if (form .ShowDialog () == DialogResult .OK)
            {
                Circle_SlidingPartitions sample = form .Sample;
                Circle circleNew = new Circle (this, mover, Auxi_Geometry .Middle (ClientRectangle), sample .Radius,
                                               Auxi_Convert .RadianToDegree (sample .Angle), sample .Values);
                circleNew .SetColors (sample .Colors);
                circles .Insert (0, circleNew);
                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
                                                            circles .Count .ToString () },      // 4
                                      RegistryValueKind .MultiString);
                    scCovers .IntoRegistry (regkey, "scCovers");
                    scAdd .IntoRegistry (regkey, "scAdd");
                    info .IntoRegistry (regkey, "Info");
                    for (int i = 0; i < circles .Count; i++)
                    {
                        circles [i] .IntoRegistry (regkey, 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");
                        circles .Clear ();
                        for (int i = 0; i < nFigures; i++)
                        {
                            Circle circle = Circle .FromRegistry (this, mover, regkey, i .ToString ());
                            if (circle != null)
                            {
                                circles .Add (circle);
                            }
                        }
                        if (scCovers == null || scAdd == null || circles .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