mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-01 17:38:41 +00:00
Auto-toggle TouchControls in-game when receiving touch/mouse input
This commit is contained in:
parent
3c5f05b284
commit
4952f17df4
15 changed files with 152 additions and 89 deletions
|
@ -723,6 +723,7 @@ protected:
|
|||
void processUserInput(f32 dtime);
|
||||
void processKeyInput();
|
||||
void processItemSelection(u16 *new_playeritem);
|
||||
bool shouldShowTouchControls();
|
||||
|
||||
void dropSelectedItem(bool single_item = false);
|
||||
void openInventory();
|
||||
|
@ -1565,6 +1566,14 @@ bool Game::createClient(const GameStartData &start_data)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool Game::shouldShowTouchControls()
|
||||
{
|
||||
const std::string &touch_controls = g_settings->get("touch_controls");
|
||||
if (touch_controls == "auto")
|
||||
return RenderingEngine::getLastPointerType() == PointerType::Touch;
|
||||
return is_yes(touch_controls);
|
||||
}
|
||||
|
||||
bool Game::initGui()
|
||||
{
|
||||
m_game_ui->init();
|
||||
|
@ -1579,7 +1588,7 @@ bool Game::initGui()
|
|||
gui_chat_console = make_irr<GUIChatConsole>(guienv, guienv->getRootGUIElement(),
|
||||
-1, chat_backend, client, &g_menumgr);
|
||||
|
||||
if (g_settings->getBool("touch_controls")) {
|
||||
if (shouldShowTouchControls()) {
|
||||
g_touchcontrols = new TouchControls(device, texture_src);
|
||||
g_touchcontrols->setUseCrosshair(!isTouchCrosshairDisabled());
|
||||
}
|
||||
|
@ -2031,6 +2040,15 @@ void Game::updateStats(RunStats *stats, const FpsControl &draw_times,
|
|||
|
||||
void Game::processUserInput(f32 dtime)
|
||||
{
|
||||
bool desired = shouldShowTouchControls();
|
||||
if (desired && !g_touchcontrols) {
|
||||
g_touchcontrols = new TouchControls(device, texture_src);
|
||||
|
||||
} else if (!desired && g_touchcontrols) {
|
||||
delete g_touchcontrols;
|
||||
g_touchcontrols = nullptr;
|
||||
}
|
||||
|
||||
// Reset input if window not active or some menu is active
|
||||
if (!device->isWindowActive() || isMenuActive() || guienv->hasFocus(gui_chat_console.get())) {
|
||||
if (m_game_focused) {
|
||||
|
|
|
@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "gui/touchcontrols.h"
|
||||
#include "hud.h"
|
||||
#include "log_internal.h"
|
||||
#include "client/renderingengine.h"
|
||||
|
||||
void KeyCache::populate_nonchanging()
|
||||
{
|
||||
|
@ -142,6 +143,11 @@ bool MyEventReceiver::OnEvent(const SEvent &event)
|
|||
}
|
||||
}
|
||||
|
||||
if (event.EventType == EET_MOUSE_INPUT_EVENT && !event.MouseInput.Simulated)
|
||||
last_pointer_type = PointerType::Mouse;
|
||||
else if (event.EventType == EET_TOUCH_INPUT_EVENT)
|
||||
last_pointer_type = PointerType::Touch;
|
||||
|
||||
// Let the menu handle events, if one is active.
|
||||
if (isMenuActive()) {
|
||||
if (g_touchcontrols)
|
||||
|
@ -237,6 +243,26 @@ float RealInputHandler::getJoystickDirection()
|
|||
return joystick.getMovementDirection();
|
||||
}
|
||||
|
||||
v2s32 RealInputHandler::getMousePos()
|
||||
{
|
||||
auto control = RenderingEngine::get_raw_device()->getCursorControl();
|
||||
if (control) {
|
||||
return control->getPosition();
|
||||
}
|
||||
|
||||
return m_mousepos;
|
||||
}
|
||||
|
||||
void RealInputHandler::setMousePos(s32 x, s32 y)
|
||||
{
|
||||
auto control = RenderingEngine::get_raw_device()->getCursorControl();
|
||||
if (control) {
|
||||
control->setPosition(x, y);
|
||||
} else {
|
||||
m_mousepos = v2s32(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* RandomInputHandler
|
||||
*/
|
||||
|
|
|
@ -23,10 +23,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "joystick_controller.h"
|
||||
#include <list>
|
||||
#include "keycode.h"
|
||||
#include "renderingengine.h"
|
||||
|
||||
class InputHandler;
|
||||
|
||||
enum class PointerType {
|
||||
Mouse,
|
||||
Touch,
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
Fast key cache for main game loop
|
||||
****************************************************************************/
|
||||
|
@ -199,6 +203,8 @@ public:
|
|||
|
||||
JoystickController *joystick = nullptr;
|
||||
|
||||
PointerType getLastPointerType() { return last_pointer_type; }
|
||||
|
||||
private:
|
||||
s32 mouse_wheel = 0;
|
||||
|
||||
|
@ -223,6 +229,8 @@ private:
|
|||
|
||||
// Intentionally not reset by clearInput/releaseAllKeys.
|
||||
bool fullscreen_is_down = false;
|
||||
|
||||
PointerType last_pointer_type = PointerType::Mouse;
|
||||
};
|
||||
|
||||
class InputHandler
|
||||
|
@ -331,25 +339,8 @@ public:
|
|||
m_receiver->dontListenForKeys();
|
||||
}
|
||||
|
||||
virtual v2s32 getMousePos()
|
||||
{
|
||||
auto control = RenderingEngine::get_raw_device()->getCursorControl();
|
||||
if (control) {
|
||||
return control->getPosition();
|
||||
}
|
||||
|
||||
return m_mousepos;
|
||||
}
|
||||
|
||||
virtual void setMousePos(s32 x, s32 y)
|
||||
{
|
||||
auto control = RenderingEngine::get_raw_device()->getCursorControl();
|
||||
if (control) {
|
||||
control->setPosition(x, y);
|
||||
} else {
|
||||
m_mousepos = v2s32(x, y);
|
||||
}
|
||||
}
|
||||
virtual v2s32 getMousePos();
|
||||
virtual void setMousePos(s32 x, s32 y);
|
||||
|
||||
virtual s32 getMouseWheel()
|
||||
{
|
||||
|
|
|
@ -172,7 +172,7 @@ static irr::IrrlichtDevice *createDevice(SIrrlichtCreationParameters params, std
|
|||
|
||||
/* RenderingEngine class */
|
||||
|
||||
RenderingEngine::RenderingEngine(IEventReceiver *receiver)
|
||||
RenderingEngine::RenderingEngine(MyEventReceiver *receiver)
|
||||
{
|
||||
sanity_check(!s_singleton);
|
||||
|
||||
|
@ -225,6 +225,8 @@ RenderingEngine::RenderingEngine(IEventReceiver *receiver)
|
|||
// This changes the minimum allowed number of vertices in a VBO. Default is 500.
|
||||
driver->setMinHardwareBufferVertexCount(4);
|
||||
|
||||
m_receiver = receiver;
|
||||
|
||||
s_singleton = this;
|
||||
|
||||
g_settings->registerChangedCallback("fullscreen", settingChangedCallback, this);
|
||||
|
|
|
@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include <vector>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "client/inputhandler.h"
|
||||
#include "irrlichttypes_extrabloated.h"
|
||||
#include "debug.h"
|
||||
#include "client/shader.h"
|
||||
|
@ -82,7 +83,7 @@ class RenderingEngine
|
|||
public:
|
||||
static const video::SColor MENU_SKY_COLOR;
|
||||
|
||||
RenderingEngine(IEventReceiver *eventReceiver);
|
||||
RenderingEngine(MyEventReceiver *eventReceiver);
|
||||
~RenderingEngine();
|
||||
|
||||
void setResizable(bool resize);
|
||||
|
@ -167,6 +168,12 @@ public:
|
|||
const irr::core::dimension2d<u32> initial_screen_size,
|
||||
const bool initial_window_maximized);
|
||||
|
||||
static PointerType getLastPointerType()
|
||||
{
|
||||
sanity_check(s_singleton && s_singleton->m_receiver);
|
||||
return s_singleton->m_receiver->getLastPointerType();
|
||||
}
|
||||
|
||||
private:
|
||||
static void settingChangedCallback(const std::string &name, void *data);
|
||||
v2u32 _getWindowSize() const;
|
||||
|
@ -174,5 +181,6 @@ private:
|
|||
std::unique_ptr<RenderingCore> core;
|
||||
irr::IrrlichtDevice *m_device = nullptr;
|
||||
irr::video::IVideoDriver *driver;
|
||||
MyEventReceiver *m_receiver = nullptr;
|
||||
static RenderingEngine *s_singleton;
|
||||
};
|
||||
|
|
|
@ -97,7 +97,10 @@ void set_default_settings()
|
|||
// Client
|
||||
settings->setDefault("address", "");
|
||||
settings->setDefault("enable_sound", "true");
|
||||
settings->setDefault("touch_controls", bool_to_cstr(has_touch));
|
||||
settings->setDefault("touch_controls", "auto");
|
||||
// Since GUI scaling shouldn't suddenly change during a session, we use
|
||||
// hardware detection for "touch_gui" instead of switching based on the last
|
||||
// input method used.
|
||||
settings->setDefault("touch_gui", bool_to_cstr(has_touch));
|
||||
settings->setDefault("sound_volume", "0.8");
|
||||
settings->setDefault("sound_volume_unfocused", "0.3");
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue