Changeset 10 for trunk/UProject.pas
- Timestamp:
- Sep 22, 2014, 3:07:02 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UProject.pas
r6 r10 10 10 type 11 11 12 { TView Port}12 { TView } 13 13 14 TViewPort = class 14 TView = class 15 private 16 FSrcRect: TRect; 17 FDestRect: TRect; 18 FZoom: Double; 19 procedure SetDestRect(AValue: TRect); 20 procedure SetSrcRect(AValue: TRect); 21 procedure SetZoom(AValue: Double); 15 22 public 16 Pos: TPoint; 17 Zoom: Double; 18 ViewSize: TPoint; 23 procedure Center(Rect: TRect); 24 constructor Create; 25 function DestToSrcPos(Pos: TPoint): TPoint; 26 function SrcToDestPos(Pos: TPoint): TPoint; 27 property DestRect: TRect read FDestRect write SetDestRect; 28 property Zoom: Double read FZoom write SetZoom; 29 property SrcRect: TRect read FSrcRect write SetSrcRect; 19 30 end; 20 31 … … 24 35 FileName: string; 25 36 Bitmap: TGBitmap; 26 View Port: TViewPort;37 View: TView; 27 38 constructor Create; 28 39 destructor Destroy; override; … … 30 41 31 42 implementation 43 44 function RectEquals(A, B: TRect): Boolean; 45 begin 46 Result := (A.Left = B.Left) and (A.Top = B.Top) and 47 (A.Right = B.Right) and (A.Bottom = B.Bottom); 48 end; 49 50 51 { TView } 52 53 procedure TView.SetDestRect(AValue: TRect); 54 var 55 Diff: TPoint; 56 begin 57 if RectEquals(FDestRect, AValue) then Exit; 58 Diff := Point(Trunc((FDestRect.Right - FDestRect.Left) / FZoom - 59 (AValue.Right - AValue.Left) / FZoom) div 2, 60 Trunc((FDestRect.Bottom - FDestRect.Top) / FZoom - 61 (AValue.Bottom - AValue.Top) / FZoom) div 2); 62 FDestRect := AValue; 63 SrcRect := Bounds(FSrcRect.Left + Diff.X, FSrcRect.Top + Diff.Y, 64 Trunc((FDestRect.Right - FDestRect.Left) / FZoom), 65 Trunc((FDestRect.Bottom - FDestRect.Top) / FZoom)); 66 end; 67 68 procedure TView.SetSrcRect(AValue: TRect); 69 begin 70 if RectEquals(FSrcRect, AValue) then Exit; 71 FSrcRect := AValue; 72 end; 73 74 procedure TView.SetZoom(AValue: Double); 75 const 76 ZoomMin = 1/10000; 77 ZoomMax = 10000; 78 begin 79 if FZoom = AValue then Exit; 80 if (FZoom > ZoomMax) or (FZoom < ZoomMin) then Exit; 81 FZoom := AValue; 82 FSrcRect := Bounds(Trunc(FSrcRect.Left + (FSrcRect.Right - FSrcRect.Left) div 2 - 83 (DestRect.Right - DestRect.Left) / FZoom / 2), 84 Trunc(FSrcRect.Top + (FSrcRect.Bottom - FSrcRect.Top) div 2 - 85 (DestRect.Bottom - DestRect.Top) / FZoom / 2), 86 Trunc((DestRect.Right - DestRect.Left) / FZoom), 87 Trunc((DestRect.Bottom - DestRect.Top) / FZoom)); 88 end; 89 90 procedure TView.Center(Rect: TRect); 91 begin 92 FSrcRect := Bounds(Rect.Left + (Rect.Right - Rect.Left) div 2 - (FSrcRect.Right - FSrcRect.Left) div 2, 93 Rect.Top + (Rect.Bottom - Rect.Top) div 2 - (FSrcRect.Bottom - FSrcRect.Top) div 2, 94 FSrcRect.Right - FSrcRect.Left, 95 FSrcRect.Bottom - FSrcRect.Top); 96 end; 97 98 constructor TView.Create; 99 begin 100 FZoom := 1; 101 end; 102 103 function TView.DestToSrcPos(Pos: TPoint): TPoint; 104 begin 105 Result := Point(Trunc(Pos.X / FZoom + FSrcRect.Left), 106 Trunc(Pos.Y / FZoom + FSrcRect.Top)); 107 end; 108 109 function TView.SrcToDestPos(Pos: TPoint): TPoint; 110 begin 111 Result := Point(Trunc((Pos.X - FSrcRect.Left) * FZoom), 112 Trunc((Pos.Y - FSrcRect.Top) * FZoom)); 113 end; 32 114 33 115 … … 37 119 begin 38 120 Bitmap := TGBitmap.Create; 39 View Port := TViewPort.Create;121 View := TView.Create; 40 122 end; 41 123 42 124 destructor TProject.Destroy; 43 125 begin 44 View Port.Free;126 View.Free; 45 127 Bitmap.Free; 46 128 inherited Destroy;
Note:
See TracChangeset
for help on using the changeset viewer.