载入中...
搜索中...
未找到
Touch.cpp
浏览该文件的文档.
1
2#include "touch/sdl/Touch.h"
3
4#include <SDL2/SDL_events.h>
5
6#include <algorithm>
7
8#include "common/Exception.h"
9
11
12namespace eve::touch::sdl {
13
14const std::vector<TouchInfo> &Touch::getTouches() const { return touches; }
15
16const TouchInfo &Touch::getTouch(int64_t id) const {
17 for (const auto &touch : touches) {
18 if (touch.id == id) return touch;
19 }
20
21 throw Exception("Invalid active touch ID: %d", id);
22}
23
24void Touch::onEvent(uint32_t eventtype, const TouchInfo &info) {
25 auto compare = [&](const TouchInfo &touch) -> bool { return touch.id == info.id; };
26
27 switch (eventtype) {
28 case SDL_FINGERDOWN:
29 touches.erase(std::remove_if(touches.begin(), touches.end(), compare), touches.end());
30 touches.push_back(info);
31 break;
32 case SDL_FINGERMOTION: {
33 for (TouchInfo &touch : touches) {
34 if (touch.id == info.id) touch = info;
35 }
36 break;
37 }
38 case SDL_FINGERUP: touches.erase(std::remove_if(touches.begin(), touches.end(), compare), touches.end()); break;
39 default: break;
40 }
41}
42
43} // namespace eve::touch::sdl
void onEvent(uint32_t eventtype, const TouchInfo &info)
由事件转换器在 SDL 事件回调中更新触点状态(见事件模块)。
Definition Touch.cpp:24
const std::vector< TouchInfo > & getTouches() const override
Gets all currently active touches.
Definition Touch.cpp:14
const TouchInfo & getTouch(int64_t id) const override
Gets a specific touch, using its ID.
Definition Touch.cpp:16