-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathResizeShapeControl.pas
More file actions
executable file
·330 lines (284 loc) · 9.01 KB
/
ResizeShapeControl.pas
File metadata and controls
executable file
·330 lines (284 loc) · 9.01 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
{*
* Resizeable Shape: A Delphi component written in Pascal offering resizeable shapes and also a container where it can be created.
* Jonas Raoni Soares da Silva <http://raoni.org>
* https://github.com/jonasraoni/resizeable-shape-component
*}
unit ResizeShapeControl;
interface
uses
Forms, SysUtils, Classes, Graphics, ExtCtrls, Controls, ResizeShape;
type
TShapeEvent = procedure(Shape: TResizeShape; const Index: Integer) of object;
TOnAfterAddShapeEvent = procedure(Shape: TResizeShape) of object;
TResizeShapeControl = class(TScrollBox)
private
FShapeList: TList;
FImage: TImage;
FRealSize: TPoint;
FCanDraw: Boolean;
FIsHandlingShape: Boolean;
FCurrentShape: TResizeShape;
FAutoCreate: Boolean;
FSizeRatio: TRatioRange;
FOnAddShape: TShapeEvent;
FOnRemoveShape: TShapeEvent;
FOnAfterAddShape: TOnAfterAddShapeEvent;
procedure MyMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure MyMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure MyMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure SetShape(Index: Integer; const Value: TResizeShape);
procedure SetImage(const Value: TPicture);
procedure SetSizeRatio(const Value: TRatioRange);
function GetShape(Index: Integer): TResizeShape;
function GetImage: TPicture;
function GetShapeCount: Integer;
protected
property ShapeList: TList read FShapeList write FShapeList;
procedure Loaded; override;
public
InternalData: Pointer;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function IndexOf(const Shape: TResizeShape): Integer;
function AddShape : TResizeShape;
procedure CancelShape;
procedure NewShape;
procedure DeleteShape( const Index: Integer);
procedure LoadFromFile( const FileName: String);
procedure UpdateIt;
procedure ClearShapes;
property IsHandling: Boolean read FIsHandlingShape write FIsHandlingShape;
property Shapes[Index: Integer]: TResizeShape read GetShape write SetShape; Default;
property ShapeCount: Integer read GetShapeCount;
published
property OnAddShape: TShapeEvent read FOnAddShape write FOnAddShape;
property OnAfterAddShape: TOnAfterAddShapeEvent read FOnAfterAddShape write FOnAfterAddShape;
property OnRemoveShape: TShapeEvent read FOnRemoveShape write FOnRemoveShape;
property AutoCreate: Boolean read FAutoCreate write FAutoCreate;
property SizeRatio: TRatioRange read FSizeRatio write SetSizeRatio;
property Image: TPicture read GetImage write SetImage;
end;
implementation
{ TResizeShapeControl }
constructor TResizeShapeControl.Create(AOwner: TComponent);
begin
inherited;
VertScrollBar.Style := ssHotTrack;
HorzScrollBar.Style := ssHotTrack;
FSizeRatio := 100;
FAutoCreate := True;
ControlStyle:= ControlStyle - [csAcceptsControls];
FShapeList := TList.Create;
FImage := TImage.Create( Self );
FImage.Stretch := True;
with FImage do begin
ControlStyle:= ControlStyle - [csFramed];
Parent := Self;
Top := 0;
Width := 0;
Left := 0;
Height := 0;
OnMouseDown := MyMouseDown;
OnMouseMove := MyMouseMove;
OnMouseUp := MyMouseUp;
end;
GetMem( InternalData, SizeOf(TPoint) );
end;
destructor TResizeShapeControl.Destroy;
begin
FImage.Free;
ClearShapes;
FShapeList.Free;
FreeMem( InternalData );
inherited Destroy;
end;
procedure TResizeShapeControl.DeleteShape(const Index: Integer);
begin
if Assigned( FShapeList.Items[Index] ) then begin
if Assigned( FOnRemoveShape ) then
FOnRemoveShape( FShapeList.Items[Index], Index);
TResizeShape( FShapeList.Items[Index] ).Free;
FShapeList.Delete( Index );
end;
end;
procedure TResizeShapeControl.MyMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
Index: Integer;
begin
Dec( X, HorzScrollBar.Position );
Dec( Y, VertScrollBar.Position );
if Button = mbLeft then begin
if FAutoCreate then
NewShape;
if FIsHandlingShape then begin
FCanDraw := True;
FCurrentShape := TResizeShape.Create( Self );
with FCurrentShape do begin
Parent := Self;
BoundsRatio := FSizeRatio;
Left := X;
Top := Y;
Width := 0;
Height := 0;
end;
Index := FShapeList.Add( FCurrentShape );
if Assigned( FOnAddShape ) then
FOnAddShape( FCurrentShape, Index );
end;
end;
end;
procedure TResizeShapeControl.MyMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
Dec( X, HorzScrollBar.Position );
Dec( Y, VertScrollBar.Position );
if FCanDraw then begin
FCurrentShape.Width := X-FCurrentShape.Left;
FCurrentShape.Height := Y-FCurrentShape.Top;
end;
Application.ProcessMessages;
PPoint(InternalData)^.X := HorzScrollBar.Position;
PPoint(InternalData)^.Y := VertScrollBar.Position;
end;
procedure TResizeShapeControl.MyMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if FIsHandlingShape then begin
FCanDraw := False;
FIsHandlingShape := False;
Cursor := crDefault;
with FCurrentShape do begin
if Width < 0 then begin
Width := Abs(Width);
Left := Left-Width;
end;
if Height < 0 then begin
Height:= Abs(Height);
Top:= Top-Height;
end;
RealBounds:=Bounds(
FCurrentShape.Left+HorzScrollBar.Position,
FCurrentShape.Top+VertScrollBar.Position,
FCurrentShape.Width,
FCurrentShape.Height
);
end;
if Assigned(FOnAfterAddShape) then
FOnAfterAddShape(FCurrentShape);
FCurrentShape := nil;
end;
end;
function TResizeShapeControl.GetShape(Index: Integer): TResizeShape;
begin
Result := nil;
if ( Index > -1 ) and ( Index < FShapeList.Count ) then
Result:= TResizeShape(FShapeList.Items[Index]);
end;
function TResizeShapeControl.IndexOf(const Shape: TResizeShape): Integer;
begin
Result := FShapeList.IndexOf(Shape);
end;
procedure TResizeShapeControl.NewShape;
begin
if Assigned( FImage.Picture ) then begin
FIsHandlingShape := True;
Cursor := crCross;
end;
end;
procedure TResizeShapeControl.CancelShape;
begin
if FIsHandlingShape then
if Assigned( FCurrentShape ) then
FCurrentShape.Free;
FCanDraw := False;
FIsHandlingShape:= False;
Cursor := crDefault;
end;
procedure TResizeShapeControl.SetShape(Index: Integer;
const Value: TResizeShape);
begin
if ( Index > -1 ) and ( Index < FShapeList.Count ) then
FShapeList.Items[Index] := Value;
end;
function TResizeShapeControl.GetImage: TPicture;
begin
Result := FImage.Picture;
end;
procedure TResizeShapeControl.SetImage(const Value: TPicture);
begin
if Assigned( Value ) and ( Value <> FImage.Picture ) then begin
FImage.Picture.Assign( Value );
UpdateIt;
end;
end;
procedure TResizeShapeControl.SetSizeRatio(const Value: TRatioRange);
var
I: Integer;
Rate: Double;
begin
if Assigned( FImage.Picture ) then begin
FSizeRatio := Value;
Rate := FSizeRatio/100;
FImage.Width := Round( FRealSize.X * Rate );
FImage.Height := Round( FRealSize.Y * Rate );
for I := 0 to Pred( FShapeList.Count ) do
TResizeShape( FShapeList[I] ).SetBoundsRate( FSizeRatio, VertScrollBar.Position, HorzScrollBar.Position );
end;
end;
procedure TResizeShapeControl.Loaded;
begin
inherited Loaded;
UpdateIt;
end;
procedure TResizeShapeControl.LoadFromFile(const FileName: String);
begin
if FileExists( FileName ) then begin
FImage.Picture.LoadFromFile( FileName );
FRealSize.X := FImage.Picture.Width;
FRealSize.Y := FImage.Picture.Height;
FImage.Width := Round( FSizeRatio/100*FRealSize.X );
FImage.Height := Round( FSizeRatio/100*FRealSize.Y );
end;
end;
function TResizeShapeControl.GetShapeCount: Integer;
begin
Result := FShapeList.Count;
end;
procedure TResizeShapeControl.UpdateIt;
begin
HorzScrollBar.Position := 0;
VertScrollBar.Position := 0;
FImage.Top := 0;
FImage.Left := 0;
FRealSize.X := FImage.Picture.Width;
FRealSize.Y := FImage.Picture.Height;
FImage.Stretch := True;
FImage.Width := FRealSize.X;
FImage.Height := FRealSize.Y;
end;
function TResizeShapeControl.AddShape: TResizeShape;
var
Index: Integer;
begin
Result := TResizeShape.Create( Self );
with Result do begin
Parent := Self;
BoundsRatio := FSizeRatio;
Left := 0;
Top := 0;
Width := 0;
Height := 0;
end;
Index := FShapeList.Add( Result );
if Assigned( FOnAddShape ) then
FOnAddShape( Result, Index );
end;
procedure TResizeShapeControl.ClearShapes;
begin
while FShapeList.Count > 0 do begin
TResizeShape( FShapeList.Items[0] ).Free;
FShapeList.Delete( 0 );
end;
end;
end.