-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSize_Box.pas
292 lines (237 loc) · 6.84 KB
/
Size_Box.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
unit Size_Box;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Cod.Visual.Button,
Cod.GDI, Cod.VarHelpers, Cod.ColorUtils, GDIPAPI, Cod.Visual.Slider,
Vcl.StdCtrls, Cod.Types, CFX.Dialogs, Types;
type
TSelectSize = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Button_Save: CButton;
Button_Cancel: CButton;
DrawBox: TPaintBox;
MoveAnim: TTimer;
Panel3: TPanel;
DefSizText: TLabel;
DefSiz: TLabel;
Panel4: TPanel;
CButton1: CButton;
CButton2: CButton;
CButton3: CButton;
CButton4: CButton;
CButton5: CButton;
procedure DrawBoxPaint(Sender: TObject);
procedure DrawBoxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure DrawBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure DrawBoxMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure MoveAnimTimer(Sender: TObject);
procedure Button_SaveClick(Sender: TObject);
procedure SelectPreset(Sender: TObject);
procedure KeyDetection(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure CButton5Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
MouseWasDown,
MouseIsDown: boolean;
TheRect: TRect;
DownPos,
LastPos: TPoint;
PenDraw: TGDIPen;
procedure WasSelected;
procedure ClearSelect;
public
{ Public declarations }
end;
var
SelectSize: TSelectSize;
// Output
NewSize: TPoint;
Multiplier: double = 1;
implementation
uses
MainUI;
{$R *.dfm}
procedure TSelectSize.SelectPreset(Sender: TObject);
begin
WasSelected;
// Select
case CButton(Sender).Tag of
1: TheRect := Rect(0, 0, DrawBox.Width, DrawBox.Height);
2: begin
if DrawBox.Width > DrawBox.Height then
TheRect := Rect(0, 0, DrawBox.Width div 2, DrawBox.Height)
else
TheRect := Rect(0, 0, DrawBox.Width, DrawBox.Height div 2);
end;
3: TheRect := Rect(0, 0, DrawBox.Width div 2, DrawBox.Height div 2);
4: TheRect := Rect(0, 0, DrawBox.Width div 3, DrawBox.Height div 3);
end;
// Draw
DrawBox.Invalidate;
end;
procedure TSelectSize.WasSelected;
begin
// The process will... never give you up, never let you down
MouseWasDown := true;
Button_Save.Enabled := MouseWasDown;
end;
procedure TSelectSize.CButton5Click(Sender: TObject);
var
A: FXDialog;
begin
A := FXDialog.Create;
// Help Dialog
try
A.Title := 'Help';
A.Text := 'Draw a rectangle size for your image to fit in the Paper.'#13 +
#13'Tips using this dialog:' +
#13' - Hold Ctrl to feely move the rectangle' +
#13' - Use the preset buttons' +
#13' - Press Ctrl + 1..5 to auto position the rectangle' +
#13' - Press Shift for precision mode';
A.Buttons := [mbOk];
A.Execute;
finally
A.Free;
end;
end;
procedure TSelectSize.ClearSelect;
begin
// Never began
MouseWasDown := false;
Button_Save.Enabled := MouseWasDown;
end;
procedure TSelectSize.DrawBoxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
// Mouse Down
MouseIsDown := true;
WasSelected;
// Precision
if ssShift in Shift then
begin
X := (trunc(X / 10) * 10);
Y := (trunc(Y / 10) * 10);
end;
// Position s
DownPos := Point(X, Y);
LastPos := DownPos;
// Ctrl
if not (ssCtrl in Shift) then
TheRect.TopLeft := DownPos;
// Also Notify on Move
DrawBoxMouseMove(Sender, Shift, X, Y);
end;
procedure TSelectSize.DrawBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
NewRect: TRect;
begin
if MouseIsDown then
begin
// Precision
if ssShift in Shift then
begin
X := (trunc(X / 10) * 10);
Y := (trunc(Y / 10) * 10);
end;
// Ctrl
NewRect := TheRect;
if ssCtrl in Shift then
NewRect.Offset(X - LastPos.X, Y - LastPos.Y)
else
NewRect.BottomRight := Point(X, Y);
if TControl(Sender).ClientRect.Contains(NewRect) then
TheRect := NewRect;
end;
// Notif
LastPos := Point(X, Y);
// Draw
DrawBox.Repaint;
end;
procedure TSelectSize.DrawBoxMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
MouseIsDown := false;
end;
procedure TSelectSize.DrawBoxPaint(Sender: TObject);
var
NormalRect: TRect;
Text: string;
begin
with DrawBox.Canvas do
begin
// Draw Main Outline
Pen.Color := clMaroon;
Pen.Style := psDash;
Brush.Style := bsSolid;
Brush.Color := clWhite;
Rectangle(ClipRect);
// Select
if MouseWasDown then
begin
// Get Normalised Rect
NormalRect := TheRect;
NormalRect.NormalizeRect;
// Draw Secondary
Pen.Color := clBlue;
Pen.Style := psDashDot;
// GDI for moving effect
GDIRectangle(NormalRect, nil, PenDraw);
// Draw Text
Font.Color := clBlue;
Text := 'Width: ' + Form1.PixelsToCmStr(NormalRect.Width * Multiplier, DPI_X)
+ ' Height: ' + Form1.PixelsToCmStr(NormalRect.Height * Multiplier, DPI_Y);
TextOut( NormalRect.Right - TextWidth(Text), NormalRect.Bottom + 5, Text);
end;
end;
end;
procedure TSelectSize.FormCreate(Sender: TObject);
var
DashArray: array [0..1] of Single;
begin
PenDraw := GetRGB(clBlue).MakeGDIPen(1.5);
DashArray[0] := 6; // length of dash
DashArray[1] := 3; // length of gap
PenDraw.SetDashStyle(DashStyleCustom);
PenDraw.SetDashPattern(@DashArray[0], Length(DashArray));
PenDraw.SetDashCap(DashCapFlat);
end;
procedure TSelectSize.FormDestroy(Sender: TObject);
begin
PenDraw.Free;
end;
procedure TSelectSize.KeyDetection(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = 46 then
ClearSelect;
if ssCtrl in Shift then
begin
case Key of
49: TheRect.Offset(-TheRect.TopLeft.X, -TheRect.TopLeft.Y);
50: TheRect.Offset(DrawBox.Width - TheRect.BottomRight.X, -TheRect.TopLeft.Y);
51: TheRect.Offset(-TheRect.TopLeft.X, DrawBox.Height - TheRect.BottomRight.Y);
52: TheRect.Offset(DrawBox.Width - TheRect.BottomRight.X, DrawBox.Height - TheRect.BottomRight.Y);
53: CenterRectInRect( TheRect, DrawBox.BoundsRect );
end;
end;
end;
procedure TSelectSize.MoveAnimTimer(Sender: TObject);
begin
// Moving Line Effect
PenDraw.SetDashOffset( PenDraw.GetDashOffset + 0.2 );
DrawBox.repaint;
end;
procedure TSelectSize.Button_SaveClick(Sender: TObject);
begin
NewSize := Point(abs(round(TheRect.Width * Multiplier)), abs(round(TheRect.Height * Multiplier)));
end;
end.