예제 #1
0
        public void OpenProject(string path)
        {
            Stream projectStream;

            if ((projectStream = File.OpenRead(path)) != null)
            {
                project.ProjectPath = $"{path}";
                projectStream.Dispose();
            }

            TimeLine.timeLine.ClearKeyframes();

            foreach (Animation animation in project.GetProjectInfo().animations)
            {
                Dictionary <string, Spritebox> spriteBoxes = new Dictionary <string, Spritebox>();

                foreach (KeyValuePair <string, SpriteboxJson> pair in animation.spriteBoxes)
                {
                    SpriteboxJson spritebox = pair.Value;

                    spriteBoxes.Add(pair.Key, Spritebox.FromJsonElement(spritebox));
                }

                TimeLine.timeLine.AddKeyframe(new Keyframe(animation.timelineX, 0, spriteBoxes, TimeLine.timeLine));
            }

            TimeLine.timeLine.TimeLineEnd = project.GetProjectInfo().TimelineEnd;
        }
예제 #2
0
        public static SpriteboxJson ToJsonElement(Spritebox spriteBox)
        {
            if (spriteBox == null)
            {
                return(null);
            }

            SpriteboxJson sprBoxJson = new SpriteboxJson()
            {
                posX         = spriteBox.GetPosition().X,
                posY         = spriteBox.GetPosition().Y,
                width        = spriteBox.GetWidth(),
                height       = spriteBox.GetHeight(),
                rotation     = spriteBox._rotation,
                sourceHeight = spriteBox.GetSourceRectangle().Height,
                sourceWidth  = spriteBox.GetSourceRectangle().Width,
                sourceX      = spriteBox.GetSourceRectangle().X,
                sourceY      = spriteBox.GetSourceRectangle().Y,
                textureKey   = spriteBox.GetTextureKey(),
                layer        = spriteBox.GetLayer(),
                visible      = spriteBox.Visible()
            };

            return(sprBoxJson);
        }
예제 #3
0
        // Create new keyframe button
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            int keyFramePos = (int)Math.Floor(TimeLineInfo.timelineMs / 16);

            Dictionary <string, Spritebox> sprBoxes = new Dictionary <string, Spritebox>();

            foreach (KeyValuePair <string, Spritebox> pair in MainWindowViewModel.MonogameWindow.GetPreviewObject().GetSpriteBoxes())
            {
                SpriteboxJson jsonSpr   = Spritebox.ToJsonElement(pair.Value);
                Spritebox     spriteBox = Spritebox.FromJsonElement(jsonSpr);
                string        newKey    = pair.Key.Substring(0, pair.Key.Length);
                sprBoxes.Add(newKey, spriteBox);
            }
            List <Keyframe> toRemove = new List <Keyframe>();

            foreach (Keyframe frame in _KeyFrames)
            {
                if (frame.PositionX == keyFramePos)
                {
                    toRemove.Add(frame);
                }
            }

            foreach (Keyframe frame in toRemove)
            {
                RemoveKeyframe(frame);
            }

            Keyframe newFrame = new Keyframe(keyFramePos, 0, sprBoxes, this);

            AddKeyframe(newFrame);
        }
예제 #4
0
        /// <summary>
        /// Displays current animation in preview window.
        /// </summary>
        public void DisplayAtScrubber()
        {
            int closestKeyframeTo       = -1;
            int closestKeyframeDistance = -1;

            MainWindowViewModel.MonogameWindow.setSprBoxSelected("enginereserved_null");

            if (_KeyFrames.Count > 0)
            {
                for (int i = 0; i < _KeyFrames.Count; i++)
                {
                    int      scrubberPos  = (int)Math.Floor(TimeLineInfo.timelineMs / 16);
                    Keyframe currentFrame = _KeyFrames.ElementAt(i);

                    bool isValid = true;

                    if (currentFrame.PositionX > scrubberPos)
                    {
                        isValid = false;
                    }

                    if (isValid)
                    {
                        if (closestKeyframeDistance == -1)
                        {
                            closestKeyframeDistance = Math.Abs(scrubberPos - _KeyFrames.ElementAt(i).PositionX);
                            closestKeyframeTo       = i;
                        }
                        else
                        {
                            if (Math.Abs(scrubberPos - _KeyFrames.ElementAt(i).PositionX) < closestKeyframeDistance)
                            {
                                closestKeyframeDistance = Math.Abs(scrubberPos - _KeyFrames.ElementAt(i).PositionX);
                                closestKeyframeTo       = i;
                            }
                        }
                    }
                }
            }

            if (closestKeyframeTo >= 0 && _KeyFrames.Count > 0)
            {
                Keyframe originalKeyframe = _KeyFrames.ElementAt(closestKeyframeTo);

                Dictionary <string, Spritebox> sprBoxes = new Dictionary <string, Spritebox>();

                foreach (KeyValuePair <string, Spritebox> pair in originalKeyframe.GetSpriteBoxes())
                {
                    SpriteboxJson jsonSpr   = Spritebox.ToJsonElement(pair.Value);
                    Spritebox     spriteBox = Spritebox.FromJsonElement(jsonSpr);
                    string        newKey    = pair.Key.Substring(0, pair.Key.Length);
                    sprBoxes.Add(newKey, spriteBox);
                }

                MainWindowViewModel.MonogameWindow.GetPreviewObject().SetSpriteBoxes(sprBoxes);
            }

            MainWindow.mainWindow.Properties.UpdateSpriteTree();
        }
