source: trunk/View.pas

Last change on this file was 143, checked in by chronos, 11 months ago

Removed U prefix from all units.

File size: 4.2 KB
Line 
1unit View;
2
3interface
4
5uses
6 Classes, SysUtils, Items, DOM;
7
8type
9 { TView }
10
11 TView = class(TItem)
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: TItem); override;
21 function PointDestToSrc(Pos: TPoint): TPoint;
22 function PointSrcToDest(Pos: TPoint): TPoint;
23 constructor Create;
24 procedure LoadFromXmlNode(Node: TDOMNode); override;
25 procedure SaveToXmlNode(Node: TDOMNode); override;
26 property SourceRect: TRect read FSourceRect write SetSourceRect;
27 property DestRect: TRect read FDestRect write SetDestRect;
28 property Zoom: Double read FZoom write SetZoom;
29 end;
30
31
32implementation
33
34uses
35 Geometric, XML;
36
37resourcestring
38 SZeroZoomNotAlowed = 'Zero zoom not allowed';
39
40{ TView }
41
42procedure TView.SetDestRect(AValue: TRect);
43var
44 Diff: TPoint;
45begin
46 if RectEquals(FDestRect, AValue) then Exit;
47 Diff := Point(Trunc((DestRect.Right - DestRect.Left) / Zoom - (AValue.Right - AValue.Left) / Zoom) div 2,
48 Trunc((DestRect.Bottom - DestRect.Top) / Zoom - (AValue.Bottom - AValue.Top) / Zoom) div 2);
49 FDestRect := AValue;
50 FSourceRect := Bounds(FSourceRect.Left + Diff.X, FSourceRect.Top + Diff.Y,
51 Trunc((DestRect.Right - DestRect.Left) / Zoom),
52 Trunc((DestRect.Bottom - DestRect.Top) / Zoom));
53end;
54
55procedure TView.SetSourceRect(AValue: TRect);
56var
57 ZX: Double;
58 ZY: Double;
59begin
60 if RectEquals(FSourceRect, AValue) then Exit;
61 FSourceRect := AValue;
62 if ((FSourceRect.Right - FSourceRect.Left) <> 0) and
63 ((FSourceRect.Bottom - FSourceRect.Top) <> 0) then begin
64 ZX := (FDestRect.Right - FDestRect.Left) / (FSourceRect.Right - FSourceRect.Left);
65 ZY := (FDestRect.Bottom - FDestRect.Top) / (FSourceRect.Bottom - FSourceRect.Top);
66 if ZX > ZY then
67 Zoom := ZY
68 else Zoom := ZX;
69 end else Zoom := 1;
70end;
71
72procedure TView.SetZoom(AValue: Double);
73begin
74 if FZoom = AValue then Exit;
75 if AValue = 0 then
76 raise Exception.Create(SZeroZoomNotAlowed);
77 FZoom := AValue;
78 FSourceRect := Bounds(Trunc(FSourceRect.Left + (FSourceRect.Right - FSourceRect.Left) div 2 - (DestRect.Right - DestRect.Left) / Zoom / 2),
79 Trunc(FSourceRect.Top + (FSourceRect.Bottom - FSourceRect.Top) div 2 - (FDestRect.Bottom - DestRect.Top) / Zoom / 2),
80 Trunc((DestRect.Right - DestRect.Left) / Zoom),
81 Trunc((DestRect.Bottom - DestRect.Top) / Zoom));
82end;
83
84procedure TView.Assign(Source: TItem);
85begin
86 if Source is TView then begin
87 FDestRect := TView(Source).FDestRect;
88 FSourceRect := TView(Source).FSourceRect;
89 FZoom := TView(Source).FZoom;
90 end;
91end;
92
93function TView.PointDestToSrc(Pos: TPoint): TPoint;
94begin
95 Result := Point(Trunc(Pos.X / FZoom + FSourceRect.Left),
96 Trunc(Pos.Y / FZoom + FSourceRect.Top));
97end;
98
99function TView.PointSrcToDest(Pos: TPoint): TPoint;
100begin
101 Result := Point(Trunc((Pos.X - FSourceRect.Left) * FZoom),
102 Trunc((Pos.Y - FSourceRect.Top) * FZoom));
103end;
104
105constructor TView.Create;
106begin
107 Zoom := 1;
108end;
109
110procedure TView.LoadFromXmlNode(Node: TDOMNode);
111begin
112 inherited;
113 FZoom := ReadDouble(Node, 'Zoom', 0);
114 FSourceRect.Left := ReadInteger(Node, 'SourceRectLeft', 0);
115 FSourceRect.Top := ReadInteger(Node, 'SourceRectTop', 0);
116 FSourceRect.Right := ReadInteger(Node, 'SourceRectRight', 0);
117 FSourceRect.Bottom := ReadInteger(Node, 'SourceRectBottom', 0);
118 FDestRect.Left := ReadInteger(Node, 'DestRectLeft', 0);
119 FDestRect.Top := ReadInteger(Node, 'DestRectTop', 0);
120 FDestRect.Right := ReadInteger(Node, 'DestRectRight', 0);
121 FDestRect.Bottom := ReadInteger(Node, 'DestRectBottom', 0);
122end;
123
124procedure TView.SaveToXmlNode(Node: TDOMNode);
125begin
126 inherited;
127 WriteDouble(Node, 'Zoom', FZoom);
128 WriteInteger(Node, 'SourceRectLeft', FSourceRect.Left);
129 WriteInteger(Node, 'SourceRectTop', FSourceRect.Top);
130 WriteInteger(Node, 'SourceRectRight', FSourceRect.Right);
131 WriteInteger(Node, 'SourceRectBottom', FSourceRect.Bottom);
132 WriteInteger(Node, 'DestRectLeft', FDestRect.Left);
133 WriteInteger(Node, 'DestRectTop', FDestRect.Top);
134 WriteInteger(Node, 'DestRectRight', FDestRect.Right);
135 WriteInteger(Node, 'DestRectBottom', FDestRect.Bottom);
136end;
137
138end.
139
Note: See TracBrowser for help on using the repository browser.