1 | unit UProject;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, UFGraphics;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TFloatPoint }
|
---|
13 |
|
---|
14 | TFloatPoint = record
|
---|
15 | X, Y: Double;
|
---|
16 | function Create(AX, AY: Double): TFloatPoint;
|
---|
17 | end;
|
---|
18 |
|
---|
19 | { TView }
|
---|
20 |
|
---|
21 | TView = class
|
---|
22 | private
|
---|
23 | FSrcRect: TRect;
|
---|
24 | FDestRect: TRect;
|
---|
25 | FZoom: Double;
|
---|
26 | procedure SetDestRect(AValue: TRect);
|
---|
27 | procedure SetSrcRect(AValue: TRect);
|
---|
28 | procedure SetZoom(AValue: Double);
|
---|
29 | public
|
---|
30 | procedure Center(Rect: TRect);
|
---|
31 | constructor Create;
|
---|
32 | procedure ZoomAll(BitmapSize: TPoint);
|
---|
33 | function DestToSrcPos(Pos: TPoint): TPoint;
|
---|
34 | function SrcToDestPos(Pos: TPoint): TPoint;
|
---|
35 | property DestRect: TRect read FDestRect write SetDestRect;
|
---|
36 | property Zoom: Double read FZoom write SetZoom;
|
---|
37 | property SrcRect: TRect read FSrcRect write SetSrcRect;
|
---|
38 | end;
|
---|
39 |
|
---|
40 | { TProject }
|
---|
41 |
|
---|
42 | TProject = class
|
---|
43 | FileName: string;
|
---|
44 | Bitmap: TFPixmap;
|
---|
45 | View: TView;
|
---|
46 | Saved: Boolean;
|
---|
47 | constructor Create;
|
---|
48 | destructor Destroy; override;
|
---|
49 | end;
|
---|
50 |
|
---|
51 | implementation
|
---|
52 |
|
---|
53 | function RectEquals(A, B: TRect): Boolean;
|
---|
54 | begin
|
---|
55 | Result := (A.Left = B.Left) and (A.Top = B.Top) and
|
---|
56 | (A.Right = B.Right) and (A.Bottom = B.Bottom);
|
---|
57 | end;
|
---|
58 |
|
---|
59 | { TFloatPoint }
|
---|
60 |
|
---|
61 | function TFloatPoint.Create(AX, AY: Double): TFloatPoint;
|
---|
62 | begin
|
---|
63 | Result.X := AX;
|
---|
64 | Result.Y := AY;
|
---|
65 | end;
|
---|
66 |
|
---|
67 |
|
---|
68 | { TView }
|
---|
69 |
|
---|
70 | procedure TView.SetDestRect(AValue: TRect);
|
---|
71 | var
|
---|
72 | Diff: TPoint;
|
---|
73 | begin
|
---|
74 | if RectEquals(FDestRect, AValue) then Exit;
|
---|
75 | Diff := Point(Trunc((FDestRect.Right - FDestRect.Left) / FZoom -
|
---|
76 | (AValue.Right - AValue.Left) / FZoom) div 2,
|
---|
77 | Trunc((FDestRect.Bottom - FDestRect.Top) / FZoom -
|
---|
78 | (AValue.Bottom - AValue.Top) / FZoom) div 2);
|
---|
79 | FDestRect := AValue;
|
---|
80 | SrcRect := Bounds(FSrcRect.Left + Diff.X, FSrcRect.Top + Diff.Y,
|
---|
81 | Trunc((FDestRect.Right - FDestRect.Left) / FZoom),
|
---|
82 | Trunc((FDestRect.Bottom - FDestRect.Top) / FZoom));
|
---|
83 | end;
|
---|
84 |
|
---|
85 | procedure TView.SetSrcRect(AValue: TRect);
|
---|
86 | begin
|
---|
87 | if RectEquals(FSrcRect, AValue) then Exit;
|
---|
88 | FSrcRect := AValue;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | procedure TView.SetZoom(AValue: Double);
|
---|
92 | const
|
---|
93 | ZoomMin = 1/10000;
|
---|
94 | ZoomMax = 10000;
|
---|
95 | begin
|
---|
96 | if FZoom = AValue then Exit;
|
---|
97 | if (FZoom > ZoomMax) or (FZoom < ZoomMin) then Exit;
|
---|
98 | FZoom := AValue;
|
---|
99 | FSrcRect := Bounds(Trunc(FSrcRect.Left + (FSrcRect.Right - FSrcRect.Left) div 2 -
|
---|
100 | (DestRect.Right - DestRect.Left) / FZoom / 2),
|
---|
101 | Trunc(FSrcRect.Top + (FSrcRect.Bottom - FSrcRect.Top) div 2 -
|
---|
102 | (DestRect.Bottom - DestRect.Top) / FZoom / 2),
|
---|
103 | Trunc((DestRect.Right - DestRect.Left) / FZoom),
|
---|
104 | Trunc((DestRect.Bottom - DestRect.Top) / FZoom));
|
---|
105 | end;
|
---|
106 |
|
---|
107 | procedure TView.Center(Rect: TRect);
|
---|
108 | begin
|
---|
109 | FSrcRect := Bounds(Rect.Left + (Rect.Right - Rect.Left) div 2 - (FSrcRect.Right - FSrcRect.Left) div 2,
|
---|
110 | Rect.Top + (Rect.Bottom - Rect.Top) div 2 - (FSrcRect.Bottom - FSrcRect.Top) div 2,
|
---|
111 | FSrcRect.Right - FSrcRect.Left,
|
---|
112 | FSrcRect.Bottom - FSrcRect.Top);
|
---|
113 | end;
|
---|
114 |
|
---|
115 | constructor TView.Create;
|
---|
116 | begin
|
---|
117 | FZoom := 1;
|
---|
118 | end;
|
---|
119 |
|
---|
120 | procedure TView.ZoomAll(BitmapSize: TPoint);
|
---|
121 | var
|
---|
122 | Factor: TFloatPoint;
|
---|
123 | begin
|
---|
124 | Factor := TFloatPoint.Create((DestRect.Right - DestRect.Left) / BitmapSize.X,
|
---|
125 | (DestRect.Bottom - DestRect.Top) / BitmapSize.Y);
|
---|
126 | if Factor.X < Factor.Y then Zoom := Factor.X
|
---|
127 | else Zoom := Factor.Y;
|
---|
128 | Center(Rect(0, 0, BitmapSize.X, BitmapSize.Y));
|
---|
129 | end;
|
---|
130 |
|
---|
131 | function TView.DestToSrcPos(Pos: TPoint): TPoint;
|
---|
132 | begin
|
---|
133 | Result := Point(Trunc(Pos.X / FZoom + FSrcRect.Left),
|
---|
134 | Trunc(Pos.Y / FZoom + FSrcRect.Top));
|
---|
135 | end;
|
---|
136 |
|
---|
137 | function TView.SrcToDestPos(Pos: TPoint): TPoint;
|
---|
138 | begin
|
---|
139 | Result := Point(Trunc((Pos.X - FSrcRect.Left) * FZoom),
|
---|
140 | Trunc((Pos.Y - FSrcRect.Top) * FZoom));
|
---|
141 | end;
|
---|
142 |
|
---|
143 |
|
---|
144 | { TProject }
|
---|
145 |
|
---|
146 | constructor TProject.Create;
|
---|
147 | begin
|
---|
148 | Bitmap := TFPixmap.Create(nil);
|
---|
149 | View := TView.Create;
|
---|
150 | end;
|
---|
151 |
|
---|
152 | destructor TProject.Destroy;
|
---|
153 | begin
|
---|
154 | View.Free;
|
---|
155 | Bitmap.Free;
|
---|
156 | inherited Destroy;
|
---|
157 | end;
|
---|
158 |
|
---|
159 | end.
|
---|
160 |
|
---|