GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/setup_joystick.cpp Lines: 37 77 48.1 %
Date: 2017-11-29 Branches: 52 146 35.6 %

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-2017  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
4
Setup_Joystick::Setup_Joystick(const Widget2 *const widget) :
44
    SetupTab(widget),
45
    mCalibrateLabel(new Label(this,
46
        // TRANSLATORS: joystick settings tab label
47

8
        _("Press the button to start calibration"))),
48
    // TRANSLATORS: joystick settings tab button
49

8
    mCalibrateButton(new Button(this, _("Calibrate"), "calibrate", this)),
50
    // TRANSLATORS: joystick settings tab button
51

8
    mDetectButton(new Button(this, _("Detect joysticks"), "detect", this)),
52
    // TRANSLATORS: joystick settings tab checkbox
53

8
    mJoystickEnabled(new CheckBox(this, _("Enable joystick"))),
54

4
    mNamesModel(new NamesModel),
55

4
    mNamesDropDown(new DropDown(this, mNamesModel)),
56
    // TRANSLATORS: joystick settings tab checkbox
57
4
    mUseInactiveCheckBox(new CheckBox(this, _("Use joystick if client "
58


20
        "window inactive"), config.getBoolValue("useInactiveJoystick"))),
59






168
    mOriginalJoystickEnabled(config.getBoolValue("joystickEnabled"))
60
{
61
    // TRANSLATORS: joystick settings tab name
62
20
    setName(_("Joystick"));
63
64
8
    Joystick::getNames(mNamesModel->getNames());
65
66
8
    mJoystickEnabled->setSelected(mOriginalJoystickEnabled);
67
20
    mJoystickEnabled->setActionEventId("joystick");
68
4
    mJoystickEnabled->addActionListener(this);
69
8
    mCalibrateButton->setEnabled(mOriginalJoystickEnabled);
70
71
20
    mNamesDropDown->setActionEventId("name");
72
4
    mNamesDropDown->addActionListener(this);
73
74
4
    if (joystick != nullptr)
75
    {
76
        mNamesDropDown->setSelected(joystick->getNumber());
77
    }
78
    else
79
    {
80

16
        unsigned int sel = config.getIntValue("selectedJoystick");
81
8
        if (sel >= CAST_U32(mNamesModel->size()))
82
4
            sel = 0;
83
4
        mNamesDropDown->setSelected(sel);
84
    }
85
86
    // Do the layout
87
8
    LayoutHelper h(this);
88
4
    ContainerPlacer place = h.getPlacer(0, 0);
89
90
4
    place(0, 0, mJoystickEnabled);
91

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

6
}