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

input.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_APPLICATION_INPUT_HPP
10 #define EAGINE_APPLICATION_INPUT_HPP
11 
12 #include "../bitfield.hpp"
13 #include "../callable_ref.hpp"
14 #include "../message_id.hpp"
15 #include "../value_with_history.hpp"
16 
17 namespace eagine::application {
18 //------------------------------------------------------------------------------
22 enum class input_value_kind : unsigned {
24  relative = 1U << 0U,
26  absolute_norm = 1U << 1U,
28  absolute_free = 1U << 2U
29 };
30 
35 
36 static inline auto operator|(input_value_kind l, input_value_kind r) noexcept
38  return {l, r};
39 }
40 
41 static inline auto all_input_value_kinds() noexcept -> input_value_kinds {
44 }
45 //------------------------------------------------------------------------------
51 template <typename T>
53 
59 template <typename T>
61 //------------------------------------------------------------------------------
62 struct input_info {
63  message_id signal_id{};
64  input_value_kind value_kind{};
65 
66  constexpr input_info(message_id sig_id, input_value_kind kind) noexcept
67  : signal_id{std::move(sig_id)}
68  , value_kind{kind} {}
69 };
70 //------------------------------------------------------------------------------
73 class input_setup {
74 public:
76  auto value_kinds(input_value_kinds init) noexcept -> auto& {
77  _value_kinds = init;
78  return *this;
79  }
80 
85  auto relative() noexcept -> auto& {
86  _value_kinds |= input_value_kind::relative;
87  return *this;
88  }
89 
94  auto absolute_norm() noexcept -> auto& {
95  _value_kinds |= input_value_kind::absolute_norm;
96  return *this;
97  }
98 
103  auto absolute_free() noexcept -> auto& {
104  _value_kinds |= input_value_kind::absolute_free;
105  return *this;
106  }
107 
112  auto any_value_kind() noexcept -> auto& {
113  _value_kinds = all_input_value_kinds();
114  return *this;
115  }
116 
118  auto has(input_value_kind kind) const noexcept -> bool {
119  return _value_kinds.has(kind);
120  }
121 
125  auto trigger() noexcept -> auto& {
126  return absolute_free().absolute_norm();
127  }
128 
130  auto invert(bool init = true) noexcept -> auto& {
131  _invert = init;
132  return *this;
133  }
134 
136  auto multiply(double mult) noexcept -> auto& {
137  _multiplier = mult;
138  return *this;
139  }
140 
142  auto multiplier() const noexcept {
143  return _invert ? -_multiplier : _multiplier;
144  }
145 
148  auto only_if(bool& flag) noexcept -> auto& {
149  _only_if = &flag;
150  return *this;
151  }
152 
155  auto is_applicable() const noexcept {
156  return !_only_if || *_only_if;
157  }
158 
159 private:
160  double _multiplier{1.0};
161  bool* _only_if{nullptr};
162  input_value_kinds _value_kinds{};
163  bool _invert{false};
164 };
165 //------------------------------------------------------------------------------
168 class input : public input_value<double> {
169 public:
170  template <typename T>
171  input(
172  const input_value<T>& value,
173  const input_info&,
174  const input_setup& setup) noexcept
175  : input_value<double>{transform(
176  [&](auto elem) { return double(setup.multiplier() * elem); },
177  value)} {}
178 
180  explicit operator bool() const noexcept {
181  return !are_equal(this->get(), 0.0);
182  }
183 };
184 //------------------------------------------------------------------------------
187 using input_handler = callable_ref<void(const input&)>;
188 
191 struct input_slot : std::tuple<message_id, input_handler> {
192  using base = std::tuple<message_id, input_handler>;
193  using base::base;
194 
196  auto id() const noexcept -> auto& {
197  return std::get<0>(*this);
198  }
199 
201  auto handler() const noexcept -> auto& {
202  return std::get<1>(*this);
203  }
204 };
205 //------------------------------------------------------------------------------
206 } // namespace eagine::application
207 
208 #endif
Application harness / wrapper code is placed in this namespace.
Definition: eagine.hpp:72
@ absolute_norm
Absolute input value normalized to <-1, 1>.
auto absolute_norm() noexcept -> auto &
Specifies that absolute normalized input values are tracked.
Definition: input.hpp:94
auto relative() noexcept -> auto &
Specifies that relative input values are tracked.
Definition: input.hpp:85
Declaration of class template storing a reference to a callable object.
Definition: callable_ref.hpp:24
auto value() const noexcept
Returns the current revision of the value.
Definition: value_with_history.hpp:165
Class representing a user input.
Definition: input.hpp:168
input_value_kind
Application input value kind bits enumeration.
Definition: input.hpp:22
constexpr auto has(bit_type bit) const noexcept
Tests if the specified bit is set.
Definition: bitfield.hpp:70
auto has(input_value_kind kind) const noexcept -> bool
Indicates if values of the specified kind are tracked.
Definition: input.hpp:118
Class that managers user input state.
Definition: input.hpp:73
Class that allows binding of a user input device to a handler callable.
Definition: input.hpp:191
auto only_if(bool &flag) noexcept -> auto &
Registers a flag that "switches" the input on and off.
Definition: input.hpp:148
Class for read-only values with history.
Definition: value_with_history.hpp:141
auto is_applicable() const noexcept
Checks if an input value is currently applicable.
Definition: input.hpp:155
auto multiplier() const noexcept
Returns the raw input value multiplier and the inverse setting.
Definition: input.hpp:142
auto id() const noexcept -> auto &
Descriptive id of this input slot (what it accomplishes).
Definition: input.hpp:196
auto value_kinds(input_value_kinds init) noexcept -> auto &
Constructor with input kinds specification.
Definition: input.hpp:76
auto invert(bool init=true) noexcept -> auto &
Specifies that the raw value from the input source should be inverted.
Definition: input.hpp:130
auto multiply(double mult) noexcept -> auto &
Specifies the multiplier for the raw value from the input source.
Definition: input.hpp:136
auto handler() const noexcept -> auto &
Reference to a callable object bound to the input.
Definition: input.hpp:201
auto absolute_free() noexcept -> auto &
Specifies that absolute unbounded input values are tracked.
Definition: input.hpp:103
auto trigger() noexcept -> auto &
Specifies that this is a trigger-like input.
Definition: input.hpp:125
bitfield< input_value_kind > input_value_kinds
Application input value kind bitfield.
Definition: input.hpp:34
auto any_value_kind() noexcept -> auto &
Specifies that input values of any kind are tracked.
Definition: input.hpp:112
@ relative
Relative input value change.
auto get() const noexcept
Returns the current revision of the value.
Definition: value_with_history.hpp:158
Class storing two identifier values representing class/method pair.
Definition: message_id.hpp:25
@ absolute_free
Absolute input value without bounds.
Class for mutable variables with history.
Definition: value_with_history.hpp:291

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