Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save apache2046/289f7b577d46ad722f428ef50846be18 to your computer and use it in GitHub Desktop.
Save apache2046/289f7b577d46ad722f428ef50846be18 to your computer and use it in GitHub Desktop.
uitouch to fingerid
const int MAX_TOUCHES= 11; //Oops, it can handle 11, not 10. Thanks @Bob_at_BH
class TouchTrack
{
public:
TouchTrack()
{
m_touchPointer = NULL;
}
void *m_touchPointer;
};
TouchTrack g_touchTracker[MAX_TOUCHES];
int GetFingerTrackIDByTouch(void* touch)
{
for (int i=0; i < MAX_TOUCHES; i++)
{
if (g_touchTracker[i].m_touchPointer == touch)
{
return i;
}
}
//LogMsg("Can't locate fingerID by touch %d", touch);
return -1;
}
int AddNewTouch(void* touch)
{
for (int i=0; i < MAX_TOUCHES; i++)
{
if (!g_touchTracker[i].m_touchPointer)
{
//hey, an empty slot, yay
g_touchTracker[i].m_touchPointer = touch;
return i;
}
}
LogMsg("Can't add new fingerID");
return -1;
}
int GetTouchesActive()
{
int count = 0;
for (int i=0; i < MAX_TOUCHES; i++)
{
if (g_touchTracker[i].m_touchPointer)
{
count++;
}
}
return count;
}
// Handles the start of a touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Enumerate through all the touch objects.
for (UITouch *touch in touches)
{
//found a touch. Is it already on our list?
int fingerID = GetFingerTrackIDByTouch(touch);
if (fingerID == -1)
{
//add it to our list
fingerID = AddNewTouch(touch);
} else
{
//already on the list. Don't send this
//LogMsg("Ignoring touch %d", fingerID);
continue;
}
CGPoint pt =[touch locationInView:self];
ConvertCoordinatesIfRequired(pt.x, pt.y);
GetMessageManager()->SendGUIEx(MESSAGE_TYPE_GUI_CLICK_START,pt.x, pt.y,fingerID);
}
#ifdef _DEBUG
//LogMsg("%d touches active", GetTouchesActive());
#endif
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// Enumerate through all the touch objects.
for (UITouch *touch in touches)
{
//found a touch. Is it already on our list?
int fingerID = GetFingerTrackIDByTouch(touch);
if (fingerID != -1)
{
g_touchTracker[fingerID].m_touchPointer = NULL; //clear it
} else
{
//wasn't on our list
continue;
}
CGPoint pt =[touch locationInView:self];
ConvertCoordinatesIfRequired(pt.x, pt.y);
GetMessageManager()->SendGUIEx(MESSAGE_TYPE_GUI_CLICK_END,pt.x, pt.y, fingerID);
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
// Enumerate through all the touch objects.
for (UITouch *touch in touches)
{
//found a touch. Is it already on our list?
int fingerID = GetFingerTrackIDByTouch(touch);
if (fingerID != -1)
{
g_touchTracker[fingerID].m_touchPointer = NULL; //clear it
} else
{
//wasn't on our list
continue;
}
CGPoint pt =[touch locationInView:self];
ConvertCoordinatesIfRequired(pt.x, pt.y);
GetMessageManager()->SendGUIEx(MESSAGE_TYPE_GUI_CLICK_END,pt.x, pt.y, fingerID);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Enumerate through all the touch objects.
for (UITouch *touch in touches)
{
//found a touch. Is it already on our list?
int fingerID = GetFingerTrackIDByTouch(touch);
if (fingerID != -1)
{
//found it
} else
{
//wasn't on our list?!
continue;
}
CGPoint pt =[touch locationInView:self];
ConvertCoordinatesIfRequired(pt.x, pt.y);
GetMessageManager()->SendGUIEx(MESSAGE_TYPE_GUI_CLICK_MOVE,pt.x, pt.y, fingerID);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment