summaryrefslogtreecommitdiff
path: root/src/joystick.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2012-04-10 02:51:44 +0300
committerAndrei Karas <akaras@inbox.ru>2012-04-10 02:56:05 +0300
commit2556c72eea6a7d28d543d991e5d5c519726028f0 (patch)
tree111cf3f26eaf3a5734ec1e22d3985897feb6cd04 /src/joystick.cpp
parent8f3be5cd0544af07c6cb65bef7a1f3ba1a3704bb (diff)
downloadplus-2556c72eea6a7d28d543d991e5d5c519726028f0.tar.gz
plus-2556c72eea6a7d28d543d991e5d5c519726028f0.tar.bz2
plus-2556c72eea6a7d28d543d991e5d5c519726028f0.tar.xz
plus-2556c72eea6a7d28d543d991e5d5c519726028f0.zip
Add joystick key assign ability in keyboard dialog.
Diffstat (limited to 'src/joystick.cpp')
-rw-r--r--src/joystick.cpp59
1 files changed, 51 insertions, 8 deletions
diff --git a/src/joystick.cpp b/src/joystick.cpp
index 035232e41..2bd8b582a 100644
--- a/src/joystick.cpp
+++ b/src/joystick.cpp
@@ -24,6 +24,7 @@
#include "client.h"
#include "configuration.h"
+#include "inputmanager.h"
#include "logger.h"
#include "debug.h"
@@ -69,7 +70,7 @@ Joystick::Joystick(int no):
mNumber = no;
for (int i = 0; i < MAX_BUTTONS; i++)
- mButtons[i] = false;
+ mActiveButtons[i] = false;
}
Joystick::~Joystick()
@@ -137,10 +138,8 @@ void Joystick::setNumber(int n)
}
}
-void Joystick::update()
+void Joystick::logic()
{
- mDirection = 0;
-
// When calibrating, don't bother the outside with our state
if (mCalibrating)
{
@@ -151,6 +150,8 @@ void Joystick::update()
if (!mEnabled || !mCalibrated)
return;
+ mDirection = 0;
+
if (mUseInactive || Client::getInputFocused())
{
// X-Axis
@@ -193,9 +194,10 @@ void Joystick::update()
// Buttons
for (int i = 0; i < mButtonsNumber; i++)
{
- mButtons[i] = (SDL_JoystickGetButton(mJoystick, i) == 1);
+ const bool state = (SDL_JoystickGetButton(mJoystick, i) == 1);
+ mActiveButtons[i] = state;
#ifdef DEBUG_JOYSTICK
- if (mButtons[i])
+ if (mActiveButtons[i])
logger->log("button: %d", i);
#endif
}
@@ -203,7 +205,7 @@ void Joystick::update()
else
{
for (int i = 0; i < mButtonsNumber; i++)
- mButtons[i] = false;
+ mActiveButtons[i] = false;
}
}
@@ -246,7 +248,7 @@ void Joystick::finishCalibration()
bool Joystick::buttonPressed(unsigned char no) const
{
- return (mEnabled && no < MAX_BUTTONS) ? mButtons[no] : false;
+ return (mEnabled && no < MAX_BUTTONS) ? mActiveButtons[no] : false;
}
void Joystick::getNames(std::vector <std::string> &names)
@@ -255,3 +257,44 @@ void Joystick::getNames(std::vector <std::string> &names)
for (int i = 0; i < joystickCount; i++)
names.push_back(SDL_JoystickName(i));
}
+
+void Joystick::update()
+{
+ inputManager.updateKeyActionMap(mKeyToAction, INPUT_JOYSTICK);
+}
+
+KeysVector *Joystick::getActionVector(const SDL_Event &event)
+{
+ const int i = getButtonFromEvent(event);
+
+ if (i < 0 || i >= mButtonsNumber)
+ return nullptr;
+// logger->log("button triggerAction: %d", i);
+ if (mKeyToAction.find(i) != mKeyToAction.end())
+ return &mKeyToAction[i];
+ return nullptr;
+}
+
+int Joystick::getButtonFromEvent(const SDL_Event &event)
+{
+ if (event.jbutton.which != mNumber)
+ return -1;
+ return event.jbutton.button;
+}
+
+bool Joystick::isActionActive(int index) const
+{
+ const KeyFunction &key = inputManager.getKey(index);
+ for (size_t i = 0; i < KeyFunctionSize; i ++)
+ {
+ if (key.values[i].type != INPUT_JOYSTICK)
+ continue;
+ const int value = key.values[i].value;
+ if (value >= 0 && value < mButtonsNumber)
+ {
+ if (mActiveButtons[value])
+ return true;
+ }
+ }
+ return false;
+}