File: taanimatedsource.pas

package info (click to toggle)
lazarus 2.2.6%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 219,980 kB
  • sloc: pascal: 1,944,919; xml: 357,634; makefile: 270,608; cpp: 57,115; sh: 3,249; java: 609; perl: 297; sql: 222; ansic: 137
file content (264 lines) | stat: -rw-r--r-- 6,932 bytes parent folder | download | duplicates (3)
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
{

 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

  Authors: Alexander Klenin

}

unit TAAnimatedSource;

{$H+}
{$WARN 6058 off : Call to subroutine "$1" marked as inline is not inlined}
interface

uses
  Classes, CustomTimer,
  TAChartUtils, TACustomSource;

type
  TCustomAnimatedChartSource = class;

  TAnimatedChartSourceItemEvent = procedure (
    ASender: TCustomAnimatedChartSource;
    AIndex: Integer; var AItem: TChartDataItem) of object;
  TAnimatedChartSourceEvent = procedure (
    ASender: TCustomAnimatedChartSource) of object;

  TCustomAnimatedChartSource = class(TCustomChartSource)
  strict private
    FAnimationInterval: Cardinal;
    FAnimationTime: Cardinal;
    FCurrentStep: Cardinal;
    FItem: TChartDataItem;
    FListener: TListener;
    FOnGetItem: TAnimatedChartSourceItemEvent;
    FOnStop: TAnimatedChartSourceEvent;
    FOrigin: TCustomChartSource;
    FProjectedSteps: Cardinal;
    FSkippedFramesCount: Cardinal;
    FStartTime: Cardinal;
    FTimer: TCustomTimer;
    procedure Changed(ASender: TObject);
    procedure OnTimer(ASender: TObject);
    procedure SetOrigin(AValue: TCustomChartSource);
  protected
    function GetCount: Integer; override;
    function GetItem(AIndex: Integer): PChartDataItem; override;
    procedure SetXCount(AValue: Cardinal); override;
    procedure SetYCount(AValue: Cardinal); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    function BasicExtent: TDoubleRect; override;
    function Extent: TDoubleRect; override;
    function ExtentCumulative: TDoubleRect; override;
    function ExtentList: TDoubleRect; override;
    function ExtentXYList: TDoubleRect; override;
    function ValuesTotal: Double; override;

    function IsAnimating: Boolean; inline;
    function Progress: Double; inline;
    procedure Start;
    procedure Stop(ACallEvent: Boolean = false);

    property CurrentStep: Cardinal read FCurrentStep;
    property ProjectedSteps: Cardinal read FProjectedSteps;
    property SkippedFramesCount: Cardinal read FSkippedFramesCount;
  published
    property AnimationInterval: Cardinal
      read FAnimationInterval write FAnimationInterval default 0;
    property AnimationTime: Cardinal
      read FAnimationTime write FAnimationTime default 0;
    property Origin: TCustomChartSource read FOrigin write SetOrigin;
  published
    property OnGetItem: TAnimatedChartSourceItemEvent
      read FOnGetItem write FOnGetItem;
    property OnStop: TAnimatedChartSourceEvent read FOnStop write FOnStop;
  end;

implementation

uses
  LCLIntf, Math, SysUtils;

{ TCustomAnimatedChartSource }

procedure TCustomAnimatedChartSource.Changed(ASender: TObject);
begin
  Unused(ASender);
  if FOrigin <> nil then begin
    FXCount := Origin.XCount;
    FYCount := Origin.YCount;
  end else begin
    FXCount := MaxInt;    // Allow source to be used by any series while Origin = nil
    FYCount := MaxInt;
  end;
  Notify;
end;

constructor TCustomAnimatedChartSource.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FXCount := MaxInt;    // Allow source to be used by any series while Origin = nil
  FYCount := MaxInt;
  FListener := TListener.Create(@FOrigin, @Changed);
  FTimer := TCustomTimer.Create(nil);
  FTimer.Enabled := false;
  FTimer.OnTimer := @OnTimer;
end;

destructor TCustomAnimatedChartSource.Destroy;
begin
  FreeAndNil(FTimer);
  FreeAndNil(FListener);
  inherited;
