ManaPlus
setup_joystick.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2018 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "configuration.h"
26 
27 #include "input/joystick.h"
28 
29 #include "gui/models/namesmodel.h"
30 
31 #include "gui/widgets/button.h"
32 #include "gui/widgets/checkbox.h"
34 #include "gui/widgets/dropdown.h"
35 #include "gui/widgets/label.h"
37 
38 #include "utils/delete2.h"
39 #include "utils/gettext.h"
40 
41 #include "debug.h"
42 
44  SetupTab(widget),
45  mCalibrateLabel(new Label(this,
46  // TRANSLATORS: joystick settings tab label
47  _("Press the button to start calibration"))),
48  // TRANSLATORS: joystick settings tab button
49  mCalibrateButton(new Button(this, _("Calibrate"), "calibrate",
50  BUTTON_SKIN, this)),
51  // TRANSLATORS: joystick settings tab button
52  mDetectButton(new Button(this, _("Detect joysticks"), "detect",
53  BUTTON_SKIN, this)),
54  // TRANSLATORS: joystick settings tab checkbox
55  mJoystickEnabled(new CheckBox(this, _("Enable joystick"),
56  false, nullptr, std::string())),
57  mNamesModel(new NamesModel),
58  mNamesDropDown(new DropDown(this, mNamesModel,
59  false, Modal_false, nullptr, std::string())),
60  mUseInactiveCheckBox(new CheckBox(this,
61  // TRANSLATORS: joystick settings tab checkbox
62  _("Use joystick if client window inactive"),
63  config.getBoolValue("useInactiveJoystick"),
64  nullptr, std::string())),
65  mOriginalJoystickEnabled(config.getBoolValue("joystickEnabled"))
66 {
67  // TRANSLATORS: joystick settings tab name
68  setName(_("Joystick"));
69 
71 
76 
79 
80  if (joystick != nullptr)
81  {
83  }
84  else
85  {
86  unsigned int sel = config.getIntValue("selectedJoystick");
87  if (sel >= CAST_U32(mNamesModel->size()))
88  sel = 0;
90  }
91 
92  // Do the layout
93  LayoutHelper h(this);
94  ContainerPlacer place = h.getPlacer(0, 0);
95 
96  place(0, 0, mJoystickEnabled, 1, 1);
97  place(0, 1, mNamesDropDown, 1, 1);
98  place(0, 2, mUseInactiveCheckBox, 1, 1);
99  place(0, 3, mDetectButton, 1, 1);
100  place(0, 4, mCalibrateLabel, 1, 1);
101  place(0, 5, mCalibrateButton, 1, 1);
102 
103  setDimension(Rect(0, 0, 365, 75));
104 }
105 
107 {
109 }
110 
112 {
113  const Widget *const source = event.getSource();
114  if (source == mJoystickEnabled)
115  {
117  }
118  else if (source == mNamesDropDown)
119  {
120  if (joystick != nullptr)
122  }
123  else if (source == mDetectButton)
124  {
125  if (joystick != nullptr)
126  {
127  joystick->reload();
130  }
131  }
132  else
133  {
134  if (joystick == nullptr)
135  return;
136 
137  if (joystick->isCalibrating())
138  {
139  // TRANSLATORS: joystick settings tab button
140  mCalibrateButton->setCaption(_("Calibrate"));
142  // TRANSLATORS: joystick settings tab label
143  (_("Press the button to start calibration"));
145  }
146  else
147  {
148  // TRANSLATORS: joystick settings tab button
149  mCalibrateButton->setCaption(_("Stop"));
151  // TRANSLATORS: joystick settings tab label
152  _("Rotate the stick and don't press buttons"));
154  }
155  }
156 }
157 
158 void Setup_Joystick::setTempEnabled(const bool sel)
159 {
162  if (joystick != nullptr)
163  {
164  if (sel)
165  joystick->open();
166  else
167  joystick->close();
168  }
169 }
170 
172 {
174 
177 
179 }
180 
182 {
183  if (joystick == nullptr)
184  return;
185 
186  config.setValue("joystickEnabled", Joystick::isEnabled());
187 
188  config.setValue("useInactiveJoystick", mUseInactiveCheckBox->isSelected());
190 }
#define CAST_U32
Definition: cast.h:30
#define _(s)
Definition: gettext.h:34
CheckBox * mJoystickEnabled
Setup_Joystick(const Widget2 *const widget)
bool mOriginalJoystickEnabled
Joystick * joystick
Definition: joystick.cpp:42
static bool isEnabled()
Definition: joystick.h:80
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
void action(const ActionEvent &event)
Definition: rect.h:72
Definition: button.h:96
Button * mDetectButton
Configuration config
DropDown * mNamesDropDown
void setDimension(const Rect &dimension)
Definition: widget.cpp:168
void close()
Definition: joystick.cpp:191
int getIntValue(const std::string &key) const
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
#define delete2(var)
Definition: delete2.h:24
static void getNames(std::vector< std::string > &names)
Definition: joystick.cpp:343
void reload()
Definition: joystick.cpp:201
int getNumber() const
Definition: joystick.h:116
NamesModel * mNamesModel
#define new
Definition: debug_new.h:147
void setCaption(const std::string &caption)
Definition: button.h:213
Button * mCalibrateButton
const std::string BUTTON_SKIN
Definition: button.h:88
void setCaption(const std::string &caption)
Definition: label.cpp:261
Definition: label.h:87
bool open()
Definition: joystick.cpp:92
void startCalibration()
Definition: joystick.cpp:301
#define nullptr
Definition: localconsts.h:44
void setNumber(const int n)
Definition: joystick.cpp:207
void finishCalibration()
Definition: joystick.cpp:327
ContainerPlacer getPlacer(const int x, const int y)
void setEnabled(const bool enabled)
Definition: widget.h:351
size_t size() const
Definition: namesmodel.h:46
CheckBox * mUseInactiveCheckBox
Definition: widget.h:97
Label * mCalibrateLabel
static void setEnabled(const bool enabled)
Definition: joystick.h:85
void setName(const std::string &name)
Definition: setuptab.h:67
StringVect & getNames()
Definition: namesmodel.h:43
void setUseInactive(const bool b)
Definition: joystick.h:119
bool isCalibrating() const
Definition: joystick.h:99
void setTempEnabled(const bool sel)
void setSelected(const bool selected)
Definition: checkbox.h:155
bool isSelected() const
Definition: checkbox.h:146
void setSelected(int selected)
Definition: dropdown.cpp:514
void setValue(const std::string &key, const std::string &value)
int getSelected() const
Definition: dropdown.cpp:509