Ejemplo n.º 1
0
    private void PerformButtonSlide(CornerButton button, SlideDirections direction, Action postAction)
    {
        var retinaCenter = new Vector2(480, 320);
        var buttonCenter = button.Position + new Vector2((float) button.VirtualRegionOnScreen.width / 2.0f, (float) button.VirtualRegionOnScreen.height / 2.0f );

        var directingVector = buttonCenter - retinaCenter;
        directingVector.Normalize();

        var sign = direction == SlideDirections.Out ? 1 : -1;
        var targetPosition =  buttonCenter + sign * directingVector * ButtonSlideDistance;
        Debug.Log(string.Format("{0}: {1}", button.Symbol, targetPosition));

        //decentralize position
        targetPosition -= new Vector2((float)button.VirtualRegionOnScreen.width / 2.0f, (float)button.VirtualRegionOnScreen.height / 2.0f);
        if (direction == SlideDirections.In) {
            // need to deal with rounding errors
            targetPosition = GetSnapPositionForButton(button);
        }

        // set different easing functions for sliding in and out
        Func<float,float> easingFunc = null;
        if (direction == SlideDirections.Out) {
            easingFunc = (x) => x * x;
        } else {
            easingFunc = (x) => (float)(Math.Log(x) + 2.0f) / 2.0f;
        }

        var controller = new UIElementSlideController(easingFunc) {
            StartPosition = button.Position,
            TargetPosition = targetPosition,
            Duration = TimeSpan.FromMilliseconds(250)
        };

        if (postAction != null) {
            controller.ControllerFinished += (sender, e) => postAction.Invoke();
        }

        button.RemoveController(button.name);
        button.QueueController(button.name, controller);
    }