From c8b615acc9817bba97626df5120c3dd2c5c1b453 Mon Sep 17 00:00:00 2001 From: grorp Date: Sun, 17 Mar 2024 14:59:50 +0100 Subject: [PATCH] Remove excessive touchscreengui.h includes (#14466) --- src/CMakeLists.txt | 1 + src/client/clientlauncher.cpp | 5 ++-- src/client/content_cso.cpp | 1 + src/client/game.cpp | 1 + src/client/inputhandler.cpp | 55 ++++++++++++++++++++++++++++++++--- src/client/inputhandler.h | 51 ++------------------------------ src/clientdynamicinfo.cpp | 55 +++++++++++++++++++++++++++++++++++ src/clientdynamicinfo.h | 32 ++------------------ 8 files changed, 115 insertions(+), 86 deletions(-) create mode 100644 src/clientdynamicinfo.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5eb6677a9..120ef277f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -368,6 +368,7 @@ set(common_SRCS ${server_SRCS} ${content_SRCS} chat.cpp + clientdynamicinfo.cpp collision.cpp content_mapnode.cpp content_nodemeta.cpp diff --git a/src/client/clientlauncher.cpp b/src/client/clientlauncher.cpp index 995381112..93ed392b9 100644 --- a/src/client/clientlauncher.cpp +++ b/src/client/clientlauncher.cpp @@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "gui/mainmenumanager.h" #include "clouds.h" +#include "gui/touchscreengui.h" #include "server.h" #include "filesys.h" #include "gui/guiMainMenu.h" @@ -210,8 +211,7 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args) break; if (g_settings->getBool("enable_touch")) { - receiver->m_touchscreengui = new TouchScreenGUI(m_rendering_engine->get_raw_device(), receiver); - g_touchscreengui = receiver->m_touchscreengui; + g_touchscreengui = new TouchScreenGUI(m_rendering_engine->get_raw_device(), receiver); } the_game( @@ -246,7 +246,6 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args) if (g_touchscreengui) { delete g_touchscreengui; g_touchscreengui = NULL; - receiver->m_touchscreengui = NULL; } /* Save the settings when leaving the game. diff --git a/src/client/content_cso.cpp b/src/client/content_cso.cpp index d6c3fd3ef..c175df72e 100644 --- a/src/client/content_cso.cpp +++ b/src/client/content_cso.cpp @@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "content_cso.h" #include +#include "client/texturesource.h" #include "clientenvironment.h" #include "client.h" #include "map.h" diff --git a/src/client/game.cpp b/src/client/game.cpp index 9459b3204..3a42f2b0c 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -40,6 +40,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "content/subgames.h" #include "client/event_manager.h" #include "fontengine.h" +#include "gui/touchscreengui.h" #include "itemdef.h" #include "log.h" #include "filesys.h" diff --git a/src/client/inputhandler.cpp b/src/client/inputhandler.cpp index 82303c455..7b2e70b54 100644 --- a/src/client/inputhandler.cpp +++ b/src/client/inputhandler.cpp @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/numeric.h" #include "inputhandler.h" #include "gui/mainmenumanager.h" +#include "gui/touchscreengui.h" #include "hud.h" void KeyCache::populate_nonchanging() @@ -102,8 +103,8 @@ bool MyEventReceiver::OnEvent(const SEvent &event) React to nothing here if a menu is active */ if (isMenuActive()) { - if (m_touchscreengui) { - m_touchscreengui->setVisible(false); + if (g_touchscreengui) { + g_touchscreengui->setVisible(false); } return g_menumgr.preprocessEvent(event); } @@ -128,9 +129,9 @@ bool MyEventReceiver::OnEvent(const SEvent &event) return true; } - } else if (m_touchscreengui && event.EventType == irr::EET_TOUCH_INPUT_EVENT) { + } else if (g_touchscreengui && event.EventType == irr::EET_TOUCH_INPUT_EVENT) { // In case of touchscreengui, we have to handle different events - m_touchscreengui->translateEvent(event); + g_touchscreengui->translateEvent(event); return true; } else if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) { @@ -195,6 +196,52 @@ bool MyEventReceiver::OnEvent(const SEvent &event) return false; } +/* + * RealInputHandler + */ +float RealInputHandler::getMovementSpeed() +{ + bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]), + b = m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]), + l = m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]), + r = m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]); + if (f || b || l || r) + { + // if contradictory keys pressed, stay still + if (f && b && l && r) + return 0.0f; + else if (f && b && !l && !r) + return 0.0f; + else if (!f && !b && l && r) + return 0.0f; + return 1.0f; // If there is a keyboard event, assume maximum speed + } + if (g_touchscreengui && g_touchscreengui->getMovementSpeed()) + return g_touchscreengui->getMovementSpeed(); + return joystick.getMovementSpeed(); +} + +float RealInputHandler::getMovementDirection() +{ + float x = 0, z = 0; + + /* Check keyboard for input */ + if (m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD])) + z += 1; + if (m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD])) + z -= 1; + if (m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT])) + x += 1; + if (m_receiver->IsKeyDown(keycache.key[KeyType::LEFT])) + x -= 1; + + if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */ + return std::atan2(x, z); + else if (g_touchscreengui && g_touchscreengui->getMovementDirection()) + return g_touchscreengui->getMovementDirection(); + return joystick.getMovementDirection(); +} + /* * RandomInputHandler */ diff --git a/src/client/inputhandler.h b/src/client/inputhandler.h index 68bd7bb03..f4fae2b0b 100644 --- a/src/client/inputhandler.h +++ b/src/client/inputhandler.h @@ -24,7 +24,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "keycode.h" #include "renderingengine.h" -#include "gui/touchscreengui.h" class InputHandler; @@ -198,15 +197,8 @@ public: keyWasReleased.clear(); } - MyEventReceiver() - { - m_touchscreengui = NULL; - } - JoystickController *joystick = nullptr; - TouchScreenGUI *m_touchscreengui; - private: s32 mouse_wheel = 0; @@ -308,48 +300,9 @@ public: return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k); } - virtual float getMovementSpeed() - { - bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]), - b = m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]), - l = m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]), - r = m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]); - if (f || b || l || r) - { - // if contradictory keys pressed, stay still - if (f && b && l && r) - return 0.0f; - else if (f && b && !l && !r) - return 0.0f; - else if (!f && !b && l && r) - return 0.0f; - return 1.0f; // If there is a keyboard event, assume maximum speed - } - if (m_receiver->m_touchscreengui && m_receiver->m_touchscreengui->getMovementSpeed()) - return m_receiver->m_touchscreengui->getMovementSpeed(); - return joystick.getMovementSpeed(); - } + virtual float getMovementSpeed(); - virtual float getMovementDirection() - { - float x = 0, z = 0; - - /* Check keyboard for input */ - if (m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD])) - z += 1; - if (m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD])) - z -= 1; - if (m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT])) - x += 1; - if (m_receiver->IsKeyDown(keycache.key[KeyType::LEFT])) - x -= 1; - - if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */ - return atan2(x, z); - else if (m_receiver->m_touchscreengui && m_receiver->m_touchscreengui->getMovementDirection()) - return m_receiver->m_touchscreengui->getMovementDirection(); - return joystick.getMovementDirection(); - } + virtual float getMovementDirection(); virtual bool cancelPressed() { diff --git a/src/clientdynamicinfo.cpp b/src/clientdynamicinfo.cpp new file mode 100644 index 000000000..a9b2a6ef3 --- /dev/null +++ b/src/clientdynamicinfo.cpp @@ -0,0 +1,55 @@ +/* +Minetest +Copyright (C) 2022-3 rubenwardy + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef SERVER + +#include "clientdynamicinfo.h" + +#include "settings.h" +#include "client/renderingengine.h" +#include "gui/touchscreengui.h" + +ClientDynamicInfo ClientDynamicInfo::getCurrent() +{ + v2u32 screen_size = RenderingEngine::getWindowSize(); + f32 density = RenderingEngine::getDisplayDensity(); + f32 gui_scaling = g_settings->getFloat("gui_scaling", 0.5f, 20.0f); + f32 hud_scaling = g_settings->getFloat("hud_scaling", 0.5f, 20.0f); + f32 real_gui_scaling = gui_scaling * density; + f32 real_hud_scaling = hud_scaling * density; + bool touch_controls = g_touchscreengui; + + return { + screen_size, real_gui_scaling, real_hud_scaling, + ClientDynamicInfo::calculateMaxFSSize(screen_size, gui_scaling), + touch_controls + }; +} + +v2f32 ClientDynamicInfo::calculateMaxFSSize(v2u32 render_target_size, f32 gui_scaling) +{ + f32 factor = (g_settings->getBool("enable_touch") ? 10 : 15) / gui_scaling; + f32 ratio = (f32)render_target_size.X / (f32)render_target_size.Y; + if (ratio < 1) + return { factor, factor / ratio }; + else + return { factor * ratio, factor }; +} + +#endif diff --git a/src/clientdynamicinfo.h b/src/clientdynamicinfo.h index bbea5d149..39faeeecc 100644 --- a/src/clientdynamicinfo.h +++ b/src/clientdynamicinfo.h @@ -20,11 +20,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #pragma once #include "irrlichttypes_bloated.h" -#ifndef SERVER -#include "settings.h" -#include "client/renderingengine.h" -#include "gui/touchscreengui.h" -#endif struct ClientDynamicInfo @@ -44,32 +39,9 @@ public: } #ifndef SERVER - static ClientDynamicInfo getCurrent() { - v2u32 screen_size = RenderingEngine::getWindowSize(); - f32 density = RenderingEngine::getDisplayDensity(); - f32 gui_scaling = g_settings->getFloat("gui_scaling", 0.5f, 20.0f); - f32 hud_scaling = g_settings->getFloat("hud_scaling", 0.5f, 20.0f); - f32 real_gui_scaling = gui_scaling * density; - f32 real_hud_scaling = hud_scaling * density; - bool touch_controls = g_touchscreengui; - - return { - screen_size, real_gui_scaling, real_hud_scaling, - ClientDynamicInfo::calculateMaxFSSize(screen_size, gui_scaling), - touch_controls - }; - } -#endif + static ClientDynamicInfo getCurrent(); private: -#ifndef SERVER - static v2f32 calculateMaxFSSize(v2u32 render_target_size, f32 gui_scaling) { - f32 factor = (g_settings->getBool("enable_touch") ? 10 : 15) / gui_scaling; - f32 ratio = (f32)render_target_size.X / (f32)render_target_size.Y; - if (ratio < 1) - return { factor, factor / ratio }; - else - return { factor * ratio, factor }; - } + static v2f32 calculateMaxFSSize(v2u32 render_target_size, f32 gui_scaling); #endif };