Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove excessive touchscreengui.h includes #14466

Merged
merged 4 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ set(common_SRCS
${server_SRCS}
${content_SRCS}
chat.cpp
clientdynamicinfo.cpp
collision.cpp
content_mapnode.cpp
content_nodemeta.cpp
Expand Down
5 changes: 2 additions & 3 deletions src/client/clientlauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/client/content_cso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "content_cso.h"
#include <IBillboardSceneNode.h>
#include "client/texturesource.h"
#include "clientenvironment.h"
#include "client.h"
#include "map.h"
Expand Down
1 change: 1 addition & 0 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
55 changes: 51 additions & 4 deletions src/client/inputhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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
*/
Expand Down
51 changes: 2 additions & 49 deletions src/client/inputhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <list>
#include "keycode.h"
#include "renderingengine.h"
#include "gui/touchscreengui.h"

class InputHandler;

Expand Down Expand Up @@ -198,15 +197,8 @@ class MyEventReceiver : public IEventReceiver
keyWasReleased.clear();
}

MyEventReceiver()
{
m_touchscreengui = NULL;
}

JoystickController *joystick = nullptr;

TouchScreenGUI *m_touchscreengui;

private:
s32 mouse_wheel = 0;

Expand Down Expand Up @@ -308,48 +300,9 @@ class RealInputHandler : public InputHandler
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 getMovementDirection()
{
float x = 0, z = 0;
virtual float getMovementSpeed();

/* 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()
{
Expand Down
55 changes: 55 additions & 0 deletions src/clientdynamicinfo.cpp
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it looks bad that the whole content of the clientdynamicinfo.cpp is inside #ifndef SERVER, but since clientdynamicinfo.h is also used on the server, I can't just move both files into the client folder.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Minetest
Copyright (C) 2022-3 rubenwardy <rw@rubenwardy.com>

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
32 changes: 2 additions & 30 deletions src/clientdynamicinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,32 +39,9 @@ struct ClientDynamicInfo
}

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