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

using MoveGraphLibrary;

namespace GraphicalPrimitives
{
    public partial class Form_Colors_Ring : Form
    {
        int version = 614;
        const string strAddRegKey = "Form_Colors_Ring";
        Mover mover;
        Point ptMouse_Down, ptMouse_Up;
        Ring_SlidingPartitions ring;
        List<MoveableText> numbers = new List<MoveableText> ();
        double fNumbersPositionCoef = 16;

        ArbitraryGroup groupGradient;
        CommentedControlLTP ccFrom = null;
        CommentedControlLTP ccTo = null;
        ResizableRectangle rrColors = null;
        SolitaryControl scApply = null;

        SolitaryControl scOK = null;

        Font fntSelected;
        Spaces spaces;

        bool bAfterInit = false;
        bool bRestore = false;

        // -------------------------------------------------
        public Form_Colors_Ring (double [] vals, List<Color> clrs, double angleDegree, Rotation dirDrawing)
        {
            InitializeComponent ();
            mover = new Mover (this);

            fntSelected = Font;
            ring = new Ring_SlidingPartitions (this, mover, new Point (200, 270), 160, 60, angleDegree, vals);
            ring .SetColors (clrs);
            ring .DrawingDirection = dirDrawing;
            numericUD_From .Maximum = ring .Values .Length - 1;
            numericUD_To .Maximum = ring .Values .Length - 1;
            numericUD_From .Value = 0;
            numericUD_To .Value = numericUD_To .Maximum;
            ConstructNumbers ();

            spaces = new Spaces (this);
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            RestoreFromRegistry ();
            if (!bRestore)
            {
                numericUD_From .Location = new Point (150, 40);
                numericUD_To .Location = new Point (350, numericUD_From .Top);
                ccFrom = new CommentedControlLTP (this, numericUD_From, Side .W, SideAlignment .Center, 4, "From");
                ccTo = new CommentedControlLTP (this, numericUD_To, Side .W, SideAlignment .Center, 4, "To");
                int cy = ccFrom .RectAround .Bottom + 2 * spaces .VerMin;
                Size sizeMin = new Size (20, 20);
                Size sizeMax = new Size (400, 400);
                rrColors = new ResizableRectangle (Rectangle .FromLTRB (ccFrom .RectAround .Left, cy,
                                                                        ccTo .RectAround .Right, cy + sizeMin .Height),
                                                   sizeMin, sizeMax, Show_Colors);
                Rectangle rc = rrColors .Rectangle;
                btnApply .Location = new Point ((rc .Left + rc .Right) / 2 - btnApply .Width / 2, rc .Bottom + spaces .VerMin);
                scApply = new SolitaryControl (btnApply);
                GroupVisibleParameters visparams = new GroupVisibleParameters (BackColor, true, true,       // "sample1", 0, 
                                                        true, Auxi_Colours .DefaultFrameColor (this), new int [] { 10, 6, 10, 10 },
                                                        true, Font, SystemColors .Highlight, 0.0, 4);
                visparams .Transparency = 0.25;
                groupGradient = new ArbitraryGroup (this, visparams, "Smooth change",
                                        ElementsArea_groupGradient, DrawElems_groupGradient, SynchroMove_groupGradient, IntoMover_groupGradient);

                int dW = Math .Max (Convert .ToInt32 (ring .RectAround .Right), groupGradient .RectAround .Right) + 2 * spaces .FormSideSpace;
                btnOK .Location = new Point (dW - (btnOK .Width + spaces .FormSideSpace),
                                             Convert .ToInt32 (ring .RectAround .Bottom) + 2 * spaces .VerMin);
                scOK = new SolitaryControl (btnOK);
                ClientSize = new Size (dW, btnOK .Bottom + spaces .FormBtmSpace);
            }
            RenewMover ();
            bAfterInit = true;
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveIntoRegistry ();
        }
        // -------------------------------------------------        RenewMover
        private void RenewMover ()
        {
            mover .Clear ();
            mover .Add (ring);
            for (int i = numbers .Count - 1; i >= 0; i--)
            {
                mover .Insert (0, numbers [i]);
            }
            groupGradient .IntoMover (mover, 0);
            mover .Insert (0, scOK);
        }
        // -------------------------------------------------        ConstructNumbers
        private void ConstructNumbers ()
        {
            numbers .Clear ();
            MoveableText textnum;
            PointF ptC = ring .Center;
            double angle = ring .Angle;
            double [] sweep_angles = ring .GetSweepAngles ();
            double dist;
            if (fNumbersPositionCoef >= 1)
            {
                dist = ring .OuterRadius + fNumbersPositionCoef;
            }
            else if (fNumbersPositionCoef >= 0)
            {
                dist = ring .InnerRadius + fNumbersPositionCoef * (ring .OuterRadius - ring .InnerRadius);
            }
            else
            {
                fNumbersPositionCoef = Math .Max (-1, fNumbersPositionCoef);
                dist = ring .InnerRadius * (1 + fNumbersPositionCoef);
            }
            for (int i = 0; i < sweep_angles .Length; i++)
            {
                angle += sweep_angles [i] / 2;
                textnum = new MoveableText (this, Point .Round (Auxi_Geometry .PointToPoint (ptC, angle, dist)), i .ToString (), fntSelected);
                textnum .Rotatable = false;
                numbers .Add (textnum);
                angle += sweep_angles [i] / 2;
            }
        }
        // -------------------------------------------------        PositionNumbers
        private void PositionNumbers ()
        {
            PointF ptC = ring .Center;
            double angle = ring .Angle;
            double [] sweep_angles = ring .GetSweepAngles ();
            for (int i = 0; i < sweep_angles .Length; i++)
            {
                angle += sweep_angles [i] / 2;
                numbers [i] .Location = Point .Round (Auxi_Geometry .RingPointBySpecialCoefficient (ptC, ring .InnerRadius, ring .OuterRadius,
                                                      angle, fNumbersPositionCoef));
                angle += sweep_angles [i] / 2;
            }
        }
        // -------------------------------------------------        Show_Colors
        void Show_Colors (Graphics grfx)
        {
            int iFrom = Convert .ToInt32 (numericUD_From .Value);
            int iTo = Convert .ToInt32 (numericUD_To .Value);
            int nClrsShow;
            Color [] clrs;
            if (iFrom <= iTo)
            {
                nClrsShow = iTo - iFrom + 1;
            }
            else
            {
                nClrsShow = ring .Colors .Count - (iFrom - iTo - 1);
            }
            if (nClrsShow > 1)
            {
                clrs = Auxi_Colours .SmoothChangedColors (nClrsShow, ring .Colors [iFrom], ring .Colors [iTo]);
            }
            else
            {
                clrs = new Color [1] { ring .Colors [iFrom] };
            }
            Rectangle rcArea = rrColors .Rectangle;
            if (rcArea .Height <= rcArea .Width)
            {
                int nW = rcArea .Width;
                int nL, nR;
                for (int i = 0; i < nClrsShow; i++)
                {
                    nL = nW * i / nClrsShow;
                    nR = nW * (i + 1) / nClrsShow;
                    grfx .FillRectangle (new SolidBrush (clrs [i]), Rectangle .FromLTRB (rcArea .Left + nL, rcArea .Top,
                                                                                         rcArea .Left + nR, rcArea .Bottom));
                }
            }
            else
            {
                int nH = rcArea .Height;
                int nT, nB;
                for (int i = 0; i < nClrsShow; i++)
                {
                    nT = nH * i / nClrsShow;
                    nB = nH * (i + 1) / nClrsShow;
                    grfx .FillRectangle (new SolidBrush (clrs [i]), Rectangle .FromLTRB (rcArea .Left, rcArea .Top + nT,
                                                                                         rcArea .Right, rcArea .Top + nB));
                }
            }
            ControlPaint .DrawBorder3D (grfx, rcArea, Border3DStyle .Sunken);
        }
        // -------------------------------------------------        ElementsArea_groupGradient
        private Rectangle ElementsArea_groupGradient ()
        {
            return (Rectangle .Union (Rectangle .Union (Rectangle .Union (ccFrom .RectAround, ccTo .RectAround),
                                                        scApply .RectAround),
                                      rrColors .RectAround));
        }
        // -------------------------------------------------        DrawElems_groupGradient
        private void DrawElems_groupGradient (Graphics grfx)
        {
            rrColors .Draw (grfx);
            ccFrom .Draw (grfx);
            ccTo .Draw (grfx);
        }
        // -------------------------------------------------        SynchroMove_groupGradient
        void SynchroMove_groupGradient (int dx, int dy)
        {
            ccFrom .Move (dx, dy);
            ccTo .Move (dx, dy);
            scApply .Move (dx, dy);
            rrColors .Move (dx, dy);
        }
        // -------------------------------------------------        IntoMover_groupGradient
        void IntoMover_groupGradient (Mover mv, int iPos)
        {
            mv .Insert (iPos, groupGradient);
            mv .Insert (iPos, rrColors);
            mv .Insert (iPos, ccFrom);
            mv .Insert (iPos, ccTo);
            mv .Insert (iPos, scApply);
        }
        // -------------------------------------------------        OnPaint 
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            ring .Draw (grfx);
            for (int i = numbers .Count - 1; i >= 0; i--)
            {
                numbers [i] .Draw (grfx);
            }
            groupGradient .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_SlidingPartitions)
                {
                    Ring_SlidingPartitions ring = grobj as Ring_SlidingPartitions;
                    if (e .Button == MouseButtons .Left)
                    {
                        if (mover .CaughtNodeShape == NodeShape .Strip)
                        {
                            ring .StartResectoring (mover .CaughtNode);
                        }
                        else
                        {
                            ring .StartResizing (e .Location, mover .CaughtNode);
                        }
                    }
                    else if (e .Button == MouseButtons .Right)
                    {
                        ring .StartRotation (e .Location);
                    }
                }
                else if (grobj is MoveableText)
                {
                    if (e .Button == MouseButtons .Left)
                    {
                        fNumbersPositionCoef = Auxi_Geometry .RingCoefficientForPoint (ring .Center, ring .InnerRadius,
                                                                                       ring .OuterRadius, e .Location);
                    }
                }
            }
            ContextMenuStrip = null;
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            ptMouse_Up = e .Location;
            double fDist = Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up);
            if (mover .Release ())
            {
            }
            else
            {
                if (e .Button == MouseButtons .Right && fDist <= 3)
                {
                    ContextMenuStrip = menuOnEmpty;
                }
            }
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            if (mover .Move (e .Location))
            {
                if (mover .CaughtSource is Ring_SlidingPartitions)
                {
                    PositionNumbers ();
                }
                else if (mover .CaughtSource is MoveableText && e .Button == MouseButtons .Left)
                {
                    fNumbersPositionCoef = Auxi_Geometry .RingCoefficientForPoint (ring .Center, ring .InnerRadius,
                                                                                   ring .OuterRadius, e .Location);
                    PositionNumbers ();
                }
                groupGradient .Update ();
                if (mover .CaughtSource is SolitaryControl || mover .CaughtSource is CommentedControlLTP ||
                    mover .CaughtSource is ArbitraryGroup)
                {
                    Update ();
                }
                Invalidate ();
            }
        }
        // -------------------------------------------------        OnMouseDoubleClick
        private void OnMouseDoubleClick (object sender, MouseEventArgs e)
        {
            if (mover .Release () && e .Button == MouseButtons .Left && mover .WasCaughtSource is Ring_SlidingPartitions)
            {
                int iSector = Auxi_Geometry .SectorOfPoint (Point .Round (ring .Center), ring .Angle, ring .GetSweepAngles (), e .Location);
                ColorDialog dlg = new ColorDialog ();

                dlg .Color = ring .GetSectorColor (iSector);
                if (dlg .ShowDialog () == DialogResult .OK)
                {
                    ring .SetSectorColor (iSector, dlg .Color);
                    Invalidate ();
                }
            }
        }
        // -------------------------------------------------        OnContextMenuChanged
        private void OnContextMenuChanged (object sender, EventArgs e)
        {
            if (ContextMenuStrip != null)
            {
                ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
            }
        }
        // -------------------------------------------------        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;
                ccFrom .Font = fntSelected;
                ccTo .Font = fntSelected;
                scApply .Font = fntSelected;
                scOK .Font = fntSelected;
                groupGradient .GroupVisibleParameters .TitleFont = fntSelected;
                groupGradient .Update ();
                for (int i = 0; i < numbers .Count; i++)
                {
                    numbers [i] .Font = fntSelected;
                }
                RenewMover ();
                Invalidate ();
            }
        }
        // -------------------------------------------------        ValueChanged_numeric
        private void ValueChanged_numeric (object sender, EventArgs e)
        {
            if (bAfterInit)
            {
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_btnApply
        private void Click_btnApply (object sender, EventArgs e)
        {
            int iFrom = Convert .ToInt32 (numericUD_From .Value);
            int iTo = Convert .ToInt32 (numericUD_To .Value);
            int nClrsChange;
            Color [] clrs;
            if (iFrom <= iTo)
            {
                nClrsChange = iTo - iFrom + 1;
            }
            else
            {
                nClrsChange = ring .Colors .Count - (iFrom - iTo - 1);
            }
            if (nClrsChange > 1)
            {
                clrs = Auxi_Colours .SmoothChangedColors (nClrsChange, ring .Colors [iFrom], ring .Colors [iTo]);
            }
            else
            {
                clrs = new Color [1] { ring .Colors [iFrom] };
            }
            int nClrsAll = ring .Colors .Count;
            for (int i = 0; i < nClrsChange; i++)
            {
                ring .SetSectorColor ((iFrom + i) % nClrsAll, clrs [i]);
            }
            Invalidate ();
        }
        // -------------------------------------------------        Sample
        public Ring_SlidingPartitions Sample
        {
            get { return (ring); }
        }

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

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
                if (regkey != null)
                {
                    regkey .SetValue (nameSize, new string [] { version .ToString (),               // 0 
                                                                ClientSize .Width .ToString (),     // 1 
                                                                ClientSize .Height .ToString (),    // 2 

                                                                fntSelected .Name,                         // 3
                                                                fntSelected .SizeInPoints .ToString (),    // 4
                                                                ((int) (fntSelected .Style)) .ToString (), // 5

                                                                ring .Center .X .ToString (),     // 6
                                                                ring .Center .Y .ToString (),     // 7
                                                                ring .OuterRadius .ToString (),        // 8
                                                                ring .InnerRadius .ToString (),        // 9
                                                                fNumbersPositionCoef .ToString (),    // 10
                                                              },
                                                RegistryValueKind .MultiString);
                    ccFrom .IntoRegistry (regkey, "FromComCtrl");
                    ccTo .IntoRegistry (regkey, "ToComCtrl");
                    rrColors .IntoRegistry (regkey, "ColorsRect");
                    scApply .IntoRegistry (regkey, "ApplyBtn");
                    groupGradient .IntoRegistry (regkey, "GroupGradient");

                    scOK .IntoRegistry (regkey, "OKBtn");
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
        // -------------------------------------------------        RestoreFromRegistry
        private void RestoreFromRegistry ()
        {
            string strkey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                bRestore = false;
                regkey = Registry .CurrentUser .OpenSubKey (strkey);
                if (regkey != null)
                {
                    string [] strs = (string []) regkey .GetValue (nameSize);
                    if (strs != null && strs .Length == 11 && Convert .ToInt32 (strs [0]) == 614)
                    {
                        ClientSize = Auxi_Convert .ToSize (strs, 1);
                        fntSelected = Auxi_Convert .ToFont (strs, 3);
                        ring .Center = Auxi_Convert .ToPointF (strs, 6);
                        ring .SetRadii (Convert .ToSingle (strs [8]), Convert .ToSingle (strs [9]));
                        fNumbersPositionCoef = Convert .ToDouble (strs [10]);
                    }
                    else
                    {
                        return;
                    }
                    ccFrom = CommentedControlLTP .FromRegistry (this, regkey, "FromComCtrl", numericUD_From);
                    ccTo = CommentedControlLTP .FromRegistry (this, regkey, "ToComCtrl", numericUD_To);
                    rrColors = ResizableRectangle .FromRegistry (regkey, "ColorsRect", Show_Colors, null);
                    scApply = SolitaryControl .FromRegistry (regkey, "ApplyBtn", btnApply);
                    groupGradient = ArbitraryGroup .FromRegistry (this, regkey, "GroupGradient",
                                         ElementsArea_groupGradient, DrawElems_groupGradient, SynchroMove_groupGradient, IntoMover_groupGradient);
                    scOK = SolitaryControl .FromRegistry (regkey, "OKBtn", btnOK);

                    if (ccFrom == null || ccTo == null || rrColors == null || scApply == null ||
                        scOK == null || groupGradient == null)
                    {
                        return;
                    }
                    ConstructNumbers ();
                    groupGradient .Update ();
                    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