OGLplus  (0.59.0) a C++ wrapper for rendering APIs

state_view.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_APPLICATION_STATE_VIEW_HPP
10 #define EAGINE_APPLICATION_STATE_VIEW_HPP
11 
12 #include "../quantities.hpp"
13 #include "../value_with_history.hpp"
14 #include <chrono>
15 
16 namespace eagine::application {
17 //------------------------------------------------------------------------------
22 template <typename T>
24 
29 template <typename T>
31 //------------------------------------------------------------------------------
35 public:
37  using clock_type = std::chrono::steady_clock;
38 
40  auto run_time() const noexcept -> clock_type::duration {
41  return clock_type::now() - _start_time;
42  }
43 
48  auto user_idle_time() const noexcept -> clock_type::duration {
49  return clock_type::now() - _user_active_time;
50  }
51 
54  auto frame_time() const noexcept -> seconds_t<float> {
55  return seconds_(_frame_time.value());
56  }
57 
60  auto frame_duration() const noexcept -> seconds_t<float> {
61  return seconds_(_frame_time.delta());
62  }
63 
66  auto is_active() const noexcept -> bool {
67  return !_new_user_idle || _has_activity;
68  }
69 
75  auto user_is_idle() const noexcept -> value_with_history<bool, 2> {
76  return {_new_user_idle, _old_user_idle};
77  }
78 
82  auto user_became_idle() const noexcept -> bool {
83  return user_is_idle().delta() > 0;
84  }
85 
89  auto user_became_active() const noexcept -> bool {
90  return user_is_idle().delta() < 0;
91  }
92 
94  auto user_idle_too_long() const noexcept -> bool {
95  // TODO: configurable interval?
96  return user_idle_time() > std::chrono::seconds{1};
97  }
98 
99 protected:
100  const clock_type::time_point _start_time{clock_type::now()};
101  clock_type::time_point _user_active_time{clock_type::now()};
102  state_variable<float> _frame_time{0.0F};
103 
104  bool _has_activity{false};
105  bool _old_user_idle{false};
106  bool _new_user_idle{false};
107 };
108 //------------------------------------------------------------------------------
109 } // namespace eagine::application
110 
111 #endif
Application harness / wrapper code is placed in this namespace.
Definition: eagine.hpp:72
auto user_idle_time() const noexcept -> clock_type::duration
Returns for how long was the user idle.
Definition: state_view.hpp:48
Read-only view of application context state values.
Definition: state_view.hpp:34
auto frame_duration() const noexcept -> seconds_t< float >
Returns the duration of the previos frame.
Definition: state_view.hpp:60
auto user_became_active() const noexcept -> bool
Indicates that the used became active (generated input events).
Definition: state_view.hpp:89
auto frame_time() const noexcept -> seconds_t< float >
Returns the simulation time of the current frame.
Definition: state_view.hpp:54
Class for read-only values with history.
Definition: value_with_history.hpp:141
static constexpr auto seconds_(T value) noexcept -> seconds_t< T >
Creates a tagged quantity storing value in seconds.
Definition: quantities.hpp:33
auto user_became_idle() const noexcept -> bool
Indicates that the used became idle.
Definition: state_view.hpp:82
auto user_is_idle() const noexcept -> value_with_history< bool, 2 >
Indicates if the user is idle (does not generate input events).
Definition: state_view.hpp:75
auto is_active() const noexcept -> bool
Indicates if the example or the user is active in some way.
Definition: state_view.hpp:66
auto run_time() const noexcept -> clock_type::duration
Returns the application run time duration.
Definition: state_view.hpp:40
auto user_idle_too_long() const noexcept -> bool
Indicates that the user became idle for too long (may depend on config).
Definition: state_view.hpp:94
std::chrono::steady_clock clock_type
The clock type for run-time duration measurements.
Definition: state_view.hpp:37
Class for mutable variables with history.
Definition: value_with_history.hpp:291
Value of type T with a specified unit or tag type U.
Definition: tagged_quantity.hpp:27

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).