/// <summary> /// Adds ALL named XAML elements into the Physics simulation. /// </summary> /// <param name="cnvContainer">The container to add items from.</param> public void AddPhysicsBodyForCanvasWithBehaviors(Canvas cnvContainer) { #if SILVERLIGHT for (int i = 0; i < cnvContainer.Children.Count(); i++) #else for (int i = 0; i < cnvContainer.Children.Count; i++) #endif { FrameworkElement element = cnvContainer.Children[i] as FrameworkElement; string elemName = element.GetValue(Canvas.NameProperty).ToString(); element.SetValue(Canvas.TagProperty, elemName); } PhysicsControllerMain.EnsureUniqueNames(cnvContainer, false); List <PhysicsObjectMain> objectsToAdd = new List <PhysicsObjectMain>(); #if SILVERLIGHT for (int i = 0; i < cnvContainer.Children.Count() - 1; i++) #else for (int i = 0; i < cnvContainer.Children.Count - 1; i++) #endif { FrameworkElement element = cnvContainer.Children[i] as FrameworkElement; PhysicsObjectMain physObject = element.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain; if (physObject != null) { objectsToAdd.Add(physObject); } } foreach (PhysicsObjectMain physObject in objectsToAdd) { AddPhysicsBody(physObject); } #if SILVERLIGHT for (int i = cnvContainer.Children.Count() - 1; i >= 0; i--) #else for (int i = cnvContainer.Children.Count - 1; i >= 0; i--) #endif { FrameworkElement element = cnvContainer.Children[i] as FrameworkElement; PhysicsJointMain joint = element.GetValue(PhysicsJointMain.PhysicsJointProperty) as PhysicsJointMain; if (joint != null) { AddJoint(joint); } } }
public static void SetPhysicsObject(DependencyObject target, PhysicsObjectMain value) { target.SetValue(PhysicsObjectMain.PhysicsObjectProperty, value); }
public PhysicsSprite AddPhysicsBody(PhysicsObjectMain physObject) { FrameworkElement element = physObject.VisualElement; string thisName = element.GetValue(Canvas.NameProperty).ToString(); element.SetValue(Canvas.TagProperty, thisName); // special case: if we are adding a UserControl _AND_ the UserControl contains one or more // Physics Controllers, then we handle that UserControl as a "nested" canvas! if (BoundaryHelper.IsInsideNestedUsercontrol(element)) //element is UserControl) { UserControl ucParent = BoundaryHelper.GetParentUserControl(element); Canvas cnvNestedPhysics = FindPhysicsCanvas(ucParent); if (cnvNestedPhysics != null) { EnsureUniqueNames(cnvNestedPhysics); if (element is UserControl) { AddPhysicsBodyForCanvasWithBehaviors(cnvNestedPhysics); return(null); } } } // look to see if we have processed this object before, and if so, // use its point collection instead of recalculating List <Point> points = FindMatchingUIElement(element); string nameKey = (string)element.GetValue(Canvas.NameProperty); FrameworkElement boundaryElement = null; if (physObject.BoundaryElement != null && physObject.BoundaryElement != string.Empty) { boundaryElement = element.FindName(physObject.BoundaryElement) as FrameworkElement; } if (nameKey == string.Empty) { nameKey = GetUniqueSpriteName(); element.Name = nameKey; } PhysicsSprite sprite = new PhysicsSprite(Simulator, _parentCanvas, element, points, DebugMode, (float)FrictionDefault, boundaryElement); sprite.Name = nameKey; if (sprite.OutlinePoints != null) { sprite.Collision += new PhysicsSprite.CollisionHandler(polygon_Collision); _parentCanvas.Children.Add(sprite); PhysicsObjects.Add(sprite.Name, sprite); sprite.Update(); } sprite.MouseLeftButtonDown += new MouseButtonEventHandler(polygon_MouseLeftButtonDown); sprite.MouseLeftButtonUp += new MouseButtonEventHandler(polygon_MouseLeftButtonUp); sprite.MouseMove += new MouseEventHandler(polygon_MouseMove); sprite.GeometryObject.CollisionGroup = physObject.CollisionGroup; sprite.BodyObject.IsStatic = physObject.IsStatic; if (physObject.RestitutionCoefficient != 0) { sprite.GeometryObject.RestitutionCoefficient = (float)physObject.RestitutionCoefficient; } if (physObject.FrictionCoefficient != 0) { sprite.GeometryObject.FrictionCoefficient = (float)physObject.FrictionCoefficient; } if (physObject.MomentOfIntertia != 0) { sprite.BodyObject.MomentOfInertia = (float)physObject.MomentOfIntertia; } if (physObject.Mass != 0) { sprite.BodyObject.Mass = (float)physObject.Mass; } return(sprite); }