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