From 8f78d4e8913f854bc776aaed9efd52a88595821c Mon Sep 17 00:00:00 2001 From: Deve Date: Sun, 4 Mar 2018 00:08:24 +0100 Subject: [PATCH] Add a popup on first run on android, so that user can choose if accelerometer should be enabled --- data/gui/init_android.stkgui | 26 +++ sources.cmake | 2 +- src/config/user_config.hpp | 6 +- src/input/multitouch_device.cpp | 2 +- src/main.cpp | 16 ++ .../dialogs/init_android_dialog.cpp | 158 ++++++++++++++++++ .../dialogs/init_android_dialog.hpp | 48 ++++++ .../dialogs/multitouch_settings_dialog.cpp | 8 +- 8 files changed, 257 insertions(+), 9 deletions(-) create mode 100644 data/gui/init_android.stkgui create mode 100644 src/states_screens/dialogs/init_android_dialog.cpp create mode 100644 src/states_screens/dialogs/init_android_dialog.hpp diff --git a/data/gui/init_android.stkgui b/data/gui/init_android.stkgui new file mode 100644 index 000000000..c8b8cb1db --- /dev/null +++ b/data/gui/init_android.stkgui @@ -0,0 +1,26 @@ + + +
+ + + + + + + + + + + + + + + +
+
+ +
+
diff --git a/sources.cmake b/sources.cmake index d4f28ae4d..ddc029d4f 100644 --- a/sources.cmake +++ b/sources.cmake @@ -1,4 +1,4 @@ -# Modify this file to change the last-modified date when you add/remove a file. +# Modify this file to change the last-modified date when you add/remove a file. # This will then trigger a new cmake run automatically. file(GLOB_RECURSE STK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.hpp") file(GLOB_RECURSE STK_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.cpp") diff --git a/src/config/user_config.hpp b/src/config/user_config.hpp index ead7a1a73..a41b40690 100644 --- a/src/config/user_config.hpp +++ b/src/config/user_config.hpp @@ -468,10 +468,10 @@ namespace UserConfigParams &m_multitouch_group, "Draw steering wheel on right side.") ); - PARAM_PREFIX IntUserConfigParam m_multitouch_accelerometer - PARAM_DEFAULT( IntUserConfigParam(0, "multitouch_accelerometer", + PARAM_PREFIX IntUserConfigParam m_multitouch_controls + PARAM_DEFAULT( IntUserConfigParam(0, "multitouch_controls", &m_multitouch_group, - "Accelerometer mode: 0 = off, 1 = tablet, 2 = phone")); + "Multitouch mode: 0 = undefined, 1 = steering wheel, 2 = accelerometer")); PARAM_PREFIX FloatUserConfigParam m_multitouch_deadzone_center PARAM_DEFAULT( FloatUserConfigParam(0.1f, "multitouch_deadzone_center", diff --git a/src/input/multitouch_device.cpp b/src/input/multitouch_device.cpp index 9d1c8e4eb..7ce754fcf 100644 --- a/src/input/multitouch_device.cpp +++ b/src/input/multitouch_device.cpp @@ -140,7 +140,7 @@ void MultitouchDevice::addButton(MultitouchButtonType type, int x, int y, #ifdef ANDROID if (button->type == MultitouchButtonType::BUTTON_STEERING) { - if (UserConfigParams::m_multitouch_accelerometer > 0 && + if (UserConfigParams::m_multitouch_controls == 2 && !m_android_device->isAccelerometerActive()) { m_android_device->activateAccelerometer(1.0f / 30); diff --git a/src/main.cpp b/src/main.cpp index 602e6b27d..68f75a0fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -228,6 +228,7 @@ #include "states_screens/register_screen.hpp" #include "states_screens/state_manager.hpp" #include "states_screens/user_screen.hpp" +#include "states_screens/dialogs/init_android_dialog.hpp" #include "states_screens/dialogs/message_dialog.hpp" #include "tracks/arena_graph.hpp" #include "tracks/track.hpp" @@ -1668,6 +1669,21 @@ int main(int argc, char *argv[] ) } Log::warn("main", "Screen size is too small!"); } + + #ifdef ANDROID + if (UserConfigParams::m_multitouch_controls == 0) + { + int32_t touch = AConfiguration_getTouchscreen( + global_android_app->config); + + if (touch != ACONFIGURATION_TOUCHSCREEN_NOTOUCH) + { + InitAndroidDialog* init_android = new InitAndroidDialog( + 0.6f, 0.6f); + GUIEngine::DialogQueue::get()->pushDialog(init_android); + } + } + #endif if (GraphicsRestrictions::isDisabled( GraphicsRestrictions::GR_DRIVER_RECENT_ENOUGH)) diff --git a/src/states_screens/dialogs/init_android_dialog.cpp b/src/states_screens/dialogs/init_android_dialog.cpp new file mode 100644 index 000000000..d446d47dc --- /dev/null +++ b/src/states_screens/dialogs/init_android_dialog.cpp @@ -0,0 +1,158 @@ +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2014-2015 SuperTuxKart-Team +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "states_screens/dialogs/init_android_dialog.hpp" + +#include "config/user_config.hpp" +#include "graphics/irr_driver.hpp" +#include "guiengine/widgets/check_box_widget.hpp" +#include "guiengine/widgets/spinner_widget.hpp" +#include "guiengine/widgets/ribbon_widget.hpp" +#include "input/device_manager.hpp" +#include "input/input_manager.hpp" +#include "input/multitouch_device.hpp" +#include "utils/translation.hpp" + +#ifdef ANDROID +#include "../../../lib/irrlicht/source/Irrlicht/CIrrDeviceAndroid.h" +#endif + +#include + + +using namespace GUIEngine; +using namespace irr; +using namespace irr::core; +using namespace irr::gui; + +// ----------------------------------------------------------------------------- + +InitAndroidDialog::InitAndroidDialog(const float w, const float h) + : ModalDialog(w, h) +{ +} + +// ----------------------------------------------------------------------------- + +InitAndroidDialog::~InitAndroidDialog() +{ +} + +// ----------------------------------------------------------------------------- + +void InitAndroidDialog::load() +{ + loadFromFile("init_android.stkgui"); +} + +// ----------------------------------------------------------------------------- + +void InitAndroidDialog::beforeAddingWidgets() +{ + bool accelerometer_available = false; + +#ifdef ANDROID + CIrrDeviceAndroid* android_device = dynamic_cast( + irr_driver->getDevice()); + assert(android_device != NULL); + accelerometer_available = android_device->isAccelerometerAvailable(); +#endif + + if (!accelerometer_available) + { + RibbonWidget* control_type = getWidget("control_type"); + assert(control_type != NULL); + + int index = control_type->findItemNamed("accelerometer"); + Widget* accelerometer = &control_type->getChildren()[index]; + accelerometer->setActive(false); + + if (UserConfigParams::m_multitouch_controls = 2) + { + UserConfigParams::m_multitouch_controls = 1; + } + } + + updateValues(); +} + +// ----------------------------------------------------------------------------- + +GUIEngine::EventPropagation InitAndroidDialog::processEvent( + const std::string& eventSource) +{ + if (eventSource == "close") + { + RibbonWidget* control_type = getWidget("control_type"); + assert(control_type != NULL); + + const std::string& selected = control_type->getSelectionIDString( + PLAYER_ID_GAME_MASTER); + int index = control_type->getSelection(PLAYER_ID_GAME_MASTER); + Widget* selected_widget = &control_type->getChildren()[index]; + + if (!selected_widget->isActivated()) + return GUIEngine::EVENT_BLOCK; + + if (selected == "steering_wheel") + { + UserConfigParams::m_multitouch_controls = 1; + } + else if (selected == "accelerometer") + { + UserConfigParams::m_multitouch_controls = 2; + } + + user_config->saveConfig(); + + ModalDialog::dismiss(); + return GUIEngine::EVENT_BLOCK; + } + + return GUIEngine::EVENT_LET; +} // processEvent + +// ----------------------------------------------------------------------------- + +void InitAndroidDialog::updateValues() +{ + RibbonWidget* control_type = getWidget("control_type"); + assert(control_type != NULL); + + if (UserConfigParams::m_multitouch_controls == 2) + { + int id = control_type->findItemNamed("accelerometer"); + control_type->setSelection(id, PLAYER_ID_GAME_MASTER); + } + else + { + int id = control_type->findItemNamed("steering_wheel"); + control_type->setSelection(id, PLAYER_ID_GAME_MASTER); + } +} + +// ----------------------------------------------------------------------------- + +bool InitAndroidDialog::onEscapePressed() +{ + UserConfigParams::m_multitouch_controls = 1; + user_config->saveConfig(); + ModalDialog::dismiss(); + return true; +} // onEscapePressed + +// ----------------------------------------------------------------------------- diff --git a/src/states_screens/dialogs/init_android_dialog.hpp b/src/states_screens/dialogs/init_android_dialog.hpp new file mode 100644 index 000000000..0fc10cff0 --- /dev/null +++ b/src/states_screens/dialogs/init_android_dialog.hpp @@ -0,0 +1,48 @@ +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2014-2015 SuperTuxKart-Team +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifndef HEADER_INIT_ANDROID_DIALOG_HPP +#define HEADER_INIT_ANDROID_DIALOG_HPP + +#include "guiengine/modaldialog.hpp" + +/** + * \brief Dialog that allows the player to adjust multitouch steering settings + * \ingroup states_screens + */ +class InitAndroidDialog : public GUIEngine::ModalDialog +{ +private: + void updateValues(); + +public: + /** + * Creates a modal dialog with given percentage of screen width and height + */ + InitAndroidDialog(const float percentWidth, const float percentHeight); + ~InitAndroidDialog(); + + virtual void beforeAddingWidgets(); + virtual void load(); + virtual bool onEscapePressed(); + + GUIEngine::EventPropagation processEvent(const std::string& eventSource); + +}; + +#endif diff --git a/src/states_screens/dialogs/multitouch_settings_dialog.cpp b/src/states_screens/dialogs/multitouch_settings_dialog.cpp index 4f450e5ee..0fb2cbe85 100644 --- a/src/states_screens/dialogs/multitouch_settings_dialog.cpp +++ b/src/states_screens/dialogs/multitouch_settings_dialog.cpp @@ -107,8 +107,8 @@ GUIEngine::EventPropagation MultitouchSettingsDialog::processEvent( CheckBoxWidget* accelerometer = getWidget("accelerometer"); assert(accelerometer != NULL); - UserConfigParams::m_multitouch_accelerometer = accelerometer-> - getState() ? 1 : 0; + UserConfigParams::m_multitouch_controls = accelerometer-> + getState() ? 2 : 1; MultitouchDevice* touch_device = input_manager->getDeviceManager()-> getMultitouchDevice(); @@ -129,7 +129,7 @@ GUIEngine::EventPropagation MultitouchSettingsDialog::processEvent( UserConfigParams::m_multitouch_deadzone_edge.revertToDefaults(); UserConfigParams::m_multitouch_deadzone_center.revertToDefaults(); UserConfigParams::m_multitouch_mode.revertToDefaults(); - UserConfigParams::m_multitouch_accelerometer.revertToDefaults(); + UserConfigParams::m_multitouch_controls.revertToDefaults(); updateValues(); @@ -167,7 +167,7 @@ void MultitouchSettingsDialog::updateValues() CheckBoxWidget* accelerometer = getWidget("accelerometer"); assert(accelerometer != NULL); - accelerometer->setState(UserConfigParams::m_multitouch_accelerometer != 0); + accelerometer->setState(UserConfigParams::m_multitouch_controls == 2); } // -----------------------------------------------------------------------------