forked from zsdwxm/KernowSoftwareFMX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ksSpeedButton.pas
294 lines (224 loc) · 8.12 KB
/
ksSpeedButton.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
{*******************************************************************************
* *
* TksSpeedButton - TSpeedButton with iOS style badge *
* *
* https://github.com/gmurt/KernowSoftwareFMX *
* *
* Copyright 2015 Graham Murt *
* *
* email: [email protected] *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*******************************************************************************}
unit ksSpeedButton;
interface
{$I ksComponents.inc}
uses Classes, FMX.StdCtrls, FMX.Graphics, ksControlBadge, ksTypes, FMX.Objects,
System.UITypes, System.UIConsts;
type
TksSpeedButtonIcon = (Custom, AlarmClock, BarChart, Barcode, Bell, BookCover, BookCoverMinus, BookCoverPlus, BookMark, BookOpen,
Calendar, Camera, Car, Clock, CloudDownload, CloudUpload, Cross, Document, Download, Earth, Email,
Fax, FileList, FileMinus, FilePlus, Files, FileStar, FileTick, Flag, Folder, FolderMinus,
FolderPlus, FolderStar, Home, Inbox, Incoming, ListBullets, ListCheckBoxes, ListImages, ListNumbered, ListTicked,
Location, More, Note, Outgoing,
PaperClip, Photo, PieChart, Pin, Presentation, Search, Settings, Share, ShoppingCart, Spanner, Speaker,
Star, Tablet, Tag, Telephone, Telephone2, TelephoneBook, Tick, Timer, Trash, Upload,
User, UserEdit, UserGroup, Users, UserSearch,
VideoCamera, VideoPlayer, Viewer,
Wifi, Window, Write);
[ComponentPlatformsAttribute(pidWin32 or pidWin64 or
{$IFDEF XE8_OR_NEWER} pidiOSDevice32 or pidiOSDevice64
{$ELSE} pidiOSDevice {$ENDIF} or pidiOSSimulator or pidAndroid)]
TksSpeedButton = class(TksBaseSpeedButton)
private
FBadge: TksControlBadge;
FIcon: TksSpeedButtonIcon;
FBitmap: TBitmap;
FMouseDown: Boolean;
FIconColor: TAlphaColor;
procedure SetBadge(Value: TksBadgeProperties);
function GetBadge: TksBadgeProperties;
procedure SetIcon(const Value: TksSpeedButtonIcon);
procedure Invalidate;
procedure SetIconColor(const Value: TAlphaColor);
protected
procedure Resize; override;
procedure Paint; override;
function GetDefaultStyleLookupName: string; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Icon: TksSpeedButtonIcon read FIcon write SetIcon;
property Badge: TksBadgeProperties read GetBadge write SetBadge;
property IconColor: TAlphaColor read FIconColor write SetIconColor default claNull;
end;
procedure Register;
implementation
uses Math, FMX.types, System.TypInfo, System.Types, ksCommon, SysUtils,
Fmx.Forms, FMX.Controls;
procedure Register;
begin
RegisterComponents('Kernow Software FMX', [TksSpeedButton]);
end;
{ TksSpeedButton }
constructor TksSpeedButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBadge := TksControlBadge.Create(Self);
FIcon := Custom;
FBitmap := TBitmap.Create;
FIconColor := claNull;
///FImage := TImage.Create(Self);
//FImage.HitTest := False;
//FImage.Locked := True;
//FImage.Stored := False;
//StyledSettings := [];
//FImage.Align := TAlignLayout.Center;
//AddObjecT(FImage);
AddObject(FBadge);
end;
destructor TksSpeedButton.Destroy;
begin
FreeAndNil(FBitmap);
{$IFDEF NEXTGEN}
FBadge.DisposeOf;
//FImage.DisposeOf;
{$ELSE}
FBadge.Free;
//FImage.Free;
{$ENDIF}
inherited;
end;
function TksSpeedButton.GetBadge: TksBadgeProperties;
begin
Result := FBadge.Properties;
end;
procedure TksSpeedButton.Resize;
begin
inherited;
FBadge.Position.X := (Width - FBadge.Width) - (Width * 0.1);
end;
function TksSpeedButton.GetDefaultStyleLookupName: string;
begin
Result := GenerateStyleName(TSpeedButton.ClassName);
end;
procedure TksSpeedButton.Invalidate;
begin
InvalidateRect(ClipRect);
end;
procedure TksSpeedButton.Paint;
var
AImageRect: TRectF;
ABmp: TBitmap;
ASaveState: TCanvasSaveState;
begin
inherited;
ASaveState := Canvas.SaveState;
try
canvas.IntersectClipRect(ClipRect);
AImageRect := RectF(0, 0, 24, 24);;
OffsetRect(AImageRect,
(Width - AImageRect.Width) / 2,
(Height - AImageRect.Height) / 2);
ABmp := TBitmap.Create;
try
ABmp.Assign(FBitmap);
if (FMouseDown) then
begin
{$IFDEF IOS}
ReplaceOpaqueColor(FBitmap, GetColorOrDefault(FIconColor, claLightskyblue));
{$ENDIF}
{$IFDEF ANDROID}
ReplaceOpaqueColor(FBitmap, GetColorOrDefault(FIconColor, claDimgray));
{$ENDIF}
end
else
begin
{$IFDEF IOS}
ReplaceOpaqueColor(FBitmap, GetColorOrDefault(FIconColor, claDodgerblue));
{$ENDIF}
{$IFDEF ANDROID}
ReplaceOpaqueColor(FBitmap, GetColorOrDefault(FIconColor, claDimgray));
{$ENDIF}
end;
Canvas.DrawBitmap(ABmp,
RectF(0, 0, ABmp.Width, ABmp.Height),
AImageRect,
1,
False);
finally
ABmp.Free;
end;
finally
Canvas.RestoreState(ASaveState);
end;
end;
procedure TksSpeedButton.SetIcon(const Value: TksSpeedButtonIcon);
var
AStream: TResourceStream;
AEnumName: String;
begin
if Value <> TksSpeedButtonIcon.Custom then
begin
AEnumName := GetENumName(TypeInfo(TksSpeedButtonIcon), Ord(Value));
AStream := TResourceStream.Create(HInstance, AEnumName, RT_RCDATA);
try
FBitmap.Clear(claNull);
FBitmap.LoadFromStream(AStream);
if (FMouseDown) then
begin
ReplaceOpaqueColor(FBitmap, GetColorOrDefault(FIconColor, claLightskyblue));
end
else
begin
ReplaceOpaqueColor(FBitmap, GetColorOrDefault(FIconColor, claDodgerblue));
end;
finally
AStream.Free;
end;
end;
FIcon := Value;
Invalidate;
end;
procedure TksSpeedButton.SetIconColor(const Value: TAlphaColor);
begin
FIconColor := Value;
Repaint;
end;
procedure TksSpeedButton.SetBadge(Value: TksBadgeProperties);
begin
FBadge.Properties.Assign(Value);
end;
procedure TksSpeedButton.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
inherited;
FMouseDown := True;
Invalidate;
//
Application.ProcessMessages;
end;
procedure TksSpeedButton.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Single);
begin
inherited;
FMouseDown := False;
Invalidate;
Application.ProcessMessages;
end;
initialization
Classes.RegisterClass(TksSpeedButton);
end.