public void RenderCanvas_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
 {
     StaticShaderBuilder.Paint(RenderCanvas);
 }
Exemplo n.º 2
0
        public MainPageViewModel(GLControl RenderCanvas, CustomDynamicTab DynamicTab)
        {
            GLControl = RenderCanvas;

            CreateFileCommand = new RelayCommand((obj) =>
            {
                var dialog = new SaveFileDialog
                {
                    DefaultExt = ".glsl",
                    Filter     = FILTER
                };
                if (dialog.ShowDialog() == true)
                {
                    var name = dialog.FileName;
                    AddToTabItems(name);
                }
            });

            AddExistFileCommand = new RelayCommand((obj) =>
            {
                var dialog = new OpenFileDialog
                {
                    Filter          = PROJECTFILTER,
                    CheckFileExists = true,
                    Multiselect     = true
                };
                if (dialog.ShowDialog() == true)
                {
                    // Open shadeproj project
                    if (dialog.FileNames.Count() == 1 && Path.GetExtension(dialog.FileNames[0]) == ".shadeproj")
                    {
                        try
                        {
                            StreamReader sr    = new StreamReader(dialog.FileNames[0]);
                            SettingModel model = new SettingModel();
                            bool flag, isOk    = true;

                            string nameOfSourseFile = sr.ReadLine();

                            isOk        &= bool.TryParse(sr.ReadLine(), out flag);
                            model.IsTime = flag;

                            isOk         &= bool.TryParse(sr.ReadLine(), out flag);
                            model.IsMouse = flag;

                            isOk            &= bool.TryParse(sr.ReadLine(), out flag);
                            model.IsViewPort = flag;

                            sr.Close();

                            if (!isOk)
                            {
                                ErrorText += "Failed open .shadeproj file" + Environment.NewLine;
                                return;
                            }

                            SettingModel = model;
                            Dictionary <string, int> solution = new Dictionary <string, int>();
                            StaticShaderLinker.LincRec(nameOfSourseFile, 0, solution);
                            foreach (var source in solution)
                            {
                                var tab = AddToTabItems(source.Key);
                                if (tab != null)
                                {
                                    FilledTab(tab, source.Key);
                                }
                            }
                        }
                        catch
                        {
                            ErrorText += "Failed open .shadeproj file" + Environment.NewLine;
                            return;
                        }
                    }
                    else
                    {
                        foreach (var name in dialog.FileNames)
                        {
                            Dictionary <string, int> solution = new Dictionary <string, int>();
                            StaticShaderLinker.LincRec(name, 0, solution);
                            foreach (var source in solution)
                            {
                                var tab = AddToTabItems(source.Key);
                                if (tab != null)
                                {
                                    FilledTab(tab, source.Key);
                                }
                            }
                        }
                    }
                }
            });

            SavePrjCommand = new RelayCommand((obj) =>
            {
                var dialog = new SaveFileDialog
                {
                    DefaultExt = ".shadeproj",
                    Filter     = "(*.SHADEPROJ)|*.SHADEPROJ"
                };
                if (dialog.ShowDialog() == true)
                {
                    var name = dialog.FileName;
                    using (StreamWriter sr = new StreamWriter(name))
                    {
                        sr.WriteLine(StaticShaderLinker.FindMain(GetTabFilesPath()));
                        sr.WriteLine(SettingModel.IsTime);
                        sr.WriteLine(SettingModel.IsMouse);
                        sr.WriteLine(SettingModel.IsViewPort);
                    }
                }
            });

            RunCommand = new RelayCommand((obj) =>
            {
                Logger _logger = Logger.Instance;
                _logger.ClearMessages();
                StaticShaderBuilder.StopRender();

                SaveInFiles(TabItems);

                StaticShaderBuilder.RenderShader(GetTabFilesPath(), SettingModel);
                StaticShaderBuilder.Paint(RenderCanvas);

                ErrorText = _logger.GetAllMessage();
            });

            SaveCommand = new RelayCommand((obj) =>
            {
                if (DynamicTab.SelectedItem == null)
                {
                    return;
                }
                var tab = DynamicTab.SelectedItem as TabItem;
                SaveInFiles(new TabItem[] { tab });
            });

            OpenSettingsCommand = new RelayCommand((obj) =>
            {
                var window = new SettingsWindow(SettingModel);
                window.Show();
            });

            StopCommand = new RelayCommand((obj) =>
            {
                StaticShaderBuilder.StopRender();
            });

            FullscreenCommand = new RelayCommand((obj) =>
            {
                StopCommand.Execute(null);
                RenderCanvas.Enabled = false;
                var full             = new FullScreenRenderWindow(() => RunCommand.Execute(null));
                full.ShowDialog();
                RenderCanvas.Enabled = true;
                RenderCanvas.Invalidate();
                RenderCanvas.MakeCurrent();
            });

            StartRecordCommand = new RelayCommand((obj) =>
            {
                var dialog = new SaveFileDialog
                {
                    Filter       = "Movie(*.MP4) | *.MP4",
                    AddExtension = true
                };
                if (dialog.ShowDialog() == true)
                {
                    StopCommand.Execute(null);
                    RenderCanvas.Enabled = false;

                    var full = new FullScreenRenderWindow(() =>
                    {
                        RunCommand.Execute(null);
                    });

                    full.RenderCanvas.Paint -= full.RenderCanvas_Paint;
                    full.RenderCanvas.Paint += (sender, args) =>
                    {
                        StaticShaderBuilder.Paint(full.RenderCanvas);
                        VideoShaderRecorder.GrabScreenshot(full.RenderCanvas);
                    };
                    VideoShaderRecorder.StartRecord(dialog.FileName, 15000);
                    full.ShowDialog();
                    VideoShaderRecorder.StopRecord();

                    RenderCanvas.Enabled = true;
                    RenderCanvas.Invalidate();
                    RenderCanvas.MakeCurrent();
                }
            });
        }