1 /*=============================================================================
2 Blobby Volley 2
3 Copyright (C) 2006 Jonathan Sieber (jonathan_sieber@yahoo.de)
4 Copyright (C) 2006 Daniel Knobe (daniel-knobe@web.de)
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 =============================================================================*/
20
21 /* header include */
22 #include "InputDevice.h"
23
24 /* other includes */
25 #include "JoystickPool.h"
26
27 // ********************************************************************************************************
28 // Interface Definition
29 // ********************************************************************************************************
30
31 /*! \class JoystickInputDevice
32 \brief Ingame Joystick input
33 */
34 class JoystickInputDevice : public InputDevice
35 {
36 private:
37 bool getAction(const JoystickAction& action);
38
39 JoystickAction mLeftAction;
40 JoystickAction mRightAction;
41 JoystickAction mJumpAction;
42 public:
~JoystickInputDevice()43 virtual ~JoystickInputDevice() {};
44 JoystickInputDevice(JoystickAction laction, JoystickAction raction,
45 JoystickAction jaction);
46
47 virtual PlayerInputAbs transferInput();
48 };
49
50 // ********************************************************************************************************
51 // Class Implementation
52 // ********************************************************************************************************
53
54 // -------------------------------------------------------------------------------------------------
55 // Creator Function
56 // -------------------------------------------------------------------------------------------------
57
createJoystrickInput(JoystickAction left,JoystickAction right,JoystickAction jump)58 InputDevice* createJoystrickInput(JoystickAction left, JoystickAction right, JoystickAction jump)
59 {
60 return new JoystickInputDevice(left, right, jump);
61 }
62
63 // -------------------------------------------------------------------------------------------------
64 // Joystick Input Device
65 // -------------------------------------------------------------------------------------------------
66
JoystickInputDevice(JoystickAction laction,JoystickAction raction,JoystickAction jaction)67 JoystickInputDevice::JoystickInputDevice(JoystickAction laction, JoystickAction raction, JoystickAction jaction)
68 : mLeftAction(laction), mRightAction(raction), mJumpAction(jaction)
69 {
70 }
71
transferInput()72 PlayerInputAbs JoystickInputDevice::transferInput()
73 {
74 return PlayerInputAbs( getAction(mLeftAction), getAction(mRightAction), getAction(mJumpAction) );
75 }
76
getAction(const JoystickAction & action)77 bool JoystickInputDevice::getAction(const JoystickAction& action)
78 {
79 if (action.joy != 0)
80 {
81 switch (action.type)
82 {
83 case JoystickAction::AXIS:
84 if (action.number < 0)
85 {
86 if (SDL_JoystickGetAxis(action.joy,
87 -action.number - 1) < -15000)
88 return true;
89 }
90 else if (action.number > 0)
91 {
92 if (SDL_JoystickGetAxis(action.joy,
93 action.number - 1) > 15000)
94 return true;
95 }
96 break;
97
98 case JoystickAction::BUTTON:
99 if (SDL_JoystickGetButton(action.joy,
100 action.number))
101 return true;
102 break;
103 }
104 }
105 return false;
106 }
107