-
Notifications
You must be signed in to change notification settings - Fork 7
/
ChatFMX.Frame.Attachment.Sticker.pas
133 lines (117 loc) · 3.46 KB
/
ChatFMX.Frame.Attachment.Sticker.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
unit ChatFMX.Frame.Attachment.Sticker;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
FMX.Objects, VK.API, VK.Entity.Sticker, System.Messaging, Skia, Skia.FMX,
FMX.Effects, FMX.Filter.Effects, ChatFMX.Frame.Attachment;
type
TFrameAttachmentSticker = class(TFrameAttachment)
ImageSticker: TImage;
AnimatedImageSticker: TSkAnimatedImage;
ShadowEffect1: TShadowEffect;
procedure AnimatedImageStickerMouseEnter(Sender: TObject);
private
FImageUrl, FImageFile: string;
FAnimImageUrl, FAnimImageFile: string;
procedure FOnReadyImage(const Sender: TObject; const M: TMessage);
procedure FOnReadyImageAnim(const Sender: TObject; const M: TMessage);
public
procedure SetVisibility(const Value: Boolean); override;
constructor Create(AOwner: TComponent; AVK: TCustomVK); override;
destructor Destroy; override;
procedure Fill(Sticker: TVkSticker);
end;
implementation
uses
ChatFMX.PreviewManager;
{$R *.fmx}
{ TFrameAttachmentSticker }
procedure TFrameAttachmentSticker.SetVisibility(const Value: Boolean);
begin
inherited;
ImageSticker.Visible := False;
AnimatedImageSticker.Visible := False;
if Value then
begin
if FAnimImageUrl.IsEmpty then
TPreview.Instance.Subscribe(FImageUrl, FOnReadyImage)
else
TPreview.Instance.Subscribe(FAnimImageUrl, FOnReadyImageAnim);
end
else
begin
ImageSticker.Bitmap := nil;
AnimatedImageSticker.Source.Data := [];
end;
end;
procedure TFrameAttachmentSticker.AnimatedImageStickerMouseEnter(Sender: TObject);
begin
if not AnimatedImageSticker.Animation.Running then
begin
AnimatedImageSticker.Animation.Progress := 0;
AnimatedImageSticker.Animation.Start;
end;
end;
constructor TFrameAttachmentSticker.Create(AOwner: TComponent; AVK: TCustomVK);
begin
inherited;
ImageSticker.Visible := False;
AnimatedImageSticker.Visible := False;
end;
destructor TFrameAttachmentSticker.Destroy;
begin
TPreview.Instance.Unsubscribe(FOnReadyImage);
TPreview.Instance.Unsubscribe(FOnReadyImageAnim);
inherited;
end;
procedure TFrameAttachmentSticker.Fill(Sticker: TVkSticker);
begin
if Sticker.AnimationUrl.IsEmpty then
FImageUrl := Sticker.Images[High(Sticker.Images)].Url
else
FAnimImageUrl := Sticker.AnimationUrl;
end;
procedure TFrameAttachmentSticker.FOnReadyImage(const Sender: TObject; const M: TMessage);
var
Data: TMessagePreview absolute M;
begin
if Data.Value.Url <> FImageUrl then
Exit;
TPreview.Instance.Unsubscribe(FOnReadyImage);
FImageFile := Data.Value.FileName;
if FImageFile.IsEmpty then
begin
ImageSticker.Bitmap := nil;
ImageSticker.Visible := False;
end
else
try
ImageSticker.Bitmap.LoadFromFile(FImageFile);
ImageSticker.Visible := True;
except
ImageSticker.Bitmap := nil;
ImageSticker.Visible := False;
end;
end;
procedure TFrameAttachmentSticker.FOnReadyImageAnim(const Sender: TObject; const M: TMessage);
var
Data: TMessagePreview absolute M;
begin
if Data.Value.Url <> FAnimImageUrl then
Exit;
TPreview.Instance.Unsubscribe(FOnReadyImageAnim);
FAnimImageFile := Data.Value.FileName;
if FAnimImageFile.IsEmpty then
begin
AnimatedImageSticker.Visible := False;
end
else
try
AnimatedImageSticker.LoadFromFile(FAnimImageFile);
AnimatedImageSticker.Visible := True;
except
AnimatedImageSticker.Visible := False;
end;
end;
end.