private void RaiseCloseShaderGroup(ShaderGroup group)
 {
     SetValue(group, false, () =>
     {
         group.Save(Workspace);
         Workspace.Shaders.FirstOrDefault((s) => s.Path == group.GetProjectPath()).IsOpen = group.IsOpen;
         ShaderGroupsView.Refresh();
     }, "IsOpen");
 }
Exemplo n.º 2
0
 private void RaiseSaveShaderProperties()
 {
     if (_selectedShaderGroup != null)
     {
         _selectedShaderGroup.Save(Workspace);
         ViewportHost.RenderThumbnail(Path.GetFullPath($"{MainWindowPageViewModel.ShaderBoxResourcesFolderLocation}{_selectedShaderGroup.UniqueName}/preview.png"),
                                      async() =>
         {
             await Application.Current.Dispatcher.InvokeAsync(() =>
             {
                 _selectedShaderGroup.Image = null;
                 ViewportHost.PopCallbackRenderThumbnail();
             });
         });
     }
 }
        private void RaiseAddHeader(ShaderGroup group)
        {
            string name = "Header";
            IEnumerable <string> headers = group.Shaders.Where((s) => s.ShaderType == ShaderType.Header).Select((s) => s.Name);

            int counter = 0;

            while (headers.Contains(name + ".hlsli"))
            {
                ++counter;
                name = "Header_" + counter.ToString("D3");
            }

            Shader shader = new Shader(name + ".hlsli", ShaderType.Header);

            group.AddShader(shader);
            group.Save(Workspace);

            ShadersFlattened.Add(shader);
        }
        private void RaiseMakeSharedHeader(Shader shader)
        {
            ShaderGroup originalGroup = shader.Group;

            shader.Group.Shaders.Remove(shader);

            string name    = shader.Name.TrimEnd(".hlsli".ToArray());
            string newName = shader.Name;

            IEnumerable <string> headers = _shaderStorage.ShaderGroups[0].Shaders.Select((s) => s.Name);
            int counter = 0;

            while (headers.Contains(newName))
            {
                ++counter;
                newName = name + "_" + counter.ToString("D3") + ".hlsli";
            }

            shader.Name = newName;

            if (!string.IsNullOrEmpty(shader.FileLocation))
            {
                try
                {
                    File.Copy(shader.FileLocation, PATH_TO_SHARED_FOLDER + name);
                    File.Delete(shader.FileLocation);
                    shader.FileLocation = PATH_TO_SHARED_FOLDER + name;
                }
                catch (Exception) { }
            }

            _shaderStorage.ShaderGroups[0].AddShader(shader);

            originalGroup.Save(Workspace);
            _shaderStorage.ShaderGroups[0].Save(Workspace);

            ShaderGroupsView.Refresh();
        }
        public MainWindowPageViewModel()
        {
            Workspace workspace = null;

            try
            {
                if (File.Exists(ShaderBoxRootFileLocation))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Workspace));
                    using (Stream stream = File.OpenRead(ShaderBoxRootFileLocation))
                    {
                        workspace = ((Workspace)serializer.Deserialize(stream));
                    }
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                if (workspace == null)
                {
                    workspace = new Workspace();
                }
                if (workspace.Shaders.Count == 0 || workspace.Shaders.FirstOrDefault((s) => s.Path == ShaderBoxSharedProject) == null)
                {
                    workspace.Shaders.Insert(0, new ShaderPath()
                    {
                        Path   = ShaderBoxSharedProject,
                        IsOpen = true
                    });
                }

                if (workspace.Models.Count == 0 || workspace.Models.FirstOrDefault((s) => s.Path == ShaderBoxBuiltInModel) == null)
                {
                    workspace.Models.Insert(0, new Model3DData()
                    {
                        Hash      = 0,
                        ImagePath = ShaderBoxBuiltInModelImage,
                        IsBuiltIn = true,
                        Name      = "Cube.obj",
                        Path      = ShaderBoxBuiltInModel
                    });
                }

                if (!File.Exists(ShaderBoxSharedProject))
                {
                    ShaderGroup group = new ShaderGroup("Shared headers [../.Shared/]", ShaderGroupType.SharedHeaders)
                    {
                        UniqueName = ShaderBoxSharedFolderName,
                        IsOpen     = true
                    };

                    Shader shader = new Shader("SB_Header.hlsli", ShaderType.Header, ShaderBoxBaseShaderLocation);
                    shader.IsBuiltIn = true;
                    group.AddShader(shader);
                    group.Save(workspace);
                }

                ShaderStorageLinker shaderStorage = new ShaderStorageLinker();
                XmlSerializer       serializer    = new XmlSerializer(typeof(ShaderGroup));
                List <ShaderPath>   toDelete      = new List <ShaderPath>();
                foreach (ShaderPath sPath in workspace.Shaders)
                {
                    try
                    {
                        using (Stream stream = File.OpenRead(sPath.Path))
                        {
                            ShaderGroup group = (ShaderGroup)serializer.Deserialize(stream);
                            group.IsOpen = sPath.IsOpen;
                            shaderStorage.ShaderGroups.Add(group);
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show($"Failed to load project: {sPath.Path}\nError: {e}", "Load error", MessageBoxButton.OK, MessageBoxImage.Error);
                        toDelete.Add(sPath);
                    }
                }
                workspace.Shaders = workspace.Shaders.Except(toDelete).ToList();

                if (!File.Exists(ShaderBoxBaseShaderLocation))
                {
                    ShaderBoxSharedFolderLocation.EnsureFolder();
                    using (StreamWriter stream = new StreamWriter(ShaderBoxBaseShaderLocation))
                    {
                        stream.Write(ShaderBoxBaseShaderContent);
                    }
                }

                _workspace = workspace;
                _workspace.PropertyChanged += new PropertyChangedEventHandler(OnResourcePropertyChanged);
                App.Container.RegisterInstance(workspace);
                App.Container.RegisterInstance(shaderStorage);

                // Resolve cyclic dependency
                foreach (ShaderGroup shaderGroup in shaderStorage.ShaderGroups)
                {
                    foreach (Shader shader in shaderGroup.Shaders)
                    {
                        shader.Group = shaderGroup;
                    }
                    foreach (AnnotationShaderGroup annotationShaderGroup in shaderGroup.AnnotationShaderGroups)
                    {
                        annotationShaderGroup.CreateUI();
                    }
                }
            }
        }