Exemplo n.º 1
0
 public void AddTransformation(Transform tx)
 {
     var tg = RenderTransform as TransformGroup;
     if (tg == null)
     {
         tg = new TransformGroup();
         RenderTransform = tg;
     }
     // Don't add duplicates
     if (tg.Children.Any(child => child.GetType() == tx.GetType()))
     {
         return;
     }
     tg.Children.Add(tx);
     RenderTransform = tg;
 }
 private void SerializeTransform(Transform ict, StringBuilder sb)
 {
     if (ict.GetType() == typeof(RotateTransform))
     {
         sb.Append("<RotateTransform><RotateTransform.Angle>");
         var be = BindingOperations.GetBindingExpression(ict, RotateTransform.AngleProperty);
         if (be != null)
         {
             var bpd = new BoundPropertyDesigner();
             bpd.Initialize(ict, ict.GetType().GetProperty("Angle"));
             sb.Append(bpd.GetValue());
         }
         else
             sb.Append((ict as RotateTransform).Angle);
         sb.Append("</RotateTransform.Angle></RotateTransform>");
     }
     else if (ict is TranslateTransform)
     {
         sb.Append("<TranslateTransform><TranslateTransform.X>");
         var be = BindingOperations.GetBindingExpression(ict, TranslateTransform.XProperty);
         if (be != null)
         {
             var bpd = new BoundPropertyDesigner();
             bpd.Initialize(ict, ict.GetType().GetProperty("X"));
             sb.Append(bpd.GetValue());
             sb.Append("</TranslateTransform.X><TranslateTransform.Y>");
         }
         else
             sb.Append("0</TranslateTransform.X><TranslateTransform.Y>");
         be = BindingOperations.GetBindingExpression(ict, TranslateTransform.YProperty);
         if (be != null)
         {
             var bpd = new BoundPropertyDesigner();
             bpd.Initialize(ict, ict.GetType().GetProperty("Y"));
             sb.Append(bpd.GetValue());
         }
         else
             sb.Append("0");
         sb.Append("</TranslateTransform.Y></TranslateTransform>");
     }
     else if (ict is SkewTransform)
     {
         sb.Append("<SkewTransform><SkewTransform.AngleX>");
         var be = BindingOperations.GetBindingExpression(ict, SkewTransform.AngleXProperty);
         if (be != null)
         {
             var bpd = new BoundPropertyDesigner();
             bpd.Initialize(ict, ict.GetType().GetProperty("AngleX"));
             sb.Append(bpd.GetValue());
             sb.Append("</SkewTransform.AngleX><SkewTransform.AngleY>");
         }
         else
             sb.Append("0</SkewTransform.AngleX><SkewTransform.AngleY>");
         be = BindingOperations.GetBindingExpression(ict, SkewTransform.AngleYProperty);
         if (be != null)
         {
             var bpd = new BoundPropertyDesigner();
             bpd.Initialize(ict, ict.GetType().GetProperty("AngleY"));
             sb.Append(bpd.GetValue());
         }
         else
             sb.Append("0");
         sb.Append("</SkewTransform.AngleY><SkewTransform.CenterX>");
         sb.Append((ict as SkewTransform).CenterX);
         sb.Append("</SkewTransform.CenterX><SkewTransform.CenterY>");
         sb.Append((ict as SkewTransform).CenterY);
         sb.Append("</SkewTransform.CenterY></SkewTransform>");
     }
 }
