1
0
mirror of https://github.com/minetest/minetest.git synced 2025-07-03 08:20:23 +02:00

Auto-toggle TouchControls in-game when receiving touch/mouse input

This commit is contained in:
grorp
2024-09-28 11:23:09 +02:00
committed by grorp
parent 3c5f05b284
commit 4952f17df4
15 changed files with 152 additions and 89 deletions

View File

@ -3615,7 +3615,7 @@ void GUIFormSpecMenu::showTooltip(const std::wstring &text,
int tooltip_offset_x = m_btn_height;
int tooltip_offset_y = m_btn_height;
if (m_pointer_type == PointerType::Touch) {
if (RenderingEngine::getLastPointerType() == PointerType::Touch) {
tooltip_offset_x *= 3;
tooltip_offset_y = 0;
if (m_pointer.X > (s32)screenSize.X / 2)

View File

@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "guiFormSpecMenu.h"
#include "client/hud.h"
#include "client/client.h"
#include "client/renderingengine.h"
#include <IVideoDriver.h>
GUIInventoryList::GUIInventoryList(gui::IGUIEnvironment *env,
@ -154,7 +155,7 @@ void GUIInventoryList::draw()
// Add hovering tooltip
bool show_tooltip = !item.empty() && hovering && !selected_item;
// Make it possible to see item tooltips on touchscreens
if (m_fs_menu->getPointerType() == PointerType::Touch) {
if (RenderingEngine::getLastPointerType() == PointerType::Touch) {
show_tooltip |= hovering && selected && m_fs_menu->getSelectedAmount() != 0;
}
if (show_tooltip) {

View File

@ -187,6 +187,7 @@ bool GUIModalMenu::simulateMouseEvent(ETOUCH_INPUT_EVENT touch_event, bool secon
mouse_event.EventType = EET_MOUSE_INPUT_EVENT;
mouse_event.MouseInput.X = m_pointer.X;
mouse_event.MouseInput.Y = m_pointer.Y;
mouse_event.MouseInput.Simulated = true;
switch (touch_event) {
case ETIE_PRESSED_DOWN:
mouse_event.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
@ -210,7 +211,6 @@ bool GUIModalMenu::simulateMouseEvent(ETOUCH_INPUT_EVENT touch_event, bool secon
}
bool retval;
m_simulated_mouse = true;
do {
if (preprocessEvent(mouse_event)) {
retval = true;
@ -222,7 +222,6 @@ bool GUIModalMenu::simulateMouseEvent(ETOUCH_INPUT_EVENT touch_event, bool secon
}
retval = target->OnEvent(mouse_event);
} while (false);
m_simulated_mouse = false;
if (!retval && !second_try)
return simulateMouseEvent(touch_event, true);
@ -330,7 +329,6 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event)
holder.grab(this); // keep this alive until return (it might be dropped downstream [?])
if (event.TouchInput.touchedCount == 1) {
m_pointer_type = PointerType::Touch;
m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
gui::IGUIElement *hovered = Environment->getRootGUIElement()->getElementFromPoint(core::position2d<s32>(m_pointer));
@ -373,9 +371,8 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event)
}
if (event.EventType == EET_MOUSE_INPUT_EVENT) {
if (!m_simulated_mouse) {
// Only set the pointer type to mouse if this is a real mouse event.
m_pointer_type = PointerType::Mouse;
if (!event.MouseInput.Simulated) {
// Only process if this is a real mouse event.
m_pointer = v2s32(event.MouseInput.X, event.MouseInput.Y);
m_touch_hovered.reset();
}

View File

@ -26,11 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <porting_android.h>
#endif
enum class PointerType {
Mouse,
Touch,
};
struct PointerAction {
v2s32 pos;
u64 time; // ms
@ -74,14 +69,10 @@ public:
porting::AndroidDialogState getAndroidUIInputState();
#endif
PointerType getPointerType() { return m_pointer_type; };
protected:
virtual std::wstring getLabelByID(s32 id) = 0;
virtual std::string getNameByID(s32 id) = 0;
// Stores the last known pointer type.
PointerType m_pointer_type = PointerType::Mouse;
// Stores the last known pointer position.
// If the last input event was a mouse event, it's the cursor position.
// If the last input event was a touch event, it's the finger position.
@ -102,9 +93,6 @@ protected:
// This is set to true if the menu is currently processing a second-touch event.
bool m_second_touch = false;
// This is set to true if the menu is currently processing a mouse event
// that was synthesized by the menu itself from a touch event.
bool m_simulated_mouse = false;
private:
IMenuManager *m_menumgr;

View File

@ -418,6 +418,11 @@ TouchControls::TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc)
m_status_text->setVisible(false);
}
TouchControls::~TouchControls()
{
releaseAll();
}
void TouchControls::addButton(std::vector<button_info> &buttons, touch_gui_button_id id,
const std::string &image, const recti &rect, bool visible)
{
@ -843,6 +848,7 @@ void TouchControls::emitMouseEvent(EMOUSE_INPUT_EVENT type)
event.MouseInput.Control = false;
event.MouseInput.ButtonStates = 0;
event.MouseInput.Event = type;
event.MouseInput.Simulated = true;
m_receiver->OnEvent(event);
}

View File

@ -33,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "itemdef.h"
#include "client/game.h"
#include "util/basic_macros.h"
namespace irr
{
@ -136,6 +137,8 @@ class TouchControls
{
public:
TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc);
~TouchControls();
DISABLE_CLASS_COPY(TouchControls);
void translateEvent(const SEvent &event);
void applyContextControls(const TouchInteractionMode &mode);