elecman
Topic Author
Posts: 90
Joined: 20 Jul 2014, 04:28

RenderTransform read and write

17 Jul 2015, 02:19

In my xaml code I have a RenderTransform which looks like this:
<Canvas Name="g4709" RenderTransform="0,1,-1,0,868.716430664063,217.774169921875">
How do I read and write the translation and rotation values in Unity C#?

Edit:
presumably it is something like this:
UIElement pitch = (UIElement)root.FindName("g4709");
RotateTransform pitchRotateTransform = new RotateTransform();
pitchRotateTransform = (RotateTransform)pitch.RenderTransform;
float debug = pitchRotateTransform.Angle;
But I get this error:
InvalidCastException: Cannot cast from source type to destination type.
It does cast to a MatrixTransform, but that doesn't contain easy functions for getting the rotation.

Edit 2:
It seems that whether or not you can cast to RotateTransform depends on what is in the xaml file. So I guess I am stuck with using the MatrixTransform. So how do I convert the matrix to a rotation value? Maybe providing a function for that would be helpful.
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: RenderTransform read and write

17 Jul 2015, 12:36

As you found out, if you write in the xaml RenderTransform="0,1,-1,0,868.716430664063,217.774169921875", the XamlReader automatically converts that string to a MatrixTransform object.

A MatrixTransform object provides a Matrix property that gives you access to a matrix (Transform2) object with all the functionality you need:
var matrixTransform = (MatrixTransform)pitch.RenderTransform;

var m = matrixTransform.Matrix; // gets current matrix
m.RotateAt(30.0f, 5.0f, 5.0f); // adds a rotation to current matrix
m = Transform2.Rot(30.0f); // creates a new rotation matrix

matrixTransform.Matrix = m; // update matrix property    
If you don't like to work with the MatrixTransform object, you can use other alternatives:

CompositeTransform:
<Canvas Name="g4709">
  <Canvas.RenderTransform>
    <CompositeTransform
      Rotation="0"
      ScaleX="1" ScaleY="1"
      SkewX="0" SkewY="0"
      TranslateX="0" TranslateY="0"/>
  </Canvas.RenderTransform>
</Canvas> 
pitch.RenderTransform = new CompositeTransform()
{
  Rotation = 0.0f,
  ScaleX = 1.0f, ScaleY = 1.0f,
  SkewX = 0.0f, SkewY = 0.0f,
  TranslateX = 0.0f, TranslateY = 0.0f
}; 
TransformGroup with RotateTransform + ScaleTransform + ...:
<Canvas Name="g4709">
  <Canvas.RenderTransform>
    <TransformGroup>
      <RotateTransform Angle="0"/>
      <ScaleTransform ScaleX="1" ScaleY="1"/>
      <SkewTransform AngleX="0" AngleY="0"/>
      <TranslateTransform X="0" Y="0"/>
    </TransformGroup>
  </Canvas.RenderTransform>
</Canvas> 
TransformGroup group = new TransformGroup();
group.Children.Add(new RotateTransform { Angle = 0.0f });
group.Children.Add(new ScaleTransform { ScaleX = 0.0f, ScaleY = 0.0f });
group.Children.Add(new SkewTransform { SkewX = 0.0f, SkewY = 0.0f });
group.Children.Add(new TranslateTransform { X = 0.0f, Y = 0.0f });
pitch.RenderTransform = group; 
 
elecman
Topic Author
Posts: 90
Joined: 20 Jul 2014, 04:28

Re: RenderTransform read and write

17 Jul 2015, 16:00

Ah, the Transform2.Rot() function surely helps :-)

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests