Rewrite touch event conversion (#10636)

This commit is contained in:
Vitaliy 2021-02-07 13:48:30 +03:00 committed by GitHub
parent 3ac07ad34d
commit 4caf156be5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 76 deletions

@ -183,6 +183,64 @@ static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent)
return false; return false;
} }
#ifdef __ANDROID__
bool GUIModalMenu::simulateMouseEvent(
gui::IGUIElement *target, ETOUCH_INPUT_EVENT touch_event)
{
SEvent mouse_event{}; // value-initialized, not unitialized
mouse_event.EventType = EET_MOUSE_INPUT_EVENT;
mouse_event.MouseInput.X = m_pointer.X;
mouse_event.MouseInput.Y = m_pointer.Y;
switch (touch_event) {
case ETIE_PRESSED_DOWN:
mouse_event.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
break;
case ETIE_MOVED:
mouse_event.MouseInput.Event = EMIE_MOUSE_MOVED;
mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
break;
case ETIE_LEFT_UP:
mouse_event.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
mouse_event.MouseInput.ButtonStates = 0;
break;
default:
return false;
}
if (preprocessEvent(mouse_event))
return true;
if (!target)
return false;
return target->OnEvent(mouse_event);
}
void GUIModalMenu::enter(gui::IGUIElement *hovered)
{
sanity_check(!m_hovered);
m_hovered.grab(hovered);
SEvent gui_event{};
gui_event.EventType = EET_GUI_EVENT;
gui_event.GUIEvent.Caller = m_hovered.get();
gui_event.GUIEvent.EventType = EGET_ELEMENT_HOVERED;
gui_event.GUIEvent.Element = gui_event.GUIEvent.Caller;
m_hovered->OnEvent(gui_event);
}
void GUIModalMenu::leave()
{
if (!m_hovered)
return;
SEvent gui_event{};
gui_event.EventType = EET_GUI_EVENT;
gui_event.GUIEvent.Caller = m_hovered.get();
gui_event.GUIEvent.EventType = EGET_ELEMENT_LEFT;
m_hovered->OnEvent(gui_event);
m_hovered.reset();
}
#endif
bool GUIModalMenu::preprocessEvent(const SEvent &event) bool GUIModalMenu::preprocessEvent(const SEvent &event)
{ {
#ifdef __ANDROID__ #ifdef __ANDROID__
@ -230,89 +288,50 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event)
} }
if (event.EventType == EET_TOUCH_INPUT_EVENT) { if (event.EventType == EET_TOUCH_INPUT_EVENT) {
SEvent translated; irr_ptr<GUIModalMenu> holder;
memset(&translated, 0, sizeof(SEvent)); holder.grab(this); // keep this alive until return (it might be dropped downstream [?])
translated.EventType = EET_MOUSE_INPUT_EVENT;
gui::IGUIElement *root = Environment->getRootGUIElement();
if (!root) { switch ((int)event.TouchInput.touchedCount) {
errorstream << "GUIModalMenu::preprocessEvent" case 1: {
<< " unable to get root element" << std::endl; if (event.TouchInput.Event == ETIE_PRESSED_DOWN || event.TouchInput.Event == ETIE_MOVED)
return false;
}
gui::IGUIElement *hovered =
root->getElementFromPoint(core::position2d<s32>(
event.TouchInput.X, event.TouchInput.Y));
translated.MouseInput.X = event.TouchInput.X;
translated.MouseInput.Y = event.TouchInput.Y;
translated.MouseInput.Control = false;
if (event.TouchInput.touchedCount == 1) {
switch (event.TouchInput.Event) {
case ETIE_PRESSED_DOWN:
m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y); m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN; if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
translated.MouseInput.ButtonStates = EMBSM_LEFT;
m_down_pos = m_pointer; m_down_pos = m_pointer;
break; gui::IGUIElement *hovered = Environment->getRootGUIElement()->getElementFromPoint(core::position2d<s32>(m_pointer));
case ETIE_MOVED: if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y); Environment->setFocus(hovered);
translated.MouseInput.Event = EMIE_MOUSE_MOVED; if (m_hovered != hovered) {
translated.MouseInput.ButtonStates = EMBSM_LEFT; leave();
break; enter(hovered);
case ETIE_LEFT_UP:
translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
translated.MouseInput.ButtonStates = 0;
hovered = root->getElementFromPoint(m_down_pos);
// we don't have a valid pointer element use last
// known pointer pos
translated.MouseInput.X = m_pointer.X;
translated.MouseInput.Y = m_pointer.Y;
// reset down pos
m_down_pos = v2s32(0, 0);
break;
default:
break;
} }
} else if ((event.TouchInput.touchedCount == 2) && gui::IGUIElement *focused = Environment->getFocus();
(event.TouchInput.Event == ETIE_PRESSED_DOWN)) { bool ret = simulateMouseEvent(focused, event.TouchInput.Event);
hovered = root->getElementFromPoint(m_down_pos); if (!ret && m_hovered != focused)
ret = simulateMouseEvent(m_hovered.get(), event.TouchInput.Event);
translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
translated.MouseInput.X = m_pointer.X;
translated.MouseInput.Y = m_pointer.Y;
if (hovered)
hovered->OnEvent(translated);
translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
translated.MouseInput.ButtonStates = EMBSM_LEFT;
if (hovered)
hovered->OnEvent(translated);
return true;
} else {
// ignore unhandled 2 touch events (accidental moving for example)
return true;
}
// check if translated event needs to be preprocessed again
if (preprocessEvent(translated))
return true;
if (hovered) {
grab();
bool retval = hovered->OnEvent(translated);
if (event.TouchInput.Event == ETIE_LEFT_UP) if (event.TouchInput.Event == ETIE_LEFT_UP)
// reset pointer leave();
m_pointer = v2s32(0, 0); return ret;
}
drop(); case 2: {
return retval; if (event.TouchInput.Event != ETIE_PRESSED_DOWN)
return true; // ignore
auto focused = Environment->getFocus();
if (!focused)
return true;
SEvent rclick_event{};
rclick_event.EventType = EET_MOUSE_INPUT_EVENT;
rclick_event.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
rclick_event.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
rclick_event.MouseInput.X = m_pointer.X;
rclick_event.MouseInput.Y = m_pointer.Y;
focused->OnEvent(rclick_event);
rclick_event.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
rclick_event.MouseInput.ButtonStates = EMBSM_LEFT;
focused->OnEvent(rclick_event);
return true;
}
default: // ignored
return true;
} }
} }
#endif #endif

@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once #pragma once
#include "irrlichttypes_extrabloated.h" #include "irrlichttypes_extrabloated.h"
#include "irr_ptr.h"
#include "util/string.h" #include "util/string.h"
class GUIModalMenu; class GUIModalMenu;
@ -100,4 +101,12 @@ private:
// This might be necessary to expose to the implementation if it // This might be necessary to expose to the implementation if it
// wants to launch other menus // wants to launch other menus
bool m_allow_focus_removal = false; bool m_allow_focus_removal = false;
#ifdef __ANDROID__
irr_ptr<gui::IGUIElement> m_hovered;
bool simulateMouseEvent(gui::IGUIElement *target, ETOUCH_INPUT_EVENT touch_event);
void enter(gui::IGUIElement *element);
void leave();
#endif
}; };