Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / C#

The theory of moveable objects

Rate me:
Please Sign up or sign in to vote.
5.00/5 (126 votes)
24 Jan 2010CPOL97 min read 120.5K   3.2K   230  
This article describes an algorithm by which an object of an arbitrary shape can be made moveable and resizable.
using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Windows .Forms;

using MoveGraphLibrary;

namespace TheoryOfMoveableObjects
{
    public partial class Form_Nodes : Form
    {
        Mover mover;
        PrimitiveCircle circle;
        PrimitiveRectangle rect;
        PrimitiveStrip strip;
        TextM info;
        bool bShowCovers = false;

        string infotext = "The cover for each object consists of one node.\r\n" +
                          "All objects are moveable; no resizing or rotation.";

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

            circle = new PrimitiveCircle (new Point (150, 120), 100, Color .Blue);
            rect = new PrimitiveRectangle (new Rectangle (240, 60, 250, 140), Color .Green);
            strip = new PrimitiveStrip (new Point (100, 340), new Point (320, 240), 30, Color .Red);
            info = new TextM (this, new Point (300, 300), infotext);
            info .BackColor = Color .Yellow;

            mover .Add (info);
            mover .Add (circle);
            mover .Add (rect);
            mover .Add (strip);
            mover .Insert (0, btnCovers);
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            strip .Draw (grfx);
            rect .Draw (grfx);
            circle .Draw (grfx);
            info .Draw (grfx);

            if (bShowCovers)
            {
                mover .DrawCovers (grfx);
            }
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs mea)
        {
            mover .Catch (mea .Location, mea .Button);
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            mover .Release ();
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs mea)
        {
            if (mover .Move (mea .Location))
            {
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_btnCovers
        private void Click_btnCovers (object sender, EventArgs e)
        {
            bShowCovers = !bShowCovers;
            Invalidate ();
        }
    }
    // ******************************************
    public class PrimitiveCircle : GraphicalObject
    {
        Point center;
        int radius;
        Color clr;

        // -------------------------------------------------
        public PrimitiveCircle (Point pt, int rad, Color color)
        {
            center = pt;
            radius = rad;
            clr = color;
        }
        // -------------------------------------------------        Draw
        public void Draw (Graphics grfx)
        {
            Auxi_Drawing .FillEllipse (grfx, center, radius, clr);
        }
        // -------------------------------------------------        DefineCover
        public override void DefineCover ()
        {
            CoverNode node = new CoverNode (0, center, radius, Cursors .SizeAll);
            cover = new Cover (new CoverNode [] { node });
            // another variant
            //cover = new Cover (new PointF [] { center }, radius);
            //cover .SetCursor (Cursors .SizeAll);
        }
        // -------------------------------------------------
        public override void Move (int dx, int dy)
        {
            center += new Size (dx, dy);
        }
        // -------------------------------------------------        MoveNode
        public override bool MoveNode (int i, int dx, int dy, Point ptM, MouseButtons catcher)
        {
            bool bRet = false;

            if (catcher == MouseButtons .Left)
            {
                Move (dx, dy);
                bRet = true;
            }
            return (bRet);
        }
    }
    // ******************************************
    public class PrimitiveRectangle : GraphicalObject
    {
        Rectangle rc;
        Color clr;

        // -------------------------------------------------
        public PrimitiveRectangle (Rectangle rect, Color color)
        {
            rc = rect;
            clr = color;
        }
        // -------------------------------------------------        Draw
        public void Draw (Graphics grfx)
        {
            grfx .FillRectangle (new SolidBrush (clr), rc);
        }
        // -------------------------------------------------        DefineCover
        public override void DefineCover ()
        {
            CoverNode node = new CoverNode (0, Auxi_Geometry .CornersOfRectangle (rc));
            node .Clearance = true;
            cover = new Cover (new CoverNode [] { node });
        }
        // -------------------------------------------------
        public override void Move (int dx, int dy)
        {
            rc .Location += new Size (dx, dy);
        }
        // -------------------------------------------------        MoveNode
        public override bool MoveNode (int i, int dx, int dy, Point ptM, MouseButtons catcher)
        {
            bool bRet = false;

            if (catcher == MouseButtons .Left)
            {
                Move (dx, dy);
                bRet = true;
            }
            return (bRet);
        }
    }
    // ******************************************
    public class PrimitiveStrip : GraphicalObject
    {
        Point pt0, pt1;
        int radius;
        SolidBrush brush;

        // -------------------------------------------------
        public PrimitiveStrip (Point ptA, Point ptB, int rad, Color color)
        {
            pt0 = ptA;
            pt1 = ptB;
            radius = rad;
            brush = new SolidBrush (color);
        }
        // -------------------------------------------------        Draw
        public void Draw (Graphics grfx)
        {
            Auxi_Drawing .FillRoundedStrip (grfx, pt0, pt1, radius, brush);
        }
        // -------------------------------------------------        DefineCover
        public override void DefineCover ()
        {
            cover = new Cover (new CoverNode [] { new CoverNode (0, pt0, pt1, radius, Cursors .SizeAll) });
        }
        // -------------------------------------------------
        public override void Move (int dx, int dy)
        {
            Size size = new Size (dx, dy);
            pt0 += size;
            pt1 += size;
        }
        // -------------------------------------------------        MoveNode
        public override bool MoveNode (int i, int dx, int dy, Point ptM, MouseButtons catcher)
        {
            Move (dx, dy);
            return (true);
        }
    }
}

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