Пример #1
0
 private void CreateImageTypeInstance(Type type, List <string> files)
 {
     onBeforeDrop?.Invoke();
     using (Document.Current.History.BeginTransaction()) {
         var nodes = new List <Node>(files.Count);
         foreach (var file in files)
         {
             if (!Utils.ExtractAssetPathOrShowAlert(file, out var assetPath, out var assetType))
             {
                 continue;
             }
             var node = CreateNode.Perform(type);
             nodes.Add(node);
             var texture  = new SerializableTexture(assetPath);
             var nodeSize = (Vector2)texture.ImageSize;
             var nodeId   = Path.GetFileNameWithoutExtension(assetPath);
             if (node is Widget)
             {
                 SetProperty.Perform(node, nameof(Widget.Texture), texture);
                 SetProperty.Perform(node, nameof(Widget.Pivot), Vector2.Half);
                 SetProperty.Perform(node, nameof(Widget.Size), nodeSize);
                 SetProperty.Perform(node, nameof(Widget.Id), nodeId);
             }
             else if (node is ParticleModifier)
             {
                 SetProperty.Perform(node, nameof(ParticleModifier.Texture), texture);
                 SetProperty.Perform(node, nameof(ParticleModifier.Size), nodeSize);
                 SetProperty.Perform(node, nameof(ParticleModifier.Id), nodeId);
             }
             postProcessNode?.Invoke(node);
         }
         foreach (var node in nodes)
         {
             SelectNode.Perform(node);
         }
         Document.Current.History.CommitTransaction();
     }
 }
Пример #2
0
        public override void ExecuteTransaction()
        {
            var groups = Document.Current?.SelectedNodes().OfType <Frame>().ToList();

            if (groups?.Count == 0)
            {
                return;
            }
            var container = (Widget)Document.Current.Container;
            var p         = container.RootFolder().Find(groups[0]);

            ClearRowSelection.Perform();
            UntieWidgetsFromBones.Perform(Document.Current.Container.Nodes.OfType <Bone>(), groups);
            foreach (var group in groups)
            {
                UnlinkFolderItem.Perform(container, group);
            }

            foreach (var group in groups)
            {
                var flipXFactor            = group.Scale.X < 0 ? -1 : 1;
                var flipYFactor            = group.Scale.Y < 0 ? -1 : 1;
                var flipVector             = Vector2.Right + Vector2.Down * flipXFactor * flipYFactor;
                var groupRootBones         = new List <Bone>();
                var groupNodes             = group.Nodes.ToList().Where(GroupNodes.IsValidNode).ToList();
                var localToParentTransform = group.CalcLocalToParentTransform();
                foreach (var node in groupNodes)
                {
                    UnlinkFolderItem.Perform(group, node);
                    InsertFolderItem.Perform(container, p, node);
                    SelectNode.Perform(node);
                    p.Index++;
                    if (node is Widget)
                    {
                        GroupNodes.TransformPropertyAndKeyframes <Vector2>(node, nameof(Widget.Position), v => localToParentTransform * v);
                        GroupNodes.TransformPropertyAndKeyframes <Vector2>(node, nameof(Widget.Scale), v => v * group.Scale);
                        GroupNodes.TransformPropertyAndKeyframes <float>(node, nameof(Widget.Rotation),
                                                                         v => v * Mathf.Sign(group.Scale.X * group.Scale.Y) + group.Rotation);
                        GroupNodes.TransformPropertyAndKeyframes <Color4>(node, nameof(Widget.Color), v => group.Color * v);
                    }
                    else if (node is Bone)
                    {
                        var root = BoneUtils.FindBoneRoot((Bone)node, groupNodes);
                        if (!groupRootBones.Contains(root))
                        {
                            GroupNodes.TransformPropertyAndKeyframes <Vector2>(node, nameof(Bone.Position), v => localToParentTransform * v);
                            GroupNodes.TransformPropertyAndKeyframes <float>(node, nameof(Bone.Rotation),
                                                                             v => (Matrix32.Rotation(v * Mathf.DegToRad) * localToParentTransform).ToTransform2().Rotation);
                            groupRootBones.Add(root);
                        }
                        else if (flipVector != Vector2.One)
                        {
                            GroupNodes.TransformPropertyAndKeyframes <Vector2>(node, nameof(Bone.Position), v => v * flipVector);
                            GroupNodes.TransformPropertyAndKeyframes <float>(node, nameof(Bone.Rotation), v => - v);
                        }
                        GroupNodes.TransformPropertyAndKeyframes <Vector2>(node, nameof(Bone.RefPosition), v => localToParentTransform * v);
                        GroupNodes.TransformPropertyAndKeyframes <float>(node, nameof(Bone.RefRotation),
                                                                         v => (Matrix32.Rotation(v * Mathf.DegToRad) * localToParentTransform).ToTransform2().Rotation);
                    }
                }
            }
        }
Пример #3
0
        public override void ExecuteTransaction()
        {
            var selectedNodes = Document.Current.SelectedNodes().Where(IsValidNode).ToList();

            if (!Utils.CalcHullAndPivot(selectedNodes, out var hull, out _))
            {
                return;
            }

            hull = hull.Transform(Document.Current.Container.AsWidget.LocalToWorldTransform.CalcInversed());
            var aabb      = hull.ToAABB();
            var container = Document.Current.Container;

            foreach (var row in Document.Current.SelectedRows())
            {
                if (row.Components.Contains <BoneRow>())
                {
                    var boneRow = row.Components.Get <BoneRow>();
                    if (!boneRow.ChildrenExpanded)
                    {
                        selectedNodes.AddRange(BoneUtils.FindBoneDescendats(boneRow.Bone, container.Nodes.OfType <Bone>()));
                    }
                }
            }
            var selectedBones = selectedNodes.OfType <Bone>().ToList();

            var   loc = container.RootFolder().Find(selectedNodes[0]);
            Frame group;

            try {
                group = (Frame)Core.Operations.CreateNode.Perform(container, loc, typeof(Frame));
            } catch (InvalidOperationException e) {
                AlertDialog.Show(e.Message);
                return;
            }
            group.Id       = selectedNodes[0].Id + "Group";
            group.Pivot    = Vector2.Half;
            group.Position = aabb.Center;
            group.Size     = aabb.Size;
            var bonesExceptSelected = container.Nodes.Except(selectedNodes).OfType <Bone>().ToList();

            UntieWidgetsFromBones.Perform(bonesExceptSelected, selectedNodes.OfType <Widget>());
            UntieWidgetsFromBones.Perform(selectedBones, container.Nodes.Except(selectedNodes).OfType <Widget>());
            var nodeKeyframesDict = new Dictionary <Node, BoneAnimationData>();
            var localRoots        = new List <Bone>();

            foreach (var bone in BoneUtils.SortBones(container.Nodes.OfType <Bone>()))
            {
                Bone localRoot;
                var  delta          = Vector2.Zero;
                var  isSelectedBone = selectedBones.Contains(bone);
                if (isSelectedBone)
                {
                    localRoot = BoneUtils.FindBoneRoot(bone, selectedNodes);
                    delta     = -aabb.A;
                }
                else
                {
                    localRoot = BoneUtils.FindBoneRoot(bone, bonesExceptSelected);
                }
                if (!localRoots.Contains(localRoot))
                {
                    if (!isSelectedBone && localRoot.BaseIndex == 0)
                    {
                        localRoots.Add(localRoot);
                        continue;
                    }
                    nodeKeyframesDict.Add(localRoot, EvaluateBoneAnimationUsingParent(localRoot, v => v + delta));
                    localRoots.Add(localRoot);
                }
            }
            SetKeyframes(nodeKeyframesDict);
            foreach (var n in selectedNodes)
            {
                UnlinkFolderItem.Perform(container, n);
            }
            int i = 0;

            foreach (var node in selectedNodes)
            {
                InsertFolderItem.Perform(group, new FolderItemLocation(group.RootFolder(), i++), node);
                if (node is Widget)
                {
                    TransformPropertyAndKeyframes <Vector2>(node, nameof(Widget.Position), v => v - aabb.A);
                }
                if (node is Bone)
                {
                    TransformPropertyAndKeyframes <Vector2>(node, nameof(Bone.RefPosition), v => v - aabb.A);
                }
            }
            group.DefaultAnimation.Frame = container.DefaultAnimation.Frame;
            ClearRowSelection.Perform();
            SelectNode.Perform(group);
        }