载入中...
搜索中...
未找到
EventSink.cpp
浏览该文件的文档.
1// eve-hot-path
2// Translates SDL key and text events into engine messages.
3//
4// This lived in the SDL event pump, which had to consult the keyboard module
5// for the key-repeat setting. Owning the translation here removes that edge.
6
7#include "common/Capability.h"
8#include "keyboard/Keyboard.h"
12
13#include <SDL2/SDL_events.h>
14#include <SDL2/SDL_keyboard.h>
15
17namespace {
18
21
22eve::keyboard::Keyboard *g_owner = nullptr;
23
24class KeyboardEventSink : public eve::platform_event::IPlatformEventSink {
25public:
26 Message *translatePlatformEvent(const void *nativeEvent) override {
27 const auto &e = *static_cast<const SDL_Event *>(nativeEvent);
28 switch (e.type) {
29 case SDL_KEYDOWN: {
30 // Repeats are dropped unless the module opted into them.
31 if (e.key.repeat) {
32 if (g_owner && !g_owner->hasKeyRepeat()) return nullptr;
33 }
34 const char *keyName = SDL_GetKeyName(e.key.keysym.sym);
35 const char *scanName = SDL_GetScancodeName(e.key.keysym.scancode);
36 return new Message("keypressed",
37 {Variant::makeString(keyName ? keyName : ""),
38 Variant::makeString(scanName ? scanName : ""),
39 Variant::makeInt(e.key.repeat ? 1 : 0)});
40 }
41 case SDL_KEYUP: {
42 const char *keyName = SDL_GetKeyName(e.key.keysym.sym);
43 const char *scanName = SDL_GetScancodeName(e.key.keysym.scancode);
44 return new Message("keyreleased",
45 {Variant::makeString(keyName ? keyName : ""),
46 Variant::makeString(scanName ? scanName : "")});
47 }
48 case SDL_TEXTINPUT:
49 return new Message("textinput", {Variant::makeString(e.text.text)});
50 case SDL_TEXTEDITING:
51 return new Message("textedited", {Variant::makeString(e.edit.text),
52 Variant::makeInt(e.edit.start),
53 Variant::makeInt(e.edit.length)});
54 default:
55 return nullptr;
56 }
57 }
58};
59
60struct Register {
61 Register() {
62 static KeyboardEventSink sink;
63 eve::cap::addListener<eve::platform_event::IPlatformEventSink>(&sink,
65 }
66} g_register;
67
68} // namespace
69
71
72} // namespace eve::keyboard::sdl
std::shared_ptr< OceanGpuSpectrum > owner
Keyboard input. Key / scancode names follow SDL naming (SDL_GetKeyName / SDL_GetScancodeName),...
Definition Keyboard.h:15
virtual bool hasKeyRepeat() const =0
True when key-repeat events are enabled.
EVENGINE_API_FOUNDATION public API.
A named event carrying an ordered list of Variant payloads. Pushed messages are heap-allocated; the q...
void bindKeyboardEventSinkOwner(eve::keyboard::Keyboard *owner) noexcept
Bind the live Keyboard module into the SDL event sink. @ownership Borrowed; cleared when the Keyboard...
Definition EventSink.cpp:70