Click here to Skip to main content
15,891,864 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 120K   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_ArbitraryGrouping : Form
    {
        const string strAddRegKey = "Form_ArbitraryGrouping";
        Mover mover;
        Point ptMouse_Down, ptMouse_Move;
        bool bRectInView = false;
        ElasticGroup frame; 
        Spaces spaces;
        List<Control> controlsSingle = new List<Control> ();
        List<Control> controlsInFrame = new List<Control> ();

        TextM info;
        string strInfo = "All the controls are moveable and resizable.\n" +
                         "Draw a frame around controls (L_Press - L_Release);\n" +
                         "the controls inside the frame will constitute a group.\n" +
                         "Moving / resizing of the inner controls can resize the group.\n" +
                         "Selection of a single element or none eliminates the group.";

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

            info = new TextM (this, new Point (100, 300), strInfo);
            info .ShowFrame = false;

            spaces = new Spaces (this); 
            spaces .HorMin = 16;
            spaces .VerMin = 16;
            foreach (Control control in Controls)
            {
                controlsSingle .Add (control);
            }
            RenewMover ();
        }
        // -------------------------------------------------        RenewMover
        private void RenewMover ()
        {
            mover .Clear ();
            foreach (Control ctrl in controlsSingle)
            {
                mover .Add (ctrl);
            }
            if (controlsInFrame .Count > 0)
            {
                frame = new ElasticGroup (this, controlsInFrame, spaces, ""); 
                frame .IntoMover (mover, 0);
            }
            mover .Add (info);
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            info .Draw (grfx);
            if (bRectInView)
            {
                grfx .DrawRectangle (Pens .Blue, Math .Min (ptMouse_Down .X, ptMouse_Move .X),
                                                 Math .Min (ptMouse_Down .Y, ptMouse_Move .Y),
                                     Math .Abs (ptMouse_Move .X - ptMouse_Down .X), Math .Abs (ptMouse_Move .Y - ptMouse_Down .Y));
            }
            if (controlsInFrame .Count > 0)
            {
                frame .Draw (grfx);
            }
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            ptMouse_Down = e .Location;
            if (!mover .Catch (e .Location, e .Button))
            {
                bRectInView = true;
            }
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            if (! mover .Release ())
            {
                if (bRectInView)
                {
                    SetGroup (new Rectangle (Math .Min (ptMouse_Down .X, e .X), Math .Min (ptMouse_Down .Y, e .Y),
                                             Math .Abs (e .X - ptMouse_Down .X), Math .Abs (e .Y - ptMouse_Down .Y)));
                    bRectInView = false;
                }
            }
            Invalidate ();
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            ptMouse_Move = e .Location;
            if (mover .Move (e .Location))
            {
                Invalidate ();
                ControlsCausedUpdate ();
            }
            else
            {
                if (bRectInView)
                {
                    Invalidate ();
                }
            }
        }
        // -------------------------------------------------        ControlsCausedUpdate
        private void ControlsCausedUpdate ()
        {
            if (mover [mover .CaughtObject] .Source is ElasticGroup)
            {
                Update ();
            }
        }
        // -------------------------------------------------        SetGroup
        private void SetGroup (Rectangle rc)
        {
            controlsSingle .Clear ();
            controlsInFrame .Clear ();

            foreach (Control control in Controls)
            {
                if (rc .Contains (control .Bounds))
                {
                    controlsInFrame .Add (control);
                }
                else
                {
                    controlsSingle .Add (control);
                }
            }
            if (controlsInFrame .Count == 1)
            {
                controlsSingle .Add (controlsInFrame [0]);
                controlsInFrame .Clear ();
            }
            RenewMover ();
        }
    }
}

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