Gustavo's profilementasBlogListsSkyDrive Tools Help
    November 03

    Zoombox

    Basic implementation of "Blendables Essentials Mix" concept for Silverlight.
     
    Generic.xaml
     

    <Style TargetType="local:Zoombox">

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="local:Zoombox">

                    <Canvas x:Name="Container">

                        <ContentPresenter>

                            <ContentPresenter.RenderTransform>

                                <TransformGroup>

                                    <ScaleTransform x:Name="Zoom" />

                                    <TranslateTransform x:Name="Pan" />

                                </TransformGroup>

                            </ContentPresenter.RenderTransform>

                        </ContentPresenter>

                    </Canvas>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

     
    Zoombox.cs
     

    public class Zoombox : ContentControl

    {

        private Canvas container = null;

        private ScaleTransform zoom = null;

        private TranslateTransform pan = null;

     

        public Zoombox()

        {

            this.DefaultStyleKey = typeof(Zoombox);

     

            ZoomMargin = 10;

        }

     

        public override void OnApplyTemplate()

        {

            base.OnApplyTemplate();

     

            container = (Canvas)GetTemplateChild("Container");

            zoom = (ScaleTransform)GetTemplateChild("Zoom");

            pan = (TranslateTransform)GetTemplateChild("Pan");

        }

     

        public void ZoomTo(Rect rect)

        {

            rect.X = rect.X - ZoomMargin / 2;

            rect.Y = rect.Y - ZoomMargin / 2;

            rect.Width = rect.Width + ZoomMargin;

            rect.Height = rect.Height + ZoomMargin;

     

            double factor = Math.Min(container.ActualWidth / rect.Width, container.ActualHeight / rect.Height);

            zoom.ScaleY = zoom.ScaleX = factor;

     

            pan.X = (container.ActualWidth - rect.Width * factor) / 2 - rect.X * factor;

            pan.Y = (container.ActualHeight - rect.Height * factor) / 2 - rect.Y * factor;

        }

        public void ZoomTo(Rect rect, TimeSpan duration)

        {

            ZoomTo(rect, duration, null);

        }

        public void ZoomTo(Rect rect, TimeSpan duration, IEasingFunction func)

        {

            rect.X = rect.X - ZoomMargin / 2;

            rect.Y = rect.Y - ZoomMargin / 2;

            rect.Width = rect.Width + ZoomMargin;

            rect.Height = rect.Height + ZoomMargin;

     

            double factor = Math.Min(container.ActualWidth / rect.Width, container.ActualHeight / rect.Height);

     

            double panx = (container.ActualWidth - rect.Width * factor) / 2 - rect.X * factor;

            double pany = (container.ActualHeight - rect.Height * factor) / 2 - rect.Y * factor;

     

            Storyboard sb = new Storyboard();

     

            DoubleAnimation zoomx_anim = new DoubleAnimation() { To = factor, Duration = duration, EasingFunction = func };

            Storyboard.SetTarget(zoomx_anim, zoom);

            Storyboard.SetTargetProperty(zoomx_anim, new PropertyPath("ScaleX"));

     

            DoubleAnimation zoomy_anim = new DoubleAnimation() { To = factor, Duration = duration, EasingFunction = func };

            Storyboard.SetTarget(zoomy_anim, zoom);

            Storyboard.SetTargetProperty(zoomy_anim, new PropertyPath("ScaleY"));

     

            DoubleAnimation panx_anim = new DoubleAnimation() { To = panx, Duration = duration, EasingFunction = func };

            Storyboard.SetTarget(panx_anim, pan);

            Storyboard.SetTargetProperty(panx_anim, new PropertyPath("X"));

     

            DoubleAnimation pany_anim = new DoubleAnimation() { To = pany, Duration = duration, EasingFunction = func };

            Storyboard.SetTarget(pany_anim, pan);

            Storyboard.SetTargetProperty(pany_anim, new PropertyPath("Y"));

     

            sb.Children.Add(zoomx_anim);

            sb.Children.Add(zoomy_anim);

            sb.Children.Add(panx_anim);

            sb.Children.Add(pany_anim);

            sb.Begin();

        }

     

        public double ZoomCache

        {

            get

            {

                if (((UIElement)Content).CacheMode == null)

                    return 0;

                else

                    return ((BitmapCache)((UIElement)Content).CacheMode).RenderAtScale;

            }

            set

            {

                if (value == 0)

                    ((UIElement)Content).CacheMode = null;

                else

                    ((UIElement)Content).CacheMode = new BitmapCache() { RenderAtScale = value };

            }

        }

        public double ZoomMargin { get; set; }

    }

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.
    Gustavo Arranhado has turned off comments on this page.

    Trackbacks

    The trackback URL for this entry is:
    http://mentas.spaces.live.com/blog/cns!A8D899E9B03A6E15!787.trak
    Weblogs that reference this entry
    • None