source: trunk/UProject.pas

Last change on this file was 33, checked in by chronos, 7 years ago
  • Added: Remember last used image size and color format.
  • Added: Automatically create blank image.
File size: 3.7 KB
Line 
1unit UProject;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UFGraphics;
9
10type
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
51implementation
52
53function RectEquals(A, B: TRect): Boolean;
54begin
55 Result := (A.Left = B.Left) and (A.Top = B.Top) and
56 (A.Right = B.Right) and (A.Bottom = B.Bottom);
57end;
58
59{ TFloatPoint }
60
61function TFloatPoint.Create(AX, AY: Double): TFloatPoint;
62begin
63 Result.X := AX;
64 Result.Y := AY;
65end;
66
67
68{ TView }
69
70procedure TView.SetDestRect(AValue: TRect);
71var
72 Diff: TPoint;
73begin
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));
83end;
84
85procedure TView.SetSrcRect(AValue: TRect);
86begin
87 if RectEquals(FSrcRect, AValue) then Exit;
88 FSrcRect := AValue;
89end;
90
91procedure TView.SetZoom(AValue: Double);
92const
93 ZoomMin = 1/10000;
94 ZoomMax = 10000;
95begin
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));
105end;
106
107procedure TView.Center(Rect: TRect);
108begin
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);
113end;
114
115constructor TView.Create;
116begin
117 FZoom := 1;
118end;
119
120procedure TView.ZoomAll(BitmapSize: TPoint);
121var
122 Factor: TFloatPoint;
123begin
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));
129end;
130
131function TView.DestToSrcPos(Pos: TPoint): TPoint;
132begin
133 Result := Point(Trunc(Pos.X / FZoom + FSrcRect.Left),
134 Trunc(Pos.Y / FZoom + FSrcRect.Top));
135end;
136
137function TView.SrcToDestPos(Pos: TPoint): TPoint;
138begin
139 Result := Point(Trunc((Pos.X - FSrcRect.Left) * FZoom),
140 Trunc((Pos.Y - FSrcRect.Top) * FZoom));
141end;
142
143
144{ TProject }
145
146constructor TProject.Create;
147begin
148 Bitmap := TFPixmap.Create(nil);
149 View := TView.Create;
150end;
151
152destructor TProject.Destroy;
153begin
154 View.Free;
155 Bitmap.Free;
156 inherited Destroy;
157end;
158
159end.
160
Note: See TracBrowser for help on using the repository browser.