Click here to Skip to main content
15,895,283 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 .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace GraphicalPrimitives
{
    public partial class Form_Add_Circle : Form
    {
        int m_version = 620;
        const string strAddRegKey = "Form_Add_Circle";
        Mover mover;
        Point ptMouse_Down, ptMouse_Up;

        CommentedControlLTP ccSectors;
        SolitaryControl scAdd;
        Circle_SlidingPartitions circle; 
        int radiusSmall = 7;
        List<PointF> ptSampleColor = new List<PointF> ();

        Font fntSelected;
        bool bAfterInit = false;
        bool bRestore = false;

        // -------------------------------------------------
        public Form_Add_Circle ()
        {
            InitializeComponent ();
            mover = new Mover (this);
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            fntSelected = Font;
            RestoreFromRegistry ();
            if (!bRestore)
            {
                DefaultView ();
            }
            numericUD_Sectors .Value = circle .Values .Length;
            RenewMover ();
            bAfterInit = true;
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveIntoRegistry ();
        }
        // -------------------------------------------------        DefaultView
        private void DefaultView ()
        {
            Spaces spaces = new Spaces (this, fntSelected);
            float fRadius = 100;
            int nSectors = 6;
            numericUD_Sectors .Value = nSectors;
            double [] vals = new double [nSectors];
            for (int i = 0; i < vals .Length; i++)
            {
                vals [i] = 2.0;
            }
            circle = new Circle_SlidingPartitions (this, mover, new PointF (2 * fRadius, 2 * fRadius), fRadius, 0, vals);
            ConstructSampleCircles ();

            numericUD_Sectors .Location = new Point (spaces .FormSideSpace, spaces .FormTopSpace);
            ccSectors = new CommentedControlLTP (this, numericUD_Sectors, "Sectors");
            int halfSize = Convert .ToInt32 (circle .Radius + 3 * radiusSmall);
            Rectangle rc = Rectangle .Round (new RectangleF (circle .Center .X - halfSize, circle .Center .Y - halfSize, 
                                                             2 * halfSize, 2 * halfSize));
            btnAdd .Location = Point .Round (new PointF (circle .Center .X - btnAdd .Width / 2, rc .Bottom + 3 * spaces .Ver_betweenFrames));
            scAdd = new SolitaryControl (btnAdd);
            ClientSize = new Size (Convert .ToInt32 (4 * fRadius), btnAdd .Bottom + spaces .FormBtmSpace);
        }
        // -------------------------------------------------        ConstructSampleCircles
        private void ConstructSampleCircles ()
        {
            ptSampleColor .Clear ();
            PointF ptC = circle .Center;
            double angle = circle .Angle;
            double [] sweep_angles = circle .GetSweepAngles ();
            double dist = circle .Radius + 2 * radiusSmall;
            for (int i = 0; i < sweep_angles .Length; i++)
            {
                angle += sweep_angles [i] / 2;
                ptSampleColor .Add (Auxi_Geometry .PointToPoint (ptC, angle, dist));
                angle += sweep_angles [i] / 2;
            }
        }
        // -------------------------------------------------        UpdateSampleCircles
        private void UpdateSampleCircles ()
        {
            PointF ptC = circle .Center;
            double angle = circle .Angle;
            double [] sweep_angles = circle .GetSweepAngles ();
            double dist = circle .Radius + 2 * radiusSmall;
            for (int i = 0; i < sweep_angles .Length; i++)
            {
                angle += sweep_angles [i] / 2;
                ptSampleColor [i] = Auxi_Geometry .PointToPoint (ptC, angle, dist);
                angle += sweep_angles [i] / 2;
            }
        }
        // -------------------------------------------------        RenewMover
        private void RenewMover ()
        {
            mover .Clear ();
            mover .Insert (0, circle);
            mover .Insert (0, ccSectors);
            mover .Insert (0, scAdd);
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            circle .Draw (grfx);
            for (int i = 0; i < ptSampleColor .Count; i++)
            {
                Auxi_Drawing .FillEllipse (grfx, ptSampleColor [i], radiusSmall, circle .Colors [i]);
                Auxi_Drawing .DrawEllipse (grfx, ptSampleColor [i], radiusSmall, Color .DarkGray);
            }
            ccSectors .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_SlidingPartitions)
                {
                    if (e .Button == MouseButtons .Left)
                    {
                        if (mover .CaughtNodeShape == NodeShape .Strip)
                        {
                            circle .StartResectoring (mover .CaughtNode);
                        }
                        else if (mover .CaughtNode == circle .Values .Length + 1)
                        {
                            circle .StartResizing (e .Location);
                        }
                    }
                    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);

            if (e .Button == MouseButtons .Left)
            {
                if (!mover .Release ())
                {
                    for (int i = 0; i < ptSampleColor .Count; i++)
                    {
                        if (Auxi_Geometry .Distance (ptSampleColor [i], e .Location) <= radiusSmall)
                        {
                            SelectColor (i);
                            Invalidate ();
                            break;
                        }
                    }
                }
            }
            else if (e .Button == MouseButtons .Right)
            {
                if (!mover .Release () && dist <= 3)
                {
                    ContextMenuStrip = menuOnEmpty;
                }
            }
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            if (mover .Move (e .Location))
            {
                GraphicalObject grobj = mover .CaughtSource;
                if (grobj is Circle_SlidingPartitions)
                {
                    UpdateSampleCircles ();
                }
                Invalidate ();
            }
        }
        // -------------------------------------------------        OnContextMenuChanged
        private void OnContextMenuChanged (object sender, EventArgs e)
        {
            if (ContextMenuStrip != null)
            {
                ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
            }
        }
        // -------------------------------------------------        SelectColor
        private void SelectColor (int i)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = circle .Colors [i];
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                circle .SetColor (i, dlg .Color);
            }
        }
        // -------------------------------------------------        ValueChanged_numericSectors
        private void ValueChanged_numericSectors (object sender, EventArgs e)
        {
            if (bAfterInit)
            {
                List<Color> clrsNew = new List<Color> ();
                for (int i = 0; i < circle .Colors .Count; i++)
                {
                    clrsNew .Add (circle .Colors [i]);
                }
                int nNewColors = Convert .ToInt32 (numericUD_Sectors .Value);
                if (nNewColors < clrsNew .Count)
                {
                    clrsNew .RemoveAt (nNewColors);
                }
                else if (nNewColors > clrsNew .Count)
                {
                    clrsNew .Add (Auxi_Colours .ColorPredefined (nNewColors));
                }

                double [] vals = new double [nNewColors];
                for (int i = 0; i < vals .Length; i++)
                {
                    vals [i] = 2.0;
                }
                circle = new Circle_SlidingPartitions (this, mover, circle .Center, circle .Radius, 0, vals);
                circle .SetColors (clrsNew);
                ConstructSampleCircles ();
                RenewMover ();
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_miFont
        private void Click_miFont (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = fntSelected;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                fntSelected = dlg .Font;
                ccSectors .Font = fntSelected;
                scAdd .Font = fntSelected;
                RenewMover ();
                Invalidate ();
            }
        }
        // -------------------------------------------------        Sample
        public Circle_SlidingPartitions Sample
        {
            get { return (circle); }
        }

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

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
                if (regkey != null)
                {
                    regkey .SetValue (nameMain, new string [] { m_version .ToString (),         // 0 
                                                                ClientSize .Width .ToString (),     // 1 
                                                                ClientSize .Height .ToString (),    // 2 
                                                                fntSelected .Name,                          // 3
                                                                fntSelected .SizeInPoints .ToString (),     // 4
                                                                ((int) (fntSelected .Style)) .ToString (),  // 5
                                                                numericUD_Sectors .Value .ToString (),          // 6
                                                             },
                                                RegistryValueKind .MultiString);
                    circle .IntoRegistry (regkey, "circle");
                    ccSectors .IntoRegistry (regkey, "ccSectors");
                    scAdd .IntoRegistry (regkey, "scAdd");
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
        // -------------------------------------------------        RestoreFromRegistry
        private void RestoreFromRegistry ()
        {
            string strkey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .OpenSubKey (strkey);
                if (regkey != null)
                {
                    string [] strs = (string []) regkey .GetValue (nameMain);
                    if (strs != null && strs .Length == 7 && Convert .ToInt32 (strs [0]) == 620)
                    {
                        ClientSize = Auxi_Convert .ToSize (strs, 1);
                        fntSelected = Auxi_Convert .ToFont (strs, 3);
                        numericUD_Sectors .Value = Convert .ToDecimal (strs [6]);
                    }
                    circle = Circle_SlidingPartitions .FromRegistry (this, mover, regkey, "circle");
                    ccSectors = CommentedControlLTP .FromRegistry (this, regkey, "ccSectors", numericUD_Sectors);
                    scAdd = SolitaryControl .FromRegistry (regkey, "scAdd", btnAdd);
                    if (circle != null && ccSectors != null && scAdd != null)
                    {
                        ConstructSampleCircles ();
                        bRestore = true;
                    }
                }
            }
            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