1
/*
2
 *  The Mana World
3
 *  Copyright (C) 2004  The Mana World Development Team
4
 *
5
 *  This file is part of The Mana World.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21
22
#ifndef JOYSTICK_H
23
#define JOYSTICK_H
24
25
#include <SDL.h>
26
27
class Joystick
28
{
29
    public:
30
        /**
31
         * Number of buttons we can handle.
32
         */
33
        enum
34
        {
35
            MAX_BUTTONS = 6
36
        };
37
38
        /**
39
         * Directions, to be used as bitmask values.
40
         */
41
        enum
42
        {
43
            UP    = 1,
44
            DOWN  = 2,
45
            LEFT  = 4,
46
            RIGHT = 8
47
        };
48
49
        /**
50
         * Initializes the joystick subsystem.
51
         */
52
        static void init();
53
54
        /**
55
         * Returns the number of available joysticks.
56
         */
57
        static int getNumberOfJoysticks() { return joystickCount; }
58
59
        /**
60
         * Constructor, pass the number of the joystick the new object
61
         * should access.
62
         */
63
        Joystick(int no);
64
65
        ~Joystick();
66
67
        bool isEnabled() const { return mEnabled; }
68
69
        void setEnabled(bool enabled) { mEnabled = enabled; }
70
71
        /**
72
         * Updates the direction and button information.
73
         */
74
        void update();
75
76
        void startCalibration();
77
78
        void finishCalibration();
79
80
        bool isCalibrating() const { return mCalibrating; }
81
82
        bool buttonPressed(unsigned char no) const;
83
84
        bool isUp() const { return mEnabled && (mDirection & UP); };
85
        bool isDown() const { return mEnabled && (mDirection & DOWN); };
86
        bool isLeft() const { return mEnabled && (mDirection & LEFT); };
87
        bool isRight() const { return mEnabled && (mDirection & RIGHT); };
88
89
    protected:
90
        unsigned char mDirection;
91
        bool mButtons[MAX_BUTTONS];
92
        SDL_Joystick *mJoystick;
93
94
        int mUpTolerance, mDownTolerance, mLeftTolerance, mRightTolerance;
95
        bool mCalibrating;
96
        bool mEnabled;
97
98
        static int joystickCount;
99
100
        void doCalibration();
101
};
102
103
#endif // JOYSTICK_H