// Runs update logic void GestureApp::update() { // Update frame rate mFrameRate = getAverageFps(); // Toggle fullscreen if ( mFullScreen != isFullScreen() ) { setFullScreen( mFullScreen ); } // Update device if ( mLeap && mLeap->isConnected() ) { mLeap->update(); } const vector<Leap::Gesture>& gestures = mFrame.getGestures(); for ( vector<Leap::Gesture>::const_iterator iter = gestures.begin(); iter != gestures.end(); ++iter ) { Gesture::Type type = iter->type(); if ( type == Gesture::Type::TYPE_CIRCLE ) { // Cast to circle gesture const Leap::CircleGesture& gesture = (Leap::CircleGesture)*iter; // Control dial mDialBrightness = 1.0f; mDialValueDest = gesture.progress(); } else if ( type == Gesture::Type::TYPE_KEY_TAP ) { // Cast to circle gesture and read data const Leap::KeyTapGesture& gesture = (Leap::KeyTapGesture)*iter; Vec2f center = warpVector( fromLeapVector( gesture.position() ) ); center -= mOffset; // Press key for ( vector<Key>::iterator iter = mKeys.begin(); iter != mKeys.end(); ++iter ) { if ( iter->mBounds.contains( center ) ) { iter->mBrightness = 1.0f; break; } } } else if ( type == Gesture::Type::TYPE_SCREEN_TAP ) { // Turn background white for screen tap mBackgroundBrightness = 1.0f; } else if ( type == Gesture::Type::TYPE_SWIPE ) { // Cast to swipe gesture and read data const Leap::SwipeGesture& gesture = (Leap::SwipeGesture)*iter; ci::Vec2f a = warpVector( fromLeapVector( gesture.startPosition() ) ); ci::Vec2f b = warpVector( fromLeapVector( gesture.position() ) ); // Update swipe position mSwipeBrightness = 1.0f; if ( gesture.state() == Gesture::State::STATE_STOP ) { mSwipePosDest = b.x < a.x ? 0.0f : 1.0f; } else { float step = mSwipeStep; mSwipePosDest += b.x < a.x ? -step : step; } mSwipePosDest = math<float>::clamp( mSwipePosDest, 0.0f, 1.0f ); } } // UI animation mDialValue = lerp( mDialValue, mDialValueDest, mDialSpeed ); mSwipePos = lerp( mSwipePos, mSwipePosDest, mSwipePosSpeed ); mBackgroundBrightness *= mFadeSpeed; mDialBrightness *= mFadeSpeed; mSwipeBrightness *= mFadeSpeed; for ( vector<Key>::iterator iter = mKeys.begin(); iter != mKeys.end(); ++iter ) { iter->mBrightness *= mFadeSpeed; } }
// Draw active gestures with dotted lines void GestureApp::drawGestures() { gl::color( ColorAf::white() ); // Iterate through gestures const vector<Leap::Gesture>& gestures = mFrame.getGestures(); for ( vector<Leap::Gesture>::const_iterator iter = gestures.begin(); iter != gestures.end(); ++iter ) { Gesture::Type type = iter->type(); if ( type == Gesture::Type::TYPE_CIRCLE ) { // Cast to circle gesture and read data const Leap::CircleGesture& gesture = (Leap::CircleGesture)*iter; Vec2f pos = warpVector( fromLeapVector( gesture.center() ) ); float progress = gesture.progress(); float radius = gesture.radius() * 2.0f; // Don't ask, it works drawDottedCircle( pos, radius, mDotRadius, mCircleResolution, progress ); } else if ( type == Gesture::Type::TYPE_KEY_TAP ) { // Cast to circle gesture and read data const Leap::KeyTapGesture& gesture = (Leap::KeyTapGesture)*iter; Vec2f center = warpVector( fromLeapVector( gesture.position() ) ); // Draw square where key press happened Vec2f size( 30.0f, 30.0f ); drawDottedRect( center, size ); } else if ( type == Gesture::Type::TYPE_SCREEN_TAP ) { // Draw big square on center of screen Vec2f center = getWindowCenter(); Vec2f size( 300.0f, 300.0f ); drawDottedRect( center, size ); } else if ( type == Gesture::Type::TYPE_SWIPE ) { // Cast to swipe gesture and read data const Leap::SwipeGesture& gesture = (Leap::SwipeGesture)*iter; ci::Vec2f a = warpVector( fromLeapVector( gesture.startPosition() ) ); ci::Vec2f b = warpVector( fromLeapVector( gesture.position() ) ); // Set draw direction float spacing = mDotRadius * 3.0f; float direction = 1.0f; if ( b.x < a.x ) { direction *= -1.0f; swap( a, b ); } // Draw swipe line Vec2f pos = a; while ( pos.x <= b.x ) { pos.x += spacing; gl::drawSolidCircle( pos, mDotRadius, 32 ); } // Draw arrow head if ( direction > 0.0f ) { pos = b; spacing *= -1.0f; } else { pos = a; pos.x += spacing; } pos.y = a.y; pos.x += spacing; gl::drawSolidCircle( pos + Vec2f( 0.0f, spacing ), mDotRadius, 32 ); gl::drawSolidCircle( pos + Vec2f( 0.0f, spacing * -1.0f ), mDotRadius, 32 ); pos.x += spacing; gl::drawSolidCircle( pos + Vec2f( 0.0f, spacing * 2.0f ), mDotRadius, 32 ); gl::drawSolidCircle( pos + Vec2f( 0.0f, spacing * -2.0f ), mDotRadius, 32 ); } } }