Exemplo n.º 3
0
		public static void ApplyTransform(DesignItem designItem, Transform transform, bool relative = true)
		{
			var changeGroup = designItem.OpenGroup("Apply Transform");
			
			Transform oldTransform = null;
			if (designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).IsSet) {
				oldTransform = designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).ValueOnInstance as Transform;
			}
			
			if (oldTransform is MatrixTransform) {
				var mt = oldTransform as MatrixTransform;
				var tg = new TransformGroup();
				if (mt.Matrix.OffsetX != 0 && mt.Matrix.OffsetY != 0)
					tg.Children.Add(new TranslateTransform(){ X = mt.Matrix.OffsetX, Y = mt.Matrix.OffsetY });
				if (mt.Matrix.M11 != 0 && mt.Matrix.M22 != 0)
					tg.Children.Add(new ScaleTransform(){ ScaleX = mt.Matrix.M11, ScaleY = mt.Matrix.M22 });

				var angle = Math.Atan2(mt.Matrix.M21, mt.Matrix.M11) * 180 / Math.PI;
				if (angle != 0)
					tg.Children.Add(new RotateTransform(){ Angle = angle });
				//if (mt.Matrix.M11 != 0 && mt.Matrix.M22 != 0)
				//	tg.Children.Add(new SkewTransform(){ ScaleX = mt.Matrix.M11, ScaleY = mt.Matrix.M22 });
			} else if (oldTransform != null && oldTransform.GetType() != transform.GetType()) {
				var tg = new TransformGroup();
				var tgDes = designItem.Services.Component.RegisterComponentForDesigner(tg);
				tgDes.ContentProperty.CollectionElements.Add(designItem.Services.Component.GetDesignItem(oldTransform));
				designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).SetValue(tg);
				oldTransform = tg;
			}
			
			
			
			if (transform is RotateTransform) {
				var rotateTransform = transform as RotateTransform;
				
				if (oldTransform is RotateTransform || oldTransform == null) {
					if (rotateTransform.Angle != 0) {
						designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).SetValue(transform);
						var angle = rotateTransform.Angle;
						if (relative && oldTransform != null) {
							angle = rotateTransform.Angle + ((RotateTransform)oldTransform).Angle;
						}
						designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).Value.Properties.GetProperty(RotateTransform.AngleProperty).SetValue(angle);
						if (rotateTransform.CenterX != 0.0)
							designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).Value.Properties.GetProperty(RotateTransform.CenterXProperty).SetValue(rotateTransform.CenterX);
						if (rotateTransform.CenterY != 0.0)
							designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).Value.Properties.GetProperty(RotateTransform.CenterYProperty).SetValue(rotateTransform.CenterY);
						
						if (oldTransform == null)
							designItem.Properties.GetProperty(FrameworkElement.RenderTransformOriginProperty).SetValue(new Point(0.5, 0.5));
					}
					else {
						designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).Reset();
						designItem.Properties.GetProperty(FrameworkElement.RenderTransformOriginProperty).Reset();
					}
				} else if (oldTransform is TransformGroup) {
					var tg = oldTransform as TransformGroup;
					var rot = tg.Children.FirstOrDefault(x=> x is RotateTransform);
					if  (rot != null) {
						designItem.Services.Component.GetDesignItem(tg).ContentProperty.CollectionElements.Remove(designItem.Services.Component.GetDesignItem(rot));
					}
					if (rotateTransform.Angle != 0) {
						var des = designItem.Services.Component.GetDesignItem(transform);
						if (des == null)
							des = designItem.Services.Component.RegisterComponentForDesigner(transform);
						designItem.Services.Component.GetDesignItem(tg).ContentProperty.CollectionElements.Add(des);
						if (oldTransform == null)
							designItem.Properties.GetProperty(FrameworkElement.RenderTransformOriginProperty).SetValue(new Point(0.5, 0.5));
					}
				} else {
						if (rotateTransform.Angle != 0) {
							designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).SetValue(transform);
							if (oldTransform == null)
								designItem.Properties.GetProperty(FrameworkElement.RenderTransformOriginProperty).SetValue(new Point(0.5, 0.5));
					}
				}
			}
		
			((DesignPanel) designItem.Services.DesignPanel).AdornerLayer.UpdateAdornersForElement(designItem.View, true);
			
			changeGroup.Commit();
		}