private bool CompileShader(Shader shader, bool saveToDisk)
        {
            CompilingShader = shader;
            RaiseSaveDocument(CompilingShader);

            CompilingShader.Errors.Clear();

            IntPtr ptr = NativeMethods.CompileShader(CompilingShader.Document.Text, CompilingShader.GetShaderTarget(),
                                                     Path.GetDirectoryName(Path.GetFullPath(CompilingShader.FileLocation)), saveToDisk, Marshal.GetFunctionPointerForDelegate(_includeHeaderCallback));
            string e = Marshal.PtrToStringAnsi(ptr);

            bool hasErrors = !string.IsNullOrWhiteSpace(e);

            if (hasErrors)
            {
                string[] errors = e.Split('\n');
                foreach (string error in errors)
                {
                    if (!string.IsNullOrWhiteSpace(error))
                    {
                        CompilingShader.Errors.Add(error);
                    }
                }
                ShaderGroupsView.MoveCurrentTo(shader);
            }
            NativeMethods.DeleteArray(ref ptr);
            CompilingShader = null;

            return(hasErrors);
        }
 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");
 }
 private void RefreshExplorerEvent(ShaderGroup shaderGroup)
 {
     if (shaderGroup != null)
     {
         foreach (Shader shader in shaderGroup.Shaders)
         {
             ShadersFlattened.Remove(shader);
         }
     }
     ShaderGroupsView.Refresh();
 }
        private void RaiseEditShaderGroup(ShaderGroup group)
        {
            ShaderGroupNotification notification = new ShaderGroupNotification();

            notification.ShaderGroup = group.Copy();
            notification.Title       = "Edit Shader";
            notification.Content     = true;

            bool          selectionNeedsUpdate = false;
            int           selectedIndex        = ShaderGroupsView.CurrentPosition;
            List <Shader> openItems            = ShaderGroupsView.Cast <Shader>().ToList();
            int           originalCount        = group.Shaders.Count;
            int           startIndex           = 0;

            if (openItems[selectedIndex].Group == group)
            {
                selectionNeedsUpdate = true;
                startIndex           = openItems.IndexOf(group.Shaders[0]);
            }

            ShaderGroupRequest.Raise(notification, (returned) =>
            {
                if (returned.Confirmed)
                {
                    int flattenedIndex = ShadersFlattened.IndexOf(group.Shaders[0]);

                    int index = _shaderStorage.ShaderGroups.IndexOf(group);
                    _shaderStorage.ShaderGroups.Remove(group);
                    _shaderStorage.ShaderGroups.Insert(index, notification.ShaderGroup);

                    group.AnnotationShaderGroups.Clear();
                    group.IsBuilded = false;

                    group.Shaders.ToList().ForEach((s) => ShadersFlattened.Remove(s));
                    for (int i = 0; i < notification.ShaderGroup.Shaders.Count; ++i)
                    {
                        ShadersFlattened.Insert(flattenedIndex + i, notification.ShaderGroup.Shaders[i]);
                    }

                    if (selectionNeedsUpdate)
                    {
                        selectedIndex = Math.Min(openItems.Count - 1, selectedIndex);
                        if (startIndex + notification.ShaderGroup.Shaders.Count <= selectedIndex)
                        {
                            selectedIndex = startIndex + notification.ShaderGroup.Shaders.Count - 1;
                        }
                        ShaderGroupsView.MoveCurrentToPosition(selectedIndex);
                    }
                }
            });
        }
 private void SelectionChanged()
 {
     if (SelectedIndex >= 0)
     {
         Shader shader = ShaderGroupsView.Cast <Shader>().ElementAt(SelectedIndex);
         if (SelectedShaderGroup != shader.Group)
         {
             SelectedShaderGroup = shader.Group;
             UpdateShaderViewport();
         }
     }
     else
     {
         SelectedShaderGroup = null;
     }
 }
        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();
        }