using System;
using System .Collections .Generic;
using System .Drawing;
using System .Windows .Forms;
using System .IO;
using Microsoft .Win32;
using MoveGraphLibrary;
namespace UserDrivenApplications
{
public class PrimitiveHouse : Building
{
int version = 606;
Rectangle rcHouse; // points 0 - 1 - 2 - 3
Point ptPien; // point 4
int roomSize = 18;
int windowSize = 10;
int doorH = 12;
int doorW = 6;
int minRoofH = 16;
int minHouseWidth, minHouseHeight;
int maxHouseWidth, maxHouseHeight, maxRoofH;
// -------------------------------------------------
public PrimitiveHouse (Rectangle rc, int roofH, Color [] clrs)
{
houseType = HouseType .Primitive;
SetColors (clrs);
DefaultSizes ();
rcHouse = new Rectangle (rc .Left, rc .Top,
Math .Min (Math .Max (minHouseWidth, rc .Width), maxHouseWidth),
Math .Min (Math .Max (minHouseHeight, rc .Height), maxHouseHeight));
ptPien = new Point ((rcHouse .Left + rcHouse .Right) / 2,
rcHouse .Top - Math .Min (Math .Max (minRoofH, Math .Abs (roofH)), maxRoofH));
}
public PrimitiveHouse (Point ptLT, Size sizeHouse, int roofH, Color [] clrs)
: this (new Rectangle (ptLT, sizeHouse), roofH, clrs)
{
}
public PrimitiveHouse (Rectangle rc, Color [] clrs)
: this (rc, 26, clrs)
{
}
public PrimitiveHouse (Point ptLT, Size sizeHouse, Color [] clrs)
: this (new Rectangle (ptLT, sizeHouse), clrs)
{
}
// ------------------------------------------------- DefaultSizes
public void DefaultSizes ()
{
minHouseWidth = roomSize * 2 + 4;
maxHouseWidth = roomSize * 5 + 4;
minHouseHeight = roomSize + 6;
maxHouseHeight = roomSize * 3 + 4;
maxRoofH = maxHouseHeight;
}
// ------------------------------------------------- Location
public override Point Location ()
{
return (rcHouse .Location);
}
// ------------------------------------------------- RectAround
public override RectangleF RectAround ()
{
return (new Rectangle (rcHouse .Left, ptPien .Y, rcHouse .Width, rcHouse .Bottom - ptPien .Y));
}
// ------------------------------------------------- MainHouse
public Rectangle MainHouse
{
get { return (rcHouse); }
}
// ------------------------------------------------- RoofHeight
public int RoofHeight
{
get { return (rcHouse .Top - ptPien .Y); }
}
// ------------------------------------------------- Copy
public override Building Copy (Point ptLT)
{
PrimitiveHouse houseNew = new PrimitiveHouse (new Rectangle (ptLT, MainHouse .Size), RoofHeight, GetColors ());
return ((Building) houseNew);
}
// ------------------------------------------------- Draw
public override void Draw (Graphics grfx)
{
grfx .FillRectangle (brushHouse, rcHouse);
grfx .DrawRectangle (penEdge, rcHouse);
Point [] ptRoof = new Point [3] { new Point (rcHouse .Left, rcHouse .Top), ptPien, new Point (rcHouse .Right, rcHouse .Top) };
grfx .FillPolygon (brushRoof, ptRoof);
grfx .DrawPolygon (penEdge, ptRoof);
// fill with windows
int nInRow = rcHouse .Width / roomSize;
int nFloor = rcHouse .Height / roomSize;
int jDoor = nInRow / 2;
int cxL = rcHouse .Left + (rcHouse .Width - nInRow * roomSize) / 2;
for (int iFloor = 0; iFloor < nFloor; iFloor++)
{
for (int j = 0; j < nInRow; j++)
{
Rectangle rcRoom = new Rectangle (cxL + j * roomSize, rcHouse .Bottom - (iFloor + 1) * roomSize, roomSize, roomSize);
if (iFloor == 0 && j == jDoor)
{
Rectangle rcDoor = new Rectangle (rcRoom .Left + (rcRoom .Width - doorW) / 2, rcRoom .Bottom - doorH, doorW, doorH);
grfx .FillRectangle (brushDoor, rcDoor);
grfx .DrawRectangle (penEdge, rcDoor);
}
else
{
Rectangle rcWin = new Rectangle (rcRoom .Left + (rcRoom .Width - windowSize) / 2,
rcRoom .Top + 4, windowSize, windowSize);
grfx .FillRectangle (brushWindow, rcWin);
grfx .DrawRectangle (penEdge, rcWin);
}
}
}
}
// ------------------------------------------------- DefineCover
// order of nodes: 4 on corners (LT, RT, RB, LB), 0, 1, 2, 3
// 1 on pien, 4
// 4 strips on sides (left, top, right, bottom) 5, 6, 7, 8
// 1 polygon node (pentagon) to fill the inner area 9
public override void DefineCover ()
{
CoverNode [] nodes = new CoverNode [4 + 1 + 4 + 1];
nodes [0] = new CoverNode (0, new PointF (rcHouse .Left, rcHouse .Top), Cursors .SizeNWSE);
nodes [1] = new CoverNode (1, new PointF (rcHouse .Right, rcHouse .Top), Cursors .SizeNESW);
nodes [2] = new CoverNode (2, new PointF (rcHouse .Right, rcHouse .Bottom), Cursors .SizeNWSE);
nodes [3] = new CoverNode (3, new PointF (rcHouse .Left, rcHouse .Bottom), Cursors .SizeNESW);
nodes [4] = new CoverNode (4, ptPien);
nodes [5] = new CoverNode (5, new PointF [] { new PointF (rcHouse .Left, rcHouse .Top), new PointF (rcHouse .Left, rcHouse .Bottom) },
Cursors .SizeWE);
nodes [6] = new CoverNode (6, new PointF [] { new PointF (rcHouse .Left, rcHouse .Top), new PointF (rcHouse .Right, rcHouse .Top) },
Cursors .SizeNS);
nodes [7] = new CoverNode (7, new PointF [] { new PointF (rcHouse .Right, rcHouse .Top), new PointF (rcHouse .Right, rcHouse .Bottom) },
Cursors .SizeWE);
nodes [8] = new CoverNode (8, new PointF [] { new PointF (rcHouse .Left, rcHouse .Bottom), new PointF (rcHouse .Right, rcHouse .Bottom) },
Cursors .SizeNS);
nodes [9] = new CoverNode (9, new PointF [] {new PointF (rcHouse .Left, rcHouse .Top),
ptPien,
new PointF (rcHouse .Right, rcHouse .Top),
new PointF (rcHouse .Right, rcHouse .Bottom),
new PointF (rcHouse .Left, rcHouse .Bottom) });
cover = new Cover (nodes);
}
// -------------------------------------------------
public override void Move (int dx, int dy)
{
rcHouse .X += dx;
rcHouse .Y += dy;
ptPien += new Size (dx, dy);
}
// ------------------------------------------------- MoveNode
// this move depends on individual restrictions
public override bool MoveNode (int i, int dx, int dy, Point ptM, MouseButtons catcher)
{
bool bRet = false;
if (catcher == MouseButtons .Left)
{
int hNew, wNew;
if (i == 9)
{
Move (dx, dy);
}
else if (i == 0) //LT corner
{
hNew = rcHouse .Height - dy;
if (minHouseHeight <= hNew && hNew <= maxHouseHeight)
{
MoveCeiling (dy);
bRet = true;
}
wNew = rcHouse .Width - dx;
if (minHouseWidth <= wNew && wNew <= maxHouseWidth)
{
MoveLeftSide (dx);
bRet = true;
}
}
else if (i == 1) // Right Top corner
{
hNew = rcHouse .Height - dy;
if (minHouseHeight <= hNew && hNew <= maxHouseHeight)
{
MoveCeiling (dy);
bRet = true;
}
wNew = rcHouse .Width + dx;
if (minHouseWidth <= wNew && wNew <= maxHouseWidth)
{
MoveRightSide (dx);
bRet = true;
}
}
else if (i == 2) // Right Bottom corner
{
hNew = rcHouse .Height + dy;
if (minHouseHeight <= hNew && hNew <= maxHouseHeight)
{
MoveFloor (dy);
bRet = true;
}
wNew = rcHouse .Width + dx;
if (minHouseWidth <= wNew && wNew <= maxHouseWidth)
{
MoveRightSide (dx);
bRet = true;
}
}
else if (i == 3) // Left Bottom corner
{
hNew = rcHouse .Height + dy;
if (minHouseHeight <= hNew && hNew <= maxHouseHeight)
{
MoveFloor (dy);
bRet = true;
}
wNew = rcHouse .Width - dx;
if (minHouseWidth <= wNew && wNew <= maxHouseWidth)
{
MoveLeftSide (dx);
bRet = true;
}
}
else if (i == 4) // Pien
{
hNew = rcHouse .Top - (ptPien .Y + dy);
if (minRoofH <= hNew && hNew <= maxRoofH)
{
ptPien .Y += dy;
bRet = true;
}
}
else if (i == 5) // left side
{
wNew = rcHouse .Width - dx;
if (minHouseWidth <= wNew && wNew <= maxHouseWidth)
{
MoveLeftSide (dx);
bRet = true;
}
}
else if (i == 6) // top side
{
hNew = rcHouse .Height - dy;
if (minHouseHeight <= hNew && hNew <= maxHouseHeight)
{
MoveCeiling (dy);
bRet = true;
}
}
else if (i == 7) // right side
{
wNew = rcHouse .Width + dx;
if (minHouseWidth <= wNew && wNew <= maxHouseWidth)
{
MoveRightSide (dx);
bRet = true;
}
}
else if (i == 8) // bottom side
{
hNew = rcHouse .Height + dy;
if (minHouseHeight <= hNew && hNew <= maxHouseHeight)
{
MoveFloor (dy);
bRet = true;
}
}
}
return (bRet);
}
// ------------------------------------------------- MoveCeiling
private void MoveCeiling (int dy)
{
rcHouse .Y += dy;
rcHouse .Height -= dy;
ptPien .Y += dy;
}
// ------------------------------------------------- MoveFloor
private void MoveFloor (int dy)
{
rcHouse .Height += dy;
}
// ------------------------------------------------- MoveLeftSide
private void MoveLeftSide (int dx)
{
rcHouse .X += dx;
rcHouse .Width -= dx;
ptPien .X = (rcHouse .Left + rcHouse .Right) / 2;
}
// ------------------------------------------------- MoveRightSide
private void MoveRightSide (int dx)
{
rcHouse .Width += dx;
ptPien .X = (rcHouse .Left + rcHouse .Right) / 2;
}
// ------------------------------------------------- IntoRegistry
public override void IntoRegistry (RegistryKey regkey, string name)
{
try
{
regkey .SetValue (name, new string [] { version .ToString (), // 0
id .ToString (), // 1
rcHouse .Left .ToString (), // 2
rcHouse .Top .ToString (), // 3
rcHouse .Width .ToString (), // 4
rcHouse .Height .ToString (), // 5
RoofHeight .ToString (), // 6
((int) (HouseColor .A)) .ToString (), // 7
((int) (HouseColor .R)) .ToString (), //
((int) (HouseColor .G)) .ToString (), //
((int) (HouseColor .B)) .ToString (), //
((int) (RoofColor .A)) .ToString (), // 11
((int) (RoofColor .R)) .ToString (), //
((int) (RoofColor .G)) .ToString (), //
((int) (RoofColor .B)) .ToString (), //
((int) (WindowColor .A)) .ToString (), // 15
((int) (WindowColor .R)) .ToString (), //
((int) (WindowColor .G)) .ToString (), //
((int) (WindowColor .B)) .ToString (), //
((int) (DoorColor .A)) .ToString (), // 19
((int) (DoorColor .R)) .ToString (), //
((int) (DoorColor .G)) .ToString (), //
((int) (DoorColor .B)) .ToString (), //
((int) (EdgeColor .A)) .ToString (), // 23
((int) (EdgeColor .R)) .ToString (), //
((int) (EdgeColor .G)) .ToString (), //
((int) (EdgeColor .B)) .ToString (), //
},
RegistryValueKind .MultiString);
}
catch
{
}
finally
{
}
}
// ------------------------------------------------- FromRegistry
public static PrimitiveHouse FromRegistry (RegistryKey regkey, string name)
{
try
{
PrimitiveHouse house = null;
string [] strs = (string []) regkey .GetValue (name);
if (strs != null && strs .Length == 27 && Convert .ToInt32 (strs [0]) == 606)
{
house = new PrimitiveHouse (Auxi_Convert .ToRectangle (strs, 2), Convert .ToInt32 (strs [6]),
new Color [] {Auxi_Convert .ToColor (strs, 7),
Auxi_Convert .ToColor (strs, 11),
Auxi_Convert .ToColor (strs, 15),
Auxi_Convert .ToColor (strs, 19),
Auxi_Convert .ToColor (strs, 23)});
if (house != null)
{
house .ID = Convert .ToInt64 (strs [1]);
}
}
return (house);
}
catch
{
return (null);
}
finally
{
}
}
// ------------------------------------------------- IntoFile
public override void IntoFile (BinaryWriter bw)
{
try
{
bw .Write (version); // 0
bw .Write (id); // 1
Auxi_IO .Write_Rectangle (bw, rcHouse); // 2 - 5
bw .Write (RoofHeight); // 6
Auxi_IO .Write_Color (bw, HouseColor); // 7 - 10
Auxi_IO .Write_Color (bw, RoofColor); // 11
Auxi_IO .Write_Color (bw, WindowColor); // 15
Auxi_IO .Write_Color (bw, DoorColor); // 19
Auxi_IO .Write_Color (bw, EdgeColor); // 23
}
catch
{
}
finally
{
}
}
// ------------------------------------------------- FromFile
public static PrimitiveHouse FromFile (BinaryReader br)
{
try
{
PrimitiveHouse house = null;
int nVer = br .ReadInt32 (); // 0
long idRead = br .ReadInt64 (); // 1
if (nVer == 606)
{
house = new PrimitiveHouse (Auxi_IO .Read_Rectangle (br), // 2 - 5
br .ReadInt32 (), // 6
new Color [] {Auxi_IO .Read_Color (br), // 7 - 10
Auxi_IO .Read_Color (br), // 11
Auxi_IO .Read_Color (br), // 15
Auxi_IO .Read_Color (br), // 19
Auxi_IO .Read_Color (br)}); // 23
if (house != null)
{
house .ID = idRead;
}
}
return (house);
}
catch
{
return (null);
}
finally
{
}
}
}
}