summaryrefslogtreecommitdiff
path: root/src/devices/TouchDevice.cpp
blob: 25b7b503b02ae7a3cf72391e9bb9e0fd9c48fc49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "TouchDevice.hpp"
#include "../defines.hpp"
#include <aquamarine/input/Input.hpp>

SP<CTouchDevice> CTouchDevice::create(SP<Aquamarine::ITouch> touch) {
    SP<CTouchDevice> pTouch = SP<CTouchDevice>(new CTouchDevice(touch));

    pTouch->self = pTouch;

    return pTouch;
}

CTouchDevice::CTouchDevice(SP<Aquamarine::ITouch> touch_) : touch(touch_) {
    if (!touch)
        return;

    listeners.destroy = touch->events.destroy.registerListener([this](std::any d) {
        events.destroy.emit();
        touch.reset();
    });

    listeners.down = touch->events.down.registerListener([this](std::any d) {
        auto E = std::any_cast<Aquamarine::ITouch::SDownEvent>(d);

        touchEvents.down.emit(SDownEvent{
            .timeMs  = E.timeMs,
            .touchID = E.touchID,
            .pos     = E.pos,
            .device  = self.lock(),
        });
    });

    listeners.up = touch->events.up.registerListener([this](std::any d) {
        auto E = std::any_cast<Aquamarine::ITouch::SUpEvent>(d);

        touchEvents.up.emit(SUpEvent{
            .timeMs  = E.timeMs,
            .touchID = E.touchID,
        });
    });

    listeners.motion = touch->events.move.registerListener([this](std::any d) {
        auto E = std::any_cast<Aquamarine::ITouch::SMotionEvent>(d);

        touchEvents.motion.emit(SMotionEvent{
            .timeMs  = E.timeMs,
            .touchID = E.touchID,
            .pos     = E.pos,
        });
    });

    listeners.cancel = touch->events.cancel.registerListener([this](std::any d) {
        auto E = std::any_cast<Aquamarine::ITouch::SCancelEvent>(d);

        touchEvents.cancel.emit(SCancelEvent{
            .timeMs  = E.timeMs,
            .touchID = E.touchID,
        });
    });

    listeners.frame = touch->events.frame.registerListener([this](std::any d) { touchEvents.frame.emit(); });

    deviceName = touch->getName();
}

bool CTouchDevice::isVirtual() {
    return false;
}

SP<Aquamarine::ITouch> CTouchDevice::aq() {
    return touch.lock();
}