diff options
Diffstat (limited to 'src/input_common/drivers/touch_screen.cpp')
| -rw-r--r-- | src/input_common/drivers/touch_screen.cpp | 89 |
1 files changed, 72 insertions, 17 deletions
diff --git a/src/input_common/drivers/touch_screen.cpp b/src/input_common/drivers/touch_screen.cpp index 8acbe4584..1753e0893 100644 --- a/src/input_common/drivers/touch_screen.cpp +++ b/src/input_common/drivers/touch_screen.cpp | |||
| @@ -14,38 +14,93 @@ constexpr PadIdentifier identifier = { | |||
| 14 | 14 | ||
| 15 | TouchScreen::TouchScreen(std::string input_engine_) : InputEngine(std::move(input_engine_)) { | 15 | TouchScreen::TouchScreen(std::string input_engine_) : InputEngine(std::move(input_engine_)) { |
| 16 | PreSetController(identifier); | 16 | PreSetController(identifier); |
| 17 | ReleaseAllTouch(); | ||
| 17 | } | 18 | } |
| 18 | 19 | ||
| 19 | void TouchScreen::TouchMoved(float x, float y, std::size_t finger) { | 20 | void TouchScreen::TouchMoved(float x, float y, std::size_t finger_id) { |
| 20 | if (finger >= 16) { | 21 | const auto index = GetIndexFromFingerId(finger_id); |
| 22 | if (!index) { | ||
| 23 | // Touch doesn't exist handle it as a new one | ||
| 24 | TouchPressed(x, y, finger_id); | ||
| 21 | return; | 25 | return; |
| 22 | } | 26 | } |
| 23 | TouchPressed(x, y, finger); | 27 | const auto i = index.value(); |
| 28 | fingers[i].is_active = true; | ||
| 29 | SetButton(identifier, static_cast<int>(i), true); | ||
| 30 | SetAxis(identifier, static_cast<int>(i * 2), x); | ||
| 31 | SetAxis(identifier, static_cast<int>(i * 2 + 1), y); | ||
| 24 | } | 32 | } |
| 25 | 33 | ||
| 26 | void TouchScreen::TouchPressed(float x, float y, std::size_t finger) { | 34 | void TouchScreen::TouchPressed(float x, float y, std::size_t finger_id) { |
| 27 | if (finger >= 16) { | 35 | if (GetIndexFromFingerId(finger_id)) { |
| 36 | // Touch already exist. Just update the data | ||
| 37 | TouchMoved(x, y, finger_id); | ||
| 28 | return; | 38 | return; |
| 29 | } | 39 | } |
| 30 | SetButton(identifier, static_cast<int>(finger), true); | 40 | const auto index = GetNextFreeIndex(); |
| 31 | SetAxis(identifier, static_cast<int>(finger * 2), x); | 41 | if (!index) { |
| 32 | SetAxis(identifier, static_cast<int>(finger * 2 + 1), y); | 42 | // No free entries. Ignore input |
| 43 | return; | ||
| 44 | } | ||
| 45 | const auto i = index.value(); | ||
| 46 | fingers[i].is_enabled = true; | ||
| 47 | fingers[i].finger_id = finger_id; | ||
| 48 | TouchMoved(x, y, finger_id); | ||
| 33 | } | 49 | } |
| 34 | 50 | ||
| 35 | void TouchScreen::TouchReleased(std::size_t finger) { | 51 | void TouchScreen::TouchReleased(std::size_t finger_id) { |
| 36 | if (finger >= 16) { | 52 | const auto index = GetIndexFromFingerId(finger_id); |
| 53 | if (!index) { | ||
| 37 | return; | 54 | return; |
| 38 | } | 55 | } |
| 39 | SetButton(identifier, static_cast<int>(finger), false); | 56 | const auto i = index.value(); |
| 40 | SetAxis(identifier, static_cast<int>(finger * 2), 0.0f); | 57 | fingers[i].is_enabled = false; |
| 41 | SetAxis(identifier, static_cast<int>(finger * 2 + 1), 0.0f); | 58 | SetButton(identifier, static_cast<int>(i), false); |
| 59 | SetAxis(identifier, static_cast<int>(i * 2), 0.0f); | ||
| 60 | SetAxis(identifier, static_cast<int>(i * 2 + 1), 0.0f); | ||
| 61 | } | ||
| 62 | |||
| 63 | std::optional<std::size_t> TouchScreen::GetIndexFromFingerId(std::size_t finger_id) const { | ||
| 64 | for (std::size_t index = 0; index < MAX_FINGER_COUNT; ++index) { | ||
| 65 | const auto& finger = fingers[index]; | ||
| 66 | if (!finger.is_enabled) { | ||
| 67 | continue; | ||
| 68 | } | ||
| 69 | if (finger.finger_id == finger_id) { | ||
| 70 | return index; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | return std::nullopt; | ||
| 74 | } | ||
| 75 | |||
| 76 | std::optional<std::size_t> TouchScreen::GetNextFreeIndex() const { | ||
| 77 | for (std::size_t index = 0; index < MAX_FINGER_COUNT; ++index) { | ||
| 78 | if (!fingers[index].is_enabled) { | ||
| 79 | return index; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | return std::nullopt; | ||
| 83 | } | ||
| 84 | |||
| 85 | void TouchScreen::ClearActiveFlag() { | ||
| 86 | for (auto& finger : fingers) { | ||
| 87 | finger.is_active = false; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | void TouchScreen::ReleaseInactiveTouch() { | ||
| 92 | for (const auto& finger : fingers) { | ||
| 93 | if (!finger.is_active) { | ||
| 94 | TouchReleased(finger.finger_id); | ||
| 95 | } | ||
| 96 | } | ||
| 42 | } | 97 | } |
| 43 | 98 | ||
| 44 | void TouchScreen::ReleaseAllTouch() { | 99 | void TouchScreen::ReleaseAllTouch() { |
| 45 | for (int index = 0; index < 16; ++index) { | 100 | for (const auto& finger : fingers) { |
| 46 | SetButton(identifier, index, false); | 101 | if (finger.is_enabled) { |
| 47 | SetAxis(identifier, index * 2, 0.0f); | 102 | TouchReleased(finger.finger_id); |
| 48 | SetAxis(identifier, index * 2 + 1, 0.0f); | 103 | } |
| 49 | } | 104 | } |
| 50 | } | 105 | } |
| 51 | 106 | ||