source: trunk/Packages/bgracontrols/custombgraimagebutton.inc

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 25.6 KB
Line 
1{%MainUnit ../custombgraimagebutton.pas}
2
3uses
4 SysUtils, BGRASliceScaling, FileUtil, IniFiles {$IFDEF WINDOWS}, mmsystem{$ENDIF};
5
6{ TCustomResizeBitmap }
7
8procedure TCustomResizeBitmap.SetFBorderHeight(AValue: integer);
9begin
10 if FBorderHeight=AValue then Exit;
11 FBorderHeight:=AValue;
12
13 if Enable then
14 begin
15 FOwner.Perform(CM_CHANGED, 0, 0);
16 FOwner.Invalidate;
17 end;
18end;
19
20procedure TCustomResizeBitmap.SetFBorderWidth(AValue: integer);
21begin
22 if FBorderWidth=AValue then Exit;
23 FBorderWidth:=AValue;
24
25 if Enable then
26 begin
27 FOwner.Perform(CM_CHANGED, 0, 0);
28 FOwner.Invalidate;
29 end;
30end;
31
32procedure TCustomResizeBitmap.SetFDrawMode(AValue: TDrawMode);
33begin
34 if FDrawMode=AValue then Exit;
35 FDrawMode:=AValue;
36
37 if Enable then
38 begin
39 FOwner.Perform(CM_CHANGED, 0, 0);
40 FOwner.Invalidate;
41 end;
42end;
43
44procedure TCustomResizeBitmap.SetFEnable(AValue: boolean);
45begin
46 Exit;
47 {
48 if FEnable=AValue then Exit;
49 FEnable:=AValue;
50
51 FOwner.Perform(CM_CHANGED, 0, 0);
52 FOwner.Invalidate;
53 }
54end;
55
56procedure TCustomResizeBitmap.SetFFillBottom(AValue: boolean);
57begin
58 if FFillBottom=AValue then Exit;
59 FFillBottom:=AValue;
60
61 if Enable then
62 begin
63 FOwner.Perform(CM_CHANGED, 0, 0);
64 FOwner.Invalidate;
65 end;
66end;
67
68procedure TCustomResizeBitmap.SetFFillCenter(AValue: boolean);
69begin
70 if FFillCenter=AValue then Exit;
71 FFillCenter:=AValue;
72
73 if Enable then
74 begin
75 FOwner.Perform(CM_CHANGED, 0, 0);
76 FOwner.Invalidate;
77 end;
78end;
79
80procedure TCustomResizeBitmap.SetFFillLeft(AValue: boolean);
81begin
82 if FFillLeft=AValue then Exit;
83 FFillLeft:=AValue;
84
85 if Enable then
86 begin
87 FOwner.Perform(CM_CHANGED, 0, 0);
88 FOwner.Invalidate;
89 end;
90end;
91
92procedure TCustomResizeBitmap.SetFFillRight(AValue: boolean);
93begin
94 if FFillRight=AValue then Exit;
95 FFillRight:=AValue;
96
97 if Enable then
98 begin
99 FOwner.Perform(CM_CHANGED, 0, 0);
100 FOwner.Invalidate;
101 end;
102end;
103
104procedure TCustomResizeBitmap.SetFFillTop(AValue: boolean);
105begin
106 if FFillTop=AValue then Exit;
107 FFillTop:=AValue;
108
109 if Enable then
110 begin
111 FOwner.Perform(CM_CHANGED, 0, 0);
112 FOwner.Invalidate;
113 end;
114end;
115
116procedure TCustomResizeBitmap.SetFResampleFilter(AValue: TResampleFilter);
117begin
118 if FResampleFilter=AValue then Exit;
119 FResampleFilter:=AValue;
120
121 if Enable then
122 begin
123 FOwner.Perform(CM_CHANGED, 0, 0);
124 FOwner.Invalidate;
125 end;
126end;
127
128procedure TCustomResizeBitmap.SetFResampleMode(AValue: TResampleMode);
129begin
130 if FResampleMode=AValue then Exit;
131 FResampleMode:=AValue;
132
133 if Enable then
134 begin
135 FOwner.Perform(CM_CHANGED, 0, 0);
136 FOwner.Invalidate;
137 end;
138end;
139
140constructor TCustomResizeBitmap.Create(AOwner: TControl);
141begin
142 FOwner := AOwner;
143 FBorderHeight := 0;
144 FBorderWidth := 0;
145 FDrawMode := dmDrawWithTransparency;
146 FResampleFilter := rfBestQuality;
147 FResampleMode := rmFineResample;
148 FEnable := True;
149 FFillLeft:= False;
150 FFillTop:= False;
151 FFillRight:= False;
152 FFillBottom:= False;
153 FFillCenter:= False;
154 inherited Create;
155end;
156
157destructor TCustomResizeBitmap.Destroy;
158begin
159 inherited Destroy;
160end;
161
162{ TCustomBGRAImageButton }
163
164procedure TCustomBGRAImageButton.SetFBitmapFile(AValue: string);
165begin
166 if FBitmapFile=AValue then Exit;
167 FBitmapFile:=AValue;
168end;
169
170procedure TCustomBGRAImageButton.SetFAnimation(AValue: boolean);
171begin
172 if FAnimation=AValue then Exit;
173 FAnimation:=AValue;
174
175 FTimerStep := 0;
176end;
177
178procedure TCustomBGRAImageButton.SetFAutoSizeExtraX(AValue: integer);
179begin
180 if FAutoSizeExtraX=AValue then Exit;
181 FAutoSizeExtraX:=AValue;
182
183 InvalidatePreferredSize;
184 AdjustSize;
185end;
186
187procedure TCustomBGRAImageButton.SetFAutoSizeExtraY(AValue: integer);
188begin
189 if FAutoSizeExtraY=AValue then Exit;
190 FAutoSizeExtraY:=AValue;
191
192 InvalidatePreferredSize;
193 AdjustSize;
194end;
195
196procedure TCustomBGRAImageButton.SetFBitmapOptions(AValue: TCustomResizeBitmap
197 );
198begin
199 if FBitmapOptions=AValue then Exit;
200 FBitmapOptions:=AValue;
201 UpdateBmp(nil);
202 Invalidate;
203end;
204
205procedure TCustomBGRAImageButton.SetFTextVisible(AValue: boolean);
206begin
207 if FTextVisible=AValue then Exit;
208 FTextVisible:=AValue;
209
210 InvalidatePreferredSize;
211 AdjustSize;
212
213 Invalidate;
214end;
215
216procedure TCustomBGRAImageButton.SetFBmp(const AValue: TBitmap);
217begin
218 if FBmp = AValue then
219 exit;
220 FBmp.Assign(AValue);
221
222 InvalidatePreferredSize;
223 AdjustSize;
224 Changed;
225
226 Invalidate;
227end;
228
229procedure TCustomBGRAImageButton.SetFChecked(AValue: boolean);
230begin
231 if FChecked = AValue then
232 Exit;
233 FChecked := AValue;
234 if FChecked = False then
235 begin
236 FBmpState := bsUp;
237 Invalidate;
238 end
239 else
240 begin
241 if FBmpState <> FCheckedState then
242 begin
243 FBmpState := FCheckedState;
244 Invalidate;
245 end;
246 end;
247end;
248
249{------------------------------------------------------------------------------}
250
251procedure TCustomBGRAImageButton.MouseDown(Button: TMouseButton;
252 Shift: TShiftState; X, Y: integer);
253begin
254 inherited MouseDown(Button, Shift, X, Y);
255
256 if Button = mbLeft then
257 begin
258 if Enabled and Sound then
259 DoPlaySound(SoundClick);
260
261 if Animation then
262 ATimerStep := 0;
263
264 DoButtonDown;
265 end;
266end;
267
268procedure TCustomBGRAImageButton.MouseEnter;
269begin
270 inherited MouseEnter;
271
272 if Enabled and Animation then
273 begin
274 ATimer.Enabled := True;
275 ATimerStep := 0;
276 end;
277
278 if Enabled and Sound then
279 DoPlaySound(SoundEnter);
280 DoButtonEnter;
281end;
282
283procedure TCustomBGRAImageButton.MouseLeave;
284begin
285 inherited MouseLeave;
286
287 if Animation then
288 begin
289 ATimer.Enabled := False;
290 ATimerStep := 0;
291 end;
292
293 if Animation and Toggle and Checked then
294 begin
295 ATimerStep := 0;
296 Invalidate;
297 end;
298
299 DoButtonLeave;
300end;
301
302procedure TCustomBGRAImageButton.MouseUp(Button: TMouseButton;
303 Shift: TShiftState; X, Y: integer);
304begin
305 inherited MouseUp(Button, Shift, X, Y);
306
307 DoButtonUp;
308end;
309
310procedure TCustomBGRAImageButton.Click;
311var
312 Form: TCustomForm;
313begin
314 if ModalResult <> mrNone then
315 begin
316 Form := GetParentForm(Self);
317 if Form <> nil then
318 Form.ModalResult := ModalResult;
319 end;
320
321 if Toggle then
322 Checked := not Checked;
323
324 inherited Click;
325end;
326
327procedure TCustomBGRAImageButton.CalculatePreferredSize(var PreferredWidth,
328 PreferredHeight: integer; WithThemeSpace: boolean);
329var
330 s: TSize;
331 ax, ay: integer;
332begin
333 if TextVisible then
334 begin
335 s := ABGRAText.TextSize(Caption);
336
337 if Shadow.Visible then
338 begin
339 if Shadow.OffsetX < 0 then
340 ax := (Shadow.OffsetX) - (Shadow.OffsetX * 2)
341 else
342 ax := Shadow.OffsetX;
343
344 if Shadow.OffsetY < 0 then
345 ay := (Shadow.OffsetY) - (Shadow.OffsetY * 2)
346 else
347 ay := Shadow.OffsetY;
348
349 Inc(s.cx, AutoSizeExtraHorizontal + 2 * ax + 2 * Shadow.Radius);
350 Inc(s.cy, AutoSizeExtraVertical + 2 * ay + 2 * Shadow.Radius);
351 end
352 else
353 begin
354 Inc(s.cx, AutoSizeExtraHorizontal);
355 Inc(s.cy, AutoSizeExtraVertical);
356 end
357 end
358 else
359 begin
360 s.cx := AutoSizeExtraHorizontal;
361 s.cy := AutoSizeExtraVertical;
362 end;
363
364 if (Bitmap <> nil) or (Bitmap.Width > 1) and (Bitmap.Height > 1) then
365 begin
366 if s.cx > Bitmap.Width then
367 PreferredWidth := s.cx
368 else
369 PreferredWidth := Bitmap.Width;
370
371 if s.cy > (Bitmap.Height div 4) then
372 PreferredHeight := s.cy
373 else
374 PreferredHeight := (Bitmap.Height div 4);
375 end;
376end;
377
378procedure TCustomBGRAImageButton.CMChanged(var Message: TLMessage);
379begin
380 UpdateBmp(nil);
381end;
382
383procedure TCustomBGRAImageButton.DoButtonDown;
384var
385 NewState: TButtonState;
386begin
387 NewState := bsDown;
388
389 if Checked then
390 begin
391 ABmpState := ACheckedState;
392 NewState := ABmpState;
393 end;
394
395 if NewState <> ABmpState then
396 begin
397 ABmpState := NewState;
398 Invalidate;
399 end;
400end;
401
402procedure TCustomBGRAImageButton.DoButtonEnter;
403var
404 NewState: TButtonState;
405begin
406 if Enabled then
407 NewState := bsHot
408 else
409 begin
410 ABmpState := bsUp;
411 NewState := ABmpState;
412 end;
413
414 if Checked then
415 begin
416 ABmpState := ACheckedState;
417 NewState := ABmpState;
418 end;
419
420 if NewState <> ABmpState then
421 begin
422 ABmpState := NewState;
423 Invalidate;
424 end;
425end;
426
427procedure TCustomBGRAImageButton.DoButtonLeave;
428var
429 NewState: TButtonState;
430begin
431 if Enabled then
432 NewState := bsUp
433 else
434 begin
435 ABmpState := bsUp;
436 NewState := ABmpState;
437 end;
438
439 if Checked then
440 begin
441 ABmpState := ACheckedState;
442 NewState := ABmpState;
443 end;
444
445 if NewState <> ABmpState then
446 begin
447 ABmpState := NewState;
448 Invalidate;
449 end;
450end;
451
452procedure TCustomBGRAImageButton.DoButtonUp;
453var
454 NewState: TButtonState;
455 p: TPoint;
456begin
457 p := ScreenToClient(Mouse.CursorPos);
458
459 if (p.x >= 0) and (p.x <= Width) and (p.y >= 0) and (p.y <= Height) then
460 NewState := bsHot
461 else
462 begin
463 NewState := bsUp;
464 if Animation then
465 ATimer.Enabled := False;
466 end;
467
468 if Checked then
469 begin
470 ABmpState := ACheckedState;
471 NewState := ABmpState;
472 end;
473
474 if NewState <> ABmpState then
475 begin
476 ABmpState := NewState;
477 Invalidate;
478 end;
479end;
480
481{------------------------------------------------------------------------------}
482
483procedure TCustomBGRAImageButton.RealSetText(const Value: TCaption);
484begin
485 inherited RealSetText(Value);
486 if TextVisible then
487 begin
488 UpdateTxt;
489 Invalidate;
490 end;
491end;
492
493procedure TCustomBGRAImageButton.CMFontChanged(var Message: TLMessage);
494begin
495 if TextVisible then
496 UpdateTxt;
497end;
498
499{------------------------------------------------------------------------------}
500
501procedure TCustomBGRAImageButton.DoPlaySound(AFileName: string);
502begin
503 if FileExistsUTF8(AFileName) then
504 begin
505 {$IFDEF WINDOWS}
506 if Assigned(FOnPlaySound) then
507 FOnPlaySound(Self, AFileName, ABmpState);
508 PlaySound(PChar(AFileName), 0, SND_ASYNC);
509 {$ELSE}
510 exit;
511 {$ENDIF}
512 end;
513end;
514
515{------------------------------------------------------------------------------}
516
517procedure TCustomBGRAImageButton.Paint;
518
519 procedure DrawFrame;
520 begin
521 with inherited Canvas do
522 begin
523 Pen.Color := clBlack;
524 Pen.Style := psDash;
525 MoveTo(0, 0);
526 LineTo(Self.Width - 1, 0);
527 LineTo(Self.Width - 1, Self.Height - 1);
528 LineTo(0, Self.Height - 1);
529 LineTo(0, 0);
530 end;
531 end;
532
533var
534 NewState: TButtonState;
535begin
536 if csDesigning in ComponentState then
537 DrawFrame;
538
539 if (Bitmap = nil) or (Bitmap.Width < 1) and (Bitmap.Height < 1) then
540 exit;
541
542 if (ABGRA.Width <> Width) or (ABGRA.Height <> Height) then
543 UpdateBmp(nil);
544
545 if TextVisible then
546 if (ABGRAText.Width <> Width) or (ABGRAText.Height <> Height) then
547 UpdateTxt;
548
549 ABGRA.Fill(BGRAPixelTransparent);
550
551 if Checked then
552 NewState := ACheckedState
553 else
554 NewState := ABmpState;
555
556 if Enabled and not ATimer.Enabled then
557 case NewState of
558 bsUp: ABGRA.PutImage(0, 0, ABGRAUp, BitmapOptions.DrawMode);
559 bsDown: ABGRA.PutImage(0, 0, ABGRADown, BitmapOptions.DrawMode);
560 bsHot: ABGRA.PutImage(0, 0, ABGRAHot, BitmapOptions.DrawMode);
561 bsDisabled: ABGRA.PutImage(0, 0, ABGRADisabled, BitmapOptions.DrawMode);
562 end
563 else if not Enabled and not ATimer.Enabled then
564 ABGRA.PutImage(0, 0, ABGRADisabled, BitmapOptions.DrawMode);
565
566 if Enabled and ATimer.Enabled then
567 case NewState of
568 bsUp: ABGRA.PutImage(0, 0, ABGRAUp, BitmapOptions.DrawMode);
569 bsDown: begin
570 ABGRA.PutImage(0, 0, ABGRADown, BitmapOptions.DrawMode);
571 ABGRA.PutImage(0, 0, ABGRAHot, dmDrawWithTransparency, ATimerStep); // always transparent
572 end;
573 bsHot: begin
574 ABGRA.PutImage(0, 0, ABGRAUp, BitmapOptions.DrawMode);
575 ABGRA.PutImage(0, 0, ABGRAHot, dmDrawWithTransparency, ATimerStep); // always transparent
576 end;
577 bsDisabled: ABGRA.PutImage(0, 0, ABGRADisabled, BitmapOptions.DrawMode);
578 end
579 else if not Enabled and ATimer.Enabled then
580 ABGRA.PutImage(0, 0, ABGRADisabled, BitmapOptions.DrawMode);
581
582 if TextVisible then
583 FBGRA.PutImage(0, 0, ABGRAText, dmDrawWithTransparency); // always transparent
584
585 if Assigned(FOnRedraw) then
586 FOnRedraw(Self, ABGRA, NewState);
587
588 {$IFDEF DEBUG}
589 //Debug: display in button Invalidate Count, Update Bitmap Count, Update Text Count.
590 Inc(FInvalidateCount);
591 ABGRA.Textout(0, 0, IntToStr(FInvalidateCount) + ',' + IntToStr(FUpdateCount) +
592 ',' + IntToStr(FUpdateCountTxt), BGRAWhite);
593 ABGRA.Textout(1, 1, IntToStr(FInvalidateCount) + ',' + IntToStr(FUpdateCount) +
594 ',' + IntToStr(FUpdateCountTxt), BGRABlack);
595 {$ENDIF}
596
597 ABGRA.Draw(Canvas, Rect(0, 0, Width, Height), False); // always transparent
598end;
599
600procedure TCustomBGRAImageButton.Animate(Sender: TObject);
601begin
602 if ATimerStep = 0 then
603 ATimerIncreasing := true;
604
605 if ATimerStep = 255 then
606 ATimerIncreasing := false;
607
608 if ATimerIncreasing then
609 Inc(FTimerStep, +17)
610 else
611 Inc(FTimerStep, -17);
612
613 Invalidate;
614end;
615
616procedure TCustomBGRAImageButton.UpdateBmp(Sender: TObject);
617var
618 themeutil: TBGRAMultiSliceScaling;
619 debug: boolean;
620 i: integer;
621begin
622 if csLoading in ComponentState then
623 exit;
624
625 if (Bitmap = nil) or (Bitmap.Width < 1) and (Bitmap.Height < 1) then
626 exit;
627
628 ABGRA.SetSize(Width, Height);
629
630 themeutil := TBGRAMultiSliceScaling.Create(FBmp, BitmapOptions.BorderHeight, BitmapOptions.BorderWidth, BitmapOptions.BorderHeight, BitmapOptions.BorderWidth, 4, sdVertical);
631
632 for i:=0 to high(themeutil.SliceScalingArray) do
633 begin
634 themeutil.SliceScalingArray[i].ResampleMode:= BitmapOptions.ResampleMode;
635 themeutil.SliceScalingArray[i].DrawMode := BitmapOptions.DrawMode;
636 themeutil.SliceScalingArray[i].ResampleFilter := BitmapOptions.ResampleFilter;
637 themeutil.SliceScalingArray[i].SliceRepeat[srpTop] := BitmapOptions.FillTop;
638 themeutil.SliceScalingArray[i].SliceRepeat[srpLeft] := BitmapOptions.FillLeft;
639 themeutil.SliceScalingArray[i].SliceRepeat[srpMiddleHorizontal] := BitmapOptions.FillCenter;
640 themeutil.SliceScalingArray[i].SliceRepeat[srpMiddleVertical] := BitmapOptions.FillCenter;
641 themeutil.SliceScalingArray[i].SliceRepeat[srpRight] := BitmapOptions.FillRight;
642 themeutil.SliceScalingArray[i].SliceRepeat[srpBottom] := BitmapOptions.FillBottom;
643 end;
644
645 {$IFDEF DEBUG}
646 debug := true;
647 {$ELSE}
648 debug := false;
649 {$ENDIF}
650
651 FBGRAUp.Free;
652 FBGRAUp := TBGRABitmap.Create(Width, Height);
653 themeutil.Draw(0, FBGRAUp, 0, 0, Width, Height, debug);
654
655 FBGRAHot.Free;
656 FBGRAHot := TBGRABitmap.Create(Width, Height);
657 themeutil.Draw(1, FBGRAHot, 0, 0, Width, Height, debug);
658
659 FBGRADown.Free;
660 FBGRADown := TBGRABitmap.Create(Width, Height);
661 themeutil.Draw(2, FBGRADown, 0, 0, Width, Height, debug);
662
663 FBGRADisabled.Free;
664 FBGRADisabled := TBGRABitmap.Create(Width, Height);
665 themeutil.Draw(3, FBGRADisabled, 0, 0, Width, Height, debug);
666
667 FreeAndNil(themeutil);
668
669 InvalidatePreferredSize;
670 AdjustSize;
671
672 if Sender is TBitmap then
673 Invalidate;
674
675 {$IFDEF DEBUG}
676 Inc(FUpdateCount);
677 {$ENDIF}
678end;
679
680procedure TCustomBGRAImageButton.UpdateTxt;
681
682function TextShadow(AWidth,AHeight: Integer; AText: String; AFontHeight: Integer;
683 ATextColor,AShadowColor: TBGRAPixel; AOffSetX,AOffSetY: Integer; ARadius: Integer = 0;
684 AFontStyle: TFontStyles = []; AFontName: String = 'Default'; AShowText: Boolean = True;
685 AShowShadow: Boolean = True; AFontQuality: TBGRAFontQuality = fqFineAntialiasing): TBGRACustomBitmap;
686var
687 bmpOut,bmpSdw: TBGRABitmap; OutTxtSize: TSize; OutX,OutY: Integer;
688begin
689 bmpOut:= TBGRABitmap.Create(AWidth,AHeight);
690 bmpOut.FontAntialias:= True;
691 bmpOut.FontHeight:= AFontHeight;
692 bmpOut.FontStyle:= AFontStyle;
693 bmpOut.FontName:= AFontName;
694 bmpOut.FontQuality:= AFontQuality;
695
696 OutTxtSize:= bmpOut.TextSize(AText);
697 OutX:= Round(AWidth/2) - Round(OutTxtSize.cx/2);
698 OutY:= Round(AHeight/2) - Round(OutTxtSize.cy/2);
699
700 if AShowShadow then
701 begin
702 bmpSdw:= TBGRABitmap.Create(OutTxtSize.cx+2*ARadius,OutTxtSize.cy+2*ARadius);
703 bmpSdw.FontAntialias:= True;
704 bmpSdw.FontHeight:= AFontHeight;
705 bmpSdw.FontStyle:= AFontStyle;
706 bmpSdw.FontName:= AFontName;
707 bmpSdw.FontQuality:= AFontQuality;
708
709 bmpSdw.TextOut(ARadius,ARadius,AText,AShadowColor);
710 BGRAReplace(bmpSdw,bmpSdw.FilterBlurRadial(ARadius,rbFast));
711 bmpOut.PutImage(OutX+AOffSetX-ARadius,OutY+AOffSetY-ARadius,bmpSdw,dmDrawWithTransparency);
712 bmpSdw.Free;
713 end;
714
715 if AShowText = True then bmpOut.TextOut(OutX,OutY,AText,ATextColor);
716
717 Result:= bmpOut;
718end;
719
720begin
721 if csLoading in ComponentState then
722 exit;
723
724 if (Bitmap = nil) or (FBGRA.Width < 1) and (FBGRA.Height < 1) then
725 exit;
726
727 ABGRAText.Fill(BGRAPixelTransparent);
728
729 BGRAReplace(FBGRAText, TextShadow(Width, Height, Caption, Font.Height,
730 ColorToBGRA(ColorToRGB(Font.Color), 255),
731 ColorToBGRA(ColorToRGB(FShadow.Color), Shadow.Alpha), Shadow.OffsetX,
732 Shadow.OffsetY, Shadow.Radius, Font.Style, Font.Name, True, Shadow.Visible) as TBGRABitmap);
733
734 InvalidatePreferredSize;
735 AdjustSize;
736
737 {$IFDEF DEBUG}
738 Inc(FUpdateCountTxt);
739 {$ENDIF}
740end;
741
742{------------------------------------------------------------------------------}
743
744constructor TCustomBGRAImageButton.Create(AOwner: TComponent);
745begin
746 inherited Create(AOwner);
747 with GetControlClassDefaultSize do
748 SetInitialBounds(0, 0, CX, CY);
749
750 AutoSizeExtraHorizontal := 24;
751 AutoSizeExtraVertical := 8;
752 Font.Color := clBlack;
753 Font.Height := 18;
754
755 FBGRA := TBGRABitmap.Create;
756 FBGRAText := TBGRABitmap.Create;
757
758 FBmp := TBitmap.Create;
759 FBmp.OnChange := @UpdateBmp;
760
761 FBGRAUp := nil;
762 FBGRADown := nil;
763 FBGRADisabled := nil;
764 FBGRAHot := nil;
765
766 FTimer := TTimer.Create(Self);
767 FTimer.Enabled := False;
768 FTimer.OnTimer := @Animate;
769 FTimer.Interval := 30;
770 FTimerStep := 0;
771
772 Shadow := TBGRATextEffectShadow.Create(Self);
773 BitmapOptions := TCustomResizeBitmap.Create(Self);
774
775 FCheckedState := bsDown;
776 FTextVisible := True;
777end;
778
779destructor TCustomBGRAImageButton.Destroy;
780begin
781 if Bitmap <> nil then
782 FBmp.Free;
783
784 FBGRA.Free;
785 FBGRAText.Free;
786
787 FBGRAUp.Free;
788 FBGRADown.Free;
789 FBGRADisabled.Free;
790 FBGRAHot.Free;
791
792 FTimer.OnTimer := nil;
793 FTimer.Free;
794
795 Shadow.Free;
796 BitmapOptions.Free;
797
798 inherited Destroy;
799end;
800
801procedure TCustomBGRAImageButton.Assign(Source: TPersistent);
802begin
803 if Source is TCustomBGRAImageButton then
804 begin
805 Loading;
806 FBmp.Assign(TCustomBGRAImageButton(Source).FBmp);
807 FBitmapFile := TCustomBGRAImageButton(Source).FBitmapFile;
808 FAnimation := TCustomBGRAImageButton(Source).FAnimation;
809 FChecked := TCustomBGRAImageButton(Source).FChecked;
810 FSound := TCustomBGRAImageButton(Source).FSound;
811 FSoundEnter := TCustomBGRAImageButton(Source).FSoundEnter;
812 FSoundClick := TCustomBGRAImageButton(Source).FSoundClick;
813 FModalResult := TCustomBGRAImageButton(Source).FModalResult;
814 FToggle := TCustomBGRAImageButton(Source).FToggle;
815 FAutoSizeExtraY := TCustomBGRAImageButton(Source).FAutoSizeExtraY;
816 FAutoSizeExtraX := TCustomBGRAImageButton(Source).FAutoSizeExtraX;
817 FTextVisible := TCustomBGRAImageButton(Source).FTextVisible;
818 { Assign Shadow }
819 FShadow.Alpha := TCustomBGRAImageButton(Source).FShadow.Alpha;
820 FShadow.Color := TCustomBGRAImageButton(Source).FShadow.Color;
821 FShadow.OffsetX := TCustomBGRAImageButton(Source).FShadow.OffsetX;
822 FShadow.OffsetY := TCustomBGRAImageButton(Source).FShadow.OffsetY;
823 FShadow.Radius := TCustomBGRAImageButton(Source).FShadow.Radius;
824 FShadow.Visible := TCustomBGRAImageButton(Source).FShadow.Visible;
825 { Assign BitmapOptions}
826 FBitmapOptions.BorderHeight := TCustomBGRAImageButton(Source).FBitmapOptions.BorderHeight;
827 FBitmapOptions.BorderWidth := TCustomBGRAImageButton(Source).FBitmapOptions.BorderWidth;
828 FBitmapOptions.DrawMode := TCustomBGRAImageButton(Source).FBitmapOptions.DrawMode;
829 FBitmapOptions.Enable := TCustomBGRAImageButton(Source).FBitmapOptions.Enable;
830 FBitmapOptions.FillBottom := TCustomBGRAImageButton(Source).FBitmapOptions.FillBottom;
831 FBitmapOptions.FillCenter := TCustomBGRAImageButton(Source).FBitmapOptions.FillCenter;
832 FBitmapOptions.FillLeft := TCustomBGRAImageButton(Source).FBitmapOptions.FillLeft;
833 FBitmapOptions.FillRight := TCustomBGRAImageButton(Source).FBitmapOptions.FillRight;
834 FBitmapOptions.FillTop := TCustomBGRAImageButton(Source).FBitmapOptions.FillTop;
835 FBitmapOptions.ResampleFilter := TCustomBGRAImageButton(Source).FBitmapOptions.ResampleFilter;
836 FBitmapOptions.ResampleMode := TCustomBGRAImageButton(Source).FBitmapOptions.ResampleMode;
837 { Assign Font }
838 Font.Assign(TCustomBGRAImageButton(Source).Font);
839 Loaded;
840
841 UpdateBmp(nil);
842 if TextVisible then
843 UpdateTxt;
844 Invalidate;
845 end
846 else
847 inherited Assign(Source);
848end;
849
850function TCustomBGRAImageButton.BitmapLoadFromFile(AFileName: String): Boolean;
851{
852* Usage *
853 button.BitmapLoadFromFile(some_path + button.BitmapFile);
854}
855var
856 temp: TBGRABitmap;
857begin
858 if FileExistsUTF8(AFileName) then
859 begin
860 temp := TBGRABitmap.Create(AFileName);
861 Bitmap := temp.Bitmap;
862 temp.Free;
863 Result := True;
864 end
865 else
866 Result := False;
867end;
868
869function TCustomBGRAImageButton.LoadFromFile(AFileName: string): Boolean;
870
871{
872* Usage *
873 button.LoadFromFile(some_path + 'button.ini');
874 button.BitmapLoadFromFile(some_path + button.BitmapFile);
875}
876
877var
878 temp: TMemIniFile;
879begin
880 if FileExistsUTF8(AFileName) then
881 begin
882 temp := TMemIniFile.Create(AFileName);
883 with Self do
884 begin
885 // Image
886 Animation := temp.ReadBool('default', 'Animation', False);
887 BitmapFile := temp.ReadString('default', 'BitmapFile', 'button.png');
888 // BitmapOptions
889 with BitmapOptions do
890 begin
891 BorderHeight := temp.ReadInteger('default', 'BorderHeight', 5);
892 BorderWidth := temp.ReadInteger('default', 'BorderWidth', 5);
893 Enable := temp.ReadBool('default', 'Enable', False);
894 FillBottom := temp.ReadBool('default', 'FillBottom', False);
895 FillCenter := temp.ReadBool('default', 'FillCenter', False);
896 FillRight := temp.ReadBool('default', 'FillRight', False);
897 FillTop := temp.ReadBool('default', 'FillTop', False);
898 end;
899 // Sound
900 Sound := temp.ReadBool('default', 'Sound', False);
901 SoundClick := temp.ReadString('default', 'SoundClick', 'click.wav');
902 SoundEnter := temp.ReadString('default', 'SoundEnter', 'enter.wav');
903 // Text
904 Caption := temp.ReadString('default', 'Caption', Caption);
905 TextVisible := temp.ReadBool('default', 'TextVisible', True);
906 Font.Color := BGRAToColor(StrToBGRA(temp.ReadString('default', 'FontColor', 'black')));
907 Font.Name := temp.ReadString('default', 'FontName', 'default');
908 Font.Height := temp.ReadInteger('default', 'FontHeight', 18);
909 // Size
910 Left := temp.ReadInteger('default', 'Left', Left);
911 Top := temp.ReadInteger('default', 'Top', Top);
912 Width := temp.ReadInteger('default', 'Width', Width);
913 Height := temp.ReadInteger('default', 'Height', Height);
914 // Shadow
915 with Shadow do
916 begin
917 Visible := temp.ReadBool('default', 'ShadowVisible', True);
918 Alpha := temp.ReadInteger('default', 'ShadowAlpha', 255);
919 Color := BGRAToColor(StrToBGRA(temp.ReadString('default', 'ShadowColor', 'black')));
920 OffsetX := temp.ReadInteger('default', 'ShadowOffsetX', 1);
921 OffsetY := temp.ReadInteger('default', 'ShadowOffsetY', 1);
922 Radius := temp.ReadInteger('default', 'ShadowRadius', 1);
923 end;
924 // Misc
925 AutoSizeExtraHorizontal := temp.ReadInteger('default', 'AutoSizeExtraHorizontal', 24);
926 AutoSizeExtraVertical := temp.ReadInteger('default', 'AutoSizeExtraVertical', 8);
927 Checked := temp.ReadBool('default', 'Checked', False);
928 Toggle := temp.ReadBool('default', 'Toggle', Toggle);
929 end;
930 temp.Free;
931 Result := True;
932 end
933 else
934 Result := False;
935end;
936
937function TCustomBGRAImageButton.SaveToFile(AFileName: string): boolean;
938
939function BGRAToStrRGB(AColor: TBGRAPixel): string;
940begin
941 result := 'rgb(' + IntToStr(AColor.red) + ',' + IntToStr(AColor.green) + ',' + IntToStr(AColor.blue) + ')'
942end;
943
944var
945 temp: TMemIniFile;
946begin
947 temp := TMemIniFile.Create(AFileName);
948 // Image
949 temp.WriteBool('default', 'Animation', Animation);
950 temp.WriteString('default', 'BitmapFile', BitmapFile);
951 // BitmapOptions
952 temp.WriteInteger('default', 'BorderHeight', BitmapOptions.BorderHeight);
953 temp.WriteInteger('default', 'BorderWidth', BitmapOptions.BorderWidth);
954 temp.WriteBool('default', 'Enable', BitmapOptions.Enable);
955 temp.WriteBool('default', 'FillBottom', BitmapOptions.FillBottom);
956 temp.WriteBool('default', 'FillCenter', BitmapOptions.FillCenter);
957 temp.WriteBool('default', 'FillRight', BitmapOptions.FillRight);
958 temp.WriteBool('default', 'FillTop', BitmapOptions.FillTop);
959 // Sound
960 temp.WriteBool('default', 'Sound', Sound);
961 temp.WriteString('default', 'SoundClick', SoundClick);
962 temp.WriteString('default', 'SoundEnter', SoundEnter);
963 // Text
964 temp.WriteString('default', 'Caption', Caption);
965 temp.WriteBool('default', 'TextVisible', TextVisible);
966 temp.WriteString('default', 'FontColor', BGRAToStrRGB(ColorToBGRA(Font.Color)));
967 temp.WriteString('default', 'FontName', Font.Name);
968 temp.WriteInteger('default', 'FontHeight', Font.Height);
969 // Size
970 temp.WriteInteger('default', 'Left', Left);
971 temp.WriteInteger('default', 'Top', Top);
972 temp.WriteInteger('default', 'Width', Width);
973 temp.WriteInteger('default', 'Height', Height);
974 // Shadow
975 temp.WriteBool('default', 'ShadowVisible', Shadow.Visible);
976 temp.WriteInteger('default', 'ShadowAlpha', Shadow.Alpha);
977 temp.WriteString('default', 'ShadowColor', BGRAToStrRGB(ColorToBGRA(Shadow.Color)));
978 temp.WriteInteger('default', 'ShadowOffsetX', Shadow.OffsetX);
979 temp.WriteInteger('default', 'ShadowOffsetY', Shadow.OffsetY);
980 temp.WriteInteger('default', 'ShadowRadius', Shadow.Radius);
981 // Misc
982 temp.WriteInteger('default', 'AutoSizeExtraHorizontal', AutoSizeExtraHorizontal);
983 temp.WriteInteger('default', 'AutoSizeExtraVertical', AutoSizeExtraVertical);
984 temp.WriteBool('default', 'Checked', Checked);
985 temp.WriteBool('default', 'Toggle', Toggle);
986 // Save and Free
987 temp.UpdateFile;
988 temp.Free;
989
990 if FileExistsUTF8(AFileName) then
991 Result := True
992 else
993 Result := False;
994end;
Note: See TracBrowser for help on using the repository browser.