载入中...
搜索中...
未找到
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.
9
10#include "common/Capability.h"
11#include "common/Module.h"
12#include "common/config.h"
14#include "touch/Touch.h"
15#include "touch/sdl/Touch.h"
16#include "window/Window.h"
17
18#include <SDL2/SDL_events.h>
19
20namespace eve::touch::sdl {
21namespace {
22
23#ifndef EVENGINE_MACOSX
25void normalizedToDPICoords(double *x, double *y) {
26 double w = 1.0, h = 1.0;
27 if (auto *win = eve::ModuleManager::getInstance<eve::window::Window>("Window")) {
28 w = win->getWidth();
29 h = win->getHeight();
30 }
31 if (x) *x = (*x) * w;
32 if (y) *y = (*y) * h;
33}
34#endif
35
36class TouchEventSink : public eve::event::IPlatformEventSink {
37public:
38 bool observePlatformEvent(const void *nativeEvent) override {
39 const auto &e = *static_cast<const SDL_Event *>(nativeEvent);
40 if (e.type != SDL_FINGERDOWN && e.type != SDL_FINGERUP && e.type != SDL_FINGERMOTION)
41 return false;
42
43 auto *touchMod = dynamic_cast<Touch *>(
44 eve::ModuleManager::getInstance<eve::touch::Touch>("Touch"));
45 if (!touchMod) return false;
46
48 info.id = static_cast<int64_t>(e.tfinger.fingerId);
49 info.x = e.tfinger.x;
50 info.y = e.tfinger.y;
51 info.dx = e.tfinger.dx;
52 info.dy = e.tfinger.dy;
53 info.pressure = e.tfinger.pressure;
54#ifndef EVENGINE_MACOSX
55 normalizedToDPICoords(&info.x, &info.y);
56 normalizedToDPICoords(&info.dx, &info.dy);
57#endif
58 touchMod->onEvent(e.type, info);
59 // Finger events update state only; they produce no queued Message.
60 return true;
61 }
62};
63
64struct Register {
65 Register() {
66 static TouchEventSink sink;
69 }
70} g_register;
71
72} // namespace
73} // namespace eve::touch::sdl
int y
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
int h
int w
I * query()
Definition Capability.h:77