载入中...
搜索中...
未找到
EventSink.cpp
浏览该文件的文档.
1// eve-hot-path
2// Feeds SDL finger events into the touch module's press state.
3//
4// This lived in the SDL event pump, which therefore had to know about the touch
5// module and its SDL backend. Owning it here removes that edge.
6//
7// SDL also exposes touch state through query functions, but some backends
8// update those on another thread, so press state is only ever advanced from
9// here. The Touch module binds itself as owner at construction so the hot path
10// never looks up ModuleManager. DPI conversion uses the SDL window id on the
11// finger event (no Window module reverse dependency).
12
13#include "common/Capability.h"
14#include "common/config.h"
17#include "touch/sdl/Touch.h"
18
19#include <SDL2/SDL_events.h>
20#include <SDL2/SDL_video.h>
21
22namespace eve::touch::sdl {
23namespace {
24
25Touch *g_touch = nullptr;
26
27#ifndef EVENGINE_MACOSX
29void normalizedToDPICoords(const SDL_TouchFingerEvent &finger, double *x, double *y) {
30 double w = 1.0, h = 1.0;
31 if (SDL_Window *native = SDL_GetWindowFromID(finger.windowID)) {
32 int iw = 0, ih = 0;
33 SDL_GetWindowSize(native, &iw, &ih);
34 if (iw > 0) w = iw;
35 if (ih > 0) h = ih;
36 }
37 if (x) *x = (*x) * w;
38 if (y) *y = (*y) * h;
39}
40#endif
41
42class TouchEventSink : public eve::platform_event::IPlatformEventSink {
43public:
44 bool shouldConsumePlatformEvent(const void *nativeEvent) override {
45 const auto &e = *static_cast<const SDL_Event *>(nativeEvent);
46 if (e.type != SDL_FINGERDOWN && e.type != SDL_FINGERUP && e.type != SDL_FINGERMOTION)
47 return false;
48
49 auto *touchMod = g_touch;
50 if (!touchMod) return false;
51
53 info.id = static_cast<int64_t>(e.tfinger.fingerId);
54 info.x = e.tfinger.x;
55 info.y = e.tfinger.y;
56 info.dx = e.tfinger.dx;
57 info.dy = e.tfinger.dy;
58 info.pressure = e.tfinger.pressure;
59#ifndef EVENGINE_MACOSX
60 normalizedToDPICoords(e.tfinger, &info.x, &info.y);
61 normalizedToDPICoords(e.tfinger, &info.dx, &info.dy);
62#endif
63 touchMod->onEvent(e.type, info);
64 // Finger events update state only; they produce no queued Message.
65 return true;
66 }
67};
68
69struct Register {
70 Register() {
71 static TouchEventSink sink;
72 eve::cap::addListener<eve::platform_event::IPlatformEventSink>(&sink,
74 }
75} g_register;
76
77} // namespace
78
79void bindTouchEventSinkOwner(Touch *owner) noexcept { g_touch = owner; }
80
81} // namespace eve::touch::sdl
double y
double x
float w
Definition AnimClip.cpp:738
int h
std::shared_ptr< OceanGpuSpectrum > owner
EVENGINE_API_FOUNDATION public API.
Active touch-point query interface (positions in window pixels).
Definition Touch.h:15
SDL touch backend; touch state is updated from the event pump.
Definition Touch.h:11
void bindTouchEventSinkOwner(Touch *owner) noexcept
Bind the live Touch module into the SDL event sink. @ownership Borrowed; cleared when the Touch modul...
Definition EventSink.cpp:79
One active contact; id is unique only for the duration of the press.
Definition Touch.h:20
glm::uvec4 info