end;

function TCustomAnimatedChartSource.BasicExtent: TDoubleRect;
begin
  if Origin = nil then
    Result := EmptyExtent
  else
    Result := Origin.BasicExtent;
end;

function TCustomAnimatedChartSource.Extent: TDoubleRect;
begin
  if Origin = nil then
    Result := EmptyExtent
  else
    Result := Origin.Extent;
end;

function TCustomAnimatedChartSource.ExtentCumulative: TDoubleRect;
begin
  if Origin = nil then
    Result := EmptyExtent
  else
    Result := Origin.ExtentCumulative;
end;

function TCustomAnimatedChartSource.ExtentList: TDoubleRect;
begin
  if Origin = nil then
    Result := EmptyExtent
  else
    Result := Origin.ExtentList;
end;

function TCustomAnimatedChartSource.ExtentXYList: TDoubleRect;
begin
  if Origin = nil then
    Result := EmptyExtent
  else
    Result := Origin.ExtentXYList;
end;

function TCustomAnimatedChartSource.ValuesTotal: Double;
begin
  if Origin = nil then
    Result := 0
  else
    Result := Origin.ValuesTotal;
end;

function TCustomAnimatedChartSource.GetCount: Integer;
begin
  if Origin = nil then
    Result := 0
  else
    Result := Origin.Count;
end;

function TCustomAnimatedChartSource.GetItem(AIndex: Integer): PChartDataItem;
begin
  if Origin = nil then exit(nil);
  if not IsAnimating then exit(Origin.Item[AIndex]);
  FItem := Origin.Item[AIndex]^;
  Result := @FItem;
  if Assigned(OnGetItem) then
    OnGetItem(Self, AIndex, FItem);
end;

function TCustomAnimatedChartSource.IsAnimating: Boolean;
begin
  Result := FTimer.Enabled;
end;

procedure TCustomAnimatedChartSource.OnTimer(ASender: TObject);
var
  d, s: Cardinal;
begin
  Unused(ASender);
  d := GetTickCount64 - FStartTime;
  if d >= AnimationTime then
    Stop(true);
  s := Round(d * ProjectedSteps / AnimationTime);
  if FCurrentStep + 1 <> s then
    FSkippedFramesCount += 1;
  FCurrentStep := s;
  Notify;
end;

function TCustomAnimatedChartSource.Progress: Double;
begin
  if ProjectedSteps = 0 then
    Result := 0
  else
    Result := (FCurrentStep - 1) / ProjectedSteps;
end;

procedure TCustomAnimatedChartSource.SetOrigin(AValue: TCustomChartSource);
begin
  if AValue = Self then
      AValue := nil;
  if FOrigin = AValue then exit;
  if FOrigin <> nil then
    FOrigin.Broadcaster.Unsubscribe(FListener);
  FOrigin := AValue;
  if FOrigin <> nil then
    FOrigin.Broadcaster.Subscribe(FListener);
  Changed(nil);
end;

procedure TCustomAnimatedChartSource.SetXCount(AValue: Cardinal);
begin
  Unused(AValue);
  raise EXCountError.Create('Cannot set XCount');
end;

procedure TCustomAnimatedChartSource.SetYCount(AValue: Cardinal);
begin
  Unused(AValue);
  raise EYCountError.Create('Cannot set YCount');
end;

procedure TCustomAnimatedChartSource.Start;
begin
  Stop;
  FSkippedFramesCount := 0;
  if (AnimationInterval = 0) or (AnimationTime <= AnimationInterval) then exit;
  FProjectedSteps := Round(AnimationTime / AnimationInterval);
  FStartTime := GetTickCount64;
  FTimer.Interval := AnimationInterval;
  FTimer.Enabled := true;
end;

procedure TCustomAnimatedChartSource.Stop(ACallEvent: Boolean);
begin
  FTimer.Enabled := false;
  FCurrentStep := 0;
  if ACallEvent and Assigned(OnStop) then
    OnStop(Self);
end;

initialization
  if ZeroValue = 0 then; // Workaround for an incorrect "unused unit" hint.

end.