载入中...
搜索中...
未找到
InputInjection.cpp
浏览该文件的文档.
1// Publishes IInputInjection via SDL_PushEvent so Keyboard/Mouse isDown state
2// matches polled events after the next PlatformEvent::pump().
3
4#include "common/Capability.h"
5#include "common/Diagnostic.h"
7#include "common/Result.h"
8#include "common/Status.h"
9
10#if __has_include(<SDL2/SDL.h>)
11#include <SDL2/SDL.h>
12#else
13#include <SDL.h>
14#endif
15
16#include <algorithm>
17#include <cstring>
18#include <string>
19#include <string_view>
20
22namespace {
23
24Result<void> pushEvent(SDL_Event& event) {
25 if (SDL_PushEvent(&event) != 1) {
27 DiagnosticCode::Failed, std::string("SDL_PushEvent failed: ") + SDL_GetError(), "input"));
28 }
29 return Result<void>::success();
30}
31
32class SdlInputInjection final : public eve::IInputInjection {
33public:
34 [[nodiscard]] Result<void> injectKey(std::string_view key, bool down, bool repeat) override {
35 if (key.empty()) {
37 Diagnostic::error(DiagnosticCode::ParseError, "key name is required", "key"));
38 }
39 const std::string keyName(key);
40 const SDL_Keycode keycode = SDL_GetKeyFromName(keyName.c_str());
41 if (keycode == SDLK_UNKNOWN) {
43 DiagnosticCode::ParseError, "unknown SDL key name '" + keyName + "'", "key"));
44 }
45 SDL_Event event{};
46 event.type = down ? SDL_KEYDOWN : SDL_KEYUP;
47 event.key.type = event.type;
48 event.key.state = down ? SDL_PRESSED : SDL_RELEASED;
49 event.key.repeat = (down && repeat) ? 1 : 0;
50 event.key.keysym.sym = keycode;
51 event.key.keysym.scancode = SDL_GetScancodeFromKey(keycode);
52 event.key.keysym.mod = KMOD_NONE;
53 return pushEvent(event);
54 }
55
56 [[nodiscard]] Result<void> injectMouseButton(int button, bool down, float x, float y) override {
57 if (button < 1 || button > 5) {
59 DiagnosticCode::ParseError, "mouse button must be 1..5 (SDL button index)", "button"));
60 }
61 SDL_Event event{};
62 event.type = down ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP;
63 event.button.type = event.type;
64 event.button.button = static_cast<Uint8>(button);
65 event.button.state = down ? SDL_PRESSED : SDL_RELEASED;
66 event.button.clicks = 1;
67 event.button.x = static_cast<Sint32>(x);
68 event.button.y = static_cast<Sint32>(y);
69 return pushEvent(event);
70 }
71
72 [[nodiscard]] Result<void> injectMouseMove(float x, float y, float dx, float dy) override {
73 SDL_Event event{};
74 event.type = SDL_MOUSEMOTION;
75 event.motion.type = SDL_MOUSEMOTION;
76 event.motion.x = static_cast<Sint32>(x);
77 event.motion.y = static_cast<Sint32>(y);
78 event.motion.xrel = static_cast<Sint32>(dx);
79 event.motion.yrel = static_cast<Sint32>(dy);
80 return pushEvent(event);
81 }
82
83 [[nodiscard]] Result<void> injectWheel(float x, float y) override {
84 SDL_Event event{};
85 event.type = SDL_MOUSEWHEEL;
86 event.wheel.type = SDL_MOUSEWHEEL;
87 event.wheel.x = static_cast<Sint32>(x);
88 event.wheel.y = static_cast<Sint32>(y);
89 event.wheel.direction = SDL_MOUSEWHEEL_NORMAL;
90#if SDL_VERSION_ATLEAST(2, 0, 18)
91 event.wheel.preciseX = x;
92 event.wheel.preciseY = y;
93#endif
94 return pushEvent(event);
95 }
96
97 [[nodiscard]] Result<void> injectText(std::string_view text) override {
98 if (text.empty()) {
100 Diagnostic::error(DiagnosticCode::ParseError, "text payload is required", "text"));
101 }
102 SDL_Event event{};
103 event.type = SDL_TEXTINPUT;
104 event.text.type = SDL_TEXTINPUT;
105 const std::size_t n =
106 std::min(text.size(), static_cast<std::size_t>(SDL_TEXTINPUTEVENT_TEXT_SIZE - 1));
107 std::memcpy(event.text.text, text.data(), n);
108 event.text.text[n] = '\0';
109 return pushEvent(event);
110 }
111};
112
113struct Register {
114 Register() {
115 static SdlInputInjection injection;
116 eve::cap::provide<eve::IInputInjection>(&injection);
117 }
118} g_register;
119
120} // namespace
121} // namespace eve::platform_event::sdl
double y
double x
Stable, structured diagnostics shared by engine modules.
SQObjectPtr key
Definition ECS.cpp:155
glm::vec3 n
Definition Grass.cpp:63
Capability for agent / MCP playtest input injection.
glm::dvec3 dx
Move-only, checked operation results for the common layer.
std::string text
float dy
Definition Shadow.cpp:151
Structured operation status used by the common Result foundation.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Inject synthetic device input into the live platform event stream. @thread Owner game-loop / MCP poll...
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
ControlEdit button(const char *label, float width=0.f, float height=0.f)
Shared immediate renderer for retained and EditorHost buttons.