/// <summary>
 /// Initializes a new instance of the <see cref="KSPictureLoginLib.KSPictureLoginController"/> class.
 /// </summary>
 /// <param name="factory">Factory class used to create specific instances of objects. A default factory is used if this is NULL.</param>
 public KSPictureLoginController(IKSPictureLoginFactory factory = null)
     : base()
 {
     this.gestureCollection = new KSPictureLoginGestureCollection ();
     if(factory == null)
     {
         factory = new KSPictureLoginDefaultFactory ();
     }
     this.Factory = factory;
 }
        public bool VerifyGestures(KSPictureLoginGestureCollection drawnGestures, KSPictureLoginGestureCollection storedGestures, RectangleF drawingViewRect)
        {
            // TODO: Show a spinner. Maybe using the progress HUD from the component store?
            if (storedGestures == null || drawnGestures.Count != storedGestures.Count)
            {
                KSPictureLoginGlobal.Log ("Cannot verify. Number of drawn gestures does not match number of gestures to verify.");
                return false;
            }

            bool allGesturesMatched = true;

            // Loop all recorded gestures. These are the ones the user has just drawn and these
            // need to be compared to the ones we got passed as GesturesToVerify.
            for (int verifyIndex = 0; verifyIndex < drawnGestures.Count; verifyIndex++)
            {
                // Compare the first drawn gesture against the first stored, then the 2nd and so on.
                // The user has to draw as many gestures as there are stored in order to pass verfication.
                var verifyGesture = storedGestures [verifyIndex];
                var storedGesture = drawnGestures[verifyIndex];

                // If the type of the drawn gesture does not match the one to compare to: fail.
                if(verifyGesture == null || storedGesture == null || (verifyGesture.GetType() != storedGesture.GetType()))
                {
                    allGesturesMatched = false;
                    break;
                }

                bool matched = false;
                if (verifyGesture is KSPictureLoginPathGesture)
                {
                    matched = this.VerifyPathGestures ((KSPictureLoginPathGesture)verifyGesture, (KSPictureLoginPathGesture)storedGesture, drawingViewRect);
                }
                else if(verifyGesture is KSPictureLoginTapGesture)
                {
                    matched = this.VerifyTapGestures ((KSPictureLoginTapGesture)verifyGesture, (KSPictureLoginTapGesture)storedGesture, drawingViewRect);
                }
                else
                {
                    KSPictureLoginGlobal.Log ("Unknown gesture type '{0}' cannot be verified.", verifyGesture);
                    allGesturesMatched = false;
                    break;
                }

                if (!matched)
                {
                    allGesturesMatched = false;
                    break;
                }
            }

            KSPictureLoginGlobal.Log ("Gesture collection verfication result: {0}", allGesturesMatched);
            return allGesturesMatched;
        }
 /// <summary>
 /// Clears the gestures recorded so far. Triggers the user's callback if registered.
 /// </summary>
 public void ClearGestures()
 {
     this.gestureCollection = new KSPictureLoginGestureCollection ();
     this.currentPathGesture = null;
     this.PictureLoginView.DrawingAreaView.ClearGestures ();
 }
        /// <summary>
        /// Visualizes the gestures.
        /// </summary>
        /// <param name="coll">gesture collection to visualize</param>
        /// <param name="debug">If set to <c>true</c>, visualize for debugging.</param>
        public virtual void VisualizeGestures(KSPictureLoginGestureCollection coll, bool debug)
        {
            if(coll == null)
            {
                return;
            }

            // Translate gesture paths to new resolution.
            foreach(var gesture in coll)
            {
                if(gesture is KSPictureLoginTapGesture)
                {
                    var location = ((KSPictureLoginTapGesture)gesture).Location.ScaleLocation(this.PictureLoginView.DrawingAreaView.Bounds.Size);
                    this.PictureLoginView.DrawingAreaView.AddTapGesture(location, debug);
                }
                else if(gesture is KSPictureLoginPathGesture)
                {
                    var path = ((KSPictureLoginPathGesture)gesture).Path;
                    var location = path[0].ScaleLocation(this.PictureLoginView.DrawingAreaView.Bounds.Size);
                    this.PictureLoginView.DrawingAreaView.StartPathGesture (location, debug);
                    if(path.Count > 1)
                    {
                        for(int i = 1; i < path.Count; i++)
                        {
                            location = path[i].ScaleLocation(this.PictureLoginView.DrawingAreaView.Bounds.Size);
                            this.PictureLoginView.DrawingAreaView.ContinuePathGesture (location, debug);
                        }
                    }
                }
            }
            this.PictureLoginView.SetNeedsDisplay ();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="KSPictureLoginLib.KSPictureLoginController"/> class.
 /// </summary>
 /// <param name="GesturesToVerify">the gesture collection to init the controller with. Use this constructor if you want to verify a recorded gesture.</param>
 /// <param name="image">Image to show</param>
 /// <param name="factory">Factory class used to create specific instances of objects. A default factory is used if this is NULL.</param>
 public KSPictureLoginController(KSPictureLoginGestureCollection GesturesToVerify, UIImage image, IKSPictureLoginFactory factory = null)
     : this(image, factory)
 {
     this.GesturesToVerify = GesturesToVerify;
 }