• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 The zlib/libpng License
3
4 Copyright (c) 2006 Chris Snyder
5
6 This software is provided 'as-is', without any express or implied warranty. In no event will
7 the authors be held liable for any damages arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose, including commercial
10 applications, and to alter it and redistribute it freely, subject to the following
11 restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not claim that
14 you wrote the original software. If you use this software in a product,
15 an acknowledgment in the product documentation would be appreciated but is
16 not required.
17
18 2. Altered source versions must be plainly marked as such, and must not be
19 misrepresented as being the original software.
20
21 3. This notice may not be removed or altered from any source distribution.
22*/
23
24#include "iphone/iPhoneInputManager.h"
25#include "iphone/iPhoneHelpers.h"
26#include "iphone/iPhoneAccelerometer.h"
27#include "iphone/iPhoneMultiTouch.h"
28#include "OISException.h"
29
30using namespace std;
31using namespace OIS;
32
33@implementation InputDelegate
34
35@synthesize touchObject;
36@synthesize accelerometerObject;
37
38- (id)init {
39    if((self = [super init])) {
40        touchObject = nil;
41        accelerometerObject = nil;
42    }
43    return self;
44}
45
46- (void)dealloc {
47    delete touchObject; touchObject = NULL;
48    delete accelerometerObject; accelerometerObject = NULL;
49
50    [super dealloc];
51}
52
53- (BOOL)canBecomeFirstResponder
54{
55    return YES;
56}
57
58#pragma mark Accelerator Event Handling
59- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
60    accelerometerObject->didAccelerate(acceleration);
61}
62
63#pragma mark Touch Event Handling
64- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
65    for(UITouch *touch in touches) {
66        touchObject->_touchEnded(touch);
67    }
68}
69
70- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
71    for(UITouch *touch in touches) {
72        touchObject->_touchMoved(touch);
73    }
74}
75
76- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
77    for(UITouch *touch in touches) {
78        touchObject->_touchCancelled(touch);
79    }
80}
81
82- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
83    for(UITouch *touch in touches) {
84        touchObject->_touchBegan(touch);
85    }
86}
87
88@end
89
90//--------------------------------------------------------------------------------//
91iPhoneInputManager::iPhoneInputManager() : InputManager("iPhone Input Manager")
92{
93    mHideMouse = true;
94	bAccelerometerUsed = bMultiTouchUsed = false;
95
96	// Setup our internal factories
97	mFactories.push_back(this);
98}
99
100//--------------------------------------------------------------------------------//
101iPhoneInputManager::~iPhoneInputManager()
102{
103    [mDelegate release]; mDelegate = nil;
104    [mWindow release]; mWindow = nil;
105}
106
107//--------------------------------------------------------------------------------//
108void iPhoneInputManager::_initialize( ParamList &paramList )
109{
110	_parseConfigSettings( paramList );
111
112    mDelegate = [[InputDelegate alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
113
114    // Set flags that we want to accept multiple finger touches and be the only one to receive touch events
115    [mDelegate setMultipleTouchEnabled:YES];
116    [mDelegate setExclusiveTouch:YES];
117    [mDelegate becomeFirstResponder];
118
119    [mWindow addSubview:mDelegate];
120}
121
122//--------------------------------------------------------------------------------//
123void iPhoneInputManager::_parseConfigSettings( ParamList &paramList )
124{
125    // Some carbon apps are running in a window, however full screen apps
126	// do not have a window, so we need to account for that too.
127	ParamList::iterator i = paramList.find("WINDOW");
128	if(i != paramList.end())
129	{
130		mWindow = (UIWindow *)strtoul(i->second.c_str(), 0, 10);
131		if(mWindow == 0)
132			mWindow = nil;
133    }
134	else
135	{
136		// else get the main active window.. user might not have access to it through some
137		// graphics libraries, if that fails then try at the application level.
138        mWindow = [[UIApplication sharedApplication] keyWindow];
139	}
140
141	if(mWindow == nil)
142		OIS_EXCEPT( E_General, "iPhoneInputManager::_parseConfigSettings >> Unable to find a window or event target" );
143}
144
145//--------------------------------------------------------------------------------//
146DeviceList iPhoneInputManager::freeDeviceList()
147{
148	DeviceList ret;
149
150	if( bAccelerometerUsed == false )
151		ret.insert(std::make_pair(OISJoyStick, mInputSystemName));
152
153	if( bMultiTouchUsed == false )
154		ret.insert(std::make_pair(OISMultiTouch, mInputSystemName));
155
156	return ret;
157}
158
159//--------------------------------------------------------------------------------//
160int iPhoneInputManager::totalDevices(Type iType)
161{
162	switch(iType)
163	{
164        case OISJoyStick: return 1;
165        case OISMultiTouch: return 1;
166        default: return 0;
167	}
168}
169
170//--------------------------------------------------------------------------------//
171int iPhoneInputManager::freeDevices(Type iType)
172{
173	switch(iType)
174	{
175        case OISJoyStick: return bAccelerometerUsed ? 0 : 1;
176        case OISMultiTouch: return bMultiTouchUsed ? 0 : 1;
177        default: return 0;
178	}
179}
180
181//--------------------------------------------------------------------------------//
182bool iPhoneInputManager::vendorExist(Type iType, const std::string & vendor)
183{
184	if( ( iType == OISMultiTouch || iType == OISJoyStick ) && vendor == mInputSystemName )
185		return true;
186
187	return false;
188}
189
190//--------------------------------------------------------------------------------//
191Object* iPhoneInputManager::createObject(InputManager* creator, Type iType, bool bufferMode,
192									  const std::string & vendor)
193{
194	Object *obj = 0;
195
196	switch(iType)
197	{
198        case OISJoyStick:
199        {
200            if( bAccelerometerUsed == false )
201                obj = new iPhoneAccelerometer(this, bufferMode);
202            break;
203        }
204        case OISMultiTouch:
205        {
206            if( bMultiTouchUsed == false )
207                obj = new iPhoneMultiTouch(this, bufferMode);
208            break;
209        }
210        default:
211            break;
212	}
213
214	if( obj == 0 )
215		OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type.");
216
217	return obj;
218}
219
220//--------------------------------------------------------------------------------//
221void iPhoneInputManager::destroyObject(Object* obj)
222{
223	delete obj;
224}
225