-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathbcroundedimage.pas
391 lines (310 loc) · 10.8 KB
/
bcroundedimage.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// SPDX-License-Identifier: LGPL-3.0-linking-exception
{
BCRoundedImage
by Lainz
Last modified: 2020-09-06 19:16 GMT-3
Changelog:
- 2020-09-06: Initial version supporting circle, rounded rectangle and square.
Changing the quality of the resample, setting the rounding.
OnPaintEvent to customize the final drawing.
- 2025-01: MaxM, Changed class ancestor to TCustomBGRAGraphicControl;
Added TBGRABitmap Bitmap draw;
Added Stretch, Proportional, Alignments.
}
unit BCRoundedImage;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
BGRABitmap, BGRABitmapTypes, BGRAGraphicControl, BCTypes;
type
TBCRoundedImage = class;
// Event to draw before the image is sent to canvas
//TBCRoundedImagePaintEvent = procedure (const Sender: TBCRoundedImage; const Bitmap: TBGRABitmap) of object;
TBCRoundedImagePaintEvent = TBGRARedrawEvent;
// Supported styles are circle, rounded rectangle and square
TBCRoundedImageStyle = (isCircle, isRoundedRectangle, isSquare);
// Control that draws an image within a rounded border
{ TBCRoundedImage }
TBCRoundedImage = class(TCustomBGRAGraphicControl)
private
FBorderStyle: TRoundRectangleOptions;
FOnPaintEvent: TBCRoundedImagePaintEvent;
FPicture: TPicture;
FImageBitmap: TBGRABitmap;
FQuality: TResampleFilter;
FStyle: TBCRoundedImageStyle;
FRounding: single;
FProportional: Boolean;
FOnChange: TNotifyEvent;
FAlignment: TAlignment;
FStretch: Boolean;
FVerticalAlignment: TTextLayout;
function GetOnPaintEvent: TBCRoundedImagePaintEvent;
procedure SetAlignment(AValue: TAlignment);
procedure SetBitmap(AValue: TBGRABitmap);
procedure SetBorderStyle(AValue: TRoundRectangleOptions);
procedure SetOnPaintEvent(AValue: TBCRoundedImagePaintEvent);
procedure SetPicture(AValue: TPicture);
procedure SetProportional(AValue: Boolean);
procedure SetQuality(AValue: TResampleFilter);
procedure SetStretch(AValue: Boolean);
procedure SetStyle(AValue: TBCRoundedImageStyle);
procedure SetRounding(AValue: single);
procedure SetVerticalAlignment(AValue: TTextLayout);
protected
procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer; WithThemeSpace: boolean); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Paint; override;
procedure Draw(ABitmap: TBGRABitmap);
property Bitmap: TBGRABitmap read FImageBitmap write setBitmap;
published
// The image that's used as background
property Picture: TPicture read FPicture write SetPicture;
// The style can be circle, rounded rectangle or square
property Style: TBCRoundedImageStyle read FStyle write SetStyle;
// The style of the rounded rectangle
property BorderStyle: TRoundRectangleOptions read FBorderStyle write SetBorderStyle;
// Rounding is used when you choose the rounded rectangle style
property Rounding: single read FRounding write SetRounding;
// The quality when resizing the image
property Quality: TResampleFilter read FQuality write SetQuality;
// Stretch Proportianally
property Proportional: Boolean read FProportional write SetProportional;
property Stretch: Boolean read FStretch write SetStretch default True;
// Alignments of the Image inside the Control
property Alignment: TAlignment read FAlignment write SetAlignment default taCenter;
property VerticalAlignment: TTextLayout read FVerticalAlignment write SetVerticalAlignment default tlCenter;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
// You can paint before the bitmap is drawn on canvas
property OnPaintEvent: TBCRoundedImagePaintEvent read GetOnPaintEvent write SetOnPaintEvent; deprecated 'Use OnRedraw instead';
property Anchors;
property Align;
property OnMouseEnter;
property OnMouseLeave;
property OnClick;
end;
{ #todo -oMaxM : we could move it to a common unit and use it in BGRAImageList too }
function CalcProportionalRect(AWidth, AHeight, AImageWidth, AImageHeight: Integer;
AHorizAlign: TAlignment; AVertAlign: TTextLayout): TRect;
procedure Register;
implementation
function CalcProportionalRect(AWidth, AHeight, AImageWidth, AImageHeight: Integer; AHorizAlign: TAlignment;
AVertAlign: TTextLayout): TRect;
var
rW, rH:Single;
newWidth,
newHeight:Integer;
begin
FillChar(Result, sizeof(Result), 0);
if (AImageWidth > 0) and (AImageHeight > 0) then
begin
rW := AImageWidth / AWidth;
rH := AImageHeight / AHeight;
if (rW > rH)
then begin
newHeight:= round(AImageHeight / rW);
newWidth := AWidth;
end
else begin
newWidth := round(AImageWidth / rH);
newHeight := AHeight;
end;
case AHorizAlign of
taCenter: Result.Left:= (AWidth-newWidth) div 2;
taRightJustify: Result.Left:= AWidth-newWidth;
end;
case AVertAlign of
tlCenter: Result.Top:= (AHeight-newHeight) div 2;
tlBottom: Result.Top:= AHeight-newHeight;
end;
Result.Right:= Result.Left+newWidth;
Result.Bottom:= Result.Top+newHeight;
end;
end;
procedure Register;
begin
RegisterComponents('BGRA Controls', [TBCRoundedImage]);
end;
procedure TBCRoundedImage.SetProportional(AValue: Boolean);
begin
if FProportional=AValue then Exit;
FProportional:=AValue;
if Assigned(FOnChange) then FOnChange(Self);
Invalidate;
end;
procedure TBCRoundedImage.SetBorderStyle(AValue: TRoundRectangleOptions);
begin
if FBorderStyle=AValue then Exit;
FBorderStyle:=AValue;
if Assigned(FOnChange) then FOnChange(Self);
Invalidate;
end;
function TBCRoundedImage.GetOnPaintEvent: TBCRoundedImagePaintEvent;
begin
Result:= OnRedraw;
end;
procedure TBCRoundedImage.SetAlignment(AValue: TAlignment);
begin
if FAlignment=AValue then Exit;
FAlignment:=AValue;
if Assigned(FOnChange) then FOnChange(Self);
Invalidate;
end;
procedure TBCRoundedImage.SetBitmap(AValue: TBGRABitmap);
begin
if (AValue <> FImageBitmap) then
begin
// Clear actual image
FImageBitmap.Free;
FImageBitmap :=TBGRABitmap.Create(Width, Height, BGRAPixelTransparent);
if (AValue<>nil) then FImageBitmap.Assign(AValue, True); // Associate the new bitmap
if Assigned(FOnChange) then FOnChange(Self);
Invalidate;
end;
end;
procedure TBCRoundedImage.SetPicture(AValue: TPicture);
begin
if (AValue <> FPicture) then
begin
// Clear actual Picture
FPicture.Free;
FPicture :=TPicture.Create;
if (AValue<>nil) then FPicture.Assign(AValue); // Associate the new Picture
if Assigned(FOnChange) then FOnChange(Self);
Invalidate;
end;
end;
procedure TBCRoundedImage.SetOnPaintEvent(AValue: TBCRoundedImagePaintEvent);
begin
OnRedraw:= AValue;
end;
procedure TBCRoundedImage.SetQuality(AValue: TResampleFilter);
begin
if FQuality = AValue then
Exit;
FQuality := AValue;
if Assigned(FOnChange) then FOnChange(Self);
Invalidate;
end;
procedure TBCRoundedImage.SetStretch(AValue: Boolean);
begin
if FStretch=AValue then Exit;
FStretch:=AValue;
if Assigned(FOnChange) then FOnChange(Self);
Invalidate;
end;
procedure TBCRoundedImage.SetStyle(AValue: TBCRoundedImageStyle);
begin
if FStyle = AValue then
Exit;
FStyle := AValue;
if Assigned(FOnChange) then FOnChange(Self);
Invalidate;
end;
procedure TBCRoundedImage.SetRounding(AValue: single);
begin
if FRounding = AValue then
Exit;
FRounding := AValue;
if Assigned(FOnChange) then FOnChange(Self);
Invalidate;
end;
procedure TBCRoundedImage.SetVerticalAlignment(AValue: TTextLayout);
begin
if FVerticalAlignment=AValue then Exit;
FVerticalAlignment:=AValue;
if Assigned(FOnChange) then FOnChange(Self);
Invalidate;
end;
{$hints off}
procedure TBCRoundedImage.CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer; WithThemeSpace: boolean);
begin
PreferredWidth := 100;
PreferredHeight := 100;
end;
constructor TBCRoundedImage.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAlignment:= taCenter;
FVerticalAlignment:= tlCenter;
FStretch:= True;
// Create the Image Bitmap
FPicture := TPicture.Create;
FImageBitmap := TBGRABitmap.Create;
FRounding := 10;
FQuality := rfBestQuality;
FBGRA.FillTransparent;
end;
destructor TBCRoundedImage.Destroy;
begin
FPicture.Free;
FImageBitmap.Free;
inherited Destroy;
end;
procedure TBCRoundedImage.Paint;
begin
if (ClientWidth <> FBGRA.Width) or (ClientHeight <> FBGRA.Height)
then FBGRA.SetSize(ClientWidth, ClientHeight);
Draw(FBGRA);
if Assigned(OnRedraw) then OnRedraw(Self, FBGRA);
FBGRA.Draw(Canvas, 0, 0, False);
end;
procedure TBCRoundedImage.Draw(ABitmap: TBGRABitmap);
var
image,
imageD: TBGRABitmap;
imgRect: TRect;
begin
ABitmap.FillTransparent;
if ((FPicture.Width = 0) or (FPicture.Height = 0)) and
FImageBitmap.Empty then exit;
try
if FImageBitmap.Empty
then image := TBGRABitmap.Create(FPicture.Bitmap)
else image := TBGRABitmap.Create(FImageBitmap.Bitmap);
imageD:= TBGRABitmap.Create(Width, Height, BGRAPixelTransparent);
if FProportional
then imgRect:= CalcProportionalRect(Width, Height, image.Width, image.Height,
FAlignment, FVerticalAlignment)
else begin
if FStretch
then imgRect:= Rect(0,0,Width,Height)
else begin
case FAlignment of
taLeftJustify: imgRect.Left:= 0;
taCenter: imgRect.Left:= (Width-image.Width) div 2;
taRightJustify: imgRect.Left:= Width-image.Width;
end;
case FVerticalAlignment of
tlTop: imgRect.Top:= 0;
tlCenter: imgRect.Top:= (Height-image.Height) div 2;
tlBottom: imgRect.Top:= Height-image.Height;
end;
imgRect.Right:= imgRect.Left+image.Width;
imgRect.Bottom:= imgRect.Top+image.Height;
end;
end;
if FStretch or FProportional then
begin
// Stretch with Quality
image.ResampleFilter := FQuality;
BGRAReplace(image, image.Resample(imgRect.Width, imgRect.Height));
end;
imageD.PutImage(imgRect.Left, imgRect.Top, image, dmDrawWithTransparency);
// Style
case FStyle of
isCircle: ABitmap.FillEllipseAntialias(Width div 2, Height div 2,
(Width div 2)-FRounding, (Height div 2)-FRounding, imageD);
isRoundedRectangle: ABitmap.FillRoundRectAntialias(0, 0, Width,
Height, FRounding, FRounding, imageD, FBorderStyle);
else ABitmap.PutImage(0, 0, imageD, dmDrawWithTransparency);
end;
finally
imageD.Free;
image.Free;
end;
end;
end.