Click here to Skip to main content
11,437,816 members (50,268 online)
Click here to Skip to main content
Add your own
alternative version

User-driven applications

, 10 Apr 2010 CPOL
User-driven applications are the programs in which full control is given to the users. Designers of such programs are responsible only for developing an instrument for solving some task, but they do not enforce users to work with this instrument according with a predefined scenario.
TheoryOfUserDrivenApplications.zip
TheoryOfUserDrivenApplications
TheoryOfUserDrivenApplications
bin
Debug
TheoryOfUserDrivenApplications.vshost.exe
Release
MoveGraphLibrary.dll
TheoryOfUserDrivenApplications.bin
TheoryOfUserDrivenApplications.exe
TheoryOfUserDrivenApplications.vshost.exe
Properties
Settings.settings
Resources
ArrowDown.bmp
ArrowUp.bmp
DELETE.BMP
Hangar.bmp
Lock.bmp
palette_.bmp
RuralHouse.bmp
RuralLeftPlus.bmp
RuralRightPlus.bmp
SimpleHouse.bmp
User_driven_applications.doc
User_driven_applications.pdf
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 UserDrivenApplications
{
    enum ReadingType { Everything, HousesOnly };

    public partial class Form_Village : Form
    {
        const string strAddRegKey = "Form_Village";
        Mover mover;
        Point ptMouse_Down, ptMouse_Up, ptMouse_Move;
        ElasticGroup groupBtns;
        BuildingsGroup groupHouses = null;
        bool bTemporaryFrameInView = false;
        Building housePressed;
        int iHousePressed;
        Color [] clrsPrimitiveSmaple;
        Color [] clrsRuralSmaple;
        Color [] clrsHangarSmaple;
        Control [] controlsInGroup;
        List<Building> houses = new List<Building> ();
        const string strFilter = "Binary files(*.bin)|*.bin|All files(*.*)|*.*";

        ClosableInfo info;
        string infotext = "Different buildings can be used to construct a village. Buildings can be moved,\n" +
                          "resized, duplicated, and colored; for some of these operations use the context menu.\n" +
                          "Any set of buildings (N > 1) can be surrounded by a mouse and united into a group;\n" +
                          "this group is moveable.  Content of the group can be changed by moving the buildings\n" +
                          "in or out and by grabbing the buildings, which are inside the group on its release.\n" +  
                          "Both groups and this information have their own menus;\n" +
                          "another menu can be called at any empty place.";

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

            clrsPrimitiveSmaple = new Color [] { Color .FromArgb (255, 128, 0), Color .Red, Color .LightBlue, Color .White, Color .DarkGray };
            clrsRuralSmaple = new Color [] { Color .Yellow, Color .Red, Color .Blue, Color .Brown, Color .DarkGray };
            clrsHangarSmaple = new Color [] { Color .LightBlue, Color .Red, Color .Blue, Color .LightGray, Color .DarkGray };

            info = new ClosableInfo (this, new Point (130, 510), infotext);
            info .BackColor = Color .LightYellow;
            controlsInGroup = new Control [] { btnPrimitive, btnRural, btnRuralLeft, btnRuralRight, btnHangar };
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            groupBtns = new ElasticGroup (this, controlsInGroup, new int [] { 10, 6, 10, 10 }, "Buildings");
            RestoreFromRegistry ();

            RenewMover ();
            btnHelp .Enabled = !info .Visible;
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveInfoToRegistry ();
        }
        // -------------------------------------------------        RenewMover
        private void RenewMover ()
        {
            mover .Clear ();
            if (info .Visible)
            {
                mover .Add (info);
            }
            groupBtns .IntoMover (mover, 0);
            mover .Insert (0, btnHelp);
            for (int i = 0; i < houses .Count; i++)
            {
                mover .Add (houses [i]);
            }
            if (groupHouses != null)
            {
                for (int i = mover .Count - 1; mover [i] .Source is Building; i--)
                {
                    if (groupHouses .Elements .Contains (mover [i] .Source as Building))
                    {
                        mover .Insert (i + 1, groupHouses);
                        break;
                    }
                }
            }
        }
        // -------------------------------------------------        Click_btnHelp
        private void Click_btnHelp (object sender, EventArgs e)
        {
            info .Visible = true;
            btnHelp .Enabled = false;
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            GraphicalObject grobj;
            Graphics grfx = e .Graphics;
            for (int i = mover .Count - 1; i >= 0; i--)
            {
                grobj = mover [i] .Source;
                if (grobj is Building)
                {
                    (grobj as Building) .Draw (grfx);
                }
                else if (grobj is BuildingsGroup)
                {
                    (grobj as BuildingsGroup) .Draw (grfx);
                }
            }
            info .Draw (grfx);
            groupBtns .Draw (grfx);
            if (bTemporaryFrameInView)
            {
                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));
            }
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            ptMouse_Down = e .Location;
            if (mover .Catch (e .Location, e .Button))
            {
            }
            else
            {
                if (e .Button == MouseButtons .Left)
                {
                    bTemporaryFrameInView = true;
                }
            }
            ContextMenuStrip = null;
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            ptMouse_Up = e .Location;
            double nDist = Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up);

            if (e .Button == MouseButtons .Left)
            {
                int iWasObject, iWasNode;
                if (mover .Release (out iWasObject, out iWasNode))
                {
                    GraphicalObject grobj = mover [iWasObject] .Source;
                    if (grobj is ClosableInfo && iWasNode == 0)
                    {
                        (grobj as ClosableInfo) .Visible = false;
                        btnHelp .Enabled = true;
                        RenewMover ();
                    }
                    if (grobj is Hangar)
                    {
                        (grobj as Hangar) .RedefineCover ();
                    }
                    if (grobj is Building && nDist <= 3)
                    {
                        housePressed = grobj as Building;
                        HouseIdentification ();
                        MoveHouseOnTop (iHousePressed);
                    }
                    if (groupHouses != null && (grobj is Building || grobj is BuildingsGroup))
                    {
                        RectangleF rc = groupHouses .Frame;
                        SetBuildingsGroup (rc);
                        RenewMover ();
                    }
                    Invalidate ();
                }
                else
                {
                    if (bTemporaryFrameInView)
                    {
                        RectangleF rc = new RectangleF (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));
                        SetBuildingsGroup (rc);
                        RenewMover ();
                        bTemporaryFrameInView = false;
                        Invalidate ();
                    }
                }
            }
            else if (e .Button == MouseButtons .Right && nDist <= 3)
            {
                int iWasObject;
                if (mover .Release (out iWasObject))
                {
                    MenuSelection (iWasObject);
                }
                else
                {
                    ContextMenuStrip = menuOnEmpty;
                }
            }
        }
        // -------------------------------------------------        MenuSelection
        private void MenuSelection (int iInMover)
        {
            GraphicalObject grobj = mover [iInMover] .Source;
            if (grobj is Building)
            {
                housePressed = grobj as Building;
                HouseIdentification ();
                ContextMenuStrip = menuOnHouse;
            }
            else if (grobj is ElasticGroup)
            {
                ContextMenuStrip = menuOnBtnsGroup;
            }
            else if (grobj is ClosableInfo)
            {
                ContextMenuStrip = menuOnInfo;
            }
            else if (grobj is BuildingsGroup)
            {
                ContextMenuStrip = menuOnBuildingsGroup;
            }
        }
        // -------------------------------------------------        SetBuildingsGroup
        private void SetBuildingsGroup (RectangleF rc)
        {
            if (groupHouses != null)
            {
                groupHouses = null;
                RenewMover ();
            }
            List<Building> elems = new List<Building> ();
            for (int i = 0; i < houses .Count; i++)
            {
                if (rc .Contains (houses [i] .RectAround ()))
                {
                    elems .Add (houses [i]);
                }
            }
            if (elems .Count > 1)
            {
                groupHouses = new BuildingsGroup (elems);
            }
        }
        // -------------------------------------------------        HouseIdentification
        private void HouseIdentification ()
        {
            for (iHousePressed = houses .Count - 1; iHousePressed >= 0; iHousePressed--)
            {
                if (houses [iHousePressed] .ID == housePressed .ID)
                {
                    break;
                }
            }
        }
        // -------------------------------------------------        MoveHouseOnTop
        private void MoveHouseOnTop (int iMove)
        {
            Building building = houses [iMove];
            houses .RemoveAt (iMove);
            houses .Insert (0, building);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            ptMouse_Move = e .Location;
            if (mover .Move (e .Location))
            {
                GraphicalObject grobj = mover .CaughtSource;
                if (grobj is ElasticGroup ||
                    grobj is FramedControl)
                {
                    Update ();
                }
                Invalidate ();
            }
            else
            {
                if (bTemporaryFrameInView)
                {
                    Invalidate ();
                }
            }
        }
        // -------------------------------------------------        OnContextMenuChanged
        private void OnContextMenuChanged (object sender, EventArgs e)
        {
            if (ContextMenuStrip != null)
            {
                ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
            }
        }
        // -------------------------------------------------        Click_btnPrimitive
        private void Click_btnPrimitive (object sender, EventArgs e)
        {
            PrimitiveHouse house = new PrimitiveHouse (Auxi_Geometry .Middle (ClientRectangle), new Size (24, 12), 16, clrsPrimitiveSmaple);
            InsertHeadHouse (house);
        }
        // -------------------------------------------------        Click_btnRural
        private void Click_btnRural (object sender, EventArgs e)
        {
            RuralHouse house = new RuralHouse (Auxi_Geometry .Middle (ClientRectangle), new Size (24, 12), 16, clrsRuralSmaple);
            InsertHeadHouse (house);
        }
        // -------------------------------------------------        Click_btnRuralLeft
        private void Click_btnRuralLeft (object sender, EventArgs e)
        {
            RuralPlusLeft house = new RuralPlusLeft (Auxi_Geometry .Middle (ClientRectangle), new Size (24, 12), 16, clrsRuralSmaple);
            InsertHeadHouse (house);
        }
        // -------------------------------------------------        Click_btnRuralRight
        private void Click_btnRuralRight (object sender, EventArgs e)
        {
            RuralPlusRight house = new RuralPlusRight (Auxi_Geometry .Middle (ClientRectangle), new Size (24, 12), 16, clrsRuralSmaple);
            InsertHeadHouse (house);
        }
        // -------------------------------------------------        Click_btnHangar
        private void Click_btnHangar (object sender, EventArgs e)
        {
            Hangar house = new Hangar (Auxi_Geometry .Middle (ClientRectangle), 40, clrsHangarSmaple);
            InsertHeadHouse (house);
        }
        // -------------------------------------------------        InsertHeadHouse
        private void InsertHeadHouse (Building house)
        {
            houses .Insert (0, house);
            RenewMover ();
            Invalidate ();
        }

        // *****   menuOnHouse   *****
        // -------------------------------------------------		Opening_menuOnHouse
        private void Opening_menuOnHouse (object sender, CancelEventArgs e)
        {
            ToolStripItemCollection items = menuOnHouse .Items;

            items ["miPutOnTop"] .Enabled = iHousePressed > 0;
            items ["miOneLevelUp"] .Enabled = iHousePressed > 0;
            items ["miOneLevelDown"] .Enabled = iHousePressed < houses .Count - 1;
            items ["miPutUnderneath"] .Enabled = iHousePressed < houses .Count - 1;

            items ["miMirrorBuilding"] .Enabled = housePressed .HouseType == HouseType .RuralPlusLeft ||
                                                  housePressed .HouseType == HouseType .RuralPlusRight;
        }
        // -------------------------------------------------		Click_miPutOnTop
        private void Click_miPutOnTop (object sender, EventArgs e)
        {
            MoveHouseOnTop (iHousePressed);
        }
        // -------------------------------------------------        Click_miOneLevelUp
        private void Click_miOneLevelUp (object sender, EventArgs e)
        {
            houses .Reverse (iHousePressed - 1, 2);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miOneLevelDown
        private void Click_miOneLevelDown (object sender, EventArgs e)
        {
            houses .Reverse (iHousePressed, 2);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miPutUnderneath
        private void Click_miPutUnderneath (object sender, EventArgs e)
        {
            houses .RemoveAt (iHousePressed);
            houses .Add (housePressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miChangeColors
        private void Click_miChangeColors (object sender, EventArgs e)
        {
            Point ptScreen = PointToScreen (ptMouse_Up);
            switch (housePressed .HouseType)
            {
                case HouseType .Primitive:
                    Form_Colors_PrimitiveHouse formPH = new Form_Colors_PrimitiveHouse (housePressed as PrimitiveHouse, ptScreen);
                    formPH .Changed += new EventHandler (HouseParam_Changed);
                    formPH .ShowDialog ();
                    break;

                case HouseType .Rural:
                    Form_Colors_RuralHouse formRH = new Form_Colors_RuralHouse (housePressed as RuralHouse, ptScreen);
                    formRH .Changed += new EventHandler (HouseParam_Changed);
                    formRH .ShowDialog ();
                    break;

                case HouseType .RuralPlusLeft:
                    Form_Colors_PlusLeft formRPL = new Form_Colors_PlusLeft (housePressed as RuralPlusLeft, ptScreen);
                    formRPL .Changed += new EventHandler (HouseParam_Changed);
                    formRPL .ShowDialog ();
                    break;

                case HouseType .RuralPlusRight:
                    Form_Colors_PlusRight formRPR = new Form_Colors_PlusRight (housePressed as RuralPlusRight, ptScreen);
                    formRPR .Changed += new EventHandler (HouseParam_Changed);
                    formRPR .ShowDialog ();
                    break;

                case HouseType .Hangar:
                    Form_Colors_Hangar formHangar = new Form_Colors_Hangar (housePressed as Hangar, ptScreen);
                    formHangar .Changed += new EventHandler (HouseParam_Changed);
                    formHangar .ShowDialog ();
                    break;
            }
        }
        // -------------------------------------------------        Click_miSetColorsFromSample
        private void Click_miSetColorsFromSample (object sender, EventArgs e)
        {
            switch (housePressed .HouseType)
            {
                case HouseType .Primitive:
                    housePressed .SetColors (clrsPrimitiveSmaple);
                    break;

                case HouseType .Rural:
                case HouseType .RuralPlusLeft:
                case HouseType .RuralPlusRight:
                    housePressed .SetColors (clrsRuralSmaple);
                    break;

                case HouseType .Hangar:
                    housePressed .SetColors (clrsHangarSmaple);
                    break;
            }
            Invalidate ();
        }
        // -------------------------------------------------        Click_miUseColorsAsSample
        private void Click_miUseColorsAsSample (object sender, EventArgs e)
        {
            switch (housePressed .HouseType)
            {
                case HouseType .Primitive:
                    clrsPrimitiveSmaple = housePressed .GetColors ();
                    break;

                case HouseType .Rural:
                case HouseType .RuralPlusLeft:
                case HouseType .RuralPlusRight:
                    clrsRuralSmaple = housePressed .GetColors ();
                    break;

                case HouseType .Hangar:
                    clrsHangarSmaple = housePressed .GetColors ();
                    break;
            }
        }
        // -------------------------------------------------		HouseParam_Changed
        private void HouseParam_Changed (object sender, EventArgs ea)
        {
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDuplicateBuilding
        private void Click_miDuplicateBuilding (object sender, EventArgs e)
        {
            Point ptCur = housePressed .Location ();
            Point pt = new Point (ptCur .X + 60, ptCur .Y + 60);
            if (!ClientRectangle .Contains (pt))
            {
                pt = new Point (ptCur .X - 60, ptCur .Y - 60);
            }
            Building newBuilding = housePressed .Copy (pt);
            InsertHeadHouse (newBuilding);
        }
        // -------------------------------------------------		Click_miMirrorBuilding
        private void Click_miMirrorBuilding (object sender, EventArgs e)
        {
            Point ptCur = housePressed .Location ();
            Point pt = new Point (ptCur .X + 100, ptCur .Y);
            if (!ClientRectangle .Contains (pt))
            {
                pt = new Point (ptCur .X - 100, ptCur .Y);
            }
            switch (housePressed .HouseType)
            {
                case HouseType .RuralPlusLeft:
                    RuralPlusLeft houseSrc = housePressed as RuralPlusLeft;
                    RuralPlusRight newHouse = new RuralPlusRight (new Rectangle (pt, houseSrc .MainHouse .Size), houseSrc .RoofHeight,
                                                                  houseSrc .SlopeShift, houseSrc .Garage .Size, houseSrc .GarageRoofHeight,
                                                                  housePressed .GetColors ());
                    InsertHeadHouse (newHouse);
                    break;

                case HouseType .RuralPlusRight:
                    RuralPlusRight src = housePressed as RuralPlusRight;
                    RuralPlusLeft newBuilding = new RuralPlusLeft (new Rectangle (pt, src .MainHouse .Size), src .RoofHeight,
                                                                   src .SlopeShift, src .Garage .Size, src .GarageRoofHeight,
                                                                   housePressed .GetColors ());
                    InsertHeadHouse (newBuilding);
                    break;
            }
        }
        // -------------------------------------------------		Click_miEraseBuilding
        private void Click_miEraseBuilding (object sender, EventArgs e)
        {
            houses .RemoveAt (iHousePressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------		Click_miEraseAllBuildings
        private void Click_miEraseAllBuildings (object sender, EventArgs e)
        {
            if (DialogResult .Yes == MessageBox .Show ("Do you want to erase all the buildings?", "Erasing all", MessageBoxButtons .YesNo,
                                                       MessageBoxIcon .Question, MessageBoxDefaultButton .Button1))
            {
                houses .Clear ();
                RenewMover ();
                Invalidate ();
            }
        }
        // -------------------------------------------------		Click_miSaveIntoClipboard
        private void Click_miSaveIntoClipboard (object sender, EventArgs e)
        {
            Bitmap bitmap = new Bitmap (ClientRectangle .Width, ClientRectangle .Height);
            Graphics grfx = Graphics .FromImage (bitmap);
            grfx .Clear (Color .White);
            for (int i = houses .Count - 1; i >= 0; i--)
            {
                houses [i] .Draw (grfx);
            }
            Clipboard .Clear ();
            Clipboard .SetDataObject (bitmap, true);
            grfx .Dispose ();
        }
        // -------------------------------------------------		Click_miSaveToFile
        private void Click_miSaveToFile (object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog ();
            dlg .Filter = strFilter;
            DialogResult dr = dlg .ShowDialog ();
            if (dr == DialogResult .OK)
            {
                FileStream fs;
                BinaryWriter bw = null;
                try
                {
                    fs = new FileStream (dlg .FileName, FileMode .Create, FileAccess .Write, FileShare .Write);
                    bw = new BinaryWriter (fs);
                    WritingIntoFile (bw);
                }
                catch
                {
                    string strText = "Can't write into file" + dlg .FileName;
                    MessageBox .Show (strText, "Problem on Saving", MessageBoxButtons .OK, MessageBoxIcon .Information);
                }
                finally
                {
                    bw .Close ();
                }
            }
        }
        // -------------------------------------------------        WritingIntoFile
        private void WritingIntoFile (BinaryWriter bw)
        {
            string str = "Village";
            bw .Write (str);

            Auxi_IO .Write_Size (bw, ClientSize);
            Auxi_IO .Write_Point (bw, btnHelp .Location);
            groupBtns .IntoFile (bw);
            for (int i = 0; i < 5; i++)
            {
                Auxi_IO .Write_Color (bw, clrsPrimitiveSmaple [i]);
            }
            for (int i = 0; i < 5; i++)
            {
                Auxi_IO .Write_Color (bw, clrsRuralSmaple [i]);
            }
            for (int i = 0; i < 5; i++)
            {
                Auxi_IO .Write_Color (bw, clrsHangarSmaple [i]);
            }

            bw .Write (houses .Count);
            for (int i = 0; i < houses .Count; i++)
            {
                bw .Write ((int) (houses [i] .HouseType));
            }
            for (int i = 0; i < houses .Count; i++)
            {
                houses [i] .IntoFile (bw);
            }
        }
        // -------------------------------------------------		ReadingFromFile
        private void ReadingFromFile (BinaryReader br, ReadingType readtype)
        {
            string str = br .ReadString ();
            if (str == "Village")
            {
                if (readtype == ReadingType .Everything)
                {
                    ClientSize = Auxi_IO .Read_Size (br);
                    btnHelp .Location = Auxi_IO .Read_Point (br);
                    groupBtns = ElasticGroup .FromFile (this, br, controlsInGroup);
                    for (int i = 0; i < 5; i++)
                    {
                        clrsPrimitiveSmaple [i] = Auxi_IO .Read_Color (br);
                    }
                    for (int i = 0; i < 5; i++)
                    {
                        clrsRuralSmaple [i] = Auxi_IO .Read_Color (br);
                    }
                    for (int i = 0; i < 5; i++)
                    {
                        clrsHangarSmaple [i] = Auxi_IO .Read_Color (br);
                    }

                    houses .Clear ();
                }
                else
                {
                    // to skip
                    Auxi_IO .Read_Size (br);
                    Auxi_IO .Read_Point (br);
                    ElasticGroup grp = ElasticGroup .FromFile (this, br, controlsInGroup);
                    for (int i = 0; i < 15; i++)
                    {
                        Auxi_IO .Read_Color (br);
                    }
                }
                int nBuilding = br .ReadInt32 ();
                if (nBuilding > 0)
                {
                    int [] nTypes = new int [nBuilding];
                    for (int i = 0; i < nBuilding; i++)
                    {
                        nTypes [i] = br .ReadInt32 ();
                    }
                    for (int i = 0; i < nBuilding; i++)
                    {
                        HouseType type = (HouseType) nTypes [i];
                        switch (type)
                        {
                            case HouseType .Primitive:
                                PrimitiveHouse prim = PrimitiveHouse .FromFile (br);
                                if (prim != null)
                                {
                                    houses .Add (prim);
                                }
                                break;
                            case HouseType .Rural:
                                RuralHouse rural = RuralHouse .FromFile (br);
                                if (rural != null)
                                {
                                    houses .Add (rural);
                                }
                                break;
                            case HouseType .RuralPlusLeft:
                                RuralPlusLeft houseL = RuralPlusLeft .FromFile (br);
                                if (houseL != null)
                                {
                                    houses .Add (houseL);
                                }
                                break;
                            case HouseType .RuralPlusRight:
                                RuralPlusRight houseR = RuralPlusRight .FromFile (br);
                                if (houseR != null)
                                {
                                    houses .Add (houseR);
                                }
                                break;
                            case HouseType .Hangar:
                                Hangar hangar = Hangar .FromFile (br);
                                if (hangar != null)
                                {
                                    houses .Add (hangar);
                                }
                                break;
                        }
                    }
                }
            }
        }
        // -------------------------------------------------		Click_miRestoreFromFile
        private void Click_miRestoreFromFile (object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog ();
            dlg .Filter = strFilter;
            if (DialogResult .OK == dlg .ShowDialog ())
            {
                FileStream fs;
                BinaryReader br = null;

                try
                {
                    fs = new FileStream (dlg .FileName, FileMode .Open, FileAccess .Read);
                    br = new BinaryReader (fs);

                    ReadingFromFile (br, ReadingType .Everything);
                    RenewMover ();
                    Invalidate ();
                }
                finally
                {
                    br .Close ();
                }
            }
        }
        // -------------------------------------------------		Click_miAddHousesFromFile
        private void Click_miAddHousesFromFile (object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog ();
            dlg .Filter = strFilter;
            if (DialogResult .OK == dlg .ShowDialog ())
            {
                FileStream fs;
                BinaryReader br = null;

                try
                {
                    fs = new FileStream (dlg .FileName, FileMode .Open, FileAccess .Read);
                    br = new BinaryReader (fs);

                    ReadingFromFile (br, ReadingType .HousesOnly);
                    RenewMover ();
                    Invalidate ();
                }
                finally
                {
                    br .Close ();
                }
            }
        }

        // *****   menuOnEmpty   *****
        // -------------------------------------------------		Opening_menuOnEmpty
        private void Opening_menuOnEmpty (object sender, CancelEventArgs e)
        {
            bool bHousesInView = houses .Count > 0;
            menuOnEmpty .Items ["miEraseAll"] .Enabled = bHousesInView;
            menuOnEmpty .Items ["miSaveIntoClipboard"] .Enabled = bHousesInView;
            menuOnEmpty .Items ["miSaveToFile"] .Enabled = bHousesInView;
        }

        // *****   menuOnBtnsGroup   *****
        // -------------------------------------------------		Opening_menuOnBtnsGroup
        private void Opening_menuOnBtnsGroup (object sender, CancelEventArgs e)
        {
            menuOnBtnsGroup .Items ["miFixUnfixElements"] .Text = groupBtns .ElementsMovable ? "Fix group's elements" : "Unfix group's elements";
        }
        // -------------------------------------------------        Click_miModifyGroup
        private void Click_miModifyGroup (object sender, EventArgs e)
        {
            groupBtns .ParametersDialog (this, PointToScreen (ptMouse_Up), ParametersChanged);
        }
        // -------------------------------------------------		ParametersChanged
        private void ParametersChanged (object sender, EventArgs ea)
        {
            groupBtns .Update ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miFixUnfixElements
        private void Click_miFixUnfixElements (object sender, EventArgs e)
        {
            groupBtns .ElementsMovable = !groupBtns .ElementsMovable;
            RenewMover ();
        }
        // -------------------------------------------------        Click_miBtnsInRow
        private void Click_miBtnsInRow (object sender, EventArgs e)
        {
            int cy = controlsInGroup [0] .Top;
            for (int i = 1; i < controlsInGroup .Length; i++)
            {
                controlsInGroup [i] .Location = new Point (controlsInGroup [i - 1] .Right + 6, cy);
            }
            groupBtns .Update ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miBtnsInColumn
        private void Click_miBtnsInColumn (object sender, EventArgs e)
        {
            int cx = controlsInGroup [0] .Left;
            for (int i = 1; i < controlsInGroup .Length; i++)
            {
                controlsInGroup [i] .Location = new Point (cx, controlsInGroup [i - 1] .Bottom + 6);
            }
            groupBtns .Update ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDefaultView
        private void Click_miDefaultView (object sender, EventArgs e)
        {
            groupBtns .CloseAllTuningForms ();
            controlsInGroup [0] .Location = new Point (32, 123);
            int cx = controlsInGroup [0] .Left;
            for (int i = 1; i < controlsInGroup .Length; i++)
            {
                controlsInGroup [i] .Location = new Point (cx, controlsInGroup [i - 1] .Bottom + 6);
            }
            groupBtns = new ElasticGroup (this, controlsInGroup, new int [] {10, 6, 10, 10}, "Buildings");
            RenewMover ();
            Invalidate ();
        }

        // *****   menuOnInfo   *****
        // -------------------------------------------------        Opening_menuOnInfo
        private void Opening_menuOnInfo (object sender, CancelEventArgs e)
        {
            ((ToolStripMenuItem) (menuOnInfo .Items ["miInfoShowFrame"])) .Checked = info .ShowFrame;
        }
        // -------------------------------------------------        Click_miInfoFont
        private void Click_miInfoFont (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = info .Font;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                info .Font = dlg .Font;
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_miInfoTextColor
        private void Click_miInfoTextColor (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = info .Color;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                info .Color = dlg .Color;
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_miInfoBackColor
        private void Click_miInfoBackColor (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = info .BackColor;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                info .BackColor = dlg .Color;
                Invalidate ();
            }
        }
        // -------------------------------------------------        DropDownOpening_miInfoTransparency
        private void DropDownOpening_miInfoTransparency (object sender, EventArgs e)
        {
            ToolStripMenuItem itemParent = (ToolStripMenuItem) sender;
            ToolStripItemCollection items = itemParent .DropDownItems;

            int A = info .BackColor .A;
            for (int i = 0; i < items .Count; i++)
            {
                ((ToolStripMenuItem) items [i]) .Checked = A == Convert .ToInt32 (255 * (1 - Convert .ToSingle (items [i] .Text)));
            }
        }
        // -------------------------------------------------        Click_miInfoTransparency
        private void Click_miInfoTransparency (object sender, EventArgs e)
        {
            Color clrBack = info .BackColor;
            Color clrNew = Color .FromArgb (Convert .ToInt32 (255 * (1 - Convert .ToSingle ((sender as ToolStripMenuItem) .Text))),
                                            clrBack .R, clrBack .G, clrBack .B);
            info .BackColor = clrNew;
            Invalidate ();
        }
        // -------------------------------------------------        Click_miInfoShowFrame
        private void Click_miInfoShowFrame (object sender, EventArgs e)
        {
            info .ShowFrame = !info .ShowFrame;
            Invalidate ();
        }

        // *****   menuOnBuildingsGroup   *****
        // -------------------------------------------------        Click_miSetColorsFromSamples
        private void Click_miSetColorsFromSamples (object sender, EventArgs e)
        {
            for (int i = 0; i < groupHouses .Elements .Count; i++)
            {
                HouseType htype = groupHouses .Elements [i] .HouseType;
                if (htype == HouseType .Primitive)
                {
                    groupHouses .Elements [i] .SetColors (clrsPrimitiveSmaple);
                }
                else if (htype == HouseType .Hangar)
                {
                    groupHouses .Elements [i] .SetColors (clrsHangarSmaple);
                }
                else
                {
                    groupHouses .Elements [i] .SetColors (clrsRuralSmaple);
                }
            }
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDuplicateBuildings
        private void Click_miDuplicateBuildings (object sender, EventArgs e)
        {
            Point ptCur, pt;
            for (int i = groupHouses .Elements .Count - 1; i >= 0; i--)
            {
                ptCur = groupHouses .Elements [i] .Location ();
                pt = new Point (ptCur .X + 60, ptCur .Y + 60);
                Building newBuilding = groupHouses .Elements [i] .Copy (pt);
                houses .Insert (0, newBuilding);
            }
            RectangleF rc = groupHouses .Frame;
            SetBuildingsGroup (rc);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miEraseBuildings
        private void Click_miEraseBuildings (object sender, EventArgs e)
        {
            for (int i = groupHouses .Elements .Count - 1; i >= 0; i--)
            {
                houses .Remove (groupHouses .Elements [i]);
            }
            groupHouses = null;
            RenewMover ();
            Invalidate ();
        }

        const string nameMain = "Main";
        const string nameTypes = "Types";
        const string namePrimitiveSample = "PrimitiveSample";
        const string nameRuralSample = "RuralSample";
        const string nameHangarSample = "HangarSample";
        // -------------------------------------------------        SaveInfoToRegistry
        private void SaveInfoToRegistry ()
        {
            string strRegKey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
                if (regkey != null)
                {
                    regkey .SetValue (nameMain, new string [] { ClientSize .Width .ToString (),     // 0
                                                                ClientSize .Height .ToString (),    // 1
                                                                btnHelp .Location .X .ToString (),  // 2
                                                                btnHelp .Location .Y .ToString (),  // 3
                                                                houses .Count .ToString () },
                                      RegistryValueKind .MultiString);
                    groupBtns .IntoRegistry (regkey, "Group");
                    info .IntoRegistry (regkey, "Info");
                    string [] types = new string [houses .Count];
                    for (int i = 0; i < houses .Count; i++)
                    {
                        types [i] = ((int) (houses [i] .HouseType)) .ToString ();
                    }
                    regkey .SetValue (nameTypes, types, RegistryValueKind .MultiString);
                    for (int i = 0; i < houses .Count; i++)
                    {
                        houses [i] .IntoRegistry (regkey, "Building_" + i .ToString ());
                    }
                    string [] strs = new string [4 * clrsPrimitiveSmaple .Length];
                    for (int i = 0; i < clrsPrimitiveSmaple .Length; i++)
                    {
                        strs [i * 4]     = ((int) (clrsPrimitiveSmaple [i] .A)) .ToString ();
                        strs [i * 4 + 1] = ((int) (clrsPrimitiveSmaple [i] .R)) .ToString ();
                        strs [i * 4 + 2] = ((int) (clrsPrimitiveSmaple [i] .G)) .ToString ();
                        strs [i * 4 + 3] = ((int) (clrsPrimitiveSmaple [i] .B)) .ToString ();
                    }
                    regkey .SetValue (namePrimitiveSample, strs, RegistryValueKind .MultiString);

                    for (int i = 0; i < clrsRuralSmaple .Length; i++)
                    {
                        strs [i * 4]     = ((int) (clrsRuralSmaple [i] .A)) .ToString ();
                        strs [i * 4 + 1] = ((int) (clrsRuralSmaple [i] .R)) .ToString ();
                        strs [i * 4 + 2] = ((int) (clrsRuralSmaple [i] .G)) .ToString ();
                        strs [i * 4 + 3] = ((int) (clrsRuralSmaple [i] .B)) .ToString ();
                    }
                    regkey .SetValue (nameRuralSample, strs, RegistryValueKind .MultiString);

                    for (int i = 0; i < clrsHangarSmaple .Length; i++)
                    {
                        strs [i * 4]     = ((int) (clrsHangarSmaple [i] .A)) .ToString ();
                        strs [i * 4 + 1] = ((int) (clrsHangarSmaple [i] .R)) .ToString ();
                        strs [i * 4 + 2] = ((int) (clrsHangarSmaple [i] .G)) .ToString ();
                        strs [i * 4 + 3] = ((int) (clrsHangarSmaple [i] .B)) .ToString ();
                    }
                    regkey .SetValue (nameHangarSample, strs, RegistryValueKind .MultiString);
                }
            }
            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 == 5)
                    {
                        ClientSize = Auxi_Convert .ToSize (strs, 0);
                        btnHelp .Location = Auxi_Convert .ToPoint (strs, 2);
                        int nHouses = Convert .ToInt32 (strs [4]);
                        ElasticGroup groupRead = ElasticGroup .FromRegistry (this, regkey, "Group", controlsInGroup);
                        if (groupRead != null)
                        {
                            groupBtns = groupRead;
                        }
                        ClosableInfo infoRead = ClosableInfo .FromRegistry (this, regkey, "Info");
                        if (infoRead != null)
                        {
                            info = infoRead;
                        }
                        if (nHouses > 0)
                        {
                            string [] strTypes = (string []) regkey .GetValue (nameTypes);
                            if (strTypes != null && strTypes .Length == nHouses)
                            {
                                houses .Clear ();
                                for (int i = 0; i < nHouses; i++)
                                {
                                    HouseType type = (HouseType) Convert .ToInt32 (strTypes [i]);
                                    switch (type)
                                    {
                                        case HouseType .Primitive:
                                            PrimitiveHouse newPrimitive = PrimitiveHouse .FromRegistry (regkey, "Building_" + i .ToString ());
                                            if (newPrimitive != null)
                                            {
                                                houses .Add (newPrimitive);
                                            }
                                            break;
                                        case HouseType .Rural:
                                            RuralHouse newRural = RuralHouse .FromRegistry (regkey, "Building_" + i .ToString ());
                                            if (newRural != null)
                                            {
                                                houses .Add (newRural);
                                            }
                                            break;
                                        case HouseType .RuralPlusLeft:
                                            RuralPlusLeft newRuralLeft = RuralPlusLeft .FromRegistry (regkey, "Building_" + i .ToString ());
                                            if (newRuralLeft != null)
                                            {
                                                houses .Add (newRuralLeft);
                                            }
                                            break;
                                        case HouseType .RuralPlusRight:
                                            RuralPlusRight newRuralRight = RuralPlusRight .FromRegistry (regkey, "Building_" + i .ToString ());
                                            if (newRuralRight != null)
                                            {
                                                houses .Add (newRuralRight);
                                            }
                                            break;
                                        case HouseType .Hangar:
                                            Hangar newHangar = Hangar .FromRegistry (regkey, "Building_" + i .ToString ());
                                            if (newHangar != null)
                                            {
                                                houses .Add (newHangar);
                                            }
                                            break;
                                    }
                                }
                            }
                        }
                    }
                    string [] strClrs = (string []) regkey .GetValue (namePrimitiveSample);
                    if (strClrs != null && strClrs .Length == 20)
                    {
                        Color [] clrs = new Color [5];
                        for (int i = 0; i < 5; i++)
                        {
                            clrs [i] = Auxi_Convert .ToColor (strClrs, i * 4);
                        }
                        clrsPrimitiveSmaple = clrs;
                    }
                    strClrs = (string []) regkey .GetValue (nameRuralSample);
                    if (strClrs != null && strClrs .Length == 20)
                    {
                        Color [] clrs = new Color [5];
                        for (int i = 0; i < 5; i++)
                        {
                            clrs [i] = Auxi_Convert .ToColor (strClrs, i * 4);
                        }
                        clrsRuralSmaple = clrs;
                    }
                    strClrs = (string []) regkey .GetValue (nameHangarSample);
                    if (strClrs != null && strClrs .Length == 20)
                    {
                        Color [] clrs = new Color [5];
                        for (int i = 0; i < 5; i++)
                        {
                            clrs [i] = Auxi_Convert .ToColor (strClrs, i * 4);
                        }
                        clrsHangarSmaple = clrs;
                    }
                }
            }
            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)

Share

About the Author

SergeyAndreyev

United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150506.1 | Last Updated 10 Apr 2010
Article Copyright 2010 by SergeyAndreyev
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid