Welcome Guest, you are in: Login

Daisley-Harrison Software

RSS RSS

Navigation




Search the wiki
»

Silverlight Point Translation Problem

RSS
Modified on 2009/10/23 14:45 by aarondh Categorized as Uncategorized
Translating client points to parent points in Silverlight 2 has a problem (The problem may still exist in Silverlight 3):

When I do rotation like this:

        private ScaleTransform m_scaleTransform;
        private RotateTransform m_angleTransform;

        private void initializeTransforms()
        {
            m_angleTransform = new RotateTransform();
            m_scaleTransform = new ScaleTransform();
            TransformGroup transform = new TransformGroup();
            transform.Children.Add(m_scaleTransform);
            transform.Children.Add(m_angleTransform);
            this.RenderTransform = transform;
            updateTransforms();
        }
        private void updateTransforms()
        {
            m_angleTransform.Angle = this.NoteAngle;
            m_angleTransform.CenterX = this.PinX;
            m_angleTransform.CenterY = this.PinY;
            m_scaleTransform.CenterX = this.PinX;
            m_scaleTransform.CenterY = this.PinY;
            switch (this.StemType)
            {
                case StemTypes.Up:
                    m_scaleTransform.ScaleX = 1.0;
                    m_scaleTransform.ScaleY = -1.0;
                    break;
                case StemTypes.Down:
                    m_scaleTransform.ScaleX = 1.0;
                    m_scaleTransform.ScaleY = 1.0;
                    break;
            }
        }

The code works just fine and correctly updates the rotation and scaling transforms for display purposes. However the standard mechanizm for translating point from ciient coordinates to parent coordinates (below) does NOT work in this case.

    Point pointInClientSpace = new Point(someClientX, someClientY);
    GeneralTransform transformFromClientToParent = clientControl.TransformToVisual(parentControl);
    Point pointInParentSpace = transformFromClientToParent( pointInClientSpace );
                    

The problem appears to be something internal to the client rendering pipeline that is not be recognized as changed when a child transform of the group transform is updated.

The solution to the problem is to create a new group transform EACH time the child transforms are updated and re-apply it to the clients RenderTransform property. If this is done then it appears the the client rendering pipeline correctly recognizes that the transform has changed... Then you can go ahead and use the TransformToVisual and everything works as it should!

The example below is an adjustment to the updateTransforms() function that corrects this problem:

        private ScaleTransform m_scaleTransform;
        private RotateTransform m_angleTransform;

        private void initializeTransforms()
        {
            m_angleTransform = new RotateTransform();
            m_scaleTransform = new ScaleTransform();
            updateTransforms();
        }
        private void updateTransforms()
        {
            m_angleTransform.Angle = this.NoteAngle;
            m_angleTransform.CenterX = this.PinX;
            m_angleTransform.CenterY = this.PinY;
            m_scaleTransform.CenterX = this.PinX;
            m_scaleTransform.CenterY = this.PinY;
            switch (this.StemType)
            {
                case StemTypes.Up:
                    m_scaleTransform.ScaleX = 1.0;
                    m_scaleTransform.ScaleY = -1.0;
                    break;
                case StemTypes.Down:
                    m_scaleTransform.ScaleX = 1.0;
                    m_scaleTransform.ScaleY = 1.0;
                    break;
            }
            TransformGroup transform = new TransformGroup();
            transform.Children.Add(m_scaleTransform);
            transform.Children.Add(m_angleTransform);
            this.RenderTransform = transform;
        }







Please visit our blog at http://blog.daisley-harrison.com

- All Content © Copyright 2010 Daisley-Harrison Software - All Rights Reserved. - ScrewTurn Wiki version 3.0.1.400.