Implement M9.3 camera navigation and bounded input intentions
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
#include "evo/client/input.hpp"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
namespace evo::client {
|
||||
void InputState::clear() noexcept {
|
||||
keys_.fill(false);
|
||||
wheel_ = 0;
|
||||
}
|
||||
void InputState::admit(bool enabled) noexcept {
|
||||
if (!enabled)
|
||||
clear();
|
||||
admitted_ = enabled;
|
||||
}
|
||||
void InputState::capture(bool captured) noexcept {
|
||||
if (captured)
|
||||
clear();
|
||||
captured_ = captured;
|
||||
}
|
||||
std::expected<void, InputError> InputState::key(Key key, bool down, bool repeat) {
|
||||
const auto index = static_cast<std::size_t>(key);
|
||||
if (index >= keys_.size())
|
||||
return std::unexpected(InputError::invalid_key);
|
||||
if (!down)
|
||||
keys_[index] = false;
|
||||
else if (enabled() && !repeat)
|
||||
keys_[index] = true;
|
||||
return {};
|
||||
}
|
||||
std::expected<void, InputError> InputState::pointer(ScreenPoint point) {
|
||||
if (!std::isfinite(point.x) || !std::isfinite(point.y) || std::abs(point.x) > 32768 ||
|
||||
std::abs(point.y) > 32768)
|
||||
return std::unexpected(InputError::invalid_pointer);
|
||||
pointer_ = point;
|
||||
return {};
|
||||
}
|
||||
std::expected<void, InputError> InputState::wheel(double steps) {
|
||||
if (!std::isfinite(steps) || std::abs(steps) > 128)
|
||||
return std::unexpected(InputError::invalid_wheel);
|
||||
if (enabled())
|
||||
wheel_ = std::clamp(wheel_ + steps, -8.0, 8.0);
|
||||
return {};
|
||||
}
|
||||
Navigation InputState::take_navigation() noexcept {
|
||||
const Navigation result{static_cast<int>(keys_[5]) - static_cast<int>(keys_[4]),
|
||||
static_cast<int>(keys_[7]) - static_cast<int>(keys_[6]), wheel_,
|
||||
pointer_};
|
||||
wheel_ = 0;
|
||||
return result;
|
||||
}
|
||||
std::expected<Intention, InputError> InputState::sample(std::uint64_t tick) {
|
||||
if (tick_ && tick <= *tick_)
|
||||
return std::unexpected(InputError::stale_tick);
|
||||
if (sequence_ == std::numeric_limits<std::uint64_t>::max())
|
||||
return std::unexpected(InputError::exhausted);
|
||||
const int x = static_cast<int>(keys_[1]) - static_cast<int>(keys_[0]);
|
||||
const int y = static_cast<int>(keys_[2]) - static_cast<int>(keys_[3]);
|
||||
const int magnitude = x != 0 && y != 0 ? 707 : 1000;
|
||||
tick_ = tick;
|
||||
return Intention{++sequence_, tick, static_cast<std::int16_t>(x * magnitude),
|
||||
static_cast<std::int16_t>(y * magnitude)};
|
||||
}
|
||||
} // namespace evo::client
|
||||
Reference in New Issue
Block a user