Oolite
Loading...
Searching...
No Matches
OOJoystickManager.h
Go to the documentation of this file.
1/*
2
3OOJoystickManager.h
4By Dylan Smith
5modified by Alex Smith and Jens Ayton
6
7JoystickHandler handles joystick events, and translates them
8into the appropriate action via a lookup table. The lookup table is
9stored as a simple array rather than an ObjC dictionary since this
10will be examined fairly often (once per frame during gameplay).
11
12Conversion methods are provided to convert between the internal
13representation and an NSDictionary (for loading/saving user defaults
14and for use in areas where portability/ease of coding are more important
15than performance such as the GUI)
16
17
18Oolite
19Copyright (C) 2004-2013 Giles C Williams and contributors
20
21This program is free software; you can redistribute it and/or
22modify it under the terms of the GNU General Public License
23as published by the Free Software Foundation; either version 2
24of the License, or (at your option) any later version.
25
26This program is distributed in the hope that it will be useful,
27but WITHOUT ANY WARRANTY; without even the implied warranty of
28MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29GNU General Public License for more details.
30
31You should have received a copy of the GNU General Public License
32along with this program; if not, write to the Free Software
33Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
34MA 02110-1301, USA.
35
36*/
37
38#pragma once
39
40#import "OOFoundation.h"
41
42// Enums are used here rather than a more complex ObjC object because
43// these are required very frequently (once per frame) so must be light
44// on CPU cycles (try and avoid too many objc sendmsgs).
45// Controls that can be an axis
46enum {
54#if OO_FOV_INFLIGHT_CONTROL_ENABLED
55 AXIS_FIELD_OF_VIEW,
56#endif
58};
59
60// Controls that can be a button
61enum {
83 BUTTON_ENERGYBOMB, // now fast activate B
88 BUTTON_CLOAK, // now fast activate A
98#if OO_FOV_INFLIGHT_CONTROL_ENABLED
99 BUTTON_INC_FIELD_OF_VIEW,
100 BUTTON_DEC_FIELD_OF_VIEW,
101#endif
122};
123
124// Stick constants
125#define MAX_STICKS 4
126#define MAX_AXES 16
127#define MAX_REAL_BUTTONS 64
128#define MAX_HATS 4
129#define MAX_BUTTONS (MAX_REAL_BUTTONS + 4 * MAX_HATS)
130#define STICK_NOFUNCTION -1
131#define STICK_AXISUNASSIGNED -10.0
132
133#define STICK_PRECISIONFAC 3
134#define STICK_NORMALDIV 32768
135#define STICK_PRECISIONDIV (STICK_PRECISIONFAC * STICK_NORMALDIV)
136
137#define STICK_DEADZONE 0.05
138
139#define STICK_MAX_DEADZONE (STICK_DEADZONE * 2)
140
141// Kind of stick device (these are bits - if any more are added,
142// the next one is 4 and so on).
143#define HW_AXIS 1
144#define HW_BUTTON 2
145
146// The threshold at which an axis can trigger a call back.
147// The max of abs(axis) is 32767.
148#define AXCBTHRESH 20000
149
150// Dictionary keys - used in the defaults file
151#define AXIS_SETTINGS @"JoystickAxes" // NSUserDefaults
152#define BUTTON_SETTINGS @"JoystickButs" // NSUserDefaults
153#define GUI_BUTTON_SETTINGS @"GuiJoystickButs" // NSUserDefaults
154#define STICK_ISAXIS @"isAxis" // YES=axis NO=button
155#define STICK_NUMBER @"stickNum" // Stick number 0 to 4
156#define STICK_AXBUT @"stickAxBt" // Axis or button number
157#define STICK_FUNCTION @"stickFunc" // Function of axis/button
158#define STICK_ROLL_AXIS_PROFILE_SETTING @"RollAxisProfile" // Joystick Profiles
159#define STICK_PITCH_AXIS_PROFILE_SETTING @"PitchAxisProfile" // Joystick Profiles
160#define STICK_YAW_AXIS_PROFILE_SETTING @"YawAxisProfile" // Joystick Profiles
161// shortcut to make code more readable when using enum as key for
162// an NSDictionary
163#define ENUMKEY(x) [NSString stringWithFormat:@"%d", x]
164
165#import "OOJoystickProfile.h"
166
167@interface OOJoystickManager : NSObject {
168 // Axis/button mapping arrays
169 int8_t axismap[MAX_STICKS][MAX_AXES];
170 int8_t buttonmap[MAX_STICKS][MAX_BUTTONS];
171 int8_t guibuttonmap[MAX_STICKS][MAX_BUTTONS];
172 BOOL true_butstate[MAX_STICKS][MAX_BUTTONS];
173 double axstate[AXIS_end];
174 BOOL butstate[BUTTON_end];
175 uint8_t hatstate[MAX_STICKS][MAX_HATS];
177
178 // Handle callbacks - the object, selector to call
179 // the desired function, and the hardware (axis or button etc.)
184
185@private
189}
190
191+ (id)sharedStickHandler;
192+ (BOOL)setStickHandlerClass:(Class)aClass;
193
194// General.
195// Note: handleSDLEvent returns a BOOL (YES we handled it or NO we
196// didn't) so in the future when more handler classes are written,
197// the GameView event loop can just go through an NSArray of handlers
198// until it finds a handler that handles the event.
199- (id)init;
200
201// Roll/pitch axis
202- (NSPoint)rollPitchAxis;
203
204// View axis
205- (NSPoint)viewAxis;
206
207// convert a dictionary into the internal function map
208- (void)setFunction:(int)function withDict:(NSDictionary*)stickFn;
209- (void)unsetAxisFunction:(int)function;
210- (void)unsetButtonFunction:(int)function;
211- (void)unsetGuiButtonFunction:(int)function;
212
213// Accessors and discovery about the hardware.
214// These work directly on the internal lookup table so to be fast
215// since they are likely to be called by the game loop.
216- (BOOL)isButtonDown:(int)button stick:(int)stickNum;
217- (BOOL)getButtonState:(int)function;
218- (double)getAxisState:(int)function;
219- (double)getSensitivity;
220
221// Axis profile handling
222- (void)setProfile:(OOJoystickAxisProfile*)profile forAxis:(int)axis;
223- (OOJoystickAxisProfile*)getProfileForAxis:(int)axis;
224- (void)saveProfileForAxis:(int)axis;
225- (void)loadProfileForAxis:(int)axis;
226
227// This one just returns a pointer to the entire state array to
228// allow for multiple lookups with only one objc_sendMsg
229- (const BOOL*)getAllButtonStates;
230
231// Hardware introspection.
232- (NSArray*)listSticks;
233
234// These use NSDictionary/NSArray since they are used outside the game
235// loop and are needed for loading/saving defaults.
236- (NSDictionary*)axisFunctions;
237- (NSDictionary*)buttonFunctions;
238- (NSDictionary*)guiButtonFunctions;
239
240// Set a callback for the next moved axis/pressed button. hwflags
241// is in the form HW_AXIS | HW_BUTTON (or just one of).
242- (void)setCallback:(SEL)selector
243 object:(id)obj
244 hardware:(char)hwflags;
245- (void)clearCallback;
246
247// Methods generally only used by this class.
248- (void)setDefaultMapping;
249- (void)clearMappings;
250- (void)clearStickStates;
251- (void)clearStickButtonState:(int)stickButton;
252- (void)saveStickSettings;
253- (void)loadStickSettings;
254
255// Methods that should be overridden by all subclasses
256- (NSUInteger)joystickCount;
257- (NSString*)nameOfJoystick:(NSUInteger)stickNumber;
258- (int16_t)getAxisWithStick:(NSUInteger)stickNum axis:(NSUInteger)axisNum;
259
260@end
@ AXIS_VIEWX
@ AXIS_PITCH
@ AXIS_YAW
@ AXIS_end
@ AXIS_THRUST
@ AXIS_ROLL
@ AXIS_PRECISION
@ AXIS_VIEWY
@ BUTTON_MFDSELECTNEXT
@ BUTTON_GUILEFT
@ BUTTON_DOCKINGCLEARANCE
@ BUTTON_end
@ BUTTON_LAUNCHMISSILE
@ BUTTON_GUISELECT
@ BUTTON_DOCKINGMUSIC
@ BUTTON_FUELINJECT
@ BUTTON_GUIRIGHT
@ BUTTON_ENERGYBOMB
@ BUTTON_TARGETINCOMINGMISSILE
@ BUTTON_PRIMEEQUIPMENT
@ BUTTON_COMPASSMODE
@ BUTTON_ROTATECARGO
@ BUTTON_GUIPAGEDOWN
@ BUTTON_VIEWSTARBOARD
@ BUTTON_VIEWAFT
@ BUTTON_GALACTICDRIVE
@ BUTTON_MFDCYCLENEXT
@ BUTTON_MFDCYCLEPREV
@ BUTTON_ARMMISSILE
@ BUTTON_HYPERDRIVE
@ BUTTON_ACTIVATEEQUIPMENT
@ BUTTON_CLOAK
@ BUTTON_NEXTTARGET
@ BUTTON_GUIDOWN
@ BUTTON_FIRE
@ BUTTON_UNARM
@ BUTTON_CYCLEMISSILE
@ BUTTON_VIEWPORT
@ BUTTON_SNAPSHOT
@ BUTTON_DOCKCPU
@ BUTTON_HYPERSPEED
@ BUTTON_ESCAPE
@ BUTTON_JETTISON
@ BUTTON_SCANNERUNZOOM
@ BUTTON_PRECISION
@ BUTTON_MODEEQUIPMENT
@ BUTTON_VIEWFORWARD
@ BUTTON_WEAPONSONLINETOGGLE
@ BUTTON_EXTVIEWCYCLE
@ BUTTON_COMMSLOG
@ BUTTON_INCTHRUST
@ BUTTON_SCANNERZOOM
@ BUTTON_GUIUP
@ BUTTON_ID
@ BUTTON_ECM
@ BUTTON_COMPASSMODE_PREV
@ BUTTON_TOGGLEHUD
@ BUTTON_DOCKCPUFAST
@ BUTTON_PREVTARGET
@ BUTTON_DECTHRUST
@ BUTTON_PRIMEEQUIPMENT_PREV
@ BUTTON_PAUSE
@ BUTTON_GUIPAGEUP
@ BUTTON_MFDSELECTPREV
uint8_t hatstate[MAX_STICKS][MAX_HATS]
OOJoystickAxisProfile * roll_profile
int8_t axismap[MAX_STICKS][MAX_AXES]
int8_t guibuttonmap[MAX_STICKS][MAX_BUTTONS]
double axstate[AXIS_end]
OOJoystickAxisProfile * pitch_profile
BOOL butstate[BUTTON_end]
int8_t buttonmap[MAX_STICKS][MAX_BUTTONS]
OOJoystickAxisProfile * yaw_profile
BOOL true_butstate[MAX_STICKS][MAX_BUTTONS]