GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/setup_joystick.cpp Lines: 42 82 51.2 %
Date: 2021-03-17 Branches: 49 128 38.3 %

Line Branch Exec Source
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
24
#include "gui/widgets/tabs/setup_joystick.h"
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"
34
#include "gui/widgets/containerplacer.h"
35
#include "gui/widgets/dropdown.h"
36
#include "gui/widgets/label.h"
37
#include "gui/widgets/layouthelper.h"
38
39
#include "utils/delete2.h"
40
#include "utils/gettext.h"
41
42
#include "debug.h"
43
44
2
Setup_Joystick::Setup_Joystick(const Widget2 *const widget) :
45
    SetupTab(widget),
46
    mCalibrateLabel(new Label(this,
47
        // TRANSLATORS: joystick settings tab label
48

4
        _("Press the button to start calibration"))),
49
    // TRANSLATORS: joystick settings tab button
50
2
    mCalibrateButton(new Button(this, _("Calibrate"), "calibrate",
51

2
        BUTTON_SKIN, this)),
52
    // TRANSLATORS: joystick settings tab button
53
2
    mDetectButton(new Button(this, _("Detect joysticks"), "detect",
54

2
        BUTTON_SKIN, this)),
55
    // TRANSLATORS: joystick settings tab checkbox
56
2
    mJoystickEnabled(new CheckBox(this, _("Enable joystick"),
57

4
        false, nullptr, std::string())),
58

2
    mNamesModel(new NamesModel),
59
2
    mNamesDropDown(new DropDown(this, mNamesModel,
60

6
        false, Modal_false, nullptr, std::string())),
61
    mUseInactiveCheckBox(new CheckBox(this,
62
        // TRANSLATORS: joystick settings tab checkbox
63
2
        _("Use joystick if client window inactive"),
64

8
        config.getBoolValue("useInactiveJoystick"),
65

4
        nullptr, std::string())),
66




62
    mOriginalJoystickEnabled(config.getBoolValue("joystickEnabled"))
67
{
68
    // TRANSLATORS: joystick settings tab name
69
6
    setName(_("Joystick"));
70
71
4
    Joystick::getNames(mNamesModel->getNames());
72
73
4
    mJoystickEnabled->setSelected(mOriginalJoystickEnabled);
74
8
    mJoystickEnabled->setActionEventId("joystick");
75
2
    mJoystickEnabled->addActionListener(this);
76
4
    mCalibrateButton->setEnabled(mOriginalJoystickEnabled);
77
78
8
    mNamesDropDown->setActionEventId("name");
79
2
    mNamesDropDown->addActionListener(this);
80
81
2
    if (joystick != nullptr)
82
    {
83
        mNamesDropDown->setSelected(joystick->getNumber());
84
    }
85
    else
86
    {
87

6
        unsigned int sel = config.getIntValue("selectedJoystick");
88
4
        if (sel >= CAST_U32(mNamesModel->size()))
89
2
            sel = 0;
90
2
        mNamesDropDown->setSelected(sel);
91
    }
92
93
    // Do the layout
94
4
    LayoutHelper h(this);
95
2
    ContainerPlacer place = h.getPlacer(0, 0);
96
97
2
    place(0, 0, mJoystickEnabled, 1, 1);
98

2
    place(0, 1, mNamesDropDown, 1, 1);
99
2
    place(0, 2, mUseInactiveCheckBox, 1, 1);
100
2
    place(0, 3, mDetectButton, 1, 1);
101
2
    place(0, 4, mCalibrateLabel, 1, 1);
102
2
    place(0, 5, mCalibrateButton, 1, 1);
103
104
2
    setDimension(Rect(0, 0, 365, 75));
105
2
}
106
107
6
Setup_Joystick::~Setup_Joystick()
108
{
109
2
    delete2(mNamesModel)
110
4
}
111
112
void Setup_Joystick::action(const ActionEvent &event)
113
{
114
    const Widget *const source = event.getSource();
115
    if (source == mJoystickEnabled)
116
    {
117
        setTempEnabled(mJoystickEnabled->isSelected());
118
    }
119
    else if (source == mNamesDropDown)
120
    {
121
        if (joystick != nullptr)
122
            joystick->setNumber(mNamesDropDown->getSelected());
123
    }
124
    else if (source == mDetectButton)
125
    {
126
        if (joystick != nullptr)
127
        {
128
            joystick->reload();
129
            Joystick::getNames(mNamesModel->getNames());
130
            mNamesDropDown->setSelected(joystick->getNumber());
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"));
142
            mCalibrateLabel->setCaption
143
                // TRANSLATORS: joystick settings tab label
144
                (_("Press the button to start calibration"));
145
            joystick->finishCalibration();
146
        }
147
        else
148
        {
149
            // TRANSLATORS: joystick settings tab button
150
            mCalibrateButton->setCaption(_("Stop"));
151
            mCalibrateLabel->setCaption(
152
                // TRANSLATORS: joystick settings tab label
153
                _("Rotate the stick and don't press buttons"));
154
            joystick->startCalibration();
155
        }
156
    }
157
}
158
159
void Setup_Joystick::setTempEnabled(const bool sel)
160
{
161
    Joystick::setEnabled(sel);
162
    mCalibrateButton->setEnabled(sel);
163
    if (joystick != nullptr)
164
    {
165
        if (sel)
166
            joystick->open();
167
        else
168
            joystick->close();
169
    }
170
}
171
172
void Setup_Joystick::cancel()
173
{
174
    Joystick::setEnabled(mOriginalJoystickEnabled);
175
176
    if (mOriginalJoystickEnabled != mJoystickEnabled->isSelected())
177
        setTempEnabled(mOriginalJoystickEnabled);
178
179
    mJoystickEnabled->setSelected(mOriginalJoystickEnabled);
180
}
181
182
void Setup_Joystick::apply()
183
{
184
    if (joystick == nullptr)
185
        return;
186
187
    config.setValue("joystickEnabled", Joystick::isEnabled());
188
189
    config.setValue("useInactiveJoystick", mUseInactiveCheckBox->isSelected());
190
    joystick->setUseInactive(mUseInactiveCheckBox->isSelected());
191

3
}