1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-10-05 19:31:04 +00: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

@ -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) {

View file

@ -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
*/

View file

@ -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()
{

View file

@ -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);

View file

@ -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;
};