There is a better way to flatten (merge) multiple transforms into a single transform than I explained in Flattening a TransformGroup. This better way involves matrix math – but luckily, we do not have to really know anything about matrix math to use it.
Posts in this series:
Read Flattening a TransformGroup for a painful introduction to this topic.
Here is the scenario: The
RenderTransform of your window is bound to a
TransformGroup contains two transformations:
_previousTransformations – represents all the user’s previous manipulations.
_currentTransformation – represents the user’s current manipulation.
As the user starts to manipulate your window, your program responds by placing those manipulations into
_currentTransformation. The resulting
GroupTransformation perfectly represents the sum of all of the user’s manipulations. This works well even if the center of rotation is different from previous centers of rotation. Once the user completed the manipulation, nothing else needs to be done.
However, before starting yet another manipulation, you will need to collapse (or combine) the two transformations into
_previousTransformations, then reset
_currentTransformation to be ready to receive the new manipulation.
Here is the code that performs the combining and reset at the start of the next manipulation:
void OnManipulationStarted(object sender, ManipulationStartedRoutedEventArgs args)
_previousTransformations.Matrix = TransformGroup.Value;
_currentTransformation.CenterX = args.Position.X;
_currentTransformation.CenterY = args.Position.Y;
args.Handled = true;
TransformGroup.Value is the matrix that represents the sum total of both
_currentTransformation. Overwriting the matrix of
_previousTransformations with this sum total is the magic step. Of course, you still need to
reset _currentTransformation and you are done.
Read PanView - The Design for more information.