From e2265c8e5f56e576a7a0970a510d3583bc90923c Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 5 Nov 2016 23:14:07 +0100 Subject: [PATCH] touch gui fix (#40) --- src/touchscreengui.cpp | 52 +++++++++++++++++++++++++++--------------- src/touchscreengui.h | 22 ++++++++++-------- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/touchscreengui.cpp b/src/touchscreengui.cpp index ad3b7875..fbb7caab 100644 --- a/src/touchscreengui.cpp +++ b/src/touchscreengui.cpp @@ -394,12 +394,12 @@ touch_gui_button_id TouchScreenGUI::getButtonID(s32 x, s32 y) return after_last_element_id; } -touch_gui_button_id TouchScreenGUI::getButtonID(int eventID) +touch_gui_button_id TouchScreenGUI::getButtonID(size_t eventID) { for (unsigned int i = 0; i < after_last_element_id; i++) { button_info* btn = &m_buttons[i]; - std::vector::iterator id = + std::vector::iterator id = std::find(btn->ids.begin(),btn->ids.end(), eventID); if (id != btn->ids.end()) @@ -436,9 +436,9 @@ bool TouchScreenGUI::isHUDButton(const SEvent &event) return false; } -bool TouchScreenGUI::isReleaseHUDButton(int eventID) +bool TouchScreenGUI::isReleaseHUDButton(size_t eventID) { - std::map::iterator iter = m_hud_ids.find(eventID); + std::map::iterator iter = m_hud_ids.find(eventID); if (iter != m_hud_ids.end()) { SEvent* translated = new SEvent(); @@ -457,7 +457,7 @@ bool TouchScreenGUI::isReleaseHUDButton(int eventID) } void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button, - int eventID, bool action) + size_t eventID, bool action) { button_info* btn = &m_buttons[button]; SEvent* translated = new SEvent(); @@ -484,7 +484,7 @@ void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button, /* remove event */ if ((!action) || (btn->immediate_release)) { - std::vector::iterator pos = + std::vector::iterator pos = std::find(btn->ids.begin(),btn->ids.end(), eventID); /* has to be in touch list */ assert(pos != btn->ids.end()); @@ -500,7 +500,7 @@ void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button, } -void TouchScreenGUI::handleReleaseEvent(int evt_id) +void TouchScreenGUI::handleReleaseEvent(size_t evt_id) { touch_gui_button_id button = getButtonID(evt_id); @@ -611,7 +611,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event) toadd.Y = event.TouchInput.Y; m_known_ids.push_back(toadd); - int eventID = event.TouchInput.ID; + size_t eventID = event.TouchInput.ID; touch_gui_button_id button = getButtonID(event.TouchInput.X, event.TouchInput.Y); @@ -636,7 +636,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event) } } - m_pointerpos[event.TouchInput.ID] = v2s32(event.TouchInput.X, event.TouchInput.Y); + storePointerPos(event.TouchInput.ID, v2s32(event.TouchInput.X, event.TouchInput.Y)); } else if (event.TouchInput.Event == ETIE_LEFT_UP) { verbosestream << "Up event for pointerid: " << event.TouchInput.ID << std::endl; @@ -644,9 +644,9 @@ void TouchScreenGUI::translateEvent(const SEvent &event) } else { assert(event.TouchInput.Event == ETIE_MOVED); - int move_idx = event.TouchInput.ID; + size_t move_idx = event.TouchInput.ID; - if (m_pointerpos[event.TouchInput.ID] == + if (loadPointerPos(event.TouchInput.ID) == v2s32(event.TouchInput.X, event.TouchInput.Y)) { return; } @@ -655,11 +655,12 @@ void TouchScreenGUI::translateEvent(const SEvent &event) if ((event.TouchInput.ID == m_move_id) && (!m_move_sent_as_mouse_event || !g_settings->getBool("touchtarget"))) { + v2s32 old_pos = loadPointerPos(event.TouchInput.ID); double distance = sqrt( - (m_pointerpos[event.TouchInput.ID].X - event.TouchInput.X) * - (m_pointerpos[event.TouchInput.ID].X - event.TouchInput.X) + - (m_pointerpos[event.TouchInput.ID].Y - event.TouchInput.Y) * - (m_pointerpos[event.TouchInput.ID].Y - event.TouchInput.Y)); + (old_pos.X - event.TouchInput.X) * + (old_pos.X - event.TouchInput.X) + + (old_pos.Y - event.TouchInput.Y) * + (old_pos.Y - event.TouchInput.Y)); if ((distance > g_settings->getU16("touchscreen_threshold")) || (m_move_has_really_moved)) { @@ -668,8 +669,8 @@ void TouchScreenGUI::translateEvent(const SEvent &event) s32 Y = event.TouchInput.Y; // update camera_yaw and camera_pitch - s32 dx = X - m_pointerpos[event.TouchInput.ID].X; - s32 dy = Y - m_pointerpos[event.TouchInput.ID].Y; + s32 dx = X - loadPointerPos(event.TouchInput.ID).X; + s32 dy = Y - loadPointerPos(event.TouchInput.ID).Y; /* adapt to similar behaviour as pc screen */ double d = g_settings->getFloat("mouse_sensitivity"); @@ -690,7 +691,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event) ->getSceneManager() ->getSceneCollisionManager() ->getRayFromScreenCoordinates(v2s32(X, Y)); - m_pointerpos[event.TouchInput.ID] = v2s32(X, Y); + storePointerPos(event.TouchInput.ID, v2s32(X, Y)); } } else if ((event.TouchInput.ID == m_move_id) && @@ -714,7 +715,7 @@ void TouchScreenGUI::handleChangedButton(const SEvent &event) if (m_buttons[i].ids.empty()) { continue; } - for (std::vector::iterator iter = m_buttons[i].ids.begin(); + for (std::vector::iterator iter = m_buttons[i].ids.begin(); iter != m_buttons[i].ids.end(); ++iter) { if (event.TouchInput.ID == *iter) { @@ -917,3 +918,16 @@ void TouchScreenGUI::show() Toggle(true); } + +inline void TouchScreenGUI::storePointerPos(size_t ID, v2s32 pos) +{ + m_pointerpos[ID] = pos; +} + +inline v2s32 TouchScreenGUI::loadPointerPos(size_t ID) +{ + std::map::const_iterator it = m_pointerpos.find(ID); + if (it == m_pointerpos.cend()) + return v2s32(0, 0); + return it->second; +} diff --git a/src/touchscreengui.h b/src/touchscreengui.h index 391021a0..0944edae 100644 --- a/src/touchscreengui.h +++ b/src/touchscreengui.h @@ -63,7 +63,6 @@ typedef enum { } touch_gui_button_id; #define MIN_DIG_TIME_MS 500 -#define MAX_TOUCH_COUNT 64 #define BUTTON_REPEAT_DELAY 0.2f extern const char *touchgui_button_imagenames[]; @@ -90,6 +89,9 @@ public: void hide(); void show(); + void storePointerPos(size_t ID, v2s32 pos); + v2s32 loadPointerPos(size_t ID); + private: IrrlichtDevice* m_device; IGUIEnvironment* m_guienv; @@ -97,7 +99,7 @@ private: ISimpleTextureSource* m_texturesource; v2u32 m_screensize; std::map > m_hud_rects; - std::map m_hud_ids; + std::map m_hud_ids; bool m_visible; // is the gui visible /* value in degree */ @@ -108,7 +110,7 @@ private: rect m_control_pad_rect; - int m_move_id; + size_t m_move_id; bool m_move_has_really_moved; s32 m_move_downtime; bool m_move_sent_as_mouse_event; @@ -118,7 +120,7 @@ private: float repeatcounter; float repeatdelay; irr::EKEY_CODE keycode; - std::vector ids; + std::vector ids; IGUIButton* guibutton; bool immediate_release; }; @@ -129,7 +131,7 @@ private: touch_gui_button_id getButtonID(s32 x, s32 y); /* gui button by eventID */ - touch_gui_button_id getButtonID(int eventID); + touch_gui_button_id getButtonID(size_t eventID); /* check if a button has changed */ void handleChangedButton(const SEvent &event); @@ -143,7 +145,7 @@ private: void loadButtonTexture(button_info* btn, const char* path, rect button_rect); struct id_status{ - int id; + size_t id; int X; int Y; }; @@ -152,19 +154,19 @@ private: std::vector m_known_ids; /* handle a button event */ - void handleButtonEvent(touch_gui_button_id bID, int eventID, bool action); + void handleButtonEvent(touch_gui_button_id bID, size_t eventID, bool action); /* handle pressed hud buttons */ bool isHUDButton(const SEvent &event); /* handle released hud buttons */ - bool isReleaseHUDButton(int eventID); + bool isReleaseHUDButton(size_t eventID); /* handle double taps */ bool doubleTapDetection(); /* handle release event */ - void handleReleaseEvent(int evt_id); + void handleReleaseEvent(size_t evt_id); /* get size of regular gui control button */ int getGuiButtonSize(); @@ -177,7 +179,7 @@ private: }; /* array for saving last known position of a pointer */ - v2s32 m_pointerpos[MAX_TOUCH_COUNT]; + std::map m_pointerpos; /* array for doubletap detection */ key_event m_key_events[2];