예제 #5
0
        public static Spritebox FromJsonElement(SpriteboxJson spriteBoxJson)
        {
            if (spriteBoxJson == null)
            {
                return(null);
            }

            Spritebox sprBox = new Spritebox(new Vector2((float)spriteBoxJson.posX, (float)spriteBoxJson.posY), spriteBoxJson.width, spriteBoxJson.height, spriteBoxJson.rotation,
                                             spriteBoxJson.textureKey, spriteBoxJson.layer, new Rectangle(spriteBoxJson.sourceX, spriteBoxJson.sourceY, spriteBoxJson.sourceWidth, spriteBoxJson.sourceHeight),
                                             null);

            sprBox.SetVisible(spriteBoxJson.visible);

            return(sprBox);
        }
예제 #6
0
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            // Open project
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "PestControl Engine Animation Project (*.animproj)|*.animproj|All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() == true)
            {
                Stream projectStream;

                if ((projectStream = openFileDialog.OpenFile()) != null)
                {
                    MainWindow.project.ProjectPath = $"{openFileDialog.FileName}";
                    projectStream.Dispose();
                }

                Close();

                TimeLine.timeLine.GetKeyframes().Clear();

                foreach (Animation animation in MainWindow.project.GetProjectInfo().animations)
                {
                    Dictionary <string, Spritebox> spriteBoxes = new Dictionary <string, Spritebox>();

                    foreach (KeyValuePair <string, SpriteboxJson> pair in animation.spriteBoxes)
                    {
                        SpriteboxJson spritebox = pair.Value;

                        spriteBoxes.Add(pair.Key, Spritebox.FromJsonElement(spritebox));
                    }

                    TimeLine.timeLine.AddKeyframe(new Keyframe(animation.timelineX, 0, spriteBoxes, TimeLine.timeLine));
                }

                TimeLine.timeLine.TimeLineEnd = MainWindow.project.GetProjectInfo().TimelineEnd;

                // If content path was invalid, show error dialog
                if (!Directory.Exists(MainWindow.project.GetProjectInfo().ContentPath))
                {
                    MessageBox.Show("Content Path is invalid or corrupt. No textures will be loaded.", "Content Path missing or corrupt");
                }
            }
        }
예제 #7
0
        public static void WriteAnimationFile(string filePath, ProjectManager projectManager)
        {
            FileInfo file = new FileInfo(filePath);

            // Null checking
            if (file == null)
            {
                return;
            }

            if (projectManager == null)
            {
                return;
            }

            BinaryWriter binaryWriter = new BinaryWriter(file.Open(FileMode.OpenOrCreate));

            // HEADER
            // PestControlAnimationBinaryFile{version number}
            binaryWriter.Write(headerName);
            binaryWriter.Write(currentVersion);

            // Project info
            binaryWriter.Write(projectManager.GetProjectInfo().Loop);
            binaryWriter.Write(projectManager.GetProjectInfo().LoopToFrame);
            binaryWriter.Write(projectManager.GetProjectInfo().ProjectName);
            binaryWriter.Write(projectManager.GetProjectInfo().TimelineEnd);

            // Keyframes
            // First write amount of keyframes
            ProjectInfoJson  projectInfo = projectManager.GetProjectInfo();
            List <Animation> animations  = projectInfo.animations;

            binaryWriter.Write(animations.Count);

            foreach (Animation frame in animations)
            {
                binaryWriter.Write(frame.timelineX);

                // Write spritebox info
                // First write amount of SpriteBoxes
                binaryWriter.Write(frame.spriteBoxes.Count);

                foreach (KeyValuePair <string, SpriteboxJson> pair in frame.spriteBoxes)
                {
                    SpriteboxJson spriteBox = pair.Value;

                    // Name of SpriteBox
                    binaryWriter.Write(pair.Key);

                    // Other info of SpriteBox
                    binaryWriter.Write(spriteBox.posX);
                    binaryWriter.Write(spriteBox.posY);
                    binaryWriter.Write(spriteBox.width);
                    binaryWriter.Write(spriteBox.height);
                    binaryWriter.Write(spriteBox.textureKey);
                    binaryWriter.Write(spriteBox.rotation);
                    binaryWriter.Write(spriteBox.sourceX);
                    binaryWriter.Write(spriteBox.sourceY);
                    binaryWriter.Write(spriteBox.sourceWidth);
                    binaryWriter.Write(spriteBox.sourceHeight);
                    binaryWriter.Write(spriteBox.layer);
                    binaryWriter.Write(spriteBox.visible);
                }
            }

            binaryWriter.Dispose();
        }
