Changeset 30
- Timestamp:
- Apr 18, 2015, 6:17:34 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UEngine.pas
r29 r30 6 6 7 7 uses 8 Classes, SysUtils, Contnrs, Graphics, Controls, ExtCtrls, Math, DateUtils; 8 Classes, SysUtils, Contnrs, Graphics, Controls, ExtCtrls, Math, DateUtils, 9 UMetaCanvas; 9 10 10 11 type … … 209 210 private 210 211 FDestRect: TRect; 212 FSourceRect: TRect; 211 213 FZoom: Double; 212 214 procedure SetDestRect(AValue: TRect); 215 procedure SetSourceRect(AValue: TRect); 213 216 procedure SetZoom(AValue: Double); 214 217 public 215 SourceRect: TRect; 218 function PointDestToSrc(Pos: TPoint): TPoint; 219 function PointSrcToDest(Pos: TPoint): TPoint; 216 220 constructor Create; 221 property SourceRect: TRect read FSourceRect write SetSourceRect; 217 222 property DestRect: TRect read FDestRect write SetDestRect; 218 223 property Zoom: Double read FZoom write SetZoom; … … 237 242 FTime: TDateTime; 238 243 FLastTime: TDateTime; 244 MetaCanvas: TMetaCanvas; 245 procedure ResizeView; 239 246 function GetExistStationShapes: TStationShapeSet; 240 247 function GetStationOnPos(Pos: TPoint): TMapStation; … … 273 280 constructor Create; 274 281 destructor Destroy; override; 275 procedure Paint( Canvas: TCanvas);282 procedure Paint(TargetCanvas: TCanvas); 276 283 property Time: TDateTime read FTime; 277 284 end; … … 307 314 NewTrainPeriod = 7; // Each week 308 315 NewPassengerPeriod = 0.3 * OneSecond; 309 NewPassengerProbability = 0.00 2;316 NewPassengerProbability = 0.003; 310 317 VisiblePassengersPerLine = 6; 311 318 … … 350 357 Trunc((DestRect.Bottom - DestRect.Top) / Zoom - (AValue.Bottom - AValue.Top) / Zoom) div 2); 351 358 FDestRect := AValue; 352 SourceRect := Bounds(SourceRect.Left + Diff.X,SourceRect.Top + Diff.Y,359 FSourceRect := Bounds(FSourceRect.Left + Diff.X, FSourceRect.Top + Diff.Y, 353 360 Trunc((DestRect.Right - DestRect.Left) / Zoom), 354 361 Trunc((DestRect.Bottom - DestRect.Top) / Zoom)); 362 end; 363 364 procedure TView.SetSourceRect(AValue: TRect); 365 var 366 ZX: Double; 367 ZY: Double; 368 begin 369 if RectEquals(FSourceRect, AValue) then Exit; 370 FSourceRect := AValue; 371 ZX := (FDestRect.Right - FDestRect.Left) / (FSourceRect.Right - FSourceRect.Left); 372 ZY := (FDestRect.Bottom - FDestRect.Top) / (FSourceRect.Bottom - FSourceRect.Top); 373 if ZX > ZY then 374 Zoom := ZY 375 else Zoom := ZX; 355 376 end; 356 377 … … 361 382 raise Exception.Create(SZeroZoomNotAlowed); 362 383 FZoom := AValue; 363 SourceRect := Bounds(Trunc(SourceRect.Left + (SourceRect.Right -SourceRect.Left) div 2 - (DestRect.Right - DestRect.Left) / Zoom / 2),364 Trunc( SourceRect.Top + (SourceRect.Bottom - SourceRect.Top) div 2 - (DestRect.Bottom - DestRect.Top) / Zoom / 2),384 FSourceRect := Bounds(Trunc(FSourceRect.Left + (FSourceRect.Right - FSourceRect.Left) div 2 - (DestRect.Right - DestRect.Left) / Zoom / 2), 385 Trunc(FSourceRect.Top + (FSourceRect.Bottom - FSourceRect.Top) div 2 - (FDestRect.Bottom - DestRect.Top) / Zoom / 2), 365 386 Trunc((DestRect.Right - DestRect.Left) / Zoom), 366 387 Trunc((DestRect.Bottom - DestRect.Top) / Zoom)); 367 388 end; 368 389 390 function TView.PointDestToSrc(Pos: TPoint): TPoint; 391 begin 392 Result := Point(Trunc(Pos.X / FZoom + FSourceRect.Left), 393 Trunc(Pos.Y / FZoom + FSourceRect.Top)); 394 end; 395 396 function TView.PointSrcToDest(Pos: TPoint): TPoint; 397 begin 398 Result := Point(Trunc((Pos.X - FSourceRect.Left) * FZoom), 399 Trunc((Pos.Y - FSourceRect.Top) * FZoom)); 400 end; 401 369 402 constructor TView.Create; 370 403 begin 371 Zoom := 1 .5;404 Zoom := 1; 372 405 end; 373 406 … … 1083 1116 1084 1117 { TEngine } 1118 1119 procedure TEngine.ResizeView; 1120 var 1121 StationRect: TRect; 1122 NewPoint: TPoint; 1123 begin 1124 // Need to see all stations on screen 1125 View.SourceRect := RectEnlarge(Stations.GetRect, 100); 1126 1127 NewPoint := Point( 1128 Trunc((View.SourceRect.Left + (View.SourceRect.Right - View.SourceRect.Left) / 2) - 1129 (View.DestRect.Left + (View.DestRect.Right - View.DestRect.Left) / 2 / View.Zoom)), 1130 Trunc((View.SourceRect.Top + (View.SourceRect.Bottom - View.SourceRect.Top) / 2) - 1131 (View.DestRect.Top + (View.DestRect.Bottom - View.DestRect.Top) / 2 / View.Zoom))); 1132 View.SourceRect := Bounds(NewPoint.X, NewPoint.Y, Trunc((View.DestRect.Right - View.DestRect.Left) / View.Zoom), 1133 Trunc((View.DestRect.Bottom - View.DestRect.Top) / View.Zoom)); 1134 end; 1085 1135 1086 1136 function TEngine.GetExistStationShapes: TStationShapeSet; … … 1618 1668 LastNewStationTime := Time; 1619 1669 Stations.AddNew; 1620 // Need to see all stations on screen 1621 View.SourceRect := RectEnlarge(Stations.GetRect, 70); 1670 ResizeView; 1622 1671 end; 1623 1672 … … 1676 1725 LastMousePos := Position; 1677 1726 if MouseHold then begin 1678 FocusedStation := GetStationOnPos( Position);1727 FocusedStation := GetStationOnPos(View.PointDestToSrc(Position)); 1679 1728 Line := nil; 1680 1729 if Assigned(TrackStationDown) then Line := TrackStationDown.Line; … … 1728 1777 SelectedTrain.Line := nil; 1729 1778 end; 1730 FocusedTrack := GetTrackOnPos( Position);1779 FocusedTrack := GetTrackOnPos(View.PointDestToSrc(Position)); 1731 1780 if Assigned(FocusedTrack.PointDown) then begin 1732 1781 SelectedTrain.Line := FocusedTrack.PointDown.Line; … … 1781 1830 1782 1831 // Train selection 1783 SelectedTrain := GetTrainOnPos( Position);1832 SelectedTrain := GetTrainOnPos(View.PointDestToSrc(Position)); 1784 1833 if Assigned(SelectedTrain) then begin 1785 1834 Exit; … … 1795 1844 1796 1845 // Line selection 1797 Track := GetTrackOnPos( Position);1846 Track := GetTrackOnPos(View.PointDestToSrc(Position)); 1798 1847 if Assigned(Track) and Assigned(Track.PointDown) and Assigned(Track.PointUp) then begin 1799 1848 SelectedLine := Track.PointDown.Line; … … 1820 1869 1821 1870 // New track creation from selected station as start 1822 Station := GetStationOnPos( Position);1871 Station := GetStationOnPos(View.PointDestToSrc(Position)); 1823 1872 if Assigned(Station) then begin 1824 1873 if Assigned(SelectedLine) and (SelectedLine.LineStations.Count = 0) then NewLine := SelectedLine … … 1862 1911 Trains.Add(NewTrain); 1863 1912 end; 1913 1914 ResizeView; 1864 1915 1865 1916 SelectedLine := nil; … … 1891 1942 if FileExists(ImageLocomotiveName) then 1892 1943 ImageLocomotive.Picture.LoadFromFile(ImageLocomotiveName); 1944 MetaCanvas := TMetaCanvas.Create; 1893 1945 end; 1894 1946 1895 1947 destructor TEngine.Destroy; 1896 1948 begin 1949 MetaCanvas.Free; 1897 1950 Trains.Free; 1898 1951 ImageLocomotive.Free; … … 1906 1959 end; 1907 1960 1908 procedure TEngine.Paint( Canvas: TCanvas);1961 procedure TEngine.Paint(TargetCanvas: TCanvas); 1909 1962 var 1910 1963 I: Integer; … … 1915 1968 Text: string; 1916 1969 Angle: Double; 1917 EndPoint: TPoint;1918 1970 PassengerPos: TPoint; 1919 1971 Direction: Integer; 1920 1972 Points: array of TPoint; 1973 Canvas: TMetaCanvas; 1921 1974 const 1922 1975 GameOverText = 'Game Over'; … … 1924 1977 GameOverStatistic = '%d passengers travelled on your metro over %d days.'; 1925 1978 begin 1926 Canvas .Brush.Color := $eff0e0;1927 Canvas. Brush.Style := bsSolid;1928 Canvas. Clear;1979 Canvas := MetaCanvas; 1980 Canvas.SetSize(Point(TargetCanvas.Width, TargetCanvas.Height)); 1981 Canvas.Reset; 1929 1982 1930 1983 // Draw station passenger overload … … 1988 2041 Canvas.Pen.Color := TrackStationDown.Line.Color; 1989 2042 Canvas.MoveTo(TrackStationDown.LineStation.TrackPoint.Position); 1990 DrawLine(Canvas, LastMousePos);2043 DrawLine(Canvas, View.PointDestToSrc(LastMousePos)); 1991 2044 end; 1992 2045 if Assigned(TrackStationUp) and Assigned(TrackStationUp.LineStation) then begin 1993 2046 Canvas.Pen.Color := TrackStationUp.Line.Color; 1994 2047 Canvas.MoveTo(TrackStationUp.LineStation.TrackPoint.Position); 1995 DrawLine(Canvas, LastMousePos);2048 DrawLine(Canvas, View.PointDestToSrc(LastMousePos)); 1996 2049 end; 1997 2050 … … 2006 2059 Canvas.Brush.Style := bsClear; 2007 2060 Canvas.Pen.Color := SelectedLine.Color; 2008 DrawShape(Canvas, Position, Shape, StationSize + Canvas.Pen.Width + 6, 0);2061 DrawShape(Canvas, Position, Shape, StationSize + Canvas.Pen.Width + 4, 0); 2009 2062 end; 2010 2063 … … 2046 2099 end; 2047 2100 2101 // Clear background 2102 TargetCanvas.Brush.Color := $eff0e0; 2103 TargetCanvas.Brush.Style := bsSolid; 2104 TargetCanvas.Clear; 2105 2106 MetaCanvas.Move(Point(-View.SourceRect.Left, -View.SourceRect.Top)); 2107 MetaCanvas.Zoom(View.Zoom); 2108 2109 // Draw meta canvas to real target canvas 2110 MetaCanvas.DrawTo(TargetCanvas); 2111 2048 2112 // Line selection 2049 2113 for I := 0 to High(LineColors) do begin 2050 2114 if Assigned(Lines.SearchByColor(LineColors[I])) then begin 2051 Canvas.Brush.Color := LineColors[I];2115 TargetCanvas.Brush.Color := LineColors[I]; 2052 2116 Size := 15; 2053 2117 end else begin 2054 Canvas.Brush.Color := clSilver;2118 TargetCanvas.Brush.Color := clSilver; 2055 2119 Size := 5; 2056 2120 end; 2057 Canvas.Pen.Color := clBlack;2121 TargetCanvas.Pen.Color := clBlack; 2058 2122 if Assigned(SelectedLine) and (SelectedLine.Color = LineColors[I]) then begin 2059 Canvas.Pen.Style := psSolid;2123 TargetCanvas.Pen.Style := psSolid; 2060 2124 end else begin 2061 Canvas.Pen.Style := psClear;2062 end; 2063 2064 Canvas.EllipseC(Canvas.Width div 2 - Length(LineColors) div 2 * LineColorsDist + I * LineColorsDist,2065 Canvas.Height - LineColorsDist, Size, Size);2125 TargetCanvas.Pen.Style := psClear; 2126 end; 2127 2128 TargetCanvas.EllipseC(TargetCanvas.Width div 2 - Length(LineColors) div 2 * LineColorsDist + I * LineColorsDist, 2129 TargetCanvas.Height - LineColorsDist, Size, Size); 2066 2130 end; 2067 2131 2068 2132 // Draw unused trains 2069 2133 Text := IntToStr(Trains.GetUnusedCount); 2070 Canvas.Draw(Canvas.Width div 2 - Length(LineColors) div 2 * LineColorsDist - 100, 2071 Canvas.Height - LineColorsDist - ImageLocomotive.Picture.Bitmap.Height div 2, ImageLocomotive.Picture.Bitmap); 2072 Canvas.Brush.Style := bsClear; 2073 Canvas.TextOut(Canvas.Width div 2 - Length(LineColors) div 2 * LineColorsDist - 50 - Canvas.TextWidth(Text), 2074 Canvas.Height - LineColorsDist - Canvas.TextHeight(Text) div 2, Text); 2075 2134 TargetCanvas.Draw(Canvas.Width div 2 - Length(LineColors) div 2 * LineColorsDist - 100, 2135 TargetCanvas.Height - LineColorsDist - ImageLocomotive.Picture.Bitmap.Height div 2, ImageLocomotive.Picture.Bitmap); 2136 TargetCanvas.Brush.Style := bsClear; 2137 TargetCanvas.TextOut(TargetCanvas.Width div 2 - Length(LineColors) div 2 * LineColorsDist - 50 - TargetCanvas.TextWidth(Text), 2138 TargetCanvas.Height - LineColorsDist - TargetCanvas.TextHeight(Text) div 2, Text); 2076 2139 2077 2140 // Status interface 2078 2141 Text := IntToStr(ServedPassengerCount); 2079 Canvas.Draw(Canvas.Width - 140, 20, ImagePassenger.Picture.Bitmap); 2080 Canvas.Brush.Style := bsClear; 2081 Canvas.TextOut(Canvas.Width - 146 - Canvas.TextWidth(Text), 25, Text); 2082 2083 DrawClock(Canvas); 2084 2142 TargetCanvas.Draw(TargetCanvas.Width - 140, 20, ImagePassenger.Picture.Bitmap); 2143 TargetCanvas.Brush.Style := bsClear; 2144 TargetCanvas.TextOut(TargetCanvas.Width - 146 - TargetCanvas.TextWidth(Text), 25, Text); 2145 2146 DrawClock(TargetCanvas); 2147 2148 // Show grabbed train by mouse 2085 2149 if Assigned(SelectedTrain) then begin 2086 Canvas.Brush.Color := clBlack; //SelectedTrain.Line.Color;2087 Canvas.Brush.Style := bsSolid;2088 Canvas.Pen.Style := psClear;2150 TargetCanvas.Brush.Color := clBlack; //SelectedTrain.Line.Color; 2151 TargetCanvas.Brush.Style := bsSolid; 2152 TargetCanvas.Pen.Style := psClear; 2089 2153 Pos := LastMousePos; 2090 2154 Angle := 0; … … 2095 2159 Points[2] := RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y + TrainSize div 3), Angle); 2096 2160 Points[3] := RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y + TrainSize div 3), Angle); 2097 Canvas.Polygon(Points);2161 TargetCanvas.Polygon(Points); 2098 2162 end; 2099 2163 … … 2101 2165 if State = gsGameOver then 2102 2166 begin 2103 Canvas.Font.Size := 40;2104 Canvas.Font.Color := clBlack;2105 Canvas.TextOut((Canvas.Width -Canvas.TextWidth(GameOverText)) div 2, 100, GameOverText);2106 Canvas.Font.Size := 14;2107 Canvas.TextOut((Canvas.Width -Canvas.TextWidth(GameOverReason)) div 2, 160, GameOverReason);2167 TargetCanvas.Font.Size := 40; 2168 TargetCanvas.Font.Color := clBlack; 2169 TargetCanvas.TextOut((TargetCanvas.Width - TargetCanvas.TextWidth(GameOverText)) div 2, 100, GameOverText); 2170 TargetCanvas.Font.Size := 14; 2171 TargetCanvas.TextOut((TargetCanvas.Width - TargetCanvas.TextWidth(GameOverReason)) div 2, 160, GameOverReason); 2108 2172 Text := Format(GameOverStatistic, [ServedPassengerCount, Trunc(Time)]); 2109 Canvas.TextOut((Canvas.Width -Canvas.TextWidth(Text)) div 2, 180, Text);2173 TargetCanvas.TextOut((TargetCanvas.Width - TargetCanvas.TextWidth(Text)) div 2, 180, Text); 2110 2174 end; 2111 2175 end; -
trunk/UFormMain.lfm
r28 r30 22 22 OnMouseMove = PaintBox1MouseMove 23 23 OnMouseUp = PaintBox1MouseUp 24 OnMouseWheelDown = PaintBox1MouseWheelDown 25 OnMouseWheelUp = PaintBox1MouseWheelUp 24 26 OnPaint = PaintBox1Paint 25 27 OnResize = PaintBox1Resize -
trunk/UFormMain.pas
r28 r30 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus, 9 ExtCtrls, UEngine, LCLType ;9 ExtCtrls, UEngine, LCLType, types; 10 10 11 11 type … … 26 26 procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton; 27 27 Shift: TShiftState; X, Y: Integer); 28 procedure PaintBox1MouseWheelDown(Sender: TObject; Shift: TShiftState; 29 MousePos: TPoint; var Handled: Boolean); 30 procedure PaintBox1MouseWheelUp(Sender: TObject; Shift: TShiftState; 31 MousePos: TPoint; var Handled: Boolean); 28 32 procedure PaintBox1Paint(Sender: TObject); 29 33 procedure PaintBox1Resize(Sender: TObject); … … 68 72 Randomize; 69 73 Engine.Map.Size := Point(PaintBox1.Width, PaintBox1.Height); 74 Engine.View.DestRect := Rect(0, 0, PaintBox1.Width, PaintBox1.Height); 70 75 Engine.Reset; 71 76 end; … … 87 92 begin 88 93 Engine.MouseUp(Button, Point(X, Y)); 94 end; 95 96 procedure TFormMain.PaintBox1MouseWheelDown(Sender: TObject; 97 Shift: TShiftState; MousePos: TPoint; var Handled: Boolean); 98 begin 99 Engine.View.Zoom := Engine.View.Zoom * 1.2; 100 end; 101 102 procedure TFormMain.PaintBox1MouseWheelUp(Sender: TObject; Shift: TShiftState; 103 MousePos: TPoint; var Handled: Boolean); 104 begin 105 Engine.View.Zoom := Engine.View.Zoom / 1.2; 89 106 end; 90 107 -
trunk/UGeometric.pas
r29 r30 24 24 function RectEquals(A, B: TRect): Boolean; 25 25 function RectEnlarge(Rect: TRect; Value: Integer): TRect; 26 function ShiftRect(ARect: TRect; Delta: TPoint): TRect; 26 27 27 28 implementation … … 135 136 function RectEnlarge(Rect: TRect; Value: Integer): TRect; 136 137 begin 137 Rect.Left := Rect.Left - Value; 138 Rect.Right := Rect.Right + Value; 139 Rect.Top := Rect.Top - Value; 140 Rect.Bottom := Rect.Bottom + Value; 138 Result.Left := Rect.Left - Value; 139 Result.Right := Rect.Right + Value; 140 Result.Top := Rect.Top - Value; 141 Result.Bottom := Rect.Bottom + Value; 142 end; 143 144 function ShiftRect(ARect: TRect; Delta: TPoint): TRect; 145 begin 146 Result := Rect(ARect.Left + Delta.X, ARect.Top + Delta.Y, 147 ARect.Right + Delta.X, ARect.Bottom + Delta.Y); 141 148 end; 142 149 -
trunk/UMetaCanvas.pas
r29 r30 6 6 7 7 uses 8 Classes, SysUtils, Graphics, Contnrs ;8 Classes, SysUtils, Graphics, Contnrs, GraphMath, Types; 9 9 10 10 type 11 TArrayOfPoint = array of TPoint; 11 12 12 13 { TCanvasObject } … … 14 15 TCanvasObject = class 15 16 procedure Paint(Canvas: TCanvas); virtual; 17 procedure Zoom(Factor: Double); virtual; 18 procedure Move(Delta: TPoint); virtual; 16 19 end; 17 20 … … 19 22 20 23 TCanvasText = class(TCanvasObject) 24 Brush: TBrush; 25 Font: TFont; 21 26 Position: TPoint; 22 27 Text: string; 23 28 procedure Paint(Canvas: TCanvas); override; 29 procedure Zoom(Factor: Double); override; 30 procedure Move(Delta: TPoint); override; 31 constructor Create; 32 destructor Destroy; override; 33 end; 34 35 { TCanvasRectangle } 36 37 TCanvasRectangle = class(TCanvasObject) 38 Pen: TPen; 39 Brush: TBrush; 40 BoundingRect: TRect; 41 procedure Paint(Canvas: TCanvas); override; 42 procedure Zoom(Factor: Double); override; 43 procedure Move(Delta: TPoint); override; 44 constructor Create; 45 destructor Destroy; override; 46 end; 47 48 { TCanvasLine } 49 50 TCanvasLine = class(TCanvasObject) 51 Pen: TPen; 52 P1, P2: TPoint; 53 procedure Paint(Canvas: TCanvas); override; 54 procedure Zoom(Factor: Double); override; 55 procedure Move(Delta: TPoint); override; 56 constructor Create; 57 destructor Destroy; override; 58 end; 59 60 { TCanvasPolygon } 61 62 TCanvasPolygon = class(TCanvasObject) 63 Pen: TPen; 64 Brush: TBrush; 65 Points: array of TPoint; 66 procedure Paint(Canvas: TCanvas); override; 67 procedure Zoom(Factor: Double); override; 68 procedure Move(Delta: TPoint); override; 69 constructor Create; 70 destructor Destroy; override; 71 end; 72 73 { TCanvasEllipse } 74 75 TCanvasEllipse = class(TCanvasObject) 76 Pen: TPen; 77 Brush: TBrush; 78 BoundingRect: TRect; 79 procedure Paint(Canvas: TCanvas); override; 80 procedure Zoom(Factor: Double); override; 81 procedure Move(Delta: TPoint); override; 82 constructor Create; 83 destructor Destroy; override; 84 end; 85 86 { TCanvasPie } 87 88 TCanvasPie = class(TCanvasObject) 89 Pen: TPen; 90 Brush: TBrush; 91 BoundingRect: TRect; 92 StartPoint: TPoint; 93 EndPoint: TPoint; 94 procedure Paint(Canvas: TCanvas); override; 95 procedure Zoom(Factor: Double); override; 96 procedure Move(Delta: TPoint); override; 97 constructor Create; 98 destructor Destroy; override; 99 end; 100 101 { TCanvasStretchDraw } 102 103 TCanvasStretchDraw = class(TCanvasObject) 104 SrcGraphic: TGraphic; 105 DestRect: TRect; 106 procedure Paint(Canvas: TCanvas); override; 107 procedure Zoom(Factor: Double); override; 108 procedure Move(Delta: TPoint); override; 109 constructor Create; 110 destructor Destroy; override; 24 111 end; 25 112 … … 28 115 TMetaCanvas = class(TCanvas) 29 116 private 30 117 FSize: TPoint; 118 FPenPos: TPoint; 119 protected 120 procedure SetHeight(AValue: Integer); override; 121 function GetHeight: Integer; override; 122 procedure SetWidth(AValue: Integer); override; 123 function GetWidth: Integer; override; 124 procedure DoLine (x1,y1,x2,y2:integer); override; 125 procedure DoTextOut(X, Y: Integer; Text: string); override; 126 procedure TextOut(X,Y: Integer; const Text: String); override; 127 procedure DoRectangle(const Bounds: TRect); override; 128 procedure DoRectangleFill(const Bounds: TRect); override; 129 procedure Polygon(Points: PPoint; NumPts: Integer; Winding: boolean = False); override; 130 procedure DoPolygon(const Points: array of TPoint); override; 131 procedure CreateHandle; override; 132 procedure Ellipse(x1, y1, x2, y2: Integer); override; 133 procedure DoEllipse(const Bounds: TRect); override; 134 procedure StretchDraw(const DestRect: TRect; SrcGraphic: TGraphic); override; 135 function TextExtent(const Text: string): TSize; override; 136 procedure DoMoveTo(X, Y: Integer); override; 137 procedure DoLineTo(X, Y: Integer); override; 138 procedure Pie(EllipseX1, EllipseY1, EllipseX2, EllipseY2, 139 StartX, StartY, EndX, EndY: Integer); override; 31 140 public 32 141 Objects: TObjectList; 142 procedure SetSize(Size: TPoint); 33 143 procedure Reset; 34 144 procedure DrawTo(Canvas: TCanvas); 145 procedure Zoom(Factor: Double); 146 procedure Move(Delta: TPoint); 35 147 constructor Create; 36 148 destructor Destroy; override; … … 39 151 implementation 40 152 153 uses 154 UGeometric; 155 156 { TCanvasPie } 157 158 procedure TCanvasPie.Paint(Canvas: TCanvas); 159 begin 160 Canvas.Brush.Assign(Brush); 161 Canvas.Pen.Assign(Pen); 162 Canvas.Pie(BoundingRect.Left, BoundingRect.Top, 163 BoundingRect.Right, BoundingRect.Bottom, StartPoint.X, StartPoint.Y, 164 EndPoint.X, EndPoint.Y); 165 end; 166 167 procedure TCanvasPie.Zoom(Factor: Double); 168 begin 169 BoundingRect := Rect(Trunc(BoundingRect.Left * Factor), 170 Trunc(BoundingRect.Top * Factor), 171 Trunc(BoundingRect.Right * Factor), 172 Trunc(BoundingRect.Bottom * Factor)); 173 Pen.Width := Trunc(Pen.Width * Factor); 174 StartPoint := Point(Trunc(StartPoint.X * Factor), Trunc(StartPoint.Y * Factor)); 175 EndPoint := Point(Trunc(EndPoint.X * Factor), Trunc(EndPoint.Y * Factor)); 176 end; 177 178 procedure TCanvasPie.Move(Delta: TPoint); 179 begin 180 BoundingRect := ShiftRect(BoundingRect, Delta); 181 StartPoint := AddPoint(StartPoint, Delta); 182 EndPoint := AddPoint(EndPoint, Delta); 183 end; 184 185 constructor TCanvasPie.Create; 186 begin 187 Pen := TPen.Create; 188 Brush := TBrush.Create; 189 end; 190 191 destructor TCanvasPie.Destroy; 192 begin 193 Pen.Free; 194 Brush.Free; 195 inherited Destroy; 196 end; 197 198 { TCanvasStretchDraw } 199 200 procedure TCanvasStretchDraw.Paint(Canvas: TCanvas); 201 begin 202 Canvas.StretchDraw(DestRect, SrcGraphic); 203 end; 204 205 procedure TCanvasStretchDraw.Zoom(Factor: Double); 206 begin 207 DestRect := Rect(Trunc(DestRect.Left * Factor), 208 Trunc(DestRect.Top * Factor), 209 Trunc(DestRect.Right * Factor), 210 Trunc(DestRect.Bottom * Factor)); 211 end; 212 213 procedure TCanvasStretchDraw.Move(Delta: TPoint); 214 begin 215 DestRect := ShiftRect(DestRect, Delta); 216 end; 217 218 constructor TCanvasStretchDraw.Create; 219 begin 220 SrcGraphic := nil; 221 end; 222 223 destructor TCanvasStretchDraw.Destroy; 224 begin 225 inherited Destroy; 226 end; 227 228 { TCanvasEllipse } 229 230 procedure TCanvasEllipse.Paint(Canvas: TCanvas); 231 begin 232 Canvas.Pen.Assign(Pen); 233 Canvas.Brush.Assign(Brush); 234 Canvas.Ellipse(BoundingRect); 235 end; 236 237 procedure TCanvasEllipse.Zoom(Factor: Double); 238 begin 239 BoundingRect := Rect(Trunc(BoundingRect.Left * Factor), 240 Trunc(BoundingRect.Top * Factor), 241 Trunc(BoundingRect.Right * Factor), 242 Trunc(BoundingRect.Bottom * Factor)); 243 Pen.Width := Trunc(Pen.Width * Factor); 244 end; 245 246 procedure TCanvasEllipse.Move(Delta: TPoint); 247 begin 248 BoundingRect := ShiftRect(BoundingRect, Delta); 249 end; 250 251 constructor TCanvasEllipse.Create; 252 begin 253 Pen := TPen.Create; 254 Brush := TBrush.Create; 255 end; 256 257 destructor TCanvasEllipse.Destroy; 258 begin 259 Pen.Free; 260 Brush.Free; 261 inherited Destroy; 262 end; 263 264 { TCanvasPolygon } 265 266 procedure TCanvasPolygon.Paint(Canvas: TCanvas); 267 begin 268 Canvas.Pen.Assign(Pen); 269 Canvas.Brush.Assign(Brush); 270 Canvas.Polygon(Points); 271 end; 272 273 procedure TCanvasPolygon.Zoom(Factor: Double); 274 var 275 I: Integer; 276 begin 277 for I := 0 to High(Points) do 278 Points[I] := Point(Trunc(Points[I].X * Factor), 279 Trunc(Points[I].Y * Factor)); 280 Pen.Width := Trunc(Pen.Width * Factor); 281 end; 282 283 procedure TCanvasPolygon.Move(Delta: TPoint); 284 var 285 I: Integer; 286 begin 287 for I := 0 to High(Points) do 288 Points[I] := AddPoint(Points[I], Delta); 289 end; 290 291 constructor TCanvasPolygon.Create; 292 begin 293 Pen := TPen.Create; 294 Brush := TBrush.Create; 295 end; 296 297 destructor TCanvasPolygon.Destroy; 298 begin 299 Brush.Free; 300 Pen.Free; 301 inherited Destroy; 302 end; 303 304 { TCanvasLine } 305 306 procedure TCanvasLine.Paint(Canvas: TCanvas); 307 begin 308 Canvas.Pen.Assign(Pen); 309 Canvas.Line(P1, P2); 310 end; 311 312 procedure TCanvasLine.Zoom(Factor: Double); 313 begin 314 P1 := Point(Trunc(P1.X * Factor), Trunc(P1.Y * Factor)); 315 P2 := Point(Trunc(P2.X * Factor), Trunc(P2.Y * Factor)); 316 Pen.Width := Trunc(Pen.Width * Factor); 317 end; 318 319 procedure TCanvasLine.Move(Delta: TPoint); 320 begin 321 P1 := AddPoint(P1, Delta); 322 P2 := AddPoint(P2, Delta); 323 end; 324 325 constructor TCanvasLine.Create; 326 begin 327 Pen := TPen.Create; 328 end; 329 330 destructor TCanvasLine.Destroy; 331 begin 332 Pen.Free; 333 inherited Destroy; 334 end; 335 336 { TCanvasRectangle } 337 338 procedure TCanvasRectangle.Paint(Canvas: TCanvas); 339 begin 340 Canvas.Pen.Assign(Pen); 341 Canvas.Brush.Assign(Brush); 342 Canvas.Rectangle(BoundingRect); 343 end; 344 345 procedure TCanvasRectangle.Zoom(Factor: Double); 346 begin 347 BoundingRect := Rect(Trunc(BoundingRect.Left * Factor), 348 Trunc(BoundingRect.Top * Factor), 349 Trunc(BoundingRect.Right * Factor), 350 Trunc(BoundingRect.Bottom * Factor)); 351 Pen.Width := Trunc(Pen.Width * Factor); 352 end; 353 354 procedure TCanvasRectangle.Move(Delta: TPoint); 355 begin 356 ShiftRect(BoundingRect, Delta); 357 end; 358 359 constructor TCanvasRectangle.Create; 360 begin 361 Pen := TPen.Create; 362 Brush := TBrush.Create; 363 end; 364 365 destructor TCanvasRectangle.Destroy; 366 begin 367 Pen.Free; 368 Brush.Free; 369 inherited Destroy; 370 end; 371 41 372 { TCanvasText } 42 373 43 374 procedure TCanvasText.Paint(Canvas: TCanvas); 44 375 begin 376 Canvas.Brush.Assign(Brush); 377 Canvas.Font.Assign(Font); 45 378 Canvas.TextOut(Position.X, Position.Y, Text); 46 379 end; 47 380 381 procedure TCanvasText.Zoom(Factor: Double); 382 begin 383 Position := Point(Trunc(Position.X * Factor), Trunc(Position.Y * Factor)); 384 Font.Size := Trunc(Font.Size * Factor); 385 end; 386 387 procedure TCanvasText.Move(Delta: TPoint); 388 begin 389 Position := AddPoint(Position, Delta); 390 end; 391 392 constructor TCanvasText.Create; 393 begin 394 Font := TFont.Create; 395 Brush := TBrush.Create; 396 end; 397 398 destructor TCanvasText.Destroy; 399 begin 400 Brush.Free; 401 Font.Free; 402 inherited Destroy; 403 end; 404 48 405 { TCanvasObject } 49 406 … … 53 410 end; 54 411 412 procedure TCanvasObject.Zoom(Factor: Double); 413 begin 414 415 end; 416 417 procedure TCanvasObject.Move(Delta: TPoint); 418 begin 419 end; 420 55 421 { TMetaCanvas } 422 423 procedure TMetaCanvas.SetHeight(AValue: Integer); 424 begin 425 FSize.Y := AValue; 426 end; 427 428 function TMetaCanvas.GetHeight: Integer; 429 begin 430 Result := FSize.Y; 431 end; 432 433 procedure TMetaCanvas.SetWidth(AValue: Integer); 434 begin 435 FSize.X := AValue; 436 end; 437 438 function TMetaCanvas.GetWidth: Integer; 439 begin 440 Result := FSize.X; 441 end; 442 443 procedure TMetaCanvas.DoLine(x1, y1, x2, y2: integer); 444 var 445 NewObj: TCanvasLine; 446 begin 447 NewObj := TCanvasLine.Create; 448 NewObj.Pen.Assign(Pen); 449 NewObj.P1 := Point(X1, Y1); 450 NewObj.P2 := Point(X2, Y2); 451 Objects.Add(NewObj); 452 end; 453 454 procedure TMetaCanvas.DoTextOut(X, Y: Integer; Text: string); 455 var 456 NewObj: TCanvasText; 457 begin 458 NewObj := TCanvasText.Create; 459 NewObj.Font.Assign(Font); 460 NewObj.Brush.Assign(Brush); 461 NewObj.Position := Point(X, Y); 462 NewObj.Text := Text; 463 Objects.Add(NewObj); 464 end; 465 466 procedure TMetaCanvas.TextOut(X, Y: Integer; const Text: String); 467 begin 468 DoTextOut(X, Y, Text); 469 end; 470 471 procedure TMetaCanvas.DoRectangle(const Bounds: TRect); 472 var 473 NewObj: TCanvasRectangle; 474 begin 475 NewObj := TCanvasRectangle.Create; 476 NewObj.Pen.Assign(Pen); 477 NewObj.BoundingRect := Bounds; 478 Objects.Add(NewObj); 479 end; 480 481 procedure TMetaCanvas.DoRectangleFill(const Bounds: TRect); 482 var 483 NewObj: TCanvasRectangle; 484 begin 485 NewObj := TCanvasRectangle.Create; 486 NewObj.Brush.Assign(Brush); 487 NewObj.Pen.Assign(Pen); 488 NewObj.BoundingRect := Bounds; 489 Objects.Add(NewObj); 490 end; 491 492 procedure TMetaCanvas.Polygon(Points: PPoint; NumPts: Integer; Winding: boolean 493 ); 494 var 495 APoints: array of TPoint; 496 I: Integer; 497 begin 498 SetLength(APoints, NumPts); 499 for I := 0 to High(APoints) do 500 APoints[I] := Points[I]; 501 DoPolygon(APoints); 502 end; 503 504 procedure TMetaCanvas.DoPolygon(const Points: array of TPoint); 505 var 506 NewObj: TCanvasPolygon; 507 I: Integer; 508 begin 509 NewObj := TCanvasPolygon.Create; 510 NewObj.Brush.Assign(Brush); 511 NewObj.Pen.Assign(Pen); 512 SetLength(NewObj.Points, Length(Points)); 513 for I := 0 to High(Points) do 514 NewObj.Points[I] := Points[I]; 515 Objects.Add(NewObj); 516 end; 517 518 procedure TMetaCanvas.CreateHandle; 519 begin 520 end; 521 522 procedure TMetaCanvas.Ellipse(x1, y1, x2, y2: Integer); 523 begin 524 DoEllipse(Rect(X1, Y1, X2, Y2)); 525 end; 526 527 procedure TMetaCanvas.DoEllipse(const Bounds: TRect); 528 var 529 NewObj: TCanvasEllipse; 530 begin 531 NewObj := TCanvasEllipse.Create; 532 NewObj.Brush.Assign(Brush); 533 NewObj.Pen.Assign(Pen); 534 NewObj.BoundingRect := Bounds; 535 Objects.Add(NewObj); 536 end; 537 538 procedure TMetaCanvas.StretchDraw(const DestRect: TRect; SrcGraphic: TGraphic); 539 var 540 NewObj: TCanvasStretchDraw; 541 begin 542 NewObj := TCanvasStretchDraw.Create; 543 NewObj.SrcGraphic := SrcGraphic; 544 NewObj.DestRect := DestRect; 545 Objects.Add(NewObj); 546 end; 547 548 function TMetaCanvas.TextExtent(const Text: string): TSize; 549 begin 550 Result := Size(0, 0); 551 end; 552 553 procedure TMetaCanvas.DoMoveTo(X, Y: Integer); 554 begin 555 FPenPos := Point(X, Y); 556 end; 557 558 procedure TMetaCanvas.DoLineTo(X, Y: Integer); 559 begin 560 DoLine(FPenPos.X, FPenPos.Y, X, Y); 561 DoMoveTo(X, Y); 562 end; 563 564 procedure TMetaCanvas.Pie(EllipseX1, EllipseY1, EllipseX2, EllipseY2, StartX, 565 StartY, EndX, EndY: Integer); 566 var 567 NewObj: TCanvasPie; 568 begin 569 NewObj := TCanvasPie.Create; 570 NewObj.Brush.Assign(Brush); 571 NewObj.Pen.Assign(Pen); 572 NewObj.BoundingRect := Rect(EllipseX1, EllipseY1, EllipseX2, EllipseY2); 573 NewObj.StartPoint := Point(StartX, StartY); 574 NewObj.EndPoint := Point(EndX, EndY); 575 Objects.Add(NewObj); 576 end; 577 578 procedure TMetaCanvas.SetSize(Size: TPoint); 579 begin 580 FSize := Size; 581 end; 56 582 57 583 procedure TMetaCanvas.Reset; … … 68 594 end; 69 595 596 procedure TMetaCanvas.Zoom(Factor: Double); 597 var 598 I: Integer; 599 begin 600 for I := 0 to Objects.Count - 1 do 601 TCanvasObject(Objects[I]).Zoom(Factor); 602 end; 603 604 procedure TMetaCanvas.Move(Delta: TPoint); 605 var 606 I: Integer; 607 begin 608 for I := 0 to Objects.Count - 1 do 609 TCanvasObject(Objects[I]).Move(Delta); 610 end; 611 70 612 constructor TMetaCanvas.Create; 71 613 begin 614 inherited; 615 FPenPos := Point(0, 0); 72 616 Objects := TObjectList.Create; 73 617 end;
Note:
See TracChangeset
for help on using the changeset viewer.