source: tags/1.3.0/UView.pas

Last change on this file was 103, checked in by chronos, 20 months ago
  • Fixed: Division by zero in view zoom level calculation on Windows.
File size: 2.8 KB
Line 
1unit UView;
2
3interface
4
5uses
6 Classes, SysUtils;
7
8type
9 { TView }
10
11 TView = class
12 private
13 FDestRect: TRect;
14 FSourceRect: TRect;
15 FZoom: Double;
16 procedure SetDestRect(AValue: TRect);
17 procedure SetSourceRect(AValue: TRect);
18 procedure SetZoom(AValue: Double);
19 public
20 procedure Assign(Source: TView);
21 function PointDestToSrc(Pos: TPoint): TPoint;
22 function PointSrcToDest(Pos: TPoint): TPoint;
23 constructor Create;
24 property SourceRect: TRect read FSourceRect write SetSourceRect;
25 property DestRect: TRect read FDestRect write SetDestRect;
26 property Zoom: Double read FZoom write SetZoom;
27 end;
28
29
30implementation
31
32uses
33 UGeometric;
34
35resourcestring
36 SZeroZoomNotAlowed = 'Zero zoom not allowed';
37
38{ TView }
39
40procedure TView.SetDestRect(AValue: TRect);
41var
42 Diff: TPoint;
43begin
44 if RectEquals(FDestRect, AValue) then Exit;
45 Diff := Point(Trunc((DestRect.Right - DestRect.Left) / Zoom - (AValue.Right - AValue.Left) / Zoom) div 2,
46 Trunc((DestRect.Bottom - DestRect.Top) / Zoom - (AValue.Bottom - AValue.Top) / Zoom) div 2);
47 FDestRect := AValue;
48 FSourceRect := Bounds(FSourceRect.Left + Diff.X, FSourceRect.Top + Diff.Y,
49 Trunc((DestRect.Right - DestRect.Left) / Zoom),
50 Trunc((DestRect.Bottom - DestRect.Top) / Zoom));
51end;
52
53procedure TView.SetSourceRect(AValue: TRect);
54var
55 ZX: Double;
56 ZY: Double;
57begin
58 if RectEquals(FSourceRect, AValue) then Exit;
59 FSourceRect := AValue;
60 if ((FSourceRect.Right - FSourceRect.Left) <> 0) and
61 ((FSourceRect.Bottom - FSourceRect.Top) <> 0) then begin
62 ZX := (FDestRect.Right - FDestRect.Left) / (FSourceRect.Right - FSourceRect.Left);
63 ZY := (FDestRect.Bottom - FDestRect.Top) / (FSourceRect.Bottom - FSourceRect.Top);
64 if ZX > ZY then
65 Zoom := ZY
66 else Zoom := ZX;
67 end else Zoom := 1;
68end;
69
70procedure TView.SetZoom(AValue: Double);
71begin
72 if FZoom = AValue then Exit;
73 if AValue = 0 then
74 raise Exception.Create(SZeroZoomNotAlowed);
75 FZoom := AValue;
76 FSourceRect := Bounds(Trunc(FSourceRect.Left + (FSourceRect.Right - FSourceRect.Left) div 2 - (DestRect.Right - DestRect.Left) / Zoom / 2),
77 Trunc(FSourceRect.Top + (FSourceRect.Bottom - FSourceRect.Top) div 2 - (FDestRect.Bottom - DestRect.Top) / Zoom / 2),
78 Trunc((DestRect.Right - DestRect.Left) / Zoom),
79 Trunc((DestRect.Bottom - DestRect.Top) / Zoom));
80end;
81
82procedure TView.Assign(Source: TView);
83begin
84 FDestRect := Source.FDestRect;
85 FSourceRect := Source.FSourceRect;
86 FZoom := Source.FZoom;
87end;
88
89function TView.PointDestToSrc(Pos: TPoint): TPoint;
90begin
91 Result := Point(Trunc(Pos.X / FZoom + FSourceRect.Left),
92 Trunc(Pos.Y / FZoom + FSourceRect.Top));
93end;
94
95function TView.PointSrcToDest(Pos: TPoint): TPoint;
96begin
97 Result := Point(Trunc((Pos.X - FSourceRect.Left) * FZoom),
98 Trunc((Pos.Y - FSourceRect.Top) * FZoom));
99end;
100
101constructor TView.Create;
102begin
103 Zoom := 1;
104end;
105
106end.
107
Note: See TracBrowser for help on using the repository browser.