forked from HemulGM/Components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HGM.AutoTextType.pas
241 lines (210 loc) · 5.68 KB
/
HGM.AutoTextType.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
unit HGM.AutoTextType;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, Vcl.ExtCtrls, HGM.Common;
type
TOnTextChange = procedure(Sender: TObject; Text: string) of object;
TAutoTypeText = class(TComponent)
private
FWait: Integer;
FText: string;
i: Integer;
Str: string;
DoErase: Boolean;
DoStop: Boolean;
Value: string;
CurrentW: Integer;
FList: TStrings;
FTimerNextPhrase: TTimer;
FTimerNextStep: TTimer;
FNextPhraseInterval: Cardinal;
FNextCharInteravl: Cardinal;
FOnTextChange: TOnTextChange;
FEditControl: TEdit;
FMemoControl: TMemo;
FLabelControl: TLabel;
procedure TimerNextPhraseTimer(Sender: TObject);
procedure TimerNextStepTimer(Sender: TObject);
procedure SetNextPhraseInterval(const Value: Cardinal);
procedure SetNextCharInteravl(const Value: Cardinal);
procedure AnimatePhrase;
procedure AnimateNextStep;
procedure SetOnTextChange(const Value: TOnTextChange);
procedure SetText(const Value: string);
procedure SetEditControl(const Value: TEdit);
procedure SetLabelControl(const Value: TLabel);
procedure SetMemoControl(const Value: TMemo);
procedure SetList(const Value: TStrings);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Start;
procedure Stop;
published
property NextPhraseInterval: Cardinal read FNextPhraseInterval write SetNextPhraseInterval default 2000;
property NextCharInteravl: Cardinal read FNextCharInteravl write SetNextCharInteravl default 70;
property OnTextChange: TOnTextChange read FOnTextChange write SetOnTextChange;
property Text: string read FText;
property LabelControl: TLabel read FLabelControl write SetLabelControl;
property EditControl: TEdit read FEditControl write SetEditControl;
property MemoControl: TMemo read FMemoControl write SetMemoControl;
property List: TStrings read FList write SetList;
end;
procedure Register;
implementation
uses
Math;
{ TAutoTypeText }
procedure Register;
begin
RegisterComponents(PackageName, [TAutoTypeText]);
end;
constructor TAutoTypeText.Create(AOwner: TComponent);
begin
inherited;
FList := TStringList.Create;
DoStop := False;
FNextCharInteravl := 70;
FNextPhraseInterval := 2000;
FWait := FNextCharInteravl;
FTimerNextPhrase := TTimer.Create(nil);
FTimerNextPhrase.Enabled := False;
FTimerNextPhrase.Interval := FNextPhraseInterval;
FTimerNextPhrase.OnTimer := TimerNextPhraseTimer;
FTimerNextStep := TTimer.Create(nil);
FTimerNextStep.Enabled := False;
FTimerNextStep.Interval := 70;
FTimerNextStep.OnTimer := TimerNextStepTimer;
CurrentW := -1;
end;
destructor TAutoTypeText.Destroy;
begin
FList.Free;
FTimerNextPhrase.Free;
FTimerNextStep.Free;
inherited;
end;
procedure TAutoTypeText.TimerNextPhraseTimer(Sender: TObject);
begin
FTimerNextPhrase.Enabled := False;
Inc(CurrentW);
if CurrentW > FList.Count - 1 then
CurrentW := 0;
if FList.Count - 1 < CurrentW then
Exit;
Value := FList[CurrentW];
AnimatePhrase;
end;
procedure TAutoTypeText.TimerNextStepTimer(Sender: TObject);
begin
FTimerNextStep.Enabled := False;
AnimateNextStep;
end;
procedure TAutoTypeText.SetEditControl(const Value: TEdit);
begin
FEditControl := Value;
end;
procedure TAutoTypeText.SetLabelControl(const Value: TLabel);
begin
FLabelControl := Value;
end;
procedure TAutoTypeText.SetList(const Value: TStrings);
begin
FList.Assign(Value);
end;
procedure TAutoTypeText.SetMemoControl(const Value: TMemo);
begin
FMemoControl := Value;
end;
procedure TAutoTypeText.SetNextCharInteravl(const Value: Cardinal);
begin
FNextCharInteravl := Value;
FWait := Value;
end;
procedure TAutoTypeText.SetNextPhraseInterval(const Value: Cardinal);
begin
FNextPhraseInterval := Value;
FTimerNextPhrase.Interval := Value;
end;
procedure TAutoTypeText.SetOnTextChange(const Value: TOnTextChange);
begin
FOnTextChange := Value;
end;
procedure TAutoTypeText.SetText(const Value: string);
begin
FText := Value;
if Assigned(FOnTextChange) then
FOnTextChange(Self, FText);
if Assigned(FEditControl) then
begin
FEditControl.Text := FText;
FEditControl.SelStart := Length(FEditControl.Text);
end;
if Assigned(FLabelControl) then
FLabelControl.Caption := FText;
if Assigned(FMemoControl) then
begin
FMemoControl.Text := FText;
FMemoControl.SelStart := Length(FMemoControl.Text);
end;
end;
procedure TAutoTypeText.Start;
begin
DoStop := False;
FTimerNextPhrase.Enabled := True;
end;
procedure TAutoTypeText.Stop;
begin
DoStop := true;
FTimerNextPhrase.Enabled := False;
end;
procedure TAutoTypeText.AnimateNextStep;
var
C: Char;
begin
if (i <= Value.Length) or DoErase then
begin
if DoErase then
begin
Delete(Str, Str.Length, 1);
DoErase := False;
Dec(i);
end
else
begin
if Random(10) = 2 then
C := Chr(RandomRange(97, 122))
else
C := Value[i];
DoErase := C <> Value[i];
Str := Str + C;
Inc(i);
end;
SetText(Str + '|');
FTimerNextStep.Interval := FWait;
if (i > Value.Length) and (not DoErase) then
FTimerNextStep.Interval := FWait * 12;
FTimerNextStep.Enabled := True;
Exit;
end;
if Str.Length > 0 then
begin
Delete(Str, Str.Length, 1);
SetText(Str + '|');
FTimerNextStep.Interval := FWait div 2;
FTimerNextStep.Enabled := True;
Exit;
end;
if not DoStop then
FTimerNextPhrase.Enabled := True;
end;
procedure TAutoTypeText.AnimatePhrase;
begin
Str := '';
DoErase := False;
i := 1;
AnimateNextStep;
end;
end.