/src/ois/src/iphone/iPhoneMultiTouch.mm
Objective C++ | 203 lines | 144 code | 30 blank | 29 comment | 25 complexity | c246653704ca937f507e02939ed7fe2a MD5 | raw file
1/* 2 The zlib/libpng License 3 4 Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com) 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#include "iphone/iPhoneMultiTouch.h" 24#include "iphone/iPhoneInputManager.h" 25 26using namespace OIS; 27 28//-------------------------------------------------------------------// 29iPhoneMultiTouch::iPhoneMultiTouch( InputManager* creator, bool buffered ) 30 : MultiTouch(creator->inputSystemName(), buffered, 0, creator) 31{ 32 iPhoneInputManager *man = static_cast<iPhoneInputManager*>(mCreator); 33 34 man->_setMultiTouchUsed(true); 35 [man->_getDelegate() setTouchObject:this]; 36} 37 38iPhoneMultiTouch::~iPhoneMultiTouch() 39{ 40 iPhoneInputManager *man = static_cast<iPhoneInputManager*>(mCreator); 41 42 man->_setMultiTouchUsed(false); 43 [man->_getDelegate() setTouchObject:nil]; 44} 45 46void iPhoneMultiTouch::_initialize() 47{ 48// mTempState.clear(); 49} 50 51void iPhoneMultiTouch::setBuffered( bool buffered ) 52{ 53 mBuffered = buffered; 54} 55 56void iPhoneMultiTouch::capture() 57{ 58#if 0 59 for( std::multiset<MultiTouchState *>::iterator i = mStates.begin(), e = mStates.end(); i != e; ++i ) 60 { 61 // Clear the state first 62 dynamic_cast<MultiTouchState *>(*i)->clear(); 63 64 if(mTempState.X.rel || mTempState.Y.rel || mTempState.Z.rel) 65 { 66 MultiTouchState *iState = dynamic_cast<MultiTouchState *>(*i); 67 // NSLog(@"%i %i %i", mTempState.X.rel, mTempState.Y.rel, mTempState.Z.rel); 68 69 // Set new relative motion values 70 iState->X.rel = mTempState.X.rel; 71 iState->Y.rel = mTempState.Y.rel; 72 iState->Z.rel = mTempState.Z.rel; 73 74 // Update absolute position 75 iState->X.abs += mTempState.X.rel; 76 iState->Y.abs += mTempState.Y.rel; 77 78 if(iState->X.abs > iState->width) 79 iState->X.abs = iState->width; 80 else if(iState->X.abs < 0) 81 iState->X.abs = 0; 82 83 if(iState->Y.abs > iState->height) 84 iState->Y.abs = iState->height; 85 else if(iState->Y.abs < 0) 86 iState->Y.abs = 0; 87 88 iState->Z.abs += mTempState.Z.rel; 89 90 //Fire off event 91 if(mListener && mBuffered) 92 mListener->touchMoved(MultiTouchEvent(this, *iState)); 93 } 94 } 95 96 mTempState.clear(); 97#endif 98} 99 100void iPhoneMultiTouch::_touchBegan(UITouch *touch) 101{ 102 UIView *touchView = static_cast<iPhoneInputManager*>(mCreator)->_getDelegate(); 103 CGPoint location = [touch locationInView:touchView]; 104 CGFloat contentScale = 1.0; 105#if __IPHONE_4_0 106 if([touchView respondsToSelector:@selector(contentScaleFactor)]) 107 contentScale = [touchView contentScaleFactor]; 108#endif 109 110 MultiTouchState newState; 111 newState.X.abs = location.x * contentScale; 112 newState.Y.abs = location.y * contentScale; 113 newState.touchType |= 1 << MT_Pressed; 114 115 if( mListener && mBuffered ) 116 { 117 mListener->touchPressed(MultiTouchEvent(this, newState)); 118 } 119 else 120 { 121 mStates.push_back(newState); 122 } 123} 124 125void iPhoneMultiTouch::_touchEnded(UITouch *touch) 126{ 127 UIView *touchView = static_cast<iPhoneInputManager*>(mCreator)->_getDelegate(); 128 CGPoint location = [touch locationInView:touchView]; 129 CGFloat contentScale = 1.0; 130#if __IPHONE_4_0 131 if([touchView respondsToSelector:@selector(contentScaleFactor)]) 132 contentScale = [touchView contentScaleFactor]; 133#endif 134 135 MultiTouchState newState; 136 newState.X.abs = location.x * contentScale; 137 newState.Y.abs = location.y * contentScale; 138 newState.touchType |= 1 << MT_Released; 139 140 if( mListener && mBuffered ) 141 { 142 mListener->touchReleased(MultiTouchEvent(this, newState)); 143 } 144 else 145 { 146 mStates.push_back(newState); 147 } 148} 149 150void iPhoneMultiTouch::_touchMoved(UITouch *touch) 151{ 152 UIView *touchView = static_cast<iPhoneInputManager*>(mCreator)->_getDelegate(); 153 CGPoint location = [touch locationInView:touchView]; 154 CGPoint previousLocation = [touch previousLocationInView:touchView]; 155 156 CGFloat contentScale = 1.0; 157#if __IPHONE_4_0 158 if([touchView respondsToSelector:@selector(contentScaleFactor)]) 159 contentScale = [touchView contentScaleFactor]; 160#endif 161 162 MultiTouchState newState; 163 newState.X.rel = (location.x - previousLocation.x) * contentScale; 164 newState.Y.rel = (location.y - previousLocation.y) * contentScale; 165 newState.X.abs = location.x * contentScale; 166 newState.Y.abs = location.y * contentScale; 167 newState.touchType |= 1 << MT_Moved; 168 169 if( mListener && mBuffered ) 170 { 171 mListener->touchMoved(MultiTouchEvent(this, newState)); 172 } 173 else 174 { 175 mStates.push_back(newState); 176 } 177} 178 179void iPhoneMultiTouch::_touchCancelled(UITouch *touch) 180{ 181 UIView *touchView = static_cast<iPhoneInputManager*>(mCreator)->_getDelegate(); 182 CGPoint location = [touch locationInView:touchView]; 183 184 CGFloat contentScale = 1.0; 185#if __IPHONE_4_0 186 if([touchView respondsToSelector:@selector(contentScaleFactor)]) 187 contentScale = [touchView contentScaleFactor]; 188#endif 189 190 MultiTouchState newState; 191 newState.X.abs = location.x * contentScale; 192 newState.Y.abs = location.y * contentScale; 193 newState.touchType |= 1 << MT_Cancelled; 194 195 if( mListener && mBuffered ) 196 { 197 mListener->touchCancelled(MultiTouchEvent(this, newState)); 198 } 199 else 200 { 201 mStates.push_back(newState); 202 } 203}