Click here to Skip to main content
15,895,538 members
Articles / Programming Languages / C++

What can be simpler than graphical primitives? Part 1

Rate me:
Please Sign up or sign in to vote.
4.96/5 (8 votes)
5 Mar 2013CPOL24 min read 24.8K   503   27  
This article is about the moving and resizing of different graphical primitives.
using System;
using System .Collections .Generic;
using System .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace GraphicalPrimitives
{
    class RectangleAllCellsInView : GraphicalObject
    {
        int m_version = 618;
        int nCells;
        int cellWidth, cellHeight;
        Rectangle rc;
        int nRows, nCols;

        Cursor cursorArea = Cursors .SizeAll;
        int radius = 5;
        int halfstrip = 3;
        Delegate_Draw m_draw = null;
        static int minCellSize = 16;

        // -------------------------------------------------
        public RectangleAllCellsInView (Rectangle rect, int cells, int singleWidth, int singleHeight)
        {
            nCells = Math .Max (1, Math .Abs (cells));
            cellWidth = Math .Max (Math .Abs (singleWidth), minCellSize);
            cellHeight = Math .Max (Math .Abs (singleHeight), minCellSize);
            nCols = rect .Width / cellWidth;
            nRows = rect .Height / cellHeight;
            if (nRows * nCols >= cells)
            {
                rc = rect;
            }
            else
            {
                nRows = 2;
                nCols = (cells + 1) / 2;
                rc = new Rectangle (rect .Left, rect .Top, nCols * cellWidth, nRows * cellHeight);
            }
        }
        // -------------------------------------------------
        public RectangleAllCellsInView (Rectangle rect, int cells, int cellSize)
            : this (rect, cells, cellSize, cellSize)
        {
        }
        // -------------------------------------------------
        public RectangleAllCellsInView (Rectangle rect, int cells, int singleWidth, int singleHeight, Delegate_Draw delegate_draw)
            : this (rect, cells, singleWidth, singleHeight)
        {
            m_draw = delegate_draw;
        }
        // -------------------------------------------------
        public RectangleAllCellsInView (Rectangle rect, int cells, int cellSize, Delegate_Draw delegate_draw)
            : this (rect, cells, cellSize, cellSize, delegate_draw)
        {
        }
        // -------------------------------------------------        MinimumCellSize
        static public int MinimumCellSize
        {
            get { return (minCellSize); }
        }
        // -------------------------------------------------        MinimumCells
        public int MinimumCells
        {
            get { return (nCells); }
        }
        // -------------------------------------------------        Rows
        public int Rows
        {
            get { return (nRows); }
        }
        // -------------------------------------------------        Columns
        public int Columns
        {
            get { return (nCols); }
        }
        // -------------------------------------------------        CellWidth
        public int CellWidth
        {
            get { return (cellWidth); }
        }
        // -------------------------------------------------        CellHeight
        public int CellHeight
        {
            get { return (cellHeight); }
        }
        // -------------------------------------------------        Area
        public Rectangle Area
        {
            get { return (rc); }
        }
        // -------------------------------------------------        CellArea
        public Rectangle CellArea (int iCell)
        {
            if (0 <= iCell && iCell < nCells)
            {
                return (new Rectangle (rc .Left + cellWidth * (iCell % nCols), rc .Top + cellHeight * (iCell / nCols), cellWidth, cellHeight));
            }
            else
            {
                return (Rectangle .Empty);
            }
        }
        // -------------------------------------------------        CellOrderByPoint   
        public int CellOrderByPoint (Point pt, out int iRow, out int jCol)
        {
            Rectangle rcFilled = new Rectangle (rc .Left, rc .Top, nCols * cellWidth, nRows * cellHeight);
            if (rcFilled .Contains (pt))
            {
                jCol = (pt .X - rcFilled .Left) / cellWidth;
                iRow = (pt .Y - rcFilled .Top) / cellHeight;
                int iCell = -1;
                if (jCol < nCols && iRow < nRows)
                {
                    iCell = iRow * nCols + jCol;
                }
                if (0 <= iCell && iCell < nCells)
                {
                    return (iCell);
                }
                else
                {
                    return (-1);
                }
            }
            else
            {
                iRow = -1;
                jCol = -1;
                return (-1);
            }
        }
        // -------------------------------------------------        DrawMethod
        public Delegate_Draw DrawMethod
        {
            set { m_draw = value; }
        }
        // -------------------------------------------------        Draw
        public void Draw (Graphics grfx)
        {
            if (m_draw != null)
            {
                m_draw (grfx);
            }
        }
        // -------------------------------------------------        AreaCursor
        public Cursor AreaCursor
        {
            set { cursorArea = value; }
        }
        // -------------------------------------------------        DefineCover
        public override void DefineCover ()
        {
            cover = new Cover (rc, Resizing .Any, radius, halfstrip);
            cover [cover .NodesCount - 1] .Cursor = cursorArea;
        }
        // -------------------------------------------------        Move
        public override void Move (int cx, int cy)
        {
            rc .X += cx;
            rc .Y += cy;
        }
        // -------------------------------------------------        MoveNode
        // number of node
        //  0   6   1
        //  4   8   5
        //  3   7   2
        public override bool MoveNode (int i, int cx, int cy, Point ptM, MouseButtons mb)
        {
            bool bRet = false;

            if (mb == MouseButtons .Left)
            {
                int dxNew, dyNew, nColNew, nRowNew;
                if (i == 8)
                {
                    Move (cx, cy);
                }
                else if (i == 0)        //LT corner
                {
                    dxNew = rc .Width - cx;
                    dyNew = rc .Height - cy;
                    nRowNew = dyNew / cellHeight;
                    nColNew = dxNew / cellWidth;
                    if (nRowNew * nColNew >= nCells)
                    {
                        MoveBorder_Top (cy);
                        MoveBorder_Left (cx);
                        bRet = true;
                    }
                }
                else if (i == 1)        // RT corner
                {
                    dxNew = rc .Width + cx;
                    dyNew = rc .Height - cy;
                    nRowNew = dyNew / cellHeight;
                    nColNew = dxNew / cellWidth;
                    if (nRowNew * nColNew >= nCells)
                    {
                        MoveBorder_Top (cy);
                        MoveBorder_Right (cx);
                        bRet = true;
                    }
                }
                else if (i == 2)        // RB corner
                {
                    dxNew = rc .Width + cx;
                    dyNew = rc .Height + cy;
                    nRowNew = dyNew / cellHeight;
                    nColNew = dxNew / cellWidth;
                    if (nRowNew * nColNew >= nCells)
                    {
                        MoveBorder_Right (cx);
                        MoveBorder_Bottom (cy);
                        bRet = true;
                    }
                }
                else if (i == 3)        // LB corner
                {
                    dxNew = rc .Width - cx;
                    dyNew = rc .Height + cy;
                    nRowNew = dyNew / cellHeight;
                    nColNew = dxNew / cellWidth;
                    if (nRowNew * nColNew >= nCells)
                    {
                        MoveBorder_Bottom (cy);
                        MoveBorder_Left (cx);
                        bRet = true;
                    }
                }
                else if (i == 4)     // on left side
                {
                    dxNew = rc .Width - cx;
                    nColNew = dxNew / cellWidth;
                    nRowNew = rc .Height / cellHeight;
                    if (nRowNew * nColNew >= nCells)
                    {
                        MoveBorder_Left (cx);
                        bRet = true;
                    }
                }
                else if (i == 5)   // on right side
                {
                    dxNew = rc .Width + cx;
                    nColNew = dxNew / cellWidth;
                    nRowNew = rc .Height / cellHeight;
                    if (nRowNew * nColNew >= nCells)
                    {
                        MoveBorder_Right (cx);
                        bRet = true;
                    }
                }
                else if (i == 6)      // on top
                {
                    dyNew = rc .Height - cy;
                    nRowNew = dyNew / cellHeight;
                    nColNew = rc .Width / cellWidth;
                    if (nRowNew * nColNew >= nCells)
                    {
                        MoveBorder_Top (cy);
                        bRet = true;
                    }
                }
                else if (i == 7)   // on bottom
                {
                    dyNew = rc .Height + cy;
                    nRowNew = dyNew / cellHeight;
                    nColNew = rc .Width / cellWidth;
                    if (nRowNew * nColNew >= nCells)
                    {
                        MoveBorder_Bottom (cy);
                        bRet = true;
                    }
                }
            }
            return (bRet);
        }
        // -------------------------------------------------        MoveBorder_Top
        private void MoveBorder_Top (int cy)
        {
            rc .Y += cy;
            rc .Height -= cy;
            nCols = rc .Width / cellWidth;
            nRows = rc .Height / cellHeight;
        }
        // -------------------------------------------------        MoveBorder_Bottom
        private void MoveBorder_Bottom (int cy)
        {
            rc .Height += cy;
            nCols = rc .Width / cellWidth;
            nRows = rc .Height / cellHeight;
        }
        // -------------------------------------------------        MoveBorder_Left
        private void MoveBorder_Left (int cx)
        {
            rc .X += cx;
            rc .Width -= cx;
            nCols = rc .Width / cellWidth;
            nRows = rc .Height / cellHeight;
        }
        // -------------------------------------------------        MoveBorder_Right
        private void MoveBorder_Right (int cx)
        {
            rc .Width += cx;
            nCols = rc .Width / cellWidth;
            nRows = rc .Height / cellHeight;
        }
        // -------------------------------------------------        IntoRegistry
        public void IntoRegistry (RegistryKey regkey, string name)
        {
            try
            {
                regkey .SetValue (name, new string [] { m_version .ToString (),     // 0
                                                        rc .Left .ToString (),      // 1
                                                        rc .Top .ToString (),       // 2
                                                        rc .Width .ToString (),     // 3
                                                        rc .Height .ToString (),    // 4
                                                        nCells .ToString (),        // 5
                                                        cellWidth .ToString (),     // 6
                                                        cellHeight .ToString (),    // 7
                                                     },
                                             RegistryValueKind .MultiString);
            }
            catch
            {
            }
            finally
            {
            }
        }
        // -------------------------------------------------        FromRegistry
        public static RectangleAllCellsInView FromRegistry (RegistryKey regkey, string name, Delegate_Draw drawmethod)
        {
            try
            {
                string [] strs = (string []) regkey .GetValue (name);
                if (strs != null && strs .Length == 8 && Convert .ToInt32 (strs [0]) >= 618)
                {
                    RectangleAllCellsInView rr = new RectangleAllCellsInView (
                                                                    Auxi_Convert .ToRectangle (strs, 1),    // rect
                                                                    Convert .ToInt32 (strs [5]),            // nCells
                                                                    Convert .ToInt32 (strs [6]),            // cellWidth
                                                                    Convert .ToInt32 (strs [7]));           // cellHeight
                    if (rr != null)
                    {
                        rr .DrawMethod = drawmethod;
                    }
                    return (rr);
                }
                else
                {
                    return (null);
                }
            }
            catch
            {
                return (null);
            }
            finally
            {
            }
        }
    }
}

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