1 | unit BGRALabelFX;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Controls, Graphics,
|
---|
9 | BGRABitmap, BGRABitmapTypes, BGRATextFXTypes, BGRATextFX, types;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TBGRALabelFX }
|
---|
14 |
|
---|
15 | TBGRALabelFX = class(TGraphicControl)
|
---|
16 | private
|
---|
17 | FBGRA: TBGRABitmap;
|
---|
18 | FTextEffect: TBGRATextEffect;
|
---|
19 | FOutline: TBGRATextEffectOutline;
|
---|
20 | FShadow: TBGRATextEffectShadow;
|
---|
21 | FPreviousCaption: string;
|
---|
22 | FPreviousFont: TFont;
|
---|
23 | protected
|
---|
24 | procedure Paint; override;
|
---|
25 | procedure RealSetText(const Value: TCaption); override;
|
---|
26 | procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
|
---|
27 | WithThemeSpace: boolean); override;
|
---|
28 | procedure UpdateTextEffect;
|
---|
29 | public
|
---|
30 | function Draw: TBGRABitmap;
|
---|
31 | constructor Create(AOwner: TComponent); override;
|
---|
32 | destructor Destroy; override;
|
---|
33 | published
|
---|
34 | property Action;
|
---|
35 | property Align;
|
---|
36 | property AutoSize;
|
---|
37 | property Caption;
|
---|
38 | property Font;
|
---|
39 | property PopupMenu;
|
---|
40 | property Outline: TBGRATextEffectOutline Read FOutline Write FOutline;
|
---|
41 | property Shadow: TBGRATextEffectShadow Read FShadow Write FShadow;
|
---|
42 | property OnClick;
|
---|
43 | property OnDblClick;
|
---|
44 | property OnMouseDown;
|
---|
45 | property OnMouseMove;
|
---|
46 | property OnMouseUp;
|
---|
47 | property OnMouseEnter;
|
---|
48 | property OnMouseLeave;
|
---|
49 | end;
|
---|
50 |
|
---|
51 | procedure Register;
|
---|
52 |
|
---|
53 | implementation
|
---|
54 |
|
---|
55 | uses
|
---|
56 | LResources;
|
---|
57 |
|
---|
58 | procedure Register;
|
---|
59 | begin
|
---|
60 | {$I bgralabelfx_icon.lrs}
|
---|
61 | RegisterComponents('BGRA Controls', [TBGRALabelFX]);
|
---|
62 | end;
|
---|
63 |
|
---|
64 | { TBGRALabelFX }
|
---|
65 |
|
---|
66 | procedure TBGRALabelFX.RealSetText(const Value: TCaption);
|
---|
67 | begin
|
---|
68 | inherited RealSetText(Value);
|
---|
69 |
|
---|
70 | if Value <> FPreviousCaption then Invalidate;
|
---|
71 | InvalidatePreferredSize;
|
---|
72 | AdjustSize;
|
---|
73 | end;
|
---|
74 |
|
---|
75 | procedure TBGRALabelFX.CalculatePreferredSize(var PreferredWidth,
|
---|
76 | PreferredHeight: integer; WithThemeSpace: boolean);
|
---|
77 | var
|
---|
78 | s: TSize;
|
---|
79 | ax, ay: integer;
|
---|
80 | begin
|
---|
81 | UpdateTextEffect;
|
---|
82 | s.cx := FTextEffect.Width;
|
---|
83 | s.cy := FTextEffect.Height;
|
---|
84 |
|
---|
85 | if FShadow.Visible then
|
---|
86 | begin
|
---|
87 | if FShadow.OffsetX < 0 then
|
---|
88 | ax := (FShadow.OffsetX) - (FShadow.OffsetX * 2)
|
---|
89 | else
|
---|
90 | ax := FShadow.OffsetX;
|
---|
91 |
|
---|
92 | if FShadow.OffsetY < 0 then
|
---|
93 | ay := (FShadow.OffsetY) - (FShadow.OffsetY * 2)
|
---|
94 | else
|
---|
95 | ay := FShadow.OffsetY;
|
---|
96 |
|
---|
97 | Inc(s.cx, 2 * ax + 2 * FShadow.Radius);
|
---|
98 | Inc(s.cy, 2 * ay + 2 * FShadow.Radius);
|
---|
99 | end;
|
---|
100 |
|
---|
101 | PreferredWidth := s.cx;
|
---|
102 | PreferredHeight := s.cy;
|
---|
103 | end;
|
---|
104 |
|
---|
105 | procedure TBGRALabelFX.UpdateTextEffect;
|
---|
106 | var NewCaption: string;
|
---|
107 | begin
|
---|
108 | if FTextEffect = nil then exit;
|
---|
109 | NewCaption := Caption;
|
---|
110 | if (NewCaption <> FPreviousCaption) or
|
---|
111 | (Font.Name <> FPreviousFont.Name) or
|
---|
112 | (Font.Style <> FPreviousFont.Style) or
|
---|
113 | (Font.Quality <> FPreviousFont.Quality) or
|
---|
114 | (Font.Orientation <> FPreviousFont.Orientation) or
|
---|
115 | (Font.Pitch <> FPreviousFont.Pitch) or
|
---|
116 | (Font.Height <> FPreviousFont.Height) or
|
---|
117 | (Font.CharSet <> FPreviousFont.CharSet) then
|
---|
118 | begin
|
---|
119 | FreeAndNil(FTextEffect);
|
---|
120 | FTextEffect := TBGRATextEffect.Create(NewCaption, Font, True);
|
---|
121 | FPreviousFont.Assign(Font);
|
---|
122 | FPreviousCaption := NewCaption;
|
---|
123 | end;
|
---|
124 | end;
|
---|
125 |
|
---|
126 | procedure TBGRALabelFX.Paint;
|
---|
127 | var
|
---|
128 | cx, cy, px, py: integer;
|
---|
129 | ax, ay: integer;
|
---|
130 | shx, shy: integer;
|
---|
131 | begin
|
---|
132 | ax := 0;
|
---|
133 | ay := 0;
|
---|
134 |
|
---|
135 | if FShadow.Visible then
|
---|
136 | begin
|
---|
137 | if FShadow.OffsetX < 0 then
|
---|
138 | ax := (FShadow.OffsetX) - (FShadow.OffsetX * 2)
|
---|
139 | else
|
---|
140 | ax := FShadow.OffsetX;
|
---|
141 |
|
---|
142 | if FShadow.OffsetY < 0 then
|
---|
143 | ay := (FShadow.OffsetY) - (FShadow.OffsetY * 2)
|
---|
144 | else
|
---|
145 | ay := FShadow.OffsetY;
|
---|
146 |
|
---|
147 | ax := 2 * ax + 2 * FShadow.Radius;
|
---|
148 | ay := 2 * ay + 2 * FShadow.Radius;
|
---|
149 | end;
|
---|
150 |
|
---|
151 | UpdateTextEffect;
|
---|
152 |
|
---|
153 | InvalidatePreferredSize;
|
---|
154 | AdjustSize;
|
---|
155 |
|
---|
156 | FBGRA.Free;
|
---|
157 | FBGRA := TBGRABitmap.Create(FTextEffect.Width + ax,
|
---|
158 | FTextEffect.Height + ay);
|
---|
159 |
|
---|
160 | { taCenter }
|
---|
161 | cx := trunc((FBGRA.Width - FTextEffect.Width) / 2);
|
---|
162 | cy := trunc((FBGRA.Height - FTextEffect.Height) / 2);
|
---|
163 | px := trunc((Width - FBGRA.Width) / 2);
|
---|
164 | py := trunc((Height - FBGRA.Height) / 2);
|
---|
165 |
|
---|
166 | if FShadow.OffsetX < 0 then
|
---|
167 | shx := - FShadow.OffsetX + FShadow.Radius
|
---|
168 | else
|
---|
169 | shx := 2 * FShadow.OffsetX + integer(FSHadow.Radius);
|
---|
170 |
|
---|
171 | if FShadow.OffsetY < 0 then
|
---|
172 | shy := - FShadow.OffsetY + FShadow.Radius
|
---|
173 | else
|
---|
174 | shy := 2 * FShadow.OffsetY + integer(FShadow.Radius);
|
---|
175 |
|
---|
176 | if FShadow.Visible then
|
---|
177 | FTextEffect.DrawShadow(FBGRA, shx,
|
---|
178 | shy,
|
---|
179 | FShadow.Radius, ColorToBGRA(FShadow.Color, FShadow.Alpha));
|
---|
180 |
|
---|
181 | if FOutline.Visible then
|
---|
182 | FTextEffect.DrawOutline(FBGRA, cx + FOutline.OffsetX, cy + FOutline.OffsetY,
|
---|
183 | ColorToBGRA(FOutline.Color, FOutline.Alpha));
|
---|
184 |
|
---|
185 | FTextEffect.Draw(FBGRA, cx, cy, ColorToBGRA(Font.Color, 255));
|
---|
186 |
|
---|
187 | FBGRA.Draw(Self.Canvas, px, py, False);
|
---|
188 | end;
|
---|
189 |
|
---|
190 | function TBGRALabelFX.Draw: TBGRABitmap;
|
---|
191 | begin
|
---|
192 | Result := FBGRA.Duplicate as TBGRABitmap;
|
---|
193 | end;
|
---|
194 |
|
---|
195 | constructor TBGRALabelFX.Create(AOwner: TComponent);
|
---|
196 | begin
|
---|
197 | inherited Create(AOwner);
|
---|
198 | SetInitialBounds(0, 0, 24, 24);
|
---|
199 | Font.Height := 24;
|
---|
200 | FBGRA := TBGRABitmap.Create;
|
---|
201 | FTextEffect := TBGRATextEffect.Create(Caption, Font, True);
|
---|
202 | FPreviousCaption:= Caption;
|
---|
203 | FPreviousFont := TFont.Create;
|
---|
204 | FPreviousFont.Assign(Font);
|
---|
205 | FOutline := TBGRATextEffectOutline.Create(Self);
|
---|
206 | FShadow := TBGRATextEffectShadow.Create(Self);
|
---|
207 | //
|
---|
208 | AutoSize := True;
|
---|
209 | FShadow.OffsetX := 0;
|
---|
210 | FShadow.OffsetY := 0;
|
---|
211 | FShadow.Radius := 5;
|
---|
212 | end;
|
---|
213 |
|
---|
214 | destructor TBGRALabelFX.Destroy;
|
---|
215 | begin
|
---|
216 | FBGRA.Free;
|
---|
217 | FTextEffect.Free;
|
---|
218 | FPreviousFont.Free;
|
---|
219 | FOutline.Free;
|
---|
220 | FShadow.Free;
|
---|
221 | inherited Destroy;
|
---|
222 | end;
|
---|
223 |
|
---|
224 | end.
|
---|