Activate accelerometer only if needed
This commit is contained in:
parent
20850c9811
commit
11b2409fab
@ -1140,8 +1140,8 @@ EventPropagation InputManager::input(const SEvent& event)
|
|||||||
// Simulate touch event on non-android devices
|
// Simulate touch event on non-android devices
|
||||||
#if !defined(ANDROID)
|
#if !defined(ANDROID)
|
||||||
MultitouchDevice* device = m_device_manager->getMultitouchDevice();
|
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))
|
type == EMIE_LMOUSE_LEFT_UP || type == EMIE_MOUSE_MOVED))
|
||||||
{
|
{
|
||||||
device->m_events[0].id = 0;
|
device->m_events[0].id = 0;
|
||||||
@ -1178,13 +1178,13 @@ EventPropagation InputManager::input(const SEvent& event)
|
|||||||
else if (event.EventType == EET_ACCELEROMETER_EVENT)
|
else if (event.EventType == EET_ACCELEROMETER_EVENT)
|
||||||
{
|
{
|
||||||
MultitouchDevice* device = m_device_manager->getMultitouchDevice();
|
MultitouchDevice* device = m_device_manager->getMultitouchDevice();
|
||||||
|
|
||||||
if (device)
|
if (device && device->isAccelerometerActive())
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < device->getButtonsCount(); i++)
|
for (unsigned int i = 0; i < device->getButtonsCount(); i++)
|
||||||
{
|
{
|
||||||
MultitouchButton* button = device->getButton(i);
|
MultitouchButton* button = device->getButton(i);
|
||||||
|
|
||||||
if (button->type != BUTTON_STEERING)
|
if (button->type != BUTTON_STEERING)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -1196,7 +1196,7 @@ EventPropagation InputManager::input(const SEvent& event)
|
|||||||
else if (UserConfigParams::m_multitouch_accelerometer == 2)
|
else if (UserConfigParams::m_multitouch_accelerometer == 2)
|
||||||
{
|
{
|
||||||
button->axis_x = (float)event.AccelerometerEvent.Y / 5.0f;
|
button->axis_x = (float)event.AccelerometerEvent.Y / 5.0f;
|
||||||
device->handleControls(button);
|
device->handleControls(button);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,7 @@ MultitouchDevice::MultitouchDevice()
|
|||||||
m_type = DT_MULTITOUCH;
|
m_type = DT_MULTITOUCH;
|
||||||
m_name = "Multitouch";
|
m_name = "Multitouch";
|
||||||
m_player = NULL;
|
m_player = NULL;
|
||||||
|
m_accelerometer_active = false;
|
||||||
#ifdef ANDROID
|
#ifdef ANDROID
|
||||||
m_android_device = dynamic_cast<CIrrDeviceAndroid*>(
|
m_android_device = dynamic_cast<CIrrDeviceAndroid*>(
|
||||||
irr_driver->getDevice());
|
irr_driver->getDevice());
|
||||||
@ -58,13 +59,6 @@ MultitouchDevice::MultitouchDevice()
|
|||||||
*/
|
*/
|
||||||
MultitouchDevice::~MultitouchDevice()
|
MultitouchDevice::~MultitouchDevice()
|
||||||
{
|
{
|
||||||
#ifdef ANDROID
|
|
||||||
if (m_android_device->isAccelerometerActive())
|
|
||||||
{
|
|
||||||
m_android_device->deactivateAccelerometer();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
clearButtons();
|
clearButtons();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,6 +143,23 @@ void MultitouchDevice::addButton(MultitouchButtonType type, int x, int y,
|
|||||||
}
|
}
|
||||||
|
|
||||||
m_buttons.push_back(button);
|
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
|
} // addButton
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@ -156,6 +167,15 @@ void MultitouchDevice::addButton(MultitouchButtonType type, int x, int y,
|
|||||||
*/
|
*/
|
||||||
void MultitouchDevice::clearButtons()
|
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)
|
for (MultitouchButton* button : m_buttons)
|
||||||
{
|
{
|
||||||
delete button;
|
delete button;
|
||||||
@ -217,7 +237,7 @@ void MultitouchDevice::updateDeviceState(unsigned int event_id)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prev_button_state != button->pressed)
|
if (prev_button_state != button->pressed)
|
||||||
{
|
{
|
||||||
update_controls = true;
|
update_controls = true;
|
||||||
@ -241,25 +261,6 @@ void MultitouchDevice::updateConfigParams()
|
|||||||
|
|
||||||
m_deadzone_edge = UserConfigParams::m_multitouch_deadzone_edge;
|
m_deadzone_edge = UserConfigParams::m_multitouch_deadzone_edge;
|
||||||
m_deadzone_edge = std::min(std::max(m_deadzone_edge, 0.0f), 0.5f);
|
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
|
} // updateConfigParams
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@ -270,10 +271,10 @@ float MultitouchDevice::getSteeringFactor(float value)
|
|||||||
{
|
{
|
||||||
if (m_deadzone_edge + m_deadzone_center >= 1.0f)
|
if (m_deadzone_edge + m_deadzone_center >= 1.0f)
|
||||||
return 1.0f;
|
return 1.0f;
|
||||||
|
|
||||||
assert(m_deadzone_edge + m_deadzone_center != 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);
|
m_deadzone_center), 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,14 +284,14 @@ float MultitouchDevice::getSteeringFactor(float value)
|
|||||||
* \param x A value from 0 to 1
|
* \param x A value from 0 to 1
|
||||||
* \param y 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)
|
float y)
|
||||||
{
|
{
|
||||||
if (UserConfigParams::m_multitouch_accelerometer == 0)
|
if (m_accelerometer_active == false)
|
||||||
{
|
{
|
||||||
button->axis_x = x;
|
button->axis_x = x;
|
||||||
}
|
}
|
||||||
|
|
||||||
button->axis_y = y;
|
button->axis_y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,11 +82,14 @@ private:
|
|||||||
* at the edge of button */
|
* at the edge of button */
|
||||||
float m_deadzone_edge;
|
float m_deadzone_edge;
|
||||||
|
|
||||||
|
/** True if accelerometer is in use */
|
||||||
|
bool m_accelerometer_active;
|
||||||
|
|
||||||
#ifdef ANDROID
|
#ifdef ANDROID
|
||||||
/** Pointer to the Android irrlicht device */
|
/** Pointer to the Android irrlicht device */
|
||||||
CIrrDeviceAndroid* m_android_device;
|
CIrrDeviceAndroid* m_android_device;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
float getSteeringFactor(float value);
|
float getSteeringFactor(float value);
|
||||||
void updateButtonAxes(MultitouchButton* button, float x, float y);
|
void updateButtonAxes(MultitouchButton* button, float x, float y);
|
||||||
|
|
||||||
@ -115,9 +118,12 @@ public:
|
|||||||
/** Returns pointer to the selected button */
|
/** Returns pointer to the selected button */
|
||||||
MultitouchButton* getButton(unsigned int i) {return m_buttons.at(i);}
|
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 updateDeviceState(unsigned int event_id);
|
||||||
void handleControls(MultitouchButton* button);
|
void handleControls(MultitouchButton* button);
|
||||||
|
|
||||||
void updateConfigParams();
|
void updateConfigParams();
|
||||||
|
|
||||||
}; // MultitouchDevice
|
}; // MultitouchDevice
|
||||||
|
Loading…
x
Reference in New Issue
Block a user