1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/*
* The Mana Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2010 The Mana Developers
*
* This file is part of The Mana Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "configuration.h"
#include "joystick.h"
#include "log.h"
#include <cassert>
int Joystick::joystickCount = 0;
void Joystick::init()
{
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
// Have SDL call SDL_JoystickUpdate() automatically
SDL_JoystickEventState(SDL_ENABLE);
joystickCount = SDL_NumJoysticks();
logger->log("%i joysticks/gamepads found", joystickCount);
for (int i = 0; i < joystickCount; i++)
logger->log("- %s", SDL_JoystickName(i));
}
Joystick::Joystick(int no):
mDirection(0),
mCalibrating(false),
mEnabled(false)
{
assert(no < joystickCount);
mJoystick = SDL_JoystickOpen(no);
// TODO Bail out!
if (!mJoystick)
{
logger->log("Couldn't open joystick: %s", SDL_GetError());
return;
}
logger->log("Axes: %i ", SDL_JoystickNumAxes(mJoystick));
logger->log("Balls: %i", SDL_JoystickNumBalls(mJoystick));
logger->log("Hats: %i", SDL_JoystickNumHats(mJoystick));
logger->log("Buttons: %i", SDL_JoystickNumButtons(mJoystick));
mEnabled = config.getBoolValue("joystickEnabled");
mUpTolerance = config.getIntValue("upTolerance");
mDownTolerance = config.getIntValue("downTolerance");
mLeftTolerance = config.getIntValue("leftTolerance");
mRightTolerance = config.getIntValue("rightTolerance");
}
Joystick::~Joystick()
{
SDL_JoystickClose(mJoystick);
}
void Joystick::update()
{
mDirection = 0;
// When calibrating, don't bother the outside with our state
if (mCalibrating)
{
doCalibration();
return;
};
if (!mEnabled)
return;
// X-Axis
int position = SDL_JoystickGetAxis(mJoystick, 0);
if (position >= mRightTolerance)
mDirection |= RIGHT;
else if (position <= mLeftTolerance)
mDirection |= LEFT;
// Y-Axis
position = SDL_JoystickGetAxis(mJoystick, 1);
if (position <= mUpTolerance)
mDirection |= UP;
else if (position >= mDownTolerance)
mDirection |= DOWN;
// Buttons
for (int i = 0; i < MAX_BUTTONS; i++)
mButtons[i] = (SDL_JoystickGetButton(mJoystick, i) == 1);
}
void Joystick::startCalibration()
{
mUpTolerance = 0;
mDownTolerance = 0;
mLeftTolerance = 0;
mRightTolerance = 0;
mCalibrating = true;
}
void Joystick::doCalibration()
{
// X-Axis
int position = SDL_JoystickGetAxis(mJoystick, 0);
if (position > mRightTolerance)
mRightTolerance = position;
else if (position < mLeftTolerance)
mLeftTolerance = position;
// Y-Axis
position = SDL_JoystickGetAxis(mJoystick, 1);
if (position > mDownTolerance)
mDownTolerance = position;
else if (position < mUpTolerance)
mUpTolerance = position;
}
void Joystick::finishCalibration()
{
config.setValue("leftTolerance", mLeftTolerance);
config.setValue("rightTolerance", mRightTolerance);
config.setValue("upTolerance", mUpTolerance);
config.setValue("downTolerance", mDownTolerance);
mCalibrating = false;
}
bool Joystick::buttonPressed(unsigned char no) const
{
return (mEnabled && no < MAX_BUTTONS) ? mButtons[no] : false;
}
|