using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Windows .Forms;
using MoveGraphLibrary;
namespace TheoryOfMoveableObjects
{
public partial class Form_Polygons : Form
{
Mover mover;
Point ptMouse_Down, ptMouse_Up;
Random rand;
int nSeed;
Group groupRegular, groupPerforated, groupChatoyant;
Color clrNewRegular = Color .Blue;
Color clrNewPerforated = Color .Red;
Color clrChatPolyCenter = Color .White;
Color clrStart = Color .Green;
Color clrEnd = Color .Cyan;
bool bShowCovers = false;
ElementRsRt elemPressed;
List<ElementRsRt> elements = new List<ElementRsRt> ();
Spaces spaces;
string [] strsRegular = new string [] { "Sides", "Color" };
string [] strsChatoyant = new string [] { "Chatoyant polygon", "Apexes", "Colors", "Center color" };
TextM info;
string infotext = "Figures can be added, resized, and modified.\r\n" +
"To duplicate or delete figures and to change\r\n" +
"their order use the context menu.\r\n" +
"No rotation in this form (look into Rotation).\r\n" +
"Groups and controls outside the groups are moveable.";
// -------------------------------------------------
public Form_Polygons ()
{
InitializeComponent ();
mover = new Mover (this);
rand = Auxi_Common .RandomByCurTime (out nSeed);
info = new TextM (this, new Point (360, 460), infotext);
info .BackColor = Color .LightYellow;
spaces = new Spaces (this);
InRegularGroupPositioning ();
Rectangle rc = Auxi_Geometry .FrameAroundControls (new Control [] { numericUD_RegSides, panelRegularClr, btnAddRegular }, spaces);
groupRegular = new Group (this, rc, "Regular polygon", MoveRegularGroup);
InPerforatedGroupPositioning ();
rc = Auxi_Geometry .FrameAroundControls (new Control [] { numericUD_PerfoSides, panelPerforatedClr, btnAddPerforated }, spaces);
groupPerforated = new Group (this, rc, "Perforated polygon", MovePerforatedGroup);
InChatoyantGroupPositioning ();
rc = Auxi_Geometry .FrameAroundControls (new Control [] {numericUD_ChatApexes, btnStartChatClr, btnEndChatClr,
panelChatCenterClr, btnAddChatoyant }, spaces);
groupChatoyant = new Group (this, rc, strsChatoyant [0], MoveChatoyantGroup);
InitialElements ();
RenewMover ();
panelRegularClr .BackColor = clrNewRegular;
panelPerforatedClr .BackColor = clrNewPerforated;
panelChatCenterClr .BackColor = clrChatPolyCenter;
}
// ------------------------------------------------- InitialElements
void InitialElements ()
{
RectangleRsRt rect = new RectangleRsRt (new Point (290, 100), 130, 80, 30, Color .Blue);
rect .Rotatable = false;
elements .Insert (0, rect);
RegularPolygonRsRt regpoly = new RegularPolygonRsRt (new Point (610, 120), 130, 5, -20, Color .Cyan);
regpoly .Rotatable = false;
elements .Insert (0, regpoly);
PerforatedPolygonRsRt perfo = new PerforatedPolygonRsRt (new Point (320, 300), 140, 60, 3, 45, Color .Green);
perfo .Rotatable = false;
elements .Insert (0, perfo);
PointF ptC = new Point (560, 320);
int nApex = 8;
int radius = 80;
PointF [] pts = Auxi_Geometry .RegularPolygon (ptC, radius, nApex, 0.15);
double [] coef = { 4, 2, 2, 3 };
for (int i = 1, j = 0; i < nApex; i += 2, j ++)
{
pts [i] = Auxi_Geometry .EllipsePoint (ptC, radius, radius,
Auxi_Convert .RadianToDegree (Auxi_Geometry .Line_Angle (ptC, pts [i])), coef [j]);
}
Color [] clrs = Auxi_Colours .SmoothChangedColors (nApex, Color .Red, Color .Blue);
ChatoyantPolygonRsRt chat = new ChatoyantPolygonRsRt (ptC, pts, clrChatPolyCenter, clrs);
chat .Rotatable = false;
elements .Insert (0, chat);
}
// ------------------------------------------------- RenewMover
// order: btnCovers - btnAddRectangle - groupChatoyant - groupPerforated - groupRegular - info - all elements
void RenewMover ()
{
mover .Clear ();
mover .Add (btnCovers);
mover .Add (btnAddRectangle);
mover .Add (groupChatoyant);
mover .Add (groupPerforated);
mover .Add (groupRegular);
mover .Add (info);
for (int i = 0; i < elements .Count; i++)
{
mover .Add (elements [i]);
}
}
// ------------------------------------------------- InRegularGroupPositioning
void InRegularGroupPositioning ()
{
int cxL = numericUD_RegSides .Left;
panelRegularClr .Location = new Point (cxL, numericUD_RegSides .Bottom + spaces .VerMin);
btnAddRegular .Location = new Point (cxL, panelRegularClr .Bottom + spaces .VerMin);
}
// ------------------------------------------------- MoveRegularGroup
void MoveRegularGroup ()
{
Rectangle rc = groupRegular .RectAround;
numericUD_RegSides .Location = new Point (rc .Left + spaces .Left_inFrame, rc .Top + spaces .Top_inFrame);
InRegularGroupPositioning ();
}
// ------------------------------------------------- InPerforatedGroupPositioning
void InPerforatedGroupPositioning ()
{
int cxL = numericUD_PerfoSides .Left;
panelPerforatedClr .Location = new Point (cxL, numericUD_PerfoSides .Bottom + spaces .VerMin);
btnAddPerforated .Location = new Point (cxL, panelPerforatedClr .Bottom + spaces .VerMin);
}
// ------------------------------------------------- MovePerforatedGroup
void MovePerforatedGroup ()
{
Rectangle rc = groupPerforated .RectAround;
numericUD_PerfoSides .Location = new Point (rc .Left + spaces .Left_inFrame, rc .Top + spaces .Top_inFrame);
InPerforatedGroupPositioning ();
}
// ------------------------------------------------- InChatoyantGroupPositioning
void InChatoyantGroupPositioning ()
{
int cxL = numericUD_ChatApexes .Left;
btnStartChatClr .Location = new Point (cxL, numericUD_ChatApexes .Bottom + spaces .VerMin);
panelChatCenterClr .Location = new Point (cxL, btnStartChatClr .Bottom + spaces .VerMin);
btnAddChatoyant .Location = new Point (cxL, panelChatCenterClr .Bottom + spaces .VerMin);
btnEndChatClr .Location = new Point (btnAddChatoyant .Right - btnEndChatClr .Width, btnStartChatClr .Top);
}
// ------------------------------------------------- MoveChatoyantGroup
void MoveChatoyantGroup ()
{
Rectangle rc = groupChatoyant .RectAround;
numericUD_ChatApexes .Location = new Point (rc .Left + spaces .Left_inFrame, rc .Top + spaces .Top_inFrame);
InChatoyantGroupPositioning ();
}
// ------------------------------------------------- OnPaint
private void OnPaint (object sender, PaintEventArgs e)
{
Graphics grfx = e .Graphics;
for (int i = elements .Count - 1; i >= 0; i--)
{
elements [i] .Draw (grfx);
if (bShowCovers)
{
elements [i] .DrawCover (grfx, mover);
}
}
info .Draw (grfx);
groupRegular .Draw (grfx);
Auxi_Drawing .DrawTextToControl (grfx, numericUD_RegSides, Side .E, 4, strsRegular [0], Font, ForeColor);
Auxi_Drawing .DrawTextToControl (grfx, panelRegularClr, Side .E, 4, strsRegular [1], Font, ForeColor);
groupPerforated .Draw (grfx);
Auxi_Drawing .DrawTextToControl (grfx, numericUD_PerfoSides, Side .E, 4, strsRegular [0], Font, ForeColor);
Auxi_Drawing .DrawTextToControl (grfx, panelPerforatedClr, Side .E, 4, strsRegular [1], Font, ForeColor);
groupChatoyant .Draw (grfx);
Auxi_Drawing .DrawTextToControl (grfx, numericUD_ChatApexes, Side .E, 4, strsChatoyant [1], Font, ForeColor);
Auxi_Drawing .DrawTextToControl (grfx, panelChatCenterClr, Side .E, 4, strsChatoyant [3], Font, ForeColor);
RectangleF rc = new RectangleF (btnStartChatClr .Right, btnStartChatClr .Top,
btnEndChatClr .Left - btnStartChatClr .Right, btnStartChatClr .Height);
int nDifClrs = Convert .ToInt32 (numericUD_ChatApexes .Value);
List<Color> clrs = Auxi_Colours .SmoothColorsList (nDifClrs, clrStart, clrEnd);
float cxL, cxR;
for (int i = 0; i < clrs .Count; i++)
{
cxL = rc .Left + rc .Width * i / clrs .Count;
cxR = rc .Left + rc .Width * (i + 1) / clrs .Count;
grfx .FillRectangle (new SolidBrush (clrs [i]), cxL, rc .Top, cxR - cxL, rc .Height);
}
Auxi_Drawing .DrawText (grfx, strsChatoyant [2], Font, 0, ForeColor, Point .Round (Auxi_Geometry .Middle (rc)), TextBasis .M);
}
// ------------------------------------------------- OnMouseDown
private void OnMouseDown (object sender, MouseEventArgs e)
{
ptMouse_Down = e .Location;
if (mover .Catch (e .Location, e .Button))
{
if (e .Button == MouseButtons .Left)
{
StartResizing (e .Location);
}
}
ContextMenuStrip = null;
}
// ------------------------------------------------- StartResizing
private void StartResizing (Point pt)
{
GraphicalObject grobj = mover .CaughtSource;
if (grobj is RegularPolygonRsRt)
{
(grobj as RegularPolygonRsRt) .StartScaling (pt);
}
else if (grobj is PerforatedPolygonRsRt)
{
(grobj as PerforatedPolygonRsRt) .StartScaling (pt, mover .CaughtNode);
}
else if (grobj is ChatoyantPolygonRsRt)
{
(grobj as ChatoyantPolygonRsRt) .StartScaling (pt, mover .CaughtNodeShape);
}
}
// ------------------------------------------------- OnMouseUp
private void OnMouseUp (object sender, MouseEventArgs e)
{
ptMouse_Up = e .Location;
int iWasCaught;
if (mover .Release (out iWasCaught))
{
GraphicalObject grobj = mover [iWasCaught] .Source;
double dist = Auxi_Geometry .Distance (ptMouse_Down, e .Location);
if (dist <= 3 && grobj is ElementRsRt)
{
if (e .Button == MouseButtons .Left)
{
PopupFigure (grobj .ID);
}
else if (e .Button == MouseButtons .Right)
{
elemPressed = grobj as ElementRsRt;
ContextMenuStrip = menuOnFigures;
}
}
}
}
// ------------------------------------------------- OnMouseMove
private void OnMouseMove (object sender, MouseEventArgs e)
{
if (mover .Move (e .Location))
{
Invalidate ();
ControlsCausedUpdate ();
}
}
// ------------------------------------------------- ControlsCausedUpdate
private void ControlsCausedUpdate ()
{
if (mover .CaughtSource is FramedControl ||
mover .CaughtSource is LinkedRectangles ||
mover .CaughtSource is Group)
{
Update ();
}
}
// ------------------------------------------------- OnContextMenuChanged
private void OnContextMenuChanged (object sender, EventArgs e)
{
if (ContextMenuStrip != null)
{
ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
}
}
// ------------------------------------------------- PopupFigure
private void PopupFigure (long id)
{
for (int i = elements .Count - 1; i > 0; i--)
{
if (id == elements [i] .ID)
{
ElementRsRt elem = elements [i];
elements .RemoveAt (i);
InsertHeadElement (elem);
break;
}
}
}
// ------------------------------------------------- InsertHeadElement
private void InsertHeadElement (ElementRsRt elem)
{
elements .Insert (0, elem);
RenewMover ();
Invalidate ();
}
// ------------------------------------------------- Click_btnCovers
private void Click_btnCovers (object sender, EventArgs e)
{
bShowCovers = !bShowCovers;
Invalidate ();
}
// ------------------------------------------------- Click_btnRegularClr
private void Click_btnRegularClr (object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog ();
dlg .Color = clrNewRegular;
if (dlg .ShowDialog () == DialogResult .OK)
{
clrNewRegular = dlg .Color;
panelRegularClr .BackColor = clrNewRegular;
}
}
// ------------------------------------------------- Click_btnPerforatedClr
private void Click_btnPerforatedClr (object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog ();
dlg .Color = clrNewPerforated;
if (dlg .ShowDialog () == DialogResult .OK)
{
clrNewPerforated = dlg .Color;
panelPerforatedClr .BackColor = clrNewPerforated;
}
}
// ------------------------------------------------- Click_btnStartChatClr
private void Click_btnStartChatClr (object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog ();
dlg .Color = clrStart;
if (dlg .ShowDialog () == DialogResult .OK)
{
clrStart = dlg .Color;
Rectangle rc = new Rectangle (btnStartChatClr .Right, btnStartChatClr .Top,
btnEndChatClr .Left - btnStartChatClr .Right, btnStartChatClr .Height);
Invalidate (rc);
}
}
// ------------------------------------------------- Click_btnEndChatClr
private void Click_btnEndChatClr (object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog ();
dlg .Color = clrEnd;
if (dlg .ShowDialog () == DialogResult .OK)
{
clrEnd = dlg .Color;
Rectangle rc = new Rectangle (btnStartChatClr .Right, btnStartChatClr .Top,
btnEndChatClr .Left - btnStartChatClr .Right, btnStartChatClr .Height);
Invalidate (rc);
}
}
// ------------------------------------------------- Click_btnChatCenterClr
private void Click_btnChatCenterClr (object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog ();
dlg .Color = clrChatPolyCenter;
if (dlg .ShowDialog () == DialogResult .OK)
{
clrChatPolyCenter = dlg .Color;
panelChatCenterClr .BackColor = clrChatPolyCenter;
}
}
// ------------------------------------------------- RandomPoint
private PointF RandomPoint
{
get
{
return (new PointF (rand .Next (nSeed) % (ClientSize .Width - 200) + 100,
rand .Next (nSeed) % (ClientSize .Height - 200) + 100));
}
}
// ------------------------------------------------- Click_btnAddRectangle
private void Click_btnAddRectangle (object sender, EventArgs e)
{
RectangleRsRt elem = new RectangleRsRt (RandomPoint, 70, 40, 0, Auxi_Colours .ColorPredefined (rand .Next (nSeed) % 21));
elem .Rotatable = false;
InsertHeadElement (elem);
}
// ------------------------------------------------- Click_btnAddRegular
private void Click_btnAddRegular (object sender, EventArgs e)
{
RegularPolygonRsRt elem = new RegularPolygonRsRt (RandomPoint, 60, Convert .ToInt32 (numericUD_RegSides .Value),
rand .Next (nSeed) % 360, clrNewRegular);
elem .Rotatable = false;
InsertHeadElement (elem);
}
// ------------------------------------------------- Click_btnAddPerforated
private void Click_btnAddPerforated (object sender, EventArgs e)
{
PerforatedPolygonRsRt elem = new PerforatedPolygonRsRt (RandomPoint, 70, 40, Convert .ToInt32 (numericUD_PerfoSides .Value),
rand .Next (nSeed) % 360, clrNewPerforated);
elem .Rotatable = false;
InsertHeadElement (elem);
}
// ------------------------------------------------- Click_btnAddChatoyant
private void Click_btnAddChatoyant (object sender, EventArgs e)
{
PointF ptC = RandomPoint;
int nApex = Convert .ToInt32 (numericUD_ChatApexes .Value);
PointF [] pts = Auxi_Geometry .RegularPolygon (ptC, 70, nApex, rand .Next (nSeed) % 360);
Color [] clrs = Auxi_Colours .SmoothChangedColors (nApex, clrStart, clrEnd);
ChatoyantPolygonRsRt elem = new ChatoyantPolygonRsRt (ptC, pts, clrChatPolyCenter, clrs);
elem .Rotatable = false;
InsertHeadElement (elem);
}
// ------------------------------------------------- OnOpening_menuOnFigures
private void OnOpening_menuOnFigures (object sender, CancelEventArgs e)
{
menuOnFigures .Items ["miPutOnTop"] .Enabled = elemPressed .ID != elements [0] .ID;
menuOnFigures .Items ["miOneLevelUp"] .Enabled = elements .Count > 1 && elemPressed .ID != elements [0] .ID;
menuOnFigures .Items ["miOneLevelDown"] .Enabled = elements .Count > 1 && elemPressed .ID != elements [elements .Count - 1] .ID;
menuOnFigures .Items ["miPutUnderneath"] .Enabled = elemPressed .ID != elements [elements .Count - 1] .ID;
menuOnFigures .Items ["miColor"] .Enabled = elemPressed .Figure != Figure .ChatoyantPolygon;
}
// ------------------------------------------------- Click_miPutOnTop
private void Click_miPutOnTop (object sender, EventArgs e)
{
PopupFigure (elemPressed .ID);
}
// ------------------------------------------------- Click_miOneLevelUp
private void Click_miOneLevelUp (object sender, EventArgs e)
{
for (int i = 1; i < elements .Count; i++)
{
if (elemPressed .ID == elements [i] .ID)
{
elements .Reverse (i - 1, 2);
RenewMover ();
Invalidate ();
break;
}
}
}
// ------------------------------------------------- Click_miOneLevelDown
private void Click_miOneLevelDown (object sender, EventArgs e)
{
for (int i = 0; i < elements .Count - 1; i++)
{
if (elemPressed .ID == elements [i] .ID)
{
elements .Reverse (i, 2);
RenewMover ();
Invalidate ();
break;
}
}
}
// ------------------------------------------------- Click_miPutUnderneath
private void Click_miPutUnderneath (object sender, EventArgs e)
{
for (int i = 0; i < elements .Count - 1; i++)
{
if (elemPressed .ID == elements [i] .ID)
{
ElementRsRt elem = elements [i];
elements .RemoveAt (i);
elements .Add (elem);
RenewMover ();
Invalidate ();
break;
}
}
}
// ------------------------------------------------- Click_miColor
private void Click_miColor (object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog ();
dlg .Color = elemPressed .GetColor ();
if (dlg .ShowDialog () == DialogResult .OK)
{
elemPressed .SetColor (dlg .Color);
Invalidate ();
}
}
// ------------------------------------------------- Click_miDuplicate
private void Click_miDuplicate (object sender, EventArgs e)
{
PointF ptCur = elemPressed .Center ();
PointF pt = new PointF (ptCur .X + 80, ptCur .Y + 80);
if (!ClientRectangle .Contains (Point .Round (pt)))
{
pt = Auxi_Geometry .Middle (ptCur, new PointF (0, 0));
}
ElementRsRt elemNew = elemPressed .Copy (pt);
InsertHeadElement (elemNew);
}
// ------------------------------------------------- Click_miDelete
private void Click_miDelete (object sender, EventArgs e)
{
long id = elemPressed .ID;
for (int i = elements .Count - 1; i >= 0; i--)
{
if (id == elements [i] .ID)
{
elements .RemoveAt (i);
RenewMover ();
Invalidate ();
break;
}
}
}
// ------------------------------------------------- Click_miDeleteAll
private void Click_miDeleteAll (object sender, EventArgs e)
{
elements .Clear ();
RenewMover ();
Invalidate ();
}
}
}