using System;
using System .Collections .Generic;
using System .Drawing;
using System .Windows .Forms;
using MoveGraphLibrary;
namespace UserDrivenApplications
{
public class StripInRectangle : GraphicalObject
{
RectangleF rc;
double coefX_anchor, coefY_anchor; // from [0, 1]; [0,0] is for left-top corner; [1,1] is for right-bottom corner
PointF ptAnchor;
LineDir linedir;
double coefX_size, coefY_size; // can be of any sign
double coef_min_size, coef_max_size; // can be of any sign
SolidBrush brush;
int halfsense = 3;
// -------------------------------------------------
public StripInRectangle (RectangleF rcParent, float coefX_pt, float coefY_pt,
LineDir dirUnchangeable, float coefX_initialsize, float coefY_initialsize,
float minSizeCoef, float maxSizeCoef, Color clr)
{
rc = rcParent;
coefX_anchor = Math .Min (Math .Max (0, coefX_pt), 1);
coefY_anchor = Math .Min (Math .Max (0, coefY_pt), 1);
ptAnchor = new PointF (rc .Left + Convert .ToSingle (coefX_anchor * rc .Width),
rc .Top + Convert .ToSingle (coefY_anchor * rc .Height));
linedir = dirUnchangeable;
coefX_size = coefX_initialsize;
coefY_size = coefY_initialsize;
coef_min_size = Math .Min (minSizeCoef, maxSizeCoef);
coef_max_size = Math .Max (minSizeCoef, maxSizeCoef);
switch (linedir)
{
case LineDir .Hor:
coefY_size = Math .Min (Math .Max (coef_min_size, coefY_size), coef_max_size);
break;
case LineDir .Ver:
coefX_size = Math .Min (Math .Max (coef_min_size, coefX_size), coef_max_size);
break;
}
brush = new SolidBrush (clr);
}
// ------------------------------------------------- ParentRect
public RectangleF ParentRect
{
get { return (rc); }
set
{
rc = value;
ptAnchor = new PointF (rc .Left + Convert .ToSingle (coefX_anchor * rc .Width),
rc .Top + Convert .ToSingle (coefY_anchor * rc .Height));
DefineCover ();
}
}
// ------------------------------------------------- Ycoef
public double Ycoef
{
get { return (coefY_size); }
}
// ------------------------------------------------- Color
public Color Color
{
get { return (brush .Color); }
set { brush = new SolidBrush (value); }
}
// ------------------------------------------------- Draw
public void Draw (Graphics grfx)
{
float dw = Convert .ToSingle (rc .Width * coefX_size);
float dh = Convert .ToSingle (rc .Height * coefY_size);
float cxL = Math .Min (ptAnchor .X, ptAnchor .X + dw);
float cxR = Math .Max (ptAnchor .X, ptAnchor .X + dw);
float cyT = Math .Min (ptAnchor .Y, ptAnchor .Y + dh);
float cyB = Math .Max (ptAnchor .Y, ptAnchor .Y + dh);
grfx .FillRectangle (brush, cxL, cyT, cxR - cxL, cyB - cyT);
}
// ------------------------------------------------- Draw
public override void DefineCover ()
{
float dw = Convert .ToSingle (rc .Width * coefX_size);
float dh = Convert .ToSingle (rc .Height * coefY_size);
float cxL = Math .Min (ptAnchor .X, ptAnchor .X + dw);
float cxR = Math .Max (ptAnchor .X, ptAnchor .X + dw);
float cyT = Math .Min (ptAnchor .Y, ptAnchor .Y + dh);
float cyB = Math .Max (ptAnchor .Y, ptAnchor .Y + dh);
CoverNode [] nodes = new CoverNode [2];
switch (linedir)
{
case LineDir .Hor:
float cy = ptAnchor .Y + dh;
nodes [0] = new CoverNode (0, new RectangleF (cxL, cy - halfsense, cxR - cxL, 2 * halfsense), Cursors .SizeNS);
break;
case LineDir .Ver:
float cx = ptAnchor .X + dw;
nodes [0] = new CoverNode (0, new RectangleF (cx - halfsense, cyT, 2 * halfsense, cyB - cyT), Cursors .SizeWE);
break;
}
nodes [1] = new CoverNode (1, new RectangleF (cxL, cyT, cxR - cxL, Math .Max (2, cyB - cyT)),
MovementFreedom .None, Cursors .Default);
cover = new Cover (nodes);
}
// ------------------------------------------------- Move
public override void Move (int dx, int dy)
{
ptAnchor += 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 && i == 0)
{
float distCur, distNext;
float coefNext;
switch (linedir)
{
case LineDir .Hor:
distCur = Convert .ToSingle (rc .Height * coefY_size);
distNext = distCur + dy;
coefNext = distNext / rc .Height;
if (coef_min_size <= coefNext && coefNext <= coef_max_size)
{
coefY_size = coefNext;
bRet = true;
}
break;
case LineDir .Ver:
distCur = Convert .ToSingle (rc .Width * coefX_size);
distNext = distCur + dx;
coefNext = distNext / rc .Width;
if (coef_min_size <= coefNext && coefNext <= coef_max_size)
{
coefX_size = coefNext;
bRet = true;
}
break;
}
}
return (bRet);
}
}
}