예제 #8
0
        public static void ReadAnimationFile(string filePath, ProjectManager projectManager, TimeLine timeLine)
        {
            FileInfo file = new FileInfo(filePath);

            // Null checking
            if (file == null)
            {
                return;
            }

            if (projectManager == null)
            {
                return;
            }

            if (timeLine == null)
            {
                return;
            }

            BinaryReader binaryReader = new BinaryReader(File.Open(filePath, FileMode.Open));

            // Check if header is correct(basically check if this is even a valid PCAF file.)
            string headerString = binaryReader.ReadString();

            if (headerString == headerName)
            {
                // Clear keyframes in timeline
                timeLine.ClearKeyframes();

                // Ok cool so this file is a PCAF file.
                // Load version number incase this is ever useful(and because we kinda have to push the reader forward for it to read correctly)
                int versionNumber = binaryReader.ReadInt32();

                // Load Project Info
                binaryReader.ReadBoolean();
                binaryReader.ReadInt32();
                binaryReader.ReadString();
                binaryReader.ReadInt32();

                int keyframeCount = binaryReader.ReadInt32();

                for (int i = 0; i < keyframeCount; i++)
                {
                    // Read timeline x coordinate
                    int timelineX = binaryReader.ReadInt32();

                    int spriteboxCount = binaryReader.ReadInt32();

                    Keyframe keyframe = new Keyframe(timelineX, 0, new Dictionary <string, monogame.objects.Spritebox>(), timeLine);

                    for (int j = 0; j < spriteboxCount; j++)
                    {
                        // Get name
                        string name = binaryReader.ReadString();

                        // General properties
                        double posX         = binaryReader.ReadDouble();
                        double posY         = binaryReader.ReadDouble();
                        int    width        = binaryReader.ReadInt32();
                        int    height       = binaryReader.ReadInt32();
                        string textureKey   = binaryReader.ReadString();
                        float  rotation     = binaryReader.ReadSingle();
                        int    sourceX      = binaryReader.ReadInt32();
                        int    sourceY      = binaryReader.ReadInt32();
                        int    sourceWidth  = binaryReader.ReadInt32();
                        int    sourceHeight = binaryReader.ReadInt32();
                        float  layer        = binaryReader.ReadSingle();
                        bool   visible      = binaryReader.ReadBoolean();

                        SpriteboxJson spriteBox = new SpriteboxJson();
                        spriteBox.posX         = posX;
                        spriteBox.posY         = posY;
                        spriteBox.width        = width;
                        spriteBox.height       = height;
                        spriteBox.textureKey   = textureKey;
                        spriteBox.rotation     = rotation;
                        spriteBox.sourceX      = sourceX;
                        spriteBox.sourceY      = sourceY;
                        spriteBox.sourceWidth  = sourceWidth;
                        spriteBox.sourceHeight = sourceHeight;
                        spriteBox.layer        = layer;
                        spriteBox.visible      = visible;

                        keyframe.AddSpriteBox(name, Spritebox.FromJsonElement(spriteBox));
                    }

                    timeLine.AddKeyframe(keyframe);
                }

                MainWindowViewModel.MonogameWindow.setSprBoxSelected("enginereserved_null");
                timeLine.DisplayAtScrubber();
            }

            binaryReader.Dispose();
        }