Loading...
Searching...
No Matches
PointerState.hpp
1#pragma once
2#include <QPointF>
3#include <QSizeF>
4
5#include <array>
6#include <mutex>
7
8namespace score::gfx
9{
24{
25 static constexpr int slots = 32;
26 struct Slot
27 {
28 float x{}, y{}, pressure{};
29 };
30
32 static QPointF toClip(QPointF win, QSizeF sz) noexcept
33 {
34 const double w = sz.width() > 0 ? sz.width() : 1.;
35 const double h = sz.height() > 0 ? sz.height() : 1.;
36 return {win.x() / w * 2. - 1., win.y() / h * -2. + 1.};
37 }
38
39 void move(int slot, float x, float y) noexcept
40 {
41 if(slot < 0 || slot >= slots)
42 return;
43 std::lock_guard l{m_mtx};
44 m_slots[slot].x = x;
45 m_slots[slot].y = y;
46 }
47
48 void press(int slot, float pressure) noexcept
49 {
50 if(slot < 0 || slot >= slots)
51 return;
52 std::lock_guard l{m_mtx};
53 m_slots[slot].pressure = pressure;
54 }
55
58 int slotForTouch(int id) noexcept
59 {
60 std::lock_guard l{m_mtx};
61 int free = -1;
62 for(int i = 0; i < slots; i++)
63 {
64 if(m_touchIds[i] == id)
65 return i;
66 if(free < 0 && m_touchIds[i] == noTouch)
67 free = i;
68 }
69 if(free < 0)
70 {
71 m_touchIds.fill(noTouch);
72 free = 0;
73 }
74 m_touchIds[free] = id;
75 return free;
76 }
77
78 void releaseTouch(int id) noexcept
79 {
80 std::lock_guard l{m_mtx};
81 for(int i = 0; i < slots; i++)
82 if(m_touchIds[i] == id)
83 {
84 m_touchIds[i] = noTouch;
85 m_slots[i].pressure = 0.f;
86 }
87 }
88
89 std::array<Slot, slots> snapshot() const noexcept
90 {
91 std::lock_guard l{m_mtx};
92 return m_slots;
93 }
94
95private:
96 static constexpr int noTouch = -1;
97 mutable std::mutex m_mtx;
98 std::array<Slot, slots> m_slots{};
99 std::array<int, slots> m_touchIds = [] {
100 std::array<int, slots> a;
101 a.fill(noTouch);
102 return a;
103 }();
104};
105}
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:11
Definition PointerState.hpp:27
Definition PointerState.hpp:24
static QPointF toClip(QPointF win, QSizeF sz) noexcept
Window position -> clip space over a window of size sz.
Definition PointerState.hpp:32
int slotForTouch(int id) noexcept
Definition PointerState.hpp:58