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", this)),
50  // TRANSLATORS: joystick settings tab button
51  mDetectButton(new Button(this, _("Detect joysticks"), "detect", this)),
52  // TRANSLATORS: joystick settings tab checkbox
53  mJoystickEnabled(new CheckBox(this, _("Enable joystick"),
54  false, nullptr, std::string())),
55  mNamesModel(new NamesModel),
56  mNamesDropDown(new DropDown(this, mNamesModel,
57  false, Modal_false, nullptr, std::string())),
58  mUseInactiveCheckBox(new CheckBox(this,
59  // TRANSLATORS: joystick settings tab checkbox
60  _("Use joystick if client window inactive"),
61  config.getBoolValue("useInactiveJoystick"),
62  nullptr, std::string())),
63  mOriginalJoystickEnabled(config.getBoolValue("joystickEnabled"))
64 {
65  // TRANSLATORS: joystick settings tab name
66  setName(_("Joystick"));
67 
69 
74 
77 
78  if (joystick != nullptr)
79  {
81  }
82  else
83  {
84  unsigned int sel = config.getIntValue("selectedJoystick");
85  if (sel >= CAST_U32(mNamesModel->size()))
86  sel = 0;
88  }
89 
90  // Do the layout
91  LayoutHelper h(this);
92  ContainerPlacer place = h.getPlacer(0, 0);
93 
94  place(0, 0, mJoystickEnabled, 1, 1);
95  place(0, 1, mNamesDropDown, 1, 1);
96  place(0, 2, mUseInactiveCheckBox, 1, 1);
97  place(0, 3, mDetectButton, 1, 1);
98  place(0, 4, mCalibrateLabel, 1, 1);
99  place(0, 5, mCalibrateButton, 1, 1);
100 
101  setDimension(Rect(0, 0, 365, 75));
102 }
103 
105 {
107 }
108 
110 {
111  const Widget *const source = event.getSource();
112  if (source == mJoystickEnabled)
113  {
115  }
116  else if (source == mNamesDropDown)
117  {
118  if (joystick != nullptr)
120  }
121  else if (source == mDetectButton)
122  {
123  if (joystick != nullptr)
124  {
125  joystick->reload();
128  }
129  }
130  else
131  {
132  if (joystick == nullptr)
133  return;
134 
135  if (joystick->isCalibrating())
136  {
137  // TRANSLATORS: joystick settings tab button
138  mCalibrateButton->setCaption(_("Calibrate"));
140  // TRANSLATORS: joystick settings tab label
141  (_("Press the button to start calibration"));
143  }
144  else
145  {
146  // TRANSLATORS: joystick settings tab button
147  mCalibrateButton->setCaption(_("Stop"));
149  // TRANSLATORS: joystick settings tab label
150  _("Rotate the stick and don't press buttons"));
152  }
153  }
154 }
155 
156 void Setup_Joystick::setTempEnabled(const bool sel)
157 {
160  if (joystick != nullptr)
161  {
162  if (sel)
163  joystick->open();
164  else
165  joystick->close();
166  }
167 }
168 
170 {
172 
175 
177 }
178 
180 {
181  if (joystick == nullptr)
182  return;
183 
184  config.setValue("joystickEnabled", Joystick::isEnabled());
185 
186  config.setValue("useInactiveJoystick", mUseInactiveCheckBox->isSelected());
188 }
#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:94
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:214
Button * mCalibrateButton
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