GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/setup_joystick.cpp Lines: 42 82 51.2 %
Date: 2018-11-12 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-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
23
#include "gui/widgets/tabs/setup_joystick.h"
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"
33
#include "gui/widgets/containerplacer.h"
34
#include "gui/widgets/dropdown.h"
35
#include "gui/widgets/label.h"
36
#include "gui/widgets/layouthelper.h"
37
38
#include "utils/delete2.h"
39
#include "utils/gettext.h"
40
41
#include "debug.h"
42
43
2
Setup_Joystick::Setup_Joystick(const Widget2 *const widget) :
44
    SetupTab(widget),
45
    mCalibrateLabel(new Label(this,
46
        // TRANSLATORS: joystick settings tab label
47

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

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

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

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

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

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

8
        config.getBoolValue("useInactiveJoystick"),
64

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




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

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

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

3
}