public void LerpKeyFrameWith(CompositeKeyFrame nextFrame, float currentLife) { float amount = MathHelper.Clamp(currentLife / this._duration, 0, 1); //Console.WriteLine("--- Lerping frame with amount: " + amount); if (Parent.Parent.RootBone != null) { // call the LerpBone on the root, and it will spread to all child bones using Recursivity CompositeBoneTransform boneTransform = GetBoneTransformFromKeyFrame(this, Parent.Parent.RootBone.Name); LerpBone(boneTransform, nextFrame, amount); } }
private void AddBoneTransformFromBone(CompositeBone bone) { CompositeBoneTransform newBoneTrans = new CompositeBoneTransform(""); newBoneTrans.Parent = this; newBoneTrans.BoneReference = bone.Name; _boneTransforms.Add(newBoneTrans); for (int i = 0; i < bone.ChildBones.Count; i++) { AddBoneTransformFromBone(bone.ChildBones[i]); } }
public void CopyValuesTo(CompositeBoneTransform target, CompositeKeyFrame newParent) { target.Parent = newParent; target.SceneItem = this.SceneItem; target.SubItem = this.SubItem; target.Position = this.Position; target.Scale = this.Scale; target.Rotation = this.Rotation; target.Opacity = this.Opacity; target.FlipHorizontal = this.FlipHorizontal; target.FlipVertical = this.FlipVertical; target.IsVisible = this.IsVisible; target.BoneReference = this.BoneReference; target.InheritPosition = this.InheritPosition; target.InheritRotation = this.InheritRotation; target.InheritScale = this.InheritScale; target.InheritVisibility = this.InheritVisibility; }
public bool GetVisibilityState(CompositeBoneTransform parentTransform) { if (this.Bone.MasterVisibility.HasValue == true) { return(this.Bone.MasterVisibility.Value); } if (parentTransform != null && this.InheritVisibility.HasValue == false && this.Bone.InheritVisibility == true || this.InheritVisibility.HasValue == true && this.InheritVisibility == true) { if (parentTransform.Bone.MasterVisibility.HasValue == true) { return(parentTransform.Bone.MasterVisibility.Value); } else if (parentTransform.CurrentVisibility == false) { return(false); } } return(_isVisible); }
public void LerpBone(CompositeBoneTransform boneTransform, CompositeKeyFrame nextFrame, float amount) { CompositeBoneTransform nextTransform; if (boneTransform.Bone.Interpolate == true) { nextTransform = GetBoneTransformFromKeyFrame(nextFrame, boneTransform.BoneReference); } else { nextTransform = GetBoneTransformFromKeyFrame( this.Parent.Parent.Animations[0].KeyFrames[0], boneTransform.BoneReference); } boneTransform.LerpSceneItemWith(nextTransform, amount, !boneTransform.Bone.Interpolate); foreach (CompositeBone bone in boneTransform.Bone.ChildBones) { CompositeBoneTransform childBoneTransform = GetBoneTransformFromKeyFrame(this, bone.Name); LerpBone(childBoneTransform, nextFrame, amount); } }
/// <summary> /// Insert a children bone at specific index /// </summary> public void InsertChildBone(int index, CompositeBone childBone) { index = IceMath.Clamp(index, 0, _childBones.Count); _childBones.Insert(index, childBone); childBone.ParentBone = this; childBone.Parent = this.Parent; CompositeBone precedingBone; if (index == 0) { precedingBone = this; } else { precedingBone = this.ChildBones[index - 1]; } // sync transforms for (int i = 0; i < Parent.Animations.Count; i++) { // loop through every keyframe to sync them for (int j = 0; j < Parent.Animations[i].KeyFrames.Count; j++) { CompositeKeyFrame keyframe = Parent.Animations[i].KeyFrames[j]; // loop to find the previous bone for (int k = 0; k < keyframe.BoneTransforms.Count; k++) { CompositeBoneTransform transform = keyframe.BoneTransforms[k]; if (transform.Bone.Equals(precedingBone)) { CompositeBoneTransform newTransform = new CompositeBoneTransform(); newTransform.Parent = keyframe; newTransform.BoneReference = childBone.Name; // insert the new bone just after the preceding bone keyframe.BoneTransforms.Insert(k + 1, newTransform); } } } } }
public void LerpSceneItemWith(CompositeBoneTransform nextState, float amount, bool nextStateOverride) { // original is only used when bone.Interpolate is set to false, and refer to the 1st KF of the 1st anim SceneItem item = GetSceneItem(); String subItem = GetSubItem(); if (String.IsNullOrEmpty(subItem) == false && item is ISubItemCollection) { ((ISubItemCollection)item).SetCurrentSubItem(subItem); } if (item != null) { item.Update(1/60f); amount = MathHelper.Clamp(amount, 0, 1); CompositeEntity parentEntity = Parent.Parent.Parent; CompositeBoneTransform parentTransform = this.ParentBoneTransform; _currentVisibleState = GetVisibilityState(parentTransform); if (_currentVisibleState == false) { return; } bool nextStateVisibility = nextState.GetVisibilityState(nextState.ParentBoneTransform); // no lerping if the next state isnt visible if (nextStateVisibility == false) { nextState = this; } if (nextStateOverride == true) { _position = nextState.Position; _scale = nextState.Scale; _rotation = nextState.Rotation; } else { _position = Vector2.Lerp(this.Position, nextState.Position, amount); _scale = Vector2.Lerp(this.Scale, nextState.Scale, amount); _rotation = MathHelper.Lerp(this.Rotation, nextState.Rotation, amount); } if (this.Opacity.HasValue == true || nextState.Opacity.HasValue == true) { if (this.Opacity.HasValue == true && nextState.Opacity.HasValue == false) { _opacity = this.Opacity.Value; } else if (this.Opacity.HasValue == false && nextState.Opacity.HasValue == true) { _opacity = nextState.Opacity.Value; } else { _opacity = IceMath.Lerp(this.Opacity.Value, nextState.Opacity.Value, amount); } } else { _opacity = null; } _transformPivot = item.GetAbsolutePivot(false); item.FlipHorizontal = parentEntity.FlipHorizontal ^ this.FlipHorizontal; item.FlipVertical = parentEntity.FlipVertical ^ this.FlipVertical; if (parentEntity.FlipHorizontal == true) { _position.X = -_position.X; _transformPivot.X = item.BoundingRectSize.X - _transformPivot.X; _rotation = -_rotation; } if (parentEntity.FlipVertical == true) { _position.Y = -_position.Y; _transformPivot.Y = item.BoundingRectSize.Y - _transformPivot.Y; _rotation = -_rotation; } if (parentEntity.Scale != Vector2.One) { _position *= parentEntity.Scale; } if (parentEntity.Rotation != 0) { Vector2 offset = _position; float length = offset.Length(); double angle = Math.Atan2((float)offset.Y, (float)offset.X) + parentEntity.Rotation; offset.X = (float)(length * Math.Cos(angle)); offset.Y = (float)(length * Math.Sin(angle)); _position = offset; } if (parentTransform != null) { if (this.InheritRotation.HasValue == false && this.Bone.InheritRotation == true || this.InheritRotation.HasValue == true && this.InheritRotation == true) { _rotation += parentTransform.CurrentRotation; } if (this.InheritScale.HasValue == false && this.Bone.InheritScale == true || this.InheritScale.HasValue == true && this.InheritScale == true) { _scale *= parentTransform.CurrentScale; } if (this.InheritPosition.HasValue == false && this.Bone.InheritPosition == true || this.InheritPosition.HasValue == true && this.InheritPosition == true) { Vector2 offset = _position; float length = offset.Length(); double angle = Math.Atan2((float)offset.Y, (float)offset.X) + parentTransform._rotation; offset.X = (float)(length * Math.Cos(angle)); offset.Y = (float)(length * Math.Sin(angle)); _position = parentTransform.CurrentPosition + offset; } } } }
public bool GetVisibilityState(CompositeBoneTransform parentTransform) { if (this.Bone.MasterVisibility.HasValue == true) { return this.Bone.MasterVisibility.Value; } if (parentTransform != null && this.InheritVisibility.HasValue == false && this.Bone.InheritVisibility == true || this.InheritVisibility.HasValue == true && this.InheritVisibility == true) { if (parentTransform.Bone.MasterVisibility.HasValue == true) { return parentTransform.Bone.MasterVisibility.Value; } else if (parentTransform.CurrentVisibility == false) { return false; } } return _isVisible; }
public void LerpSceneItemWith(CompositeBoneTransform nextState, float amount, bool nextStateOverride) { // original is only used when bone.Interpolate is set to false, and refer to the 1st KF of the 1st anim SceneItem item = GetSceneItem(); String subItem = GetSubItem(); if (String.IsNullOrEmpty(subItem) == false && item is ISubItemCollection) { ((ISubItemCollection)item).SetCurrentSubItem(subItem); } if (item != null) { item.Update(1 / 60f); amount = MathHelper.Clamp(amount, 0, 1); CompositeEntity parentEntity = Parent.Parent.Parent; CompositeBoneTransform parentTransform = this.ParentBoneTransform; _currentVisibleState = GetVisibilityState(parentTransform); if (_currentVisibleState == false) { return; } bool nextStateVisibility = nextState.GetVisibilityState(nextState.ParentBoneTransform); // no lerping if the next state isnt visible if (nextStateVisibility == false) { nextState = this; } if (nextStateOverride == true) { _position = nextState.Position; _scale = nextState.Scale; _rotation = nextState.Rotation; } else { _position = Vector2.Lerp(this.Position, nextState.Position, amount); _scale = Vector2.Lerp(this.Scale, nextState.Scale, amount); _rotation = MathHelper.Lerp(this.Rotation, nextState.Rotation, amount); } if (this.Opacity.HasValue == true || nextState.Opacity.HasValue == true) { if (this.Opacity.HasValue == true && nextState.Opacity.HasValue == false) { _opacity = this.Opacity.Value; } else if (this.Opacity.HasValue == false && nextState.Opacity.HasValue == true) { _opacity = nextState.Opacity.Value; } else { _opacity = IceMath.Lerp(this.Opacity.Value, nextState.Opacity.Value, amount); } } else { _opacity = null; } _transformPivot = item.GetAbsolutePivot(false); item.FlipHorizontal = parentEntity.FlipHorizontal ^ this.FlipHorizontal; item.FlipVertical = parentEntity.FlipVertical ^ this.FlipVertical; if (parentEntity.FlipHorizontal == true) { _position.X = -_position.X; _transformPivot.X = item.BoundingRectSize.X - _transformPivot.X; _rotation = -_rotation; } if (parentEntity.FlipVertical == true) { _position.Y = -_position.Y; _transformPivot.Y = item.BoundingRectSize.Y - _transformPivot.Y; _rotation = -_rotation; } if (parentEntity.Scale != Vector2.One) { _position *= parentEntity.Scale; } if (parentEntity.Rotation != 0) { Vector2 offset = _position; float length = offset.Length(); double angle = Math.Atan2((float)offset.Y, (float)offset.X) + parentEntity.Rotation; offset.X = (float)(length * Math.Cos(angle)); offset.Y = (float)(length * Math.Sin(angle)); _position = offset; } if (parentTransform != null) { if (this.InheritRotation.HasValue == false && this.Bone.InheritRotation == true || this.InheritRotation.HasValue == true && this.InheritRotation == true) { _rotation += parentTransform.CurrentRotation; } if (this.InheritScale.HasValue == false && this.Bone.InheritScale == true || this.InheritScale.HasValue == true && this.InheritScale == true) { _scale *= parentTransform.CurrentScale; } if (this.InheritPosition.HasValue == false && this.Bone.InheritPosition == true || this.InheritPosition.HasValue == true && this.InheritPosition == true) { Vector2 offset = _position; float length = offset.Length(); double angle = Math.Atan2((float)offset.Y, (float)offset.X) + parentTransform._rotation; offset.X = (float)(length * Math.Cos(angle)); offset.Y = (float)(length * Math.Sin(angle)); _position = parentTransform.CurrentPosition + offset; } } } }
public void AddCompositeBoneTransform(CompositeBoneTransform compositeBoneTransform) { BoneTransforms.Add(compositeBoneTransform); compositeBoneTransform.Parent = this; }
private static void SetProperty(string p, object targetObject, XmlNode node, object defaultValue) { if (targetObject is SceneItem) { TraceLogger.TraceVerbose("Setting Property " + p + " On SceneItem - " + ((SceneItem)targetObject).Name); } else { TraceLogger.TraceVerbose("Setting Property " + p + " On Object - " + targetObject.GetType().Name); } PropertyInfo _prop = targetObject.GetType().GetProperty(p); if (_prop == null) { return; } XmlNode _newNode = node.SelectSingleNode(_prop.Name); if (_newNode == null) { if (defaultValue != null) { _prop.SetValue(targetObject, defaultValue, null); return; } if (_prop.PropertyType == typeof(bool)) { _prop.SetValue(targetObject, false, null); } else if (_prop.PropertyType == typeof(bool?)) { _prop.SetValue(targetObject, null, null); } else if (_prop.PropertyType == typeof(int)) { _prop.SetValue(targetObject, 0, null); } else if (_prop.PropertyType == typeof(String)) { _prop.SetValue(targetObject, String.Empty, null); } else if (_prop.PropertyType == typeof(Vector2)) { _prop.SetValue(targetObject, Vector2.Zero, null); } else if (_prop.PropertyType == typeof(Point)) { _prop.SetValue(targetObject, Point.Zero, null); } return; } TraceLogger.TraceVerbose("Property Type [" + _prop.PropertyType.Name + "]"); if (_prop.PropertyType == typeof(Vector2)) { _prop.SetValue(targetObject, ParseVector(_newNode), null); } else if (_prop.PropertyType == typeof(List<Vector2>)) { List<Vector2> list = new List<Vector2>(); foreach (XmlNode vecnode in _newNode.ChildNodes) { list.Add(ParseVector(vecnode)); } _prop.SetValue(targetObject, list, null); } /* FARSEER tile polygone serialization else if (_prop.PropertyType == typeof(List<Polygon>)) { List<Polygon> list = new List<Polygon>(); foreach (XmlNode vecnode in _newNode.ChildNodes) { list.Add(Polygon.FromString(vecnode.InnerText)); } _prop.SetValue(targetObject, list, null); }*/ else if (_prop.PropertyType == typeof(Rectangle?)) { if (_newNode.Attributes.Count == 4) { _prop.SetValue(targetObject, ParseRectangleNullable(_newNode), null); } else { _prop.SetValue(targetObject, null, null); } } else if (_prop.PropertyType == typeof(Rectangle)) { if (_newNode.Attributes.Count == 4) { _prop.SetValue(targetObject, ParseRectangle(_newNode), null); } } else if (_prop.PropertyType == typeof(Point)) { Point _point = new Point( int.Parse(node.SelectSingleNode(_prop.Name + "/X").InnerText, CultureInfo.InvariantCulture), int.Parse(node.SelectSingleNode(_prop.Name + "/Y").InnerText, CultureInfo.InvariantCulture)); _prop.SetValue(targetObject, _point, null); } else if (_prop.PropertyType == typeof(float)) { _prop.SetValue(targetObject, Single.Parse(_newNode.InnerText, CultureInfo.InvariantCulture), null); } else if (_prop.PropertyType == typeof(Color)) { Color newColor = (Color)ParseToColor(_newNode.InnerText); _prop.SetValue(targetObject, newColor, null); } else if (_prop.PropertyType == typeof(bool)) { _prop.SetValue(targetObject, bool.Parse(_newNode.InnerText), null); } else if (_prop.PropertyType == typeof(bool?)) { if (String.IsNullOrEmpty(_newNode.InnerText) == false) { _prop.SetValue(targetObject, bool.Parse(_newNode.InnerText), null); } else { _prop.SetValue(targetObject, null, null); } } else if (_prop.PropertyType == typeof(byte)) { #if(WINDOWS) _prop.SetValue(targetObject, byte.Parse(_newNode.InnerText, CultureInfo.InvariantCulture), null); #else byte b = (byte)int.Parse(_newNode.InnerText); _prop.SetValue(targetObject, b, null); #endif } else if (_prop.PropertyType == typeof(byte?)) { if (String.IsNullOrEmpty(_newNode.InnerText) == false) { #if(WINDOWS) _prop.SetValue(targetObject, byte.Parse(_newNode.InnerText, CultureInfo.InvariantCulture), null); #else byte b = (byte)int.Parse(_newNode.InnerText); _prop.SetValue(targetObject, b, null); #endif } } else if (_prop.PropertyType == typeof(int)) { _prop.SetValue(targetObject, int.Parse(_newNode.InnerText, CultureInfo.InvariantCulture), null); } else if (_prop.PropertyType == typeof(string)) { _prop.SetValue(targetObject, _newNode.InnerText, null); } else if (_prop.PropertyType.BaseType == typeof(Enum)) { #if(WINDOWS) Enum val = (Enum)Enum.Parse(_prop.PropertyType, _newNode.InnerText); _prop.SetValue(targetObject, Convert.ChangeType(val, _prop.PropertyType), null); #else Enum val = (Enum)Enum.Parse(_prop.PropertyType, _newNode.InnerText, true); _prop.SetValue(targetObject, Convert.ChangeType(val, _prop.PropertyType, null), null); #endif } else if (_prop.PropertyType == typeof(CompositeBone)) { CompositeBone bone = GetCompositeBone(_newNode); _prop.SetValue(targetObject, bone, null); } else if (_prop.PropertyType == typeof(List<CompositeBone>)) { CompositeBone parent = targetObject as CompositeBone; foreach (XmlNode vecnode in _newNode.ChildNodes) { CompositeBone anim = GetCompositeBone(vecnode); anim.ParentBone = parent; parent.ChildBones.Add(anim); } } else if (_prop.PropertyType == typeof(Dictionary<string, SceneItem>)) { Dictionary<string, SceneItem> dict = new Dictionary<string, SceneItem>(); foreach (XmlNode vecnode in _newNode.ChildNodes) { string type = vecnode.Attributes[0].InnerText; XmlNode _itemNode = vecnode.ChildNodes[0]; SceneItem item = LoadSceneItem(_itemNode, _storedScene); dict.Add(type, item); } _prop.SetValue(targetObject, dict, null); } else if (_prop.PropertyType == typeof(List<CompositeAnimation>)) { List<CompositeAnimation> list = new List<CompositeAnimation>(); foreach (XmlNode vecnode in _newNode.ChildNodes) { CompositeAnimation anim = new CompositeAnimation(); anim.Parent = targetObject as CompositeEntity; anim.Name = vecnode.Attributes["Name"].Value; SetProperty("LerpLastFrameWithFirst", anim, vecnode); SetProperty("Speed", anim, vecnode); SetProperty("KeyFrames", anim, vecnode); SetIAnimationProperties(vecnode, anim as IAnimation); list.Add(anim); } _prop.SetValue(targetObject, list, null); } else if (_prop.PropertyType == typeof(List<CompositeKeyFrame>)) { List<CompositeKeyFrame> list = new List<CompositeKeyFrame>(); foreach (XmlNode vecnode in _newNode.ChildNodes) { CompositeKeyFrame animKeyFrame = new CompositeKeyFrame(); animKeyFrame.Parent = targetObject as CompositeAnimation; animKeyFrame.Name = vecnode.Attributes["Name"].Value; animKeyFrame.Duration = int.Parse(vecnode.Attributes["Duration"].Value, CultureInfo.InvariantCulture); //Bone transforms XmlNode _boneTransNode = vecnode.SelectSingleNode("BoneTransforms"); foreach (XmlNode transNode in _boneTransNode.ChildNodes) { CompositeBoneTransform boneTransform = new CompositeBoneTransform(); boneTransform.Parent = animKeyFrame; SetProperty("BoneReference", boneTransform, transNode); SetProperty("SceneItem", boneTransform, transNode); SetProperty("SubItem", boneTransform, transNode); SetProperty("IsVisible", boneTransform, transNode, true); SetProperty("Position", boneTransform, transNode, Vector2.Zero); SetProperty("Rotation", boneTransform, transNode, 0); SetProperty("Opacity", boneTransform, transNode); SetProperty("Scale", boneTransform, transNode, Vector2.One); SetProperty("InheritPosition", boneTransform, transNode); SetProperty("InheritRotation", boneTransform, transNode); SetProperty("InheritScale", boneTransform, transNode); SetProperty("InheritVisibility", boneTransform, transNode); SetProperty("FlipHorizontal", boneTransform, transNode); SetProperty("FlipVertical", boneTransform, transNode); animKeyFrame.AddCompositeBoneTransform(boneTransform); } list.Add(animKeyFrame); } _prop.SetValue(targetObject, list, null); } else if (_prop.PropertyType == typeof(List<AnimationFrame>)) { List<AnimationFrame> listFrames = new List<AnimationFrame>(); foreach (XmlNode vecnode in _newNode.ChildNodes) { AnimationFrame frame = new AnimationFrame(); frame.Area = vecnode.Attributes["Area"].Value; frame.Duration = int.Parse(vecnode.Attributes["Duration"].Value, CultureInfo.InvariantCulture); listFrames.Add(frame); } _prop.SetValue(targetObject, listFrames, null); } else if (_prop.PropertyType == typeof(LinearProperty)) { LinearProperty lin = (LinearProperty)_prop.GetValue(targetObject, null); lin.Description = _newNode.SelectSingleNode("Description").InnerText; lin.LowerBound = int.Parse(_newNode.SelectSingleNode("LowerBound").InnerText, CultureInfo.InvariantCulture); lin.UpperBound = int.Parse(_newNode.SelectSingleNode("UpperBound").InnerText, CultureInfo.InvariantCulture); foreach (XmlNode item in _newNode.SelectSingleNode("Values").ChildNodes) { lin.Values.Add(new Vector2( float.Parse(item.SelectSingleNode("X").InnerText, CultureInfo.InvariantCulture), float.Parse(item.SelectSingleNode("Y").InnerText, CultureInfo.InvariantCulture))); } } else { TraceLogger.TraceWarning("Not Set By SetProperty - " + p); } }