summaryrefslogtreecommitdiff
path: root/src/gui/setup_joystick.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2011-11-09 02:51:52 +0300
committerAndrei Karas <akaras@inbox.ru>2011-11-09 03:02:42 +0300
commit274737b8d9b46dfcf6fc696123e869bbf6adaaf6 (patch)
tree08bfff598179dd6083a38599811054f50e6a4e97 /src/gui/setup_joystick.cpp
parent0260dcea2e9fbd06ba2ee166862d089dc72516f4 (diff)
downloadmv-274737b8d9b46dfcf6fc696123e869bbf6adaaf6.tar.gz
mv-274737b8d9b46dfcf6fc696123e869bbf6adaaf6.tar.bz2
mv-274737b8d9b46dfcf6fc696123e869bbf6adaaf6.tar.xz
mv-274737b8d9b46dfcf6fc696123e869bbf6adaaf6.zip
Add support for joystick selection.
Fix enable/disable joystick support. Increase max joystick buttons number to 64. Prevent joystick usage without calibration.
Diffstat (limited to 'src/gui/setup_joystick.cpp')
-rw-r--r--src/gui/setup_joystick.cpp104
1 files changed, 91 insertions, 13 deletions
diff --git a/src/gui/setup_joystick.cpp b/src/gui/setup_joystick.cpp
index aadfc11e3..a1f3c8cea 100644
--- a/src/gui/setup_joystick.cpp
+++ b/src/gui/setup_joystick.cpp
@@ -24,54 +24,114 @@
#include "configuration.h"
#include "joystick.h"
+#include "logger.h"
#include "gui/widgets/button.h"
#include "gui/widgets/checkbox.h"
+#include "gui/widgets/dropdown.h"
#include "gui/widgets/label.h"
#include "gui/widgets/layouthelper.h"
#include "utils/gettext.h"
+#include <guichan/listmodel.hpp>
+
#include "debug.h"
extern Joystick *joystick;
+class NamesModel : public gcn::ListModel
+{
+ public:
+ NamesModel()
+ { }
+
+ virtual ~NamesModel()
+ { }
+
+ virtual int getNumberOfElements()
+ {
+ return static_cast<int>(mNames.size());
+ }
+
+ virtual std::string getElementAt(int i)
+ {
+ if (i >= getNumberOfElements() || i < 0)
+ return _("???");
+
+ return mNames[i];
+ }
+
+ std::vector<std::string> mNames;
+};
+
+
Setup_Joystick::Setup_Joystick():
mCalibrateLabel(new Label(_("Press the button to start calibration"))),
mCalibrateButton(new Button(_("Calibrate"), "calibrate", this)),
- mJoystickEnabled(new CheckBox(_("Enable joystick")))
+ mJoystickEnabled(new CheckBox(_("Enable joystick"))),
+ mNamesModel(new NamesModel())
{
setName(_("Joystick"));
- mOriginalJoystickEnabled = !config.getBoolValue("joystickEnabled");
- mJoystickEnabled->setSelected(mOriginalJoystickEnabled);
+ Joystick::getNames(mNamesModel->mNames);
+ mOriginalJoystickEnabled = config.getBoolValue("joystickEnabled");
+ mJoystickEnabled->setSelected(mOriginalJoystickEnabled);
+ mJoystickEnabled->setActionEventId("joystick");
mJoystickEnabled->addActionListener(this);
+ mCalibrateButton->setEnabled(mOriginalJoystickEnabled);
+
+ mNamesDropDown = new DropDown(mNamesModel);
+ mNamesDropDown->setActionEventId("name");
+ mNamesDropDown->addActionListener(this);
+
+ if (joystick)
+ {
+ mNamesDropDown->setSelected(joystick->getNumber());
+ }
+ else
+ {
+ unsigned sel = config.getIntValue("selectedJoystick");
+ if (sel >= mNamesModel->mNames.size())
+ sel = 0;
+ mNamesDropDown->setSelected(sel);
+ }
// Do the layout
LayoutHelper h(this);
ContainerPlacer place = h.getPlacer(0, 0);
place(0, 0, mJoystickEnabled);
- place(0, 1, mCalibrateLabel);
- place.getCell().matchColWidth(0, 0);
- place = h.getPlacer(0, 1);
- place(0, 0, mCalibrateButton);
+ place(0, 1, mNamesDropDown);
+ place(0, 2, mCalibrateLabel);
+ place(0, 3, mCalibrateButton);
setDimension(gcn::Rectangle(0, 0, 365, 75));
}
-void Setup_Joystick::action(const gcn::ActionEvent &event)
+Setup_Joystick::~Setup_Joystick()
{
- if (!joystick)
- return;
+ delete mNamesModel;
+ mNamesModel = nullptr;
+}
+void Setup_Joystick::action(const gcn::ActionEvent &event)
+{
if (event.getSource() == mJoystickEnabled)
{
- joystick->setEnabled(mJoystickEnabled->isSelected());
+ setTempEnabled(mJoystickEnabled->isSelected());
+ }
+ else if (event.getSource() == mNamesDropDown)
+ {
+ if (joystick)
+ joystick->setNumber(mNamesDropDown->getSelected());
}
else
{
+ if (!joystick)
+ return;
+
if (joystick->isCalibrating())
{
mCalibrateButton->setCaption(_("Calibrate"));
@@ -88,17 +148,35 @@ void Setup_Joystick::action(const gcn::ActionEvent &event)
}
}
+void Setup_Joystick::setTempEnabled(bool sel)
+{
+ Joystick::setEnabled(sel);
+ mCalibrateButton->setEnabled(sel);
+ if (joystick)
+ {
+ if (sel)
+ joystick->open();
+ else
+ joystick->close();
+ }
+}
+
void Setup_Joystick::cancel()
{
if (joystick)
joystick->setEnabled(mOriginalJoystickEnabled);
+ if (mOriginalJoystickEnabled != mJoystickEnabled->isSelected())
+ setTempEnabled(mOriginalJoystickEnabled);
+
mJoystickEnabled->setSelected(mOriginalJoystickEnabled);
}
void Setup_Joystick::apply()
{
+ if (!joystick)
+ return;
+
config.setValue("joystickEnabled",
- joystick ? joystick->isEnabled() : false);
+ joystick ? joystick->isEnabled() : false);
}
-