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