From 4bfc14dc1c3cee7c3c39a871a1f7a69838115dd4 Mon Sep 17 00:00:00 2001 From: Lorenzooone Date: Sat, 24 Jan 2026 18:14:53 +0100 Subject: [PATCH] Switch to new Touch interface for SFML --- include/frontend.hpp | 7 +++++++ source/WindowScreen_Menu.cpp | 24 ++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/include/frontend.hpp b/include/frontend.hpp index c4de0d6..65b23c8 100755 --- a/include/frontend.hpp +++ b/include/frontend.hpp @@ -47,6 +47,11 @@ #include "event_structs.hpp" #include "shaders_list.hpp" +// SFML currently does not have a define for the maximum amount of fingers... +// Make one to "fix this", though it will need code to reject extra +// fingers... :/ What a mess. +#define NUM_FINGERS_SUPPORTED 10 + struct HeldTime { std::chrono::time_point start_time; bool started; @@ -161,6 +166,8 @@ private: HeldTime right_click_action; HeldTime controller_button_action; bool consumed_touch_long_press; + bool is_touch_currently_active[NUM_FINGERS_SUPPORTED]; + sf::Vector2i touch_position[NUM_FINGERS_SUPPORTED]; const float touch_short_press_timer = 0.2f; const float touch_long_press_timer = 1.5f; const float mouse_timeout = 5.0f; diff --git a/source/WindowScreen_Menu.cpp b/source/WindowScreen_Menu.cpp index 41b288f..2211b84 100755 --- a/source/WindowScreen_Menu.cpp +++ b/source/WindowScreen_Menu.cpp @@ -97,8 +97,12 @@ static double FPSArrayGetAverage(FPSArray *array) { } void WindowScreen::init_menus() { + for(int i = 0; i < NUM_FINGERS_SUPPORTED; i++) + this->is_touch_currently_active[i] = false; + this->last_menu_change_time = std::chrono::high_resolution_clock::now(); this->last_data_format_change_time = std::chrono::high_resolution_clock::now(); + this->connection_menu = new ConnectionMenu(this->text_rectangle_pool); this->main_menu = new MainMenu(this->text_rectangle_pool); this->video_menu = new VideoMenu(this->text_rectangle_pool); @@ -2641,6 +2645,20 @@ void WindowScreen::poll_window(bool do_everything) { if(this->shared_data->input_data.enable_mouse_input) events_queue.emplace(mouse_movement->position); } + else if(const auto* touch_began = event->getIf()) { + if(touch_began->finger < NUM_FINGERS_SUPPORTED) { + this->is_touch_currently_active[touch_began->finger] = true; + this->touch_position[touch_began->finger] = touch_began->position; + } + } + else if(const auto* touch_moved = event->getIf()) { + if(touch_moved->finger < NUM_FINGERS_SUPPORTED) + this->touch_position[touch_moved->finger] = touch_moved->position; + } + else if(const auto* touch_ended = event->getIf()) { + if(touch_ended->finger < NUM_FINGERS_SUPPORTED) + this->is_touch_currently_active[touch_ended->finger] = false; + } } } if(this->m_win.hasFocus()) { @@ -2658,11 +2676,11 @@ void WindowScreen::poll_window(bool do_everything) { } if(!found) check_held_reset(false, this->controller_button_action); - bool touch_active = sf::Touch::isDown(0); + bool touch_active = this->is_touch_currently_active[0]; check_held_reset(touch_active, this->touch_action); check_held_reset(touch_active, this->touch_right_click_action); if(touch_active) { - sf::Vector2i touch_pos = sf::Touch::getPosition(0, this->m_win); + sf::Vector2i touch_pos = this->touch_position[0]; auto curr_time = std::chrono::high_resolution_clock::now(); const std::chrono::duration diff = curr_time - this->touch_right_click_action.start_time; if(diff.count() > this->touch_long_press_timer) { @@ -2687,6 +2705,8 @@ void WindowScreen::poll_window(bool do_everything) { } } else { + for(int i = 0; i < NUM_FINGERS_SUPPORTED; i++) + this->is_touch_currently_active[i] = false; this->reset_held_times(); check_held_reset(false, this->touch_right_click_action); }