forked from Mirrorlandia_minetest/minetest
Rewrite touch event conversion (#10636)
This commit is contained in:
parent
3ac07ad34d
commit
4caf156be5
@ -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;
|
if (event.TouchInput.Event == ETIE_LEFT_UP)
|
||||||
translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
|
leave();
|
||||||
translated.MouseInput.X = m_pointer.X;
|
return ret;
|
||||||
translated.MouseInput.Y = m_pointer.Y;
|
}
|
||||||
if (hovered)
|
case 2: {
|
||||||
hovered->OnEvent(translated);
|
if (event.TouchInput.Event != ETIE_PRESSED_DOWN)
|
||||||
|
return true; // ignore
|
||||||
translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
|
auto focused = Environment->getFocus();
|
||||||
translated.MouseInput.ButtonStates = EMBSM_LEFT;
|
if (!focused)
|
||||||
|
return true;
|
||||||
if (hovered)
|
SEvent rclick_event{};
|
||||||
hovered->OnEvent(translated);
|
rclick_event.EventType = EET_MOUSE_INPUT_EVENT;
|
||||||
|
rclick_event.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
|
||||||
return true;
|
rclick_event.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
|
||||||
} else {
|
rclick_event.MouseInput.X = m_pointer.X;
|
||||||
// ignore unhandled 2 touch events (accidental moving for example)
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
default: // ignored
|
||||||
// check if translated event needs to be preprocessed again
|
|
||||||
if (preprocessEvent(translated))
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (hovered) {
|
|
||||||
grab();
|
|
||||||
bool retval = hovered->OnEvent(translated);
|
|
||||||
|
|
||||||
if (event.TouchInput.Event == ETIE_LEFT_UP)
|
|
||||||
// reset pointer
|
|
||||||
m_pointer = v2s32(0, 0);
|
|
||||||
|
|
||||||
drop();
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#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
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user