/// <summary>
        /// 计算并设置Scroll的偏移量
        /// </summary>
        /// <param name="offSetX">鼠标X轴移动的偏移量</param>
        /// <param name="offSetY">鼠标Y轴移动的偏移量</param>
        void ComputeScrollOffSet(double offSetX, double offSetY)
        {
            double FocusRectTop     = (double)FocusRect.GetValue(Canvas.TopProperty);
            double FocusRectLeft    = (double)FocusRect.GetValue(Canvas.LeftProperty);
            double ViewportHostTop  = (double)ViewportHost.GetValue(Canvas.TopProperty);
            double ViewportHostLeft = (double)ViewportHost.GetValue(Canvas.LeftProperty);

            //msgBox.Text = "FocusRect.Height" + FocusRect.Height + "   ViewportHost.Height" + ViewportHost.Height;
            if (offSetY > 0 && (FocusRect.Height + 8) < ViewportHost.Height && (ViewportHostTop + ViewportHost.Height) > (FocusRectTop + FocusRect.Height)) //鼠标向下且未出ViewportHost区域时
            {
                imageScroll.ScrollToVerticalOffset(imageScroll.VerticalOffset + (offSetY / 2) * (Viewport.Height / (ViewportHost.Height - FocusRectTop - FocusRect.Height)));
            }

            if (offSetY < 0 && (FocusRect.Height + 8) < ViewportHost.Height && ViewportHostTop < FocusRectTop) //鼠标向上且未出ViewportHost区域时
            {
                imageScroll.ScrollToVerticalOffset(imageScroll.VerticalOffset + (offSetY / 2) * ((Viewport.Height / FocusRectTop)));
            }

            if (offSetX > 0 && (FocusRect.Width + 8) < ViewportHost.Width && (ViewportHostLeft + ViewportHost.Width) > (FocusRectLeft + FocusRect.Width)) //鼠标向右且未出ViewportHost区域时
            {
                imageScroll.ScrollToHorizontalOffset(imageScroll.HorizontalOffset + (offSetX / 2) * (Viewport.Width / (ViewportHost.Width - FocusRectLeft - FocusRect.Width)));
            }

            if (offSetX < 0 && (FocusRect.Width + 8) < ViewportHost.Width && ViewportHostLeft < FocusRectLeft) //鼠标向左且未出ViewportHost区域时
            {
                imageScroll.ScrollToHorizontalOffset(imageScroll.HorizontalOffset + (offSetX / 2) * ((Viewport.Width / FocusRectLeft)));
            }
        }
        /// <summary>
        /// 加载图片文件信息
        /// </summary>
        /// <param name="fileStream"></param>
        public void LoadImageStream(FileStream fileStream, Slider zoomInOut)
        {
            double width = ViewportHost.Width, height = ViewportHost.Height;
            //hack:获取相应的图片高宽信息
            BitmapImage bitmapImage = new BitmapImage();

            bitmapImage.SetSource(fileStream);
            zoomInOut.Maximum = bitmapImage.PixelWidth;

            #region 用获取的图片高宽初始化Viewport,FocusRect区域和以slider
            if (bitmapImage.PixelWidth < bitmapImage.PixelHeight) //当图片宽小于高时
            {
                if (bitmapImage.PixelWidth > width)               //当图片宽度超过可视区域的宽度时
                {
                    height = ((double)width / bitmapImage.PixelWidth) * bitmapImage.PixelHeight;
                    //zoomInOut.Value = (double)width / bitmapImage.PixelWidth;
                }
                else //未超过时则使用图片的高宽初始化显示区域
                {
                    width  = bitmapImage.PixelWidth;
                    height = bitmapImage.PixelHeight;
                }
            }
            else//当图片高小于宽时
            {
                if (bitmapImage.PixelHeight > height)//当图片高度超过可视区域的高度时
                {
                    width = ((double)height / bitmapImage.PixelHeight) * bitmapImage.PixelWidth;
                    //zoomInOut.Value = (double)height / bitmapImage.PixelHeight;
                }
                else//未超过时则使用图片的高宽初始化显示区域
                {
                    width  = bitmapImage.PixelWidth;
                    height = bitmapImage.PixelHeight;
                }
            }

            Viewport.Width  = zoomInOut.Value = width;
            Viewport.Height = height;
            Viewport.SetValue(Canvas.TopProperty, (double)ViewportHost.GetValue(Canvas.TopProperty) + (ViewportHost.Height - Viewport.Height) / 2);
            Viewport.SetValue(Canvas.LeftProperty, (double)ViewportHost.GetValue(Canvas.LeftProperty) + (ViewportHost.Width - Viewport.Width) / 2);

            FocusRect.Width  = width >= 100 ? 100 : width;
            FocusRect.Height = height >= 100 ? 100 : height;
            FocusRect.SetValue(Canvas.TopProperty, (double)ViewportHost.GetValue(Canvas.TopProperty) + (ViewportHost.Height - FocusRect.Height) / 2);
            FocusRect.SetValue(Canvas.LeftProperty, (double)ViewportHost.GetValue(Canvas.LeftProperty) + (ViewportHost.Width - FocusRect.Width) / 2);

            zoomInOut.Minimum       = 16;
            zoomInOut.ValueChanged += new RoutedPropertyChangedEventHandler <double>(ViewportSlider_ValueChanged);
            imageRatio              = (double)bitmapImage.PixelHeight / bitmapImage.PixelWidth;

            SetRectangles();
            #endregion

            selectedImage.SetSource(fileStream);
        }
        private void InitializeViewport()
        {
            _viewportHost        = new ViewportHost(ViewportCanvas.ActualWidth, ViewportCanvas.ActualHeight, _viewport);
            ViewportCanvas.Child = _viewportHost;

            _viewportHost.MessageHook += new HwndSourceHook(ControlMsgFilter);

            _hasInitialized = true;
            SizeChanged    -= OnSizeChanged;
        }
Exemple #4
0
 public ViewportControl()
 {
     InitializeComponent();
     Loaded += Border_Loaded;
     Application.Current.MainWindow.Closing += (s, e) =>
     {
         Child = null;
         ViewportHost.Dispose();
         ViewportHost = null;
     };
 }
        /// <summary>
        /// 滑动条事件处理代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ViewportSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e)
        {
            Slider ZoomInOut = sender as Slider;

            if (ZoomInOut.Value >= ZoomInOut.Minimum && imageRatio * ZoomInOut.Value >= ZoomInOut.Minimum)
            {
                Viewport.Width  = ZoomInOut.Value;
                Viewport.Height = imageRatio * ZoomInOut.Value;
                Viewport.SetValue(Canvas.TopProperty, (double)ViewportHost.GetValue(Canvas.TopProperty) + (ViewportHost.Width - Viewport.Height) / 2);
                Viewport.SetValue(Canvas.LeftProperty, (double)ViewportHost.GetValue(Canvas.LeftProperty) + (ViewportHost.Width - Viewport.Width) / 2);
                AssureFocusRectZoomInZone(ZoomInOut.Value, ZoomInOut.Minimum);
            }
        }
Exemple #6
0
        private void Border_Loaded(object sender, RoutedEventArgs e)
        {
            Loaded -= Border_Loaded;

            Binding binding = new Binding();

            binding.Path   = new PropertyPath("ViewportHost");
            binding.Source = DataContext;
            binding.Mode   = BindingMode.OneWayToSource;
            BindingOperations.SetBinding(this, ViewportHostProperty, binding);

            ViewportHost = new ViewportHost(ViewportWindowCreated);
            ViewportHost.SetUnregisteredCallback(ViewportWindowDestroyed);
            ++((MainWindowPage)Application.Current.MainWindow).WindowsToClose;
        }
 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();
             });
         });
     }
 }
Exemple #8
0
 public void MarshalBuffer(ViewportHost host, bool isStandard)
 {
     if (Bytes.Length != 0)
     {
         if (isStandard)
         {
             host.UpdateShaderVariables(uint.Parse(Regex.Match(Register, @"\d+").Value), (int)AnnotationShaderGroup.Type,
                                        Bytes, (uint)Bytes.Length);
         }
         else
         {
             host.UpdatePPShaderVariables(uint.Parse(Regex.Match(Register, @"\d+").Value),
                                          Bytes, (uint)Bytes.Length);
         }
     }
 }
        private void UpdateShaderViewport()
        {
            if (ViewportHost != null)
            {
                if (SelectedShaderGroup != null && SelectedShaderGroup.IsBuilded)
                {
                    string[] shaderLocations = new string[5];
                    foreach (Shader s in SelectedShaderGroup.Shaders)
                    {
                        if (s.ShaderType != ShaderType.Header)
                        {
                            shaderLocations[(int)s.ShaderType] = $"{MainWindowPageViewModel.ShaderBoxResourcesFolderLocation}{SelectedShaderGroup.UniqueName}/.cso/{s.GetShaderTarget().Substring(0, 2)}.cso";
                        }
                    }
                    bool isStandard = SelectedShaderGroup.ShaderGroupType == ShaderGroupType.Standard;
                    if (isStandard)
                    {
                        ViewportHost.SetShaders(shaderLocations[0] ?? "", shaderLocations[1] ?? "", shaderLocations[2] ?? "", shaderLocations[3] ?? "", shaderLocations[4] ?? "");
                        ViewportHost.SetTopology((int)SelectedShaderGroup.Topology);
                        ViewportHost.SetRasterizerState((int)SelectedShaderGroup.CullMode, (int)SelectedShaderGroup.FillMode);
                    }
                    else
                    {
                        ViewportHost.SetPPShader(shaderLocations[4] ?? "");
                    }
                    _settedGroup = SelectedShaderGroup;

                    foreach (AnnotationShaderGroup annotationShaderGroup in SelectedShaderGroup.AnnotationShaderGroups)
                    {
                        foreach (AnnotationGroup annotationGroup in annotationShaderGroup.Buffers)
                        {
                            foreach (AnnotationVariable annotationVariable in annotationGroup.AnnotationVariables)
                            {
                                annotationVariable.UpdateBuffer(false);
                            }
                            annotationGroup.MarshalBuffer(ViewportHost, isStandard);
                        }
                    }
                }

                ViewportHost.SetModel(SelectedModel?.Path ?? Workspace.Models[0].Path);
                ViewportHost.SetCameraOffset(CameraOffset);

                RaisePropertyChanged("SelectedModel");
            }
        }
        /// <summary>
        /// 初始化相应元素信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FocusRectangle_Loaded(object sender, RoutedEventArgs e)
        {
            Viewport.MinHeight = Viewport.MinWidth = 16;
            Viewport.SetValue(Canvas.TopProperty, (double)ViewportHost.GetValue(Canvas.TopProperty) + (ViewportHost.Height - Viewport.Height) / 2);
            Viewport.SetValue(Canvas.LeftProperty, (double)ViewportHost.GetValue(Canvas.LeftProperty) + (ViewportHost.Width - Viewport.Width) / 2);

            //初始设置FocusRect
            FocusRect.Width     = FocusRect.Height = 100;
            FocusRect.MaxWidth  = ViewportHost.Width;
            FocusRect.MaxHeight = ViewportHost.Height;
            FocusRect.MinHeight = FocusRect.MinWidth = 8;
            FocusRect.SetValue(Canvas.TopProperty, (double)ViewportHost.GetValue(Canvas.TopProperty) + (ViewportHost.Height - FocusRect.Height) / 2);
            FocusRect.SetValue(Canvas.LeftProperty, (double)ViewportHost.GetValue(Canvas.LeftProperty) + (ViewportHost.Width - FocusRect.Width) / 2);

            #region 8个小正方形位置
            //左上
            SmallRect[0] = new Rectangle()
            {
                Name = "SmallRect0", Width = Square.Width, Height = Square.Height, Fill = new SolidColorBrush(color)
            };
            //上中间
            SmallRect[4] = new Rectangle()
            {
                Name = "SmallRect4", Width = Square.Width, Height = Square.Height, Fill = new SolidColorBrush(color)
            };
            //右上
            SmallRect[1] = new Rectangle()
            {
                Name = "SmallRect1", Width = Square.Width, Height = Square.Height, Fill = new SolidColorBrush(color)
            };
            //左下
            SmallRect[2] = new Rectangle()
            {
                Name = "SmallRect2", Width = Square.Width, Height = Square.Height, Fill = new SolidColorBrush(color)
            };
            //下中间
            SmallRect[5] = new Rectangle()
            {
                Name = "SmallRect5", Width = Square.Width, Height = Square.Height, Fill = new SolidColorBrush(color)
            };
            //右下
            SmallRect[3] = new Rectangle()
            {
                Name = "SmallRect3", Width = Square.Width, Height = Square.Height, Fill = new SolidColorBrush(color)
            };
            //左中间
            SmallRect[6] = new Rectangle()
            {
                Name = "SmallRect6", Width = Square.Width, Height = Square.Height, Fill = new SolidColorBrush(color)
            };
            //右中间
            SmallRect[7] = new Rectangle()
            {
                Name = "SmallRect7", Width = Square.Width, Height = Square.Height, Fill = new SolidColorBrush(color)
            };

            SetRectangles();
            #endregion

            #region 事件绑定
            foreach (Rectangle smallRectangle in SmallRect)
            {
                smallRectangle.Fill                 = new SolidColorBrush(color);
                smallRectangle.MouseMove           += new MouseEventHandler(smallRectangle_MouseMove);
                smallRectangle.MouseLeftButtonUp   += new MouseButtonEventHandler(smallRectangle_MouseLeftButtonUp);
                smallRectangle.MouseLeftButtonDown += new MouseButtonEventHandler(smallRectangle_MouseLeftButtonDown);
                smallRectangle.MouseEnter          += new MouseEventHandler(smallRectangle_MouseEnter);
                LayoutRoot.Children.Add(smallRectangle);
            }
            FocusRect.MouseMove           += new MouseEventHandler(FocusRect_MouseMove);
            FocusRect.MouseLeftButtonDown += new MouseButtonEventHandler(FocusRect_MouseLeftButtonDown);
            FocusRect.MouseLeftButtonUp   += new MouseButtonEventHandler(FocusRect_MouseLeftButtonUp);
            FocusRect.MouseEnter          += new MouseEventHandler(FocusRect_MouseEnter);
            FocusRect.MouseLeave          += new MouseEventHandler(FocusRect_MouseLeave);
            #endregion
        }
        /// <summary>
        /// FocusRect是否在Viewport中,如不在,则确保其不超出Viewport区域
        /// </summary>
        /// <returns></returns>
        bool AssureFocusRectMoveInZone(string elementName)
        {
            bool result = true;
            //try
            //{
            double ViewPortTop   = (double)Viewport.GetValue(Canvas.TopProperty);
            double ViewPortLeft  = (double)Viewport.GetValue(Canvas.LeftProperty);
            double FocusRectTop  = (double)FocusRect.GetValue(Canvas.TopProperty);
            double FocusRectLeft = (double)FocusRect.GetValue(Canvas.LeftProperty);

            if (Viewport.Height > ViewportHost.Height)    //已使用放大功能,向上拖动
            {
                if (0 > FocusRectTop)
                {
                    FocusRect.SetValue(Canvas.TopProperty, (double)ViewportHost.GetValue(Canvas.TopProperty) + 4);
                    result = false;
                }
            }
            else
            {
                if (ViewPortTop > FocusRectTop)
                {
                    FocusRect.SetValue(Canvas.TopProperty, ViewPortTop);
                    result = false;
                }
            }

            if (Viewport.Width >= ViewportHost.Width)    //已使用放大功能,向左拖动
            {
                if (0 > FocusRectLeft)
                {
                    FocusRect.SetValue(Canvas.LeftProperty, (double)ViewportHost.GetValue(Canvas.LeftProperty) + 4);
                    result = false;
                }
            }
            else
            {
                if (ViewPortLeft > FocusRectLeft)
                {
                    FocusRect.SetValue(Canvas.LeftProperty, ViewPortLeft);
                    result = false;
                }
            }

            if (Viewport.Width >= ViewportHost.Width)    //已使用放大功能,向右拖动
            {
                if ((ViewportHost.Width) < (FocusRect.Width + FocusRectLeft))
                {
                    if (elementName == "FocusRect")
                    {
                        FocusRect.SetValue(Canvas.LeftProperty, ViewportHost.Width - FocusRect.Width - 4);
                    }
                    else
                    {
                        FocusRect.Width = ViewportHost.Width - FocusRectLeft - 4;
                    }
                    result = false;
                }
            }
            else
            {
                if ((Viewport.Width + ViewPortLeft) < (FocusRect.Width + FocusRectLeft))
                {
                    if (elementName == "FocusRect")
                    {
                        FocusRect.SetValue(Canvas.LeftProperty, ViewPortLeft + Viewport.Width - FocusRect.Width);
                    }
                    else
                    {
                        FocusRect.Width = ViewPortLeft + Viewport.Width - FocusRectLeft;
                    }
                    result = false;
                }
            }

            if (Viewport.Height > ViewportHost.Height)    //已使用放大功能,向下拖动
            {
                if ((ViewportHost.Height) < (FocusRect.Height + FocusRectTop))
                {
                    if (elementName == "FocusRect")
                    {
                        FocusRect.SetValue(Canvas.TopProperty, ViewportHost.Height - FocusRect.Height - 4);
                    }
                    else
                    {
                        FocusRect.Height = ViewportHost.Height - FocusRectTop - 4;
                    }
                    result = false;
                }
            }
            else
            {
                if ((Viewport.Height + ViewPortTop) < (FocusRect.Height + FocusRectTop))
                {
                    if (elementName == "FocusRect")
                    {
                        FocusRect.SetValue(Canvas.TopProperty, ViewPortTop + Viewport.Height - FocusRect.Height);
                    }
                    else
                    {
                        FocusRect.Height = ViewPortTop + Viewport.Height - FocusRectTop;
                    }
                    result = false;
                }
            }

            //}
            //catch
            //{
            //    result = false;
            //}
            return(result);
        }
        private void RaiseBuildShaderGroup()
        {
            Shader shader = ShaderGroupsView.CurrentItem as Shader;

            if (shader.Group.ShaderGroupType != ShaderGroupType.SharedHeaders)
            {
                shader.Group.AnnotationShaderGroups.Clear();

                bool     hasErrors       = false;
                string[] shaderLocations = new string[5];
                foreach (Shader s in shader.Group.Shaders)
                {
                    if (s.ShaderType != ShaderType.Header)
                    {
                        hasErrors |= CompileShader(s, true);
                        shaderLocations[(int)s.ShaderType] = $"{MainWindowPageViewModel.ShaderBoxResourcesFolderLocation}{shader.Group.UniqueName}/.cso/{s.GetShaderTarget().Substring(0, 2)}.cso";
                        if (hasErrors)
                        {
                            shader.Group.IsBuilded = false;
                            if (Directory.Exists(Path.GetDirectoryName(Path.GetFullPath(s.FileLocation)) + "/.cso"))
                            {
                                Directory.Delete(Path.GetDirectoryName(Path.GetFullPath(s.FileLocation)) + "/.cso", true);
                            }
                            break;
                        }
                    }
                }

                // Only refresh shader variables if compilation of all shaders succesfull

                if (!hasErrors)
                {
                    SelectedShaderGroup.IsBuilded = true;

                    if (SelectedShaderGroup.ShaderGroupType == ShaderGroupType.Standard)
                    {
                        if (SelectedShaderGroup == _settedGroup)
                        {
                            ViewportHost.UpdateShaders(
                                shaderLocations[0] ?? "", shaderLocations[1] ?? "", shaderLocations[2] ?? "", shaderLocations[3] ?? "", shaderLocations[4] ?? "");
                        }
                        else
                        {
                            // Workaround so we don't update the wrong shader, if shaders wasn't in cache we load everything double, not good
                            ViewportHost.SetShaders(
                                shaderLocations[0] ?? "", shaderLocations[1] ?? "", shaderLocations[2] ?? "", shaderLocations[3] ?? "", shaderLocations[4] ?? "");
                            ViewportHost.UpdateShaders(
                                shaderLocations[0] ?? "", shaderLocations[1] ?? "", shaderLocations[2] ?? "", shaderLocations[3] ?? "", shaderLocations[4] ?? "");
                            ViewportHost.SetTopology((int)SelectedShaderGroup.Topology);
                            ViewportHost.SetRasterizerState((int)SelectedShaderGroup.CullMode, (int)SelectedShaderGroup.FillMode);
                        }
                    }
                    else
                    {
                        if (SelectedShaderGroup == _settedGroup)
                        {
                            ViewportHost.UpdatePPShader(shaderLocations[4] ?? "");
                        }
                        else
                        {
                            ViewportHost.SetPPShader(shaderLocations[4] ?? "");
                            ViewportHost.UpdatePPShader(shaderLocations[4] ?? "");
                        }
                    }
                    try
                    {
                        foreach (Shader s in SelectedShaderGroup.Shaders)
                        {
                            SyntaxTree            syntaxTree = SyntaxFactory.ParseSyntaxTree(SourceText.From(s.Document.Text), null, null);
                            AnnotationShaderGroup options    = UpdateVariables(syntaxTree, s.ShaderType);
                            if (options == null)
                            {
                                break;
                            }
                            if (options.Buffers.Count > 0)
                            {
                                SelectedShaderGroup.AnnotationShaderGroups.Add(options);
                            }
                        }

                        ViewportHost.RenderThumbnail(Path.GetFullPath($"{MainWindowPageViewModel.ShaderBoxResourcesFolderLocation}{SelectedShaderGroup.UniqueName}/preview.png"),
                                                     async() =>
                        {
                            await Application.Current.Dispatcher.InvokeAsync(() =>
                            {
                                _selectedShaderGroup.Image = null;
                                ViewportHost.PopCallbackRenderThumbnail();
                            });
                        });
                    }
                    catch (Exception)
                    {
                        SelectedShaderGroup.AnnotationShaderGroups.Clear();
                    }
                }
                SelectedShaderGroup.Save(Workspace);
            }
        }