Skip to content

Instantly share code, notes, and snippets.

@dejaime
Last active January 3, 2016 07:19
Show Gist options
  • Save dejaime/8428686 to your computer and use it in GitHub Desktop.
Save dejaime/8428686 to your computer and use it in GitHub Desktop.
Update() method of the GestureInterpreter class, HLGE
//Shortcut to call updateStep how many times necessary.
void GestureInterpreter::update() {
while (updateStep());
}
//Updates the Interpreter until it process all events or find a match, whatever comes first.
//May need to be called multiple times.
//Returns true on a match or false when it is empty.
bool GestureInterpreter::updateStep() {
controller->update(); // Updates the controller, allowing it to generate our cute events.
//Need to add error handling.
InputEvent* evt = controller->getNextEvent(); // Grab the next input since the last call of this function.
if (!evt) return false; // If this is null, it means there were no new inputs.
do { //Log the received input and try to match a pattern.
if ( translator->exist(evt->getKeyCode()) && eventLog.size() >= maxLogSize ) { // Check if the log is at its max size.
delete ( eventLog.front() ); // If it is, call the destructor on its oldest entry.
eventLog.erase(eventLog.begin()); // Erase its old position.
}
eventLog.push_back(evt); // Push the new event into the log.
//For every pattern, starting from the last one
for (PatternVectorIterator pIt = patternVector.end(); pIt != patternVector.begin(); /*--pIt*/) {
--pIt; //The decrement was moved inside the loop since end() is out of the boundaries.
if ( (pIt)->first->compareTail(eventLog.back()->getType(), translator->translate(eventLog.back()->getKeyCode())) ) {
//Check if the event matches the first entry of the pattern
int totalMatches;
for (totalMatches = 1; // From the last input entry
(totalMatches <= (pIt)->first->getSize() &&// Until the comparisons hit the end of the pattern
((pIt)->first->compare( (pIt)->first->getSize()-totalMatches, eventLog[eventLog.size()-totalMatches]->getType(), //While matching the input Type
translator->translate(eventLog[eventLog.size()-totalMatches]->getKeyCode()), getTimeDiff( (eventLog.size()-totalMatches))))) //As well as the keycode and time boundaries
;) {
++totalMatches; // Passed the tests, we have one more match.
}
if (totalMatches - 1 == (pIt)->first->getSize()) { //If it successfully matched all the inputs, totalMatches will be (the pattern's size + 1)
if ((pIt)->second) (pIt)->second(); //Call the respective callback function, if not null.
return true; //Returns now, to avoid multiple matching
}
}
}
evt = controller->getNextEvent(); // Get one more event from the controller, and repeat the loop.
} while (evt); // Will break as soon as there are no more events on the controller.
return false; // No matches.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment