mirror of
https://github.com/kunkundi/crossdesk.git
synced 2026-12-04 15:19:58 +08:00
142 lines
4.6 KiB
C++
142 lines
4.6 KiB
C++
/*
|
|
* @Author: DI JUNKUN
|
|
* @Date: 2026-11-29
|
|
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
|
*/
|
|
|
|
#ifndef _MOBILE_GAME_INPUT_H_
|
|
#define _MOBILE_GAME_INPUT_H_
|
|
|
|
#include <remote_action.h>
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cmath>
|
|
|
|
#include "mobile_game_layout.h"
|
|
|
|
namespace crossdesk::mobile_controller {
|
|
|
|
// Local UI controls shared by the three mobile bridges. Only ordinary mouse
|
|
// and keyboard events leave the controller. All calls run on its RTC queue.
|
|
enum class GameControl {
|
|
reset = 0,
|
|
stick = 1,
|
|
look = 2,
|
|
fire = 3,
|
|
aim = 4,
|
|
primary = 5,
|
|
secondary = 6
|
|
};
|
|
|
|
// Legacy hosts accept positioned button events. Hosts advertising relative
|
|
// input also accept position-free buttons and camera motion.
|
|
struct GamePointer {
|
|
bool relative = false;
|
|
float x = .5f, y = .5f;
|
|
};
|
|
|
|
class GameInput {
|
|
public:
|
|
// Positive entries are keyboard VKs. Negative entries own mouse buttons.
|
|
const auto& HeldBindings() const { return bindings_; }
|
|
|
|
template <typename Send>
|
|
void Update(GameControl control, float x, float y, int binding,
|
|
const GamePointer& pointer, Send send) {
|
|
if (!std::isfinite(x) || !std::isfinite(y)) return;
|
|
const int id = static_cast<int>(control);
|
|
constexpr int first = static_cast<int>(GameControl::fire);
|
|
if (id >= first && id < first + kGameButtonLimit) {
|
|
const int slot = id - first;
|
|
// Older overlays sent zero for the two dedicated mouse buttons.
|
|
if (x != 0 && binding == 0 && slot < 2) binding = -1 - slot;
|
|
if (x == 0 || (binding >= -3 && binding <= 255 && binding != 0))
|
|
SetBinding(4 + slot, x == 0 ? 0 : binding, pointer, send);
|
|
return;
|
|
}
|
|
switch (control) {
|
|
case GameControl::reset:
|
|
for (size_t i = 0; i < bindings_.size(); ++i) SetBinding(i, 0, pointer, send);
|
|
remainder_x_ = remainder_y_ = 0;
|
|
break;
|
|
case GameControl::stick: {
|
|
const float threshold =
|
|
std::max(.25f, .42f * std::max(std::abs(x), std::abs(y)));
|
|
const std::array<bool, 4> engaged = {(y < -threshold), (x < -threshold),
|
|
(y > threshold), (x > threshold)};
|
|
const uint32_t directions =
|
|
binding ? static_cast<uint32_t>(binding) : 0x44534157u;
|
|
// Acquire before releasing so crossing between two directions mapped
|
|
// to the same key preserves a continuous hold.
|
|
for (size_t i = 0; i < engaged.size(); ++i)
|
|
if (engaged[i]) SetBinding(i, (directions >> (8 * i)) & 255, pointer, send);
|
|
for (size_t i = 0; i < engaged.size(); ++i)
|
|
if (!engaged[i]) SetBinding(i, 0, pointer, send);
|
|
break;
|
|
}
|
|
case GameControl::look: {
|
|
if (!pointer.relative) return;
|
|
const float dx = Quantize(x, remainder_x_);
|
|
const float dy = Quantize(y, remainder_y_);
|
|
if (dx || dy) {
|
|
RemoteAction action{};
|
|
action.type = mouse;
|
|
action.m = {0, 0, 0, move, true, dx, dy};
|
|
send(action);
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
private:
|
|
static float Quantize(float delta, float& remainder) {
|
|
const float value = std::clamp(delta + remainder, -kMaxRelativeMouseDelta,
|
|
kMaxRelativeMouseDelta);
|
|
// Truncating keeps a stationary axis quiet even at half a pixel.
|
|
const float whole = std::trunc(value);
|
|
remainder = value - whole;
|
|
return whole;
|
|
}
|
|
|
|
template <typename Send>
|
|
void SetBinding(size_t slot, int binding, const GamePointer& pointer, Send& send) {
|
|
const int old = bindings_[slot];
|
|
if (old == binding) return;
|
|
bindings_[slot] = 0;
|
|
auto edge = [&](int code, bool down) {
|
|
if (!code || std::find(bindings_.begin(), bindings_.end(), code) !=
|
|
bindings_.end())
|
|
return;
|
|
RemoteAction action{};
|
|
if (code > 0) {
|
|
action.type = keyboard;
|
|
action.k = {static_cast<size_t>(code), 0, false,
|
|
down ? key_down : key_up};
|
|
} else {
|
|
const std::array<MouseFlag, 3> flags =
|
|
down ? std::array<MouseFlag, 3>{left_down, right_down, middle_down}
|
|
: std::array<MouseFlag, 3>{left_up, right_up, middle_up};
|
|
action.type = mouse;
|
|
action.m = {pointer.relative ? 0.f : pointer.x, pointer.relative ? 0.f : pointer.y,
|
|
0, flags[-code - 1], pointer.relative, 0, 0};
|
|
}
|
|
send(action);
|
|
};
|
|
edge(old, false);
|
|
edge(binding, true);
|
|
bindings_[slot] = binding;
|
|
}
|
|
|
|
std::array<int, 4 + kGameButtonLimit> bindings_{};
|
|
float remainder_x_ = 0;
|
|
float remainder_y_ = 0;
|
|
};
|
|
|
|
} // namespace crossdesk::mobile_controller
|
|
|
|
#endif
|