Click here to Skip to main content
15,893,487 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_Rings_ClassicalCover : Form
    {
        int m_version = 620;
        const string strAddRegKey = "Form_Rings_ClassicalCover";

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

        bool bRestore = false;

        // -------------------------------------------------
        public Form_Rings_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);

            rings .Clear ();
            rings .Add (new Ring (this, mover, new PointF (260, 130), 130, 80, 0, RandomValues (7)));
            rings .Add (new Ring (this, mover, new PointF (500, 180), 70, 50, 30, RandomValues (4)));
            rings .Add (new Ring (this, mover, new PointF (220, 330), 170, 70, 
                                  new double [] { 12, 6, 4, 1, 5, 7, 3, 6.5, 2.8, 10, 3.7 }, Color .Lime, Color .Cyan));
            rings .Add (new Ring (this, mover, new PointF (480, 350), 80, 40, Color .Yellow));
            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 < rings .Count; i++)
            {
                mover .Add (rings [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 Ring)
                {
                    (grobj as Ring) .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 Ring)
                {
                    Ring ring = grobj as Ring;
                    if (e .Button == MouseButtons .Left)
                    {
                        ring .StartResizing (e .Location, mover .CaughtNode);
                    }
                    else if (e .Button == MouseButtons .Right)
                    {
                        ring .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 Ring)
                    {
                        (grobj as Ring) .RedefineCover ();
                        Invalidate ();
                    }
                }
                else if (e .Button == MouseButtons .Right && dist <= 3)
                {
                    if (grobj is Ring)
                    {
                        long idRing = mover .WasCaughtSource .ID;
                        for (int i = rings .Count - 1; i >= 0; i--)
                        {
                            if (idRing == rings [i] .ID)
                            {
                                iRingPressed = i;
                                ringPressed = rings [i];
                                break;
                            }
                        }
                        ContextMenuStrip = menuOnRings;
                    }
                    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));
            }
        }

        // *****   menuOnRngs   *****
        // -------------------------------------------------        OnOpening_menuOnRings
        private void OnOpening_menuOnRings (object sender, CancelEventArgs e)
        {
            ToolStripItemCollection items = menuOnRings .Items;

            items ["miPutOnTop"] .Enabled = iRingPressed > 0;
            items ["miOneLevelUp"] .Enabled = iRingPressed > 0;
            items ["miOneLevelDown"] .Enabled = iRingPressed < rings .Count - 1;
            items ["miPutUnderneath"] .Enabled = iRingPressed < rings .Count - 1;
        }
        // -------------------------------------------------        Click_miPutOnTop
        private void Click_miPutOnTop (object sender, EventArgs e)
        {
            rings .RemoveAt (iRingPressed);
            rings .Insert (0, ringPressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miOneLevelUp
        private void Click_miOneLevelUp (object sender, EventArgs e)
        {
            rings .Reverse (iRingPressed - 1, 2);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miOneLevelDown
        private void Click_miOneLevelDown (object sender, EventArgs e)
        {
            rings .Reverse (iRingPressed, 2);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miPutUnderneath
        private void Click_miPutUnderneath (object sender, EventArgs e)
        {
            rings .RemoveAt (iRingPressed);
            rings .Add (ringPressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDuplicate
        private void Click_miDuplicate (object sender, EventArgs e)
        {
            Ring ringNew = ringPressed .Copy (new PointF (ringPressed .Center .X + 60, ringPressed .Center .Y + 60));
            rings .Insert (0, ringNew);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDelete
        private void Click_miDelete (object sender, EventArgs e)
        {
            rings .RemoveAt (iRingPressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miColors
        private void Click_miColors (object sender, EventArgs e)
        {
            if (ringPressed .Colors .Count == 1)
            {
                ColorDialog dlg = new ColorDialog ();

                dlg .Color = ringPressed .Color;
                if (dlg .ShowDialog () == DialogResult .OK)
                {
                    ringPressed .Color = dlg .Color;
                    Invalidate ();
                }
            }
            else
            {
                Form_Colors_Ring form = new Form_Colors_Ring (ringPressed .Values, ringPressed .Colors,
                                                              Auxi_Convert .RadianToDegree (ringPressed .Angle), ringPressed .DrawingDirection);
                if (DialogResult .OK == form .ShowDialog ())
                {
                    Ring_SlidingPartitions sample = form .Sample;
                    Ring ringNew = new Ring (this, mover, ringPressed .Center, ringPressed .OuterRadius, ringPressed .InnerRadius,
                                             Auxi_Convert .RadianToDegree (sample .Angle), sample .Values);
                    ringNew .SetColors (sample .Colors);
                    rings .RemoveAt (iRingPressed);
                    rings .Insert (iRingPressed, ringNew);
                    RenewMover ();
                    Invalidate ();
                }
            }
        }
        // -------------------------------------------------        Click_miChangeDrawingDirection
        private void Click_miChangeDrawingDirection (object sender, EventArgs e)
        {
            ringPressed .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_Ring form = new Form_Add_Ring ();
            if (form .ShowDialog () == DialogResult .OK)
            {
                Ring_SlidingPartitions sample = form .Sample;
                Ring ringNew = new Ring (this, mover, Auxi_Geometry .Middle (ClientRectangle),
                                         sample .OuterRadius, sample .InnerRadius, 0, sample .Values);
                ringNew .SetColors (sample .Colors);
                rings .Insert (0, ringNew);
                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
                                                            rings .Count .ToString () },        // 4
                                      RegistryValueKind .MultiString);
                    scCovers .IntoRegistry (regkey, "scCovers");
                    scAdd .IntoRegistry (regkey, "scAdd");
                    info .IntoRegistry (regkey, "Info");
                    for (int i = 0; i < rings .Count; i++)
                    {
                        rings [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");
                        rings .Clear ();
                        for (int i = 0; i < nFigures; i++)
                        {
                            Ring ring = Ring .FromRegistry (this, mover, regkey, i .ToString ());
                            if (ring != null)
                            {
                                rings .Add (ring);
                            }
                        }
                        if (scCovers == null || scAdd == null || rings .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