diff --git a/src/input/input_manager.cpp b/src/input/input_manager.cpp index 9594ca1ef..0e83b0af4 100644 --- a/src/input/input_manager.cpp +++ b/src/input/input_manager.cpp @@ -1140,8 +1140,8 @@ EventPropagation InputManager::input(const SEvent& event) // Simulate touch event on non-android devices #if !defined(ANDROID) MultitouchDevice* device = m_device_manager->getMultitouchDevice(); - - if (device != NULL && (type == EMIE_LMOUSE_PRESSED_DOWN || + + if (device != NULL && (type == EMIE_LMOUSE_PRESSED_DOWN || type == EMIE_LMOUSE_LEFT_UP || type == EMIE_MOUSE_MOVED)) { device->m_events[0].id = 0; @@ -1178,13 +1178,13 @@ EventPropagation InputManager::input(const SEvent& event) else if (event.EventType == EET_ACCELEROMETER_EVENT) { MultitouchDevice* device = m_device_manager->getMultitouchDevice(); - - if (device) + + if (device && device->isAccelerometerActive()) { for (unsigned int i = 0; i < device->getButtonsCount(); i++) { MultitouchButton* button = device->getButton(i); - + if (button->type != BUTTON_STEERING) continue; @@ -1196,7 +1196,7 @@ EventPropagation InputManager::input(const SEvent& event) else if (UserConfigParams::m_multitouch_accelerometer == 2) { button->axis_x = (float)event.AccelerometerEvent.Y / 5.0f; - device->handleControls(button); + device->handleControls(button); } } } diff --git a/src/input/multitouch_device.cpp b/src/input/multitouch_device.cpp index 7dc2db7af..e73bfefd5 100644 --- a/src/input/multitouch_device.cpp +++ b/src/input/multitouch_device.cpp @@ -36,6 +36,7 @@ MultitouchDevice::MultitouchDevice() m_type = DT_MULTITOUCH; m_name = "Multitouch"; m_player = NULL; + m_accelerometer_active = false; #ifdef ANDROID m_android_device = dynamic_cast( irr_driver->getDevice()); @@ -58,13 +59,6 @@ MultitouchDevice::MultitouchDevice() */ MultitouchDevice::~MultitouchDevice() { -#ifdef ANDROID - if (m_android_device->isAccelerometerActive()) - { - m_android_device->deactivateAccelerometer(); - } -#endif - clearButtons(); } @@ -149,6 +143,23 @@ void MultitouchDevice::addButton(MultitouchButtonType type, int x, int y, } m_buttons.push_back(button); + +#ifdef ANDROID + if (button->type == MultitouchButtonType::BUTTON_STEERING) + { + if (UserConfigParams::m_multitouch_accelerometer > 0 && + !m_android_device->isAccelerometerActive()) + { + m_android_device->activateAccelerometer(1.0f / 30); + + if (m_android_device->isAccelerometerActive()) + { + m_accelerometer_active = true; + } + } + + } +#endif } // addButton // ---------------------------------------------------------------------------- @@ -156,6 +167,15 @@ void MultitouchDevice::addButton(MultitouchButtonType type, int x, int y, */ void MultitouchDevice::clearButtons() { +#ifdef ANDROID + if (m_accelerometer_active == true && + m_android_device->isAccelerometerActive()) + { + m_android_device->deactivateAccelerometer(); + m_accelerometer_active = false; + } +#endif + for (MultitouchButton* button : m_buttons) { delete button; @@ -217,7 +237,7 @@ void MultitouchDevice::updateDeviceState(unsigned int event_id) } } } - + if (prev_button_state != button->pressed) { update_controls = true; @@ -241,25 +261,6 @@ void MultitouchDevice::updateConfigParams() m_deadzone_edge = UserConfigParams::m_multitouch_deadzone_edge; m_deadzone_edge = std::min(std::max(m_deadzone_edge, 0.0f), 0.5f); - -#ifdef ANDROID - if (UserConfigParams::m_multitouch_accelerometer > 0 && - !m_android_device->isAccelerometerActive()) - { - m_android_device->activateAccelerometer(1.0f / 30); - - // Disable accelerometer if it couldn't be activated - if (!m_android_device->isAccelerometerActive()) - { - UserConfigParams::m_multitouch_accelerometer = 0; - } - } - else if (UserConfigParams::m_multitouch_accelerometer == 0 && - m_android_device->isAccelerometerActive()) - { - m_android_device->deactivateAccelerometer(); - } -#endif } // updateConfigParams // ---------------------------------------------------------------------------- @@ -270,10 +271,10 @@ float MultitouchDevice::getSteeringFactor(float value) { if (m_deadzone_edge + m_deadzone_center >= 1.0f) return 1.0f; - + assert(m_deadzone_edge + m_deadzone_center != 1.0f); - - return std::min((value - m_deadzone_center) / (1.0f - m_deadzone_edge - + + return std::min((value - m_deadzone_center) / (1.0f - m_deadzone_edge - m_deadzone_center), 1.0f); } @@ -283,14 +284,14 @@ float MultitouchDevice::getSteeringFactor(float value) * \param x A value from 0 to 1 * \param y A value from 0 to 1 */ -void MultitouchDevice::updateButtonAxes(MultitouchButton* button, float x, +void MultitouchDevice::updateButtonAxes(MultitouchButton* button, float x, float y) { - if (UserConfigParams::m_multitouch_accelerometer == 0) + if (m_accelerometer_active == false) { button->axis_x = x; } - + button->axis_y = y; } diff --git a/src/input/multitouch_device.hpp b/src/input/multitouch_device.hpp index 63f0376f8..47c644f22 100644 --- a/src/input/multitouch_device.hpp +++ b/src/input/multitouch_device.hpp @@ -82,11 +82,14 @@ private: * at the edge of button */ float m_deadzone_edge; + /** True if accelerometer is in use */ + bool m_accelerometer_active; + #ifdef ANDROID /** Pointer to the Android irrlicht device */ CIrrDeviceAndroid* m_android_device; #endif - + float getSteeringFactor(float value); void updateButtonAxes(MultitouchButton* button, float x, float y); @@ -115,9 +118,12 @@ public: /** Returns pointer to the selected button */ MultitouchButton* getButton(unsigned int i) {return m_buttons.at(i);} + /** True if accelerometer is in use */ + bool isAccelerometerActive() {return m_accelerometer_active;} + void updateDeviceState(unsigned int event_id); void handleControls(MultitouchButton* button); - + void updateConfigParams(); }; // MultitouchDevice