Debian Codegraph @

src/ui/castlecontrols_timer.inc

raw
1 {%MainUnit castlecontrols.pas}
2 {
3 Copyright 2010-2018 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 {$ifdef read_interface}
18
19 { Timer, running the @link(OnTimer) event periodically. }
20 TCastleTimer = class(TCastleUserInterface)
21 strict private
22 FCounteractDelays: boolean;
23 FIntervalSeconds, IntervalRemaining: TFloatTime;
24 FOnTimer: TNotifyEvent;
25 procedure SetIntervalSeconds(AValue: TFloatTime);
26 protected
27 procedure DoTimer; virtual;
28 public
29 procedure Update(const SecondsPassed: Single;
30 var HandleInput: boolean); override;
31 function PropertySections(const PropertyName: String): TPropertySections; override;
32 published
33 { How often should we call OnTimer. Value of 0 means to call OnTimer
34 in every Update event. }
35 property IntervalSeconds: TFloatTime read FIntervalSeconds write SetIntervalSeconds;
36 { The event called periodically. }
37 property OnTimer: TNotifyEvent read FOnTimer write FOnTimer;
38 { Should we counteract the delays in timer by firing next event sooner.
39 This helps to keep a constant frequency of timer events over a long time,
40 and to keep multiple things (like multiple timer instances)
41 perfectly synchronized with each other.
42 But it may cause to execute a @italic(lot) of timer events at once,
43 in case the application hung for some time. }
44 property CounteractDelays: boolean
45 read FCounteractDelays write FCounteractDelays default false;
46 end;
47
48 {$endif read_interface}
49
50 {$ifdef read_implementation}
51
52 { TCastleTimer --------------------------------------------------------------- }
53
54 procedure TCastleTimer.SetIntervalSeconds(AValue: TFloatTime);
55 begin
56 if FIntervalSeconds <> AValue then
57 begin
58 FIntervalSeconds := AValue;
59 IntervalRemaining := FIntervalSeconds;
60 end;
61 end;
62
63 procedure TCastleTimer.DoTimer;
64 begin
65 if Assigned(OnTimer) then
66 OnTimer(Self);
67 end;
68
69 procedure TCastleTimer.Update(const SecondsPassed: Single; var HandleInput: boolean);
70 begin
71 inherited;
72 if IntervalSeconds <> 0 then
73 begin
74 IntervalRemaining := IntervalRemaining - SecondsPassed;
75 if CounteractDelays then
76 begin
77 while IntervalRemaining < 0 do
78 begin
79 IntervalRemaining := IntervalRemaining + IntervalSeconds;
80 DoTimer;
81 end;
82 end else
83 begin
84 if IntervalRemaining < 0 then
85 begin
86 IntervalRemaining := IntervalSeconds;
87 DoTimer;
88 end;
89 end;
90 end else
91 begin
92 { for IntervalSeconds = 0, just call timer at every Update. }
93 DoTimer;
94 end;
95 end;
96
97 function TCastleTimer.PropertySections(
98 const PropertyName: String): TPropertySections;
99 begin
100 if (PropertyName = 'IntervalSeconds') or
101 (PropertyName = 'CounteractDelays') then
102 Result := [psBasic]
103 else
104 Result := inherited PropertySections(PropertyName);
105 end;
106
107 {$endif read_implementation}