Gestures

In addition to direct access to mouse/touch input, Chilli Source also has a Gesture API. Tap, hold, drag, pinch and rotation gestures are all provided. Most of these have multi-pointer variants, i.e a two pointer tap. The gesture API is also easy to extend if further gesture types are required.

To use gestures they must first be added to the Gesture System. The Gesture System is a State System so it much be acquired from the relevant State.

CSInput::TapGestureSPtr tapGesture = std::make_shared();
CSInput::GestureSystem* gestureSystem = in_state->GetSystem();
gestureSystem->AddGesture(g_tapGesture);

Once added to the gesture system listening to gesture events is similar to listening to pointer events.

m_tapConnection = tapGesture->GetTappedEvent().OpenConnection([](const CSInput::TapGesture* in_gesture, const CSCore::Vector2& in_position)
{
   CS_LOG_VERBOSE("Tapped!");
});

Once a gesture is finished with it should be removed from the gesture system.

gestureSystem->RemoveGesture(tapGesture.get());

When working with multiple gestures it is often required that two different gestures cannot fire at the same time. This is managed through gesture conflict resolution. By default all gesture conflicts are allowed. To change this a conflict resolution delegate should be set on the Gesture System. The following ensures a pinch gesture always takes precedence over a drag gesture but allows all other conflicts.

gestureSystem->SetConflictResolutionDelegate([](const CSInput::Gesture* in_previous, const CSInput::Gesture* in_new) -> CSInput::GestureSystem::ConflictResult
{
 if (in_previous->IsA() == true && in_new->IsA() == true)
 {
  return CSInput::GestureSystem::ConflictResult::k_newGesture;
 }
  
 if (in_previous->IsA() == true && in_new->IsA() == true)
 {
  return CSInput::GestureSystem::ConflictResult::k_existingGesture;
 }
  
 return CSInput::GestureSystem::ConflictResult::k_bothGestures;
});