iPhoneMultiTouch.mm 5.12 KB
Newer Older
1
2
/*
 The zlib/libpng License
Ben Hymers's avatar
Ben Hymers committed
3

4
 Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
Ben Hymers's avatar
Ben Hymers committed
5

6
7
 This software is provided 'as-is', without any express or implied warranty. In no event will
 the authors be held liable for any damages arising from the use of this software.
Ben Hymers's avatar
Ben Hymers committed
8
9

 Permission is granted to anyone to use this software for any purpose, including commercial
10
11
 applications, and to alter it and redistribute it freely, subject to the following
 restrictions:
Ben Hymers's avatar
Ben Hymers committed
12
13
14
15

 1. The origin of this software must not be misrepresented; you must not claim that
 you wrote the original software. If you use this software in a product,
 an acknowledgment in the product documentation would be appreciated but is
16
 not required.
Ben Hymers's avatar
Ben Hymers committed
17
18

 2. Altered source versions must be plainly marked as such, and must not be
19
 misrepresented as being the original software.
Ben Hymers's avatar
Ben Hymers committed
20

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 3. This notice may not be removed or altered from any source distribution.
 */
#include "iphone/iPhoneMultiTouch.h"
#include "iphone/iPhoneInputManager.h"

using namespace OIS;

//-------------------------------------------------------------------//
iPhoneMultiTouch::iPhoneMultiTouch( InputManager* creator, bool buffered )
	: MultiTouch(creator->inputSystemName(), buffered, 0, creator)
{
	iPhoneInputManager *man = static_cast<iPhoneInputManager*>(mCreator);

    man->_setMultiTouchUsed(true);
    [man->_getDelegate() setTouchObject:this];
}

iPhoneMultiTouch::~iPhoneMultiTouch()
{
	iPhoneInputManager *man = static_cast<iPhoneInputManager*>(mCreator);
Ben Hymers's avatar
Ben Hymers committed
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    man->_setMultiTouchUsed(false);
    [man->_getDelegate() setTouchObject:nil];
}

void iPhoneMultiTouch::_initialize()
{
//    mTempState.clear();
}

void iPhoneMultiTouch::setBuffered( bool buffered )
{
	mBuffered = buffered;
}

void iPhoneMultiTouch::capture()
{
#if 0
    for( std::multiset<MultiTouchState *>::iterator i = mStates.begin(), e = mStates.end(); i != e; ++i )
    {
        // Clear the state first
        dynamic_cast<MultiTouchState *>(*i)->clear();

        if(mTempState.X.rel || mTempState.Y.rel || mTempState.Z.rel)
        {
            MultiTouchState *iState = dynamic_cast<MultiTouchState *>(*i);
    //		NSLog(@"%i %i %i", mTempState.X.rel, mTempState.Y.rel, mTempState.Z.rel);

            // Set new relative motion values
            iState->X.rel = mTempState.X.rel;
            iState->Y.rel = mTempState.Y.rel;
            iState->Z.rel = mTempState.Z.rel;
Ben Hymers's avatar
Ben Hymers committed
73

74
75
76
            // Update absolute position
            iState->X.abs += mTempState.X.rel;
            iState->Y.abs += mTempState.Y.rel;
Ben Hymers's avatar
Ben Hymers committed
77

78
79
80
81
82
83
84
85
86
            if(iState->X.abs > iState->width)
                iState->X.abs = iState->width;
            else if(iState->X.abs < 0)
                iState->X.abs = 0;

            if(iState->Y.abs > iState->height)
                iState->Y.abs = iState->height;
            else if(iState->Y.abs < 0)
                iState->Y.abs = 0;
Ben Hymers's avatar
Ben Hymers committed
87

88
            iState->Z.abs += mTempState.Z.rel;
Ben Hymers's avatar
Ben Hymers committed
89

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
            //Fire off event
            if(mListener && mBuffered)
                mListener->touchMoved(MultiTouchEvent(this, *iState));
        }
    }

	mTempState.clear();
#endif
}

void iPhoneMultiTouch::_touchBegan(UITouch *touch)
{
    CGPoint location = [touch locationInView:static_cast<iPhoneInputManager*>(mCreator)->_getDelegate()];

    MultiTouchState newState;
    newState.X.abs = location.x;
    newState.Y.abs = location.y;
    newState.touchType |= 1 << MT_Pressed;

    if( mListener && mBuffered )
    {
        mListener->touchPressed(MultiTouchEvent(this, newState));
    }
    else
    {
        mStates.push_back(newState);
    }
}

void iPhoneMultiTouch::_touchEnded(UITouch *touch)
{
    CGPoint location = [touch locationInView:static_cast<iPhoneInputManager*>(mCreator)->_getDelegate()];

    MultiTouchState newState;
    newState.X.abs = location.x;
    newState.Y.abs = location.y;
    newState.touchType |= 1 << MT_Released;

    if( mListener && mBuffered )
    {
        mListener->touchReleased(MultiTouchEvent(this, newState));
    }
    else
    {
        mStates.push_back(newState);
    }
}

void iPhoneMultiTouch::_touchMoved(UITouch *touch)
{
    CGPoint location = [touch locationInView:static_cast<iPhoneInputManager*>(mCreator)->_getDelegate()];
    CGPoint previousLocation = [touch previousLocationInView:static_cast<iPhoneInputManager*>(mCreator)->_getDelegate()];

    MultiTouchState newState;
    newState.X.rel = (location.x - previousLocation.x);
    newState.Y.rel = (location.y - previousLocation.y);
    newState.X.abs = location.x;
    newState.Y.abs = location.y;
    newState.touchType |= 1 << MT_Moved;

    if( mListener && mBuffered )
    {
        mListener->touchMoved(MultiTouchEvent(this, newState));
    }
    else
    {
        mStates.push_back(newState);
    }
}

void iPhoneMultiTouch::_touchCancelled(UITouch *touch)
{
    CGPoint location = [touch locationInView:static_cast<iPhoneInputManager*>(mCreator)->_getDelegate()];

    MultiTouchState newState;
    newState.X.abs = location.x;
    newState.Y.abs = location.y;
    newState.touchType |= 1 << MT_Cancelled;

    if( mListener && mBuffered )
    {
        mListener->touchCancelled(MultiTouchEvent(this, newState));
    }
    else
    {
        mStates.push_back(newState);
    }
}