Successfully applied Uncle Bob’s lessons

sticker,375x360I was absolutely tired of repeating myself when making the same calculations for both X and Y, Width, and Height. So, I contacted Robert C. Martin and got the answer a few months ago. At that moment, I felt almost the same as when I solved a difficult equation. And now I have put it to work, and POCOOOCK! It shouted like a little puppy.

TRIPLE HIT!

I still don’t like to pass 3 arguments to a  method, although it’s private. Fine cinnamon ;D

public class MarginBasedScalingStrategy : ScalingStrategy
    {

        public MarginBasedScalingStrategy(FrameworkElement element) : base(element) { }

        public override void ApplyDeltaHorizontal(double delta)
        {
            Element.Margin = ApplyDeltaToThickness(delta, Element.Margin, Hook);
        }

        public override void ApplyDeltaVertical(double delta)
        {
            Element.Margin = ApplyDeltaToThickness(delta, Element.Margin.Swap(), Hook.Swap()).Swap();
        }

        private Thickness ApplyDeltaToThickness(double delta, Thickness originalMargin, Point hookPoint)
        {
            var left = originalMargin.Left;
            var right = originalMargin.Right;

            var finalLeft = left + (delta * hookPoint.X);
            var finalRight = right + (delta * (1 - hookPoint.X));

            return new Thickness(finalLeft, 0, finalRight, 0);
        }
    }

Go, go, go!

Quercus Rotundifolia Empowered Code®

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s