styyx-util 1.0
Utility Header for SKSE Plugin development
Loading...
Searching...
No Matches
st-timer.h
Go to the documentation of this file.
1#pragma once
2
3namespace StyyxUtil
4{
16{
17
18
20 void Start()
21 {
22 if (!running)
23 {
24 startTime = std::chrono::steady_clock::now();
25 running = true;
26 }
27 }
28
29 void Stop() { running = false; }
31 void Reset() { startTime = std::chrono::steady_clock::now(); }
32
35 [[nodiscard]] double ElapsedSeconds() const
36 {
37 if (!running)
38 {
39 return 0.0;
40 }
41 const auto now = std::chrono::steady_clock::now();
42 return std::chrono::duration<double>(now - startTime).count();
43 }
44
47 [[nodiscard]] bool IsRunning() const { return running; }
48
51 void StartLimited(double a_seconds)
52 {
53 limitSeconds = a_seconds;
54 limited = true;
55 startTime = std::chrono::steady_clock::now();
56 running = true;
57 }
58
61 bool IsExpired()
62 {
63 if (!running || !limited)
64 {
65 return false;
66 }
67
69 {
70 Stop();
71 return true;
72 }
73 return false;
74 }
75
78 [[nodiscard]] double RemainingSeconds() const
79 {
80 if (!running || !limited)
81 {
82 return 0.0;
83 }
84
85 return std::max(0.0, limitSeconds - ElapsedSeconds());
86 }
87
88 private:
89 bool limited{false};
90 double limitSeconds{0.0};
91 std::chrono::steady_clock::time_point startTime{};
92 bool running{false};
93};
94} // namespace StyyxUtil
Definition st-actor.h:7
Timer class used to do x after y time.
Definition st-timer.h:16
bool running
Definition st-timer.h:92
double ElapsedSeconds() const
Get the time the timer runs for.
Definition st-timer.h:35
double RemainingSeconds() const
Get the remaining seconds a limited timer runs for.
Definition st-timer.h:78
void StartLimited(double a_seconds)
Start a time for a limited amount of time.
Definition st-timer.h:51
bool limited
Definition st-timer.h:89
double limitSeconds
Definition st-timer.h:90
bool IsRunning() const
Check if the timer is running or not.
Definition st-timer.h:47
std::chrono::steady_clock::time_point startTime
Definition st-timer.h:91
void Stop()
Stop the timer.
Definition st-timer.h:29
void Start()
Start the timer.
Definition st-timer.h:20
void Reset()
Reset timer. Basically set it back to 0.
Definition st-timer.h:31
bool IsExpired()
Check if a limited timer has ran out or not.
Definition st-timer.h:61