Guest User

Untitled

a guest
Jan 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.96 KB | None | 0 0
  1. unit shapes;
  2.  
  3. interface
  4.  
  5. uses Windows, SysUtils, Classes, Graphics, Types;
  6.  
  7. type
  8.   TRPen = record
  9.     Color: TColor;
  10.     Width: integer;
  11.     Style: TPenStyle;
  12.   end;
  13.  
  14. type
  15.   TRBrush = record
  16.     Color: TColor;
  17.     Style: TBrushStyle;
  18.   end;
  19.  
  20. Type
  21.   TFShape = class
  22.   private
  23.     procedure SetStyle(CanvasOut: TCanvas);
  24.   public
  25.     PenF: TRPen;
  26.     BrushF: TRBrush;
  27.     PointStart: TPoint;
  28.     Locked: Boolean;
  29.     DrawIt: Boolean;
  30.     procedure Draw(CanvasOut: TCanvas); virtual; abstract;
  31.     procedure Move(X, Y: integer); virtual; abstract;
  32.     procedure AddPoint(X, Y: integer; CanvasOut: TCanvas); virtual; abstract;
  33.     procedure DrawObject(X, Y: integer; CanvasOut: TCanvas); virtual; abstract;
  34.     constructor Create(const Source: TCanvas); virtual;
  35.   end;
  36.  
  37. type
  38.   TPoints = array of TPoint;
  39.  
  40. Type
  41.   TFFreeHand = class(TFShape)
  42.   private
  43.     Points: TPoints;
  44.     procedure DrawLastLine(CanvasOut: TCanvas);
  45.   public
  46.     procedure Move(X, Y: integer); override;
  47.     procedure AddPoint(X, Y: integer; CanvasOut: TCanvas); override;
  48.     procedure Draw(CanvasOut: TCanvas); override;
  49.   end;
  50.  
  51. Type
  52.   TFStandartShape = class(TFShape)
  53.   public
  54.     PointEnd: TPoint;
  55.     procedure Draw(CanvasOut: TCanvas); override;
  56.     procedure Move(X, Y: integer); override;
  57.   end;
  58.  
  59. type
  60.   TFline = class(TFStandartShape)
  61.   public
  62.     procedure DrawObject(X, Y: integer; CanvasOut: TCanvas); override;
  63.   end;
  64.  
  65. type
  66.   TFRect = class(TFStandartShape)
  67.   public
  68.     procedure DrawObject(X, Y: integer; CanvasOut: TCanvas); override;
  69.   end;
  70.  
  71. type
  72.   TFEllipse = class(TFStandartShape)
  73.   public
  74.     procedure DrawObject(X, Y: integer; CanvasOut: TCanvas); override;
  75.   end;
  76.  
  77. type
  78.   TFRoundRect = class(TFStandartShape)
  79.   public
  80.     Corners: integer;
  81.     procedure DrawObject(X, Y: integer; CanvasOut: TCanvas); override;
  82.  
  83.   end;
  84.  
  85. type
  86.   TFShapeList = class
  87.   private
  88.     List: array of TFShape;
  89.     function GetCount: integer;
  90.     function GetItem(Index: integer): TFShape;
  91.     procedure PutItem(Index: integer; Item: TFShape);
  92.   public
  93.     property Count: integer read GetCount;
  94.     property Items[Index: integer]: TFShape read GetItem write PutItem;
  95.     function Add(Sender: TFShape): integer;
  96.     procedure Delete(Index: integer);
  97.     procedure Swap(Index1, Index2: integer);
  98.     procedure DrawAll(CanvasOut: TCanvas);
  99.   end;
  100.  
  101. implementation
  102.  
  103. { TFShapeList }
  104.  
  105. function TFShapeList.GetCount: integer;
  106. begin
  107.   Result := Length(List);
  108. end;
  109.  
  110. function TFShapeList.GetItem(Index: integer): TFShape;
  111. begin
  112.   if (Index < 0) or (Index > Count - 1) then
  113.   begin
  114.     Raise Exception.Create('Array out of range Error');
  115.     exit;
  116.   end;
  117.   Result := List[Index];
  118. end;
  119.  
  120. procedure TFShapeList.PutItem(Index: integer; Item: TFShape);
  121. begin
  122.   if (Index < 0) or (Index > Count - 1) then
  123.   begin
  124.     Raise Exception.Create('Array out of range Error');
  125.     exit;
  126.   end;
  127.   List[Index] := Item;
  128. end;
  129.  
  130. procedure TFShapeList.Swap(Index1, Index2: integer);
  131. var
  132.   tmp: TFShape;
  133. begin
  134.   if (Index1 < 0) or (Index2 < 0) or (Index1 > Count - 1) or
  135.     (Index2 > Count - 1) then
  136.   begin
  137.     Raise Exception.Create('Array out of range Error');
  138.     exit;
  139.   end;
  140.   tmp := List[Index1];
  141.   List[Index1] := List[Index2];
  142.   List[Index2] := tmp;
  143. end;
  144.  
  145. procedure TFShapeList.Delete(Index: integer);
  146. var
  147.   i: integer;
  148. begin
  149.   if (Index < 0) or (Index > Count - 1) then
  150.   begin
  151.     Raise Exception.Create('Array out of range Error');
  152.     exit;
  153.   end;
  154.   List[Index].Free;
  155.   for i := Index to High(List) - 1 do
  156.     List[i] := List[i + 1];
  157.   SetLength(List, Length(List) - 1);
  158. end;
  159.  
  160. function TFShapeList.Add(Sender: TFShape): integer;
  161. begin
  162.   SetLength(List, Length(List) + 1);
  163.   List[ High(List)] := Sender;
  164.   Result := Count - 1;
  165. end;
  166.  
  167. procedure TFShapeList.DrawAll(CanvasOut: TCanvas);
  168. var
  169.   p: TFShape;
  170. begin
  171.   for p in List do
  172.     p.Draw(CanvasOut)
  173. end;
  174.  
  175. { TFShape }
  176.  
  177. constructor TFShape.Create;
  178. begin
  179.   inherited Create;
  180.   PenF.Color := clBlack;
  181.   PenF.Width := 1;
  182.   PenF.Style := psSolid;
  183.   BrushF.Color := clWhite;
  184.   BrushF.Style := bsSolid;
  185.   Locked := false;
  186. end;
  187.  
  188. procedure TFShape.SetStyle(CanvasOut: TCanvas);
  189. begin
  190.   with CanvasOut do
  191.   begin
  192.     Pen.Color := PenF.Color;
  193.     Pen.Width := PenF.Width;
  194.     Pen.Style := PenF.Style;
  195.     Brush.Color := BrushF.Color;
  196.     Brush.Style := BrushF.Style;
  197.   end;
  198. end;
  199.  
  200. { TFFreeHand }
  201. procedure TFFreeHand.AddPoint(X, Y: integer; CanvasOut: TCanvas);
  202. begin
  203.   SetLength(Points, Length(Points) + 1);
  204.   Points[ High(Points)] := Point(X, Y);
  205.   DrawLastLine(CanvasOut);
  206. end;
  207.  
  208. procedure TFFreeHand.DrawLastLine(CanvasOut: TCanvas);
  209. begin
  210.   with CanvasOut do
  211.   begin
  212.     SetStyle(CanvasOut);
  213.     if Length(Points) = 1 then
  214.       MoveTo(PointStart.X, PointStart.Y);
  215.     LineTo(Points[Length(Points) - 1].X, Points[Length(Points) - 1].Y);
  216.   end;
  217. end;
  218.  
  219. procedure TFFreeHand.Draw(CanvasOut: TCanvas);
  220. var
  221.   p: TPoint;
  222. begin
  223.   if not DrawIt then
  224.     exit;
  225.   with CanvasOut do
  226.   begin
  227.     SetStyle;
  228.     if (PointStart.X = Points[0].X) and (PointStart.Y = Points[0].Y) and
  229.       (PenF.Width = 1) then
  230.       Pixels[Points[0].X, Points[0].Y] := PenF.Color;
  231.     MoveTo(PointStart.X, PointStart.Y);
  232.     for p in Points do
  233.       LineTo(p.X, p.Y);
  234.   end;
  235. end;
  236.  
  237. procedure TFFreeHand.Move(X, Y: integer);
  238. var
  239.   p: TPoint;
  240. begin
  241.   if Locked then
  242.     exit;
  243.   Inc(PointStart.X, X);
  244.   Inc(PointStart.Y, Y);
  245.   for p in Points do
  246.   begin
  247.     Inc(p.X, X);
  248.     Inc(p.Y, Y);
  249.   end;
  250. end;
  251.  
  252. { TFLine }
  253. procedure TFline.DrawObject(X, Y: integer; CanvasOut: TCanvas);
  254. begin
  255.   SetStyle(CanvasOut);
  256.   with CanvasOut do
  257.   begin
  258.     MoveTo(PointStart.X, PointStart.Y);
  259.     LineTo(X, Y);
  260.   end;
  261. end;
  262.  
  263. { TFRect }
  264. procedure TFRect.DrawObject(X, Y: integer; Load: Boolean = false);
  265. begin
  266.   SetStyle(CanvasOut);
  267.   CanvasOut.Rectangle(PointStart.X, PointStart.Y, X, Y);
  268. end;
  269.  
  270. { TFEllipse }
  271. procedure TFEllipse.DrawObject(X, Y: integer; Load: Boolean = false);
  272. begin
  273.   SetStyle(CanvasOut);
  274.   CanvasOut.Ellipse(PointStart.X, PointStart.Y, X, Y);
  275. end;
  276.  
  277. { TFRoundRect }
  278. procedure TFRoundRect.DrawObject(X, Y: integer; Load: Boolean = false);
  279. begin
  280.   SetStyle(CanvasOut);
  281.   CanvasOut.RoundRect(PointStart.X, PointStart.Y, X, Y, Corners, Corners);
  282. end;
  283.  
  284. end.
Add Comment
Please, Sign In to add comment