Пример #1
0
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            d3dDragHandler = new DragHandler(d3dCanvas)
            {
                CursorOver = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.SizeAll, 1)
            };
            d2dDragHandler = new DragHandler(d2dCanvas)
            {
                CursorOver = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.SizeAll, 1)
            };

            d3dBrush          = new ImageBrush();
            d3dRectangle.Fill = d3dBrush;

            d2dBrush          = new ImageBrush();
            d2dRectangle.Fill = d2dBrush;

            // Safely dispose any previous instance
            // Creates a new DeviceManager (Direct3D, Direct2D, DirectWrite, WIC)
            deviceManager = new DeviceManager();

            // New CubeRenderer
            cubeRenderer  = new CubeRenderer();
            shapeRenderer = new ShapeRenderer();

            int pixelWidth  = (int)(d3dRectangle.Width * DisplayProperties.LogicalDpi / 96.0);
            int pixelHeight = (int)(d3dRectangle.Height * DisplayProperties.LogicalDpi / 96.0);

            // Use CoreWindowTarget as the rendering target (Initialize SwapChain, RenderTargetView, DepthStencilView, BitmapTarget)
            d3dTarget            = new SurfaceImageSourceTarget(pixelWidth, pixelHeight);
            d3dBrush.ImageSource = d3dTarget.ImageSource;

            d2dTarget            = new SurfaceImageSourceTarget(pixelWidth, pixelHeight);
            d2dBrush.ImageSource = d2dTarget.ImageSource;

            // Add Initializer to device manager
            deviceManager.OnInitialize += d3dTarget.Initialize;
            deviceManager.OnInitialize += d2dTarget.Initialize;
            deviceManager.OnInitialize += cubeRenderer.Initialize;
            deviceManager.OnInitialize += shapeRenderer.Initialize;

            // Render the cube within the CoreWindow
            d3dTarget.OnRender += cubeRenderer.Render;
            d2dTarget.OnRender += shapeRenderer.Render;

            // Initialize the device manager and all registered deviceManager.OnInitialize
            deviceManager.Initialize(DisplayProperties.LogicalDpi);

            CoreWindow.GetForCurrentThread().VisibilityChanged += (_, args) =>
            {
                if (args.Visible)
                {
                    BindRenderingEvents();
                }
                else
                {
                    UnbindRenderingEvents();
                }
            };
        }
Пример #2
0
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            d2dDragHandler = new DragHandler(d2dCanvas) { CursorOver = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.SizeAll, 1) };

            d2dBrush = new ImageBrush();
            d2dRectangle.Fill = d2dBrush;

            effectRenderer = new EffectRenderer();

            deviceManager = new DeviceManager();

            int pixelWidth = (int)(d2dRectangle.Width * DisplayProperties.LogicalDpi / 96.0);
            int pixelHeight = (int)(d2dRectangle.Height * DisplayProperties.LogicalDpi / 96.0);

            d2dTarget = new SurfaceImageSourceTarget(pixelWidth, pixelHeight);
            d2dBrush.ImageSource = d2dTarget.ImageSource;
            d2dTarget.OnRender += effectRenderer.Render;

            deviceManager.OnInitialize += d2dTarget.Initialize;
            deviceManager.OnInitialize += effectRenderer.Initialize;
            deviceManager.Initialize(DisplayProperties.LogicalDpi);

            var window = CoreWindow.GetForCurrentThread();
            if (window.Visible)
                BindRenderingEvents();

            window.VisibilityChanged += (_, args) =>
                                        {
                                            if (args.Visible)
                                                BindRenderingEvents();
                                            else
                                                UnbindRenderingEvents();
                                        };
        }
Пример #3
0
        /// <summary>
        /// Attaches a renderer.
        /// </summary>
        /// <param name="pixelWidth">The width of the display area in pixels.</param>
        /// <param name="pixelHeight">The height of the display area in pixels.</param>
        /// <exception cref="InvalidOperationException">A renderer is already attached.</exception>
        private void AttachRenderer(int pixelWidth, int pixelHeight)
        {
            lock (this.deviceManager)
            {
                if (this.terminalRenderer != null)
                {
                    throw new InvalidOperationException("Renderer already attached.");
                }

                this.terminalRenderer = new ScreenDisplayRenderer(this, this.terminal.RenderableScreen);
                this.d2dTarget        = new SurfaceImageSourceTarget(pixelWidth, pixelHeight);
                this.forceRender      = true;

                this.deviceManager.OnInitialize += this.d2dTarget.Initialize;
                this.deviceManager.OnInitialize += this.terminalRenderer.Initialize;
                this.deviceManager.Initialize(DisplayInformation.GetForCurrentView().LogicalDpi);

                this.rectangle.Fill = new ImageBrush()
                {
                    ImageSource = this.d2dTarget.ImageSource
                };
                this.d2dTarget.OnRender     += terminalRenderer.Render;
                CompositionTarget.Rendering += CompositionTarget_Rendering;
            }
        }
Пример #4
0
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            d2dDragHandler = new DragHandler(d2dCanvas)
            {
                CursorOver = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.SizeAll, 1)
            };

            d2dBrush          = new ImageBrush();
            d2dRectangle.Fill = d2dBrush;

            effectRenderer = new EffectRenderer(d2dRectangle, root);

            deviceManager = new DeviceManager();

            int pixelWidth  = (int)(d2dRectangle.Width * DisplayProperties.LogicalDpi / 96.0);
            int pixelHeight = (int)(d2dRectangle.Height * DisplayProperties.LogicalDpi / 96.0);

            d2dTarget            = new SurfaceImageSourceTarget(pixelWidth, pixelHeight);
            d2dBrush.ImageSource = d2dTarget.ImageSource;
            d2dTarget.OnRender  += effectRenderer.Render;

            deviceManager.OnInitialize += d2dTarget.Initialize;
            deviceManager.OnInitialize += effectRenderer.Initialize;
            deviceManager.Initialize(DisplayProperties.LogicalDpi);


            // Setup rendering callback
            CompositionTarget.Rendering += CompositionTarget_Rendering;
        }
Пример #5
0
        /// <summary>
        /// Creates the device manager and image source when the viewport is loaded.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
        private void Viewport3DXLoaded(object sender, RoutedEventArgs e)
        {
            int pixelWidth  = (int)(this.ActualWidth * DisplayProperties.LogicalDpi / 96.0);
            int pixelHeight = (int)(this.ActualHeight * DisplayProperties.LogicalDpi / 96.0);

            // Safely dispose any previous instance
            // Creates a new DeviceManager (Direct3D, Direct2D, DirectWrite, WIC)
            this.deviceManager = new DeviceManager();

            // Use CoreWindowTarget as the rendering target (Initialize SwapChain, RenderTargetView, DepthStencilView, BitmapTarget)
            this.d3dTarget              = new SurfaceImageSourceTarget(pixelWidth, pixelHeight);
            this.imageBrush             = new ImageBrush();
            this.imageBrush.ImageSource = this.d3dTarget.ImageSource;
            this.Background             = this.imageBrush;

            this.deviceManager.OnInitialize += this.d3dTarget.Initialize;
            this.deviceManager.OnInitialize += this.Initialize;

            this.d3dTarget.OnRender += this.Render;

            // Initialize the device manager and all registered deviceManager.OnInitialize
            this.deviceManager.Initialize(DisplayProperties.LogicalDpi);

            // Setup rendering callback
            CompositionTarget.Rendering += this.CompositionTargetRendering;

            // Callback on DpiChanged
            DisplayProperties.LogicalDpiChanged += this.DisplayPropertiesLogicalDpiChanged;
        }
Пример #6
0
        //public SharpDX.Direct2D1.Bitmap1 CreateBitmapTarget()
        //{
        //    return d2dTarget.CreateNewBitmapTarget();
        //}

        void DrawingSurfaceSIS_Unloaded(object sender, RoutedEventArgs e)
        {
            CompositionTarget.Rendering -= CompositionTarget_Rendering;

            _deviceManager.OnInitialize -= _sisTarget1.Initialize;
            _deviceManager.OnInitialize -= _sisTarget2.Initialize;

            _deviceManager.OnInitialize -= _effectRenderer.Initialize;
            _deviceManager.OnInitialize -= _magicRenderer.Initialize;

            _deviceManager.Dispose();
            _deviceManager = null;

            _sisTarget1.OnRender -= _effectRenderer.Render;
            _sisTarget1.Dispose();
            _sisTarget1 = null;

            _sisTarget2.OnRender -= _magicRenderer.Render;
            _sisTarget2.Dispose();
            _sisTarget2 = null;

            _hasInitializedSurface = false;

            this.Unloaded -= DrawingSurfaceSIS_Unloaded;
        }
        //public SharpDX.Direct2D1.Bitmap1 CreateBitmapTarget()
        //{
        //    return d2dTarget.CreateNewBitmapTarget();
        //}

        //void DrawingSurfaceSIS_Unloaded(object sender, RoutedEventArgs e)
        //{

        //    Unload();


        //}


        public void Unload()
        {
            //if (_effectRenderer != null && _sisTarget1!=null ) _sisTarget1.OnRender -= _effectRenderer.Render;
            if (_renderRendererAction != null)
            {
                _loadAssetUriRendererAction = null;
            }
            CompositionTarget.Rendering -= CompositionTarget_Rendering;
            //this.Unloaded -= DrawingSurfaceSIS_Unloaded;


            if (_deviceManager != null && _sisTarget1 != null)
            {
                _deviceManager.OnInitialize -= _sisTarget1.Initialize;
            }
            //if(_deviceManager!=null && _effectRenderer!=null) { _deviceManager.OnInitialize -= _effectRenderer.Initialize; }
            if (_initializeRendererAction != null)
            {
                _initializeRendererAction = null;
            }

            //_deviceManager.Dispose();
            //_deviceManager = null;

            if (_unloadRendererAction != null)
            {
                //_effectRenderer.Unload();
                //_effectRenderer = null;
                _unloadRendererAction();
                _unloadRendererAction = null;
            }

            if (_sisTarget1 != null)
            {
                _sisTarget1.Dispose();
                _sisTarget1 = null;
            }
            _hasInitializedSurface = false;

            _deviceManager = null;

            d2dRectangleBottom.Fill = null;

            if (_ibTarget1 != null)
            {
                _ibTarget1.ImageSource = null;
                _ibTarget1             = null;
            }



            _initializeUIRendererAction = null;
            _loadAssetUriRendererAction = null;
            _unloadRendererAction       = null;
        }
        private void mainGrid_Loaded(object sender, RoutedEventArgs e)
        {
            if (!_hasInitializedSurface)
            {
                _ibTarget1 = new ImageBrush();

                d2dRectangleBottom.Opacity = 1.0f;
                d2dRectangleBottom.Fill    = _ibTarget1;

                //_deviceManager = new DeviceManager();


                int pixelWidth  = (int)(d2dRectangleBottom.ActualWidth * DisplayProperties.LogicalDpi / 96.0);
                int pixelHeight = (int)(d2dRectangleBottom.ActualHeight * DisplayProperties.LogicalDpi / 96.0);

                _sisTarget1 = new SurfaceImageSourceTarget(pixelWidth, pixelHeight);

                _ibTarget1.ImageSource = _sisTarget1.ImageSource;

                //_sisTarget1.OnRender += _effectRenderer.Render;
                _sisTarget1.OnRender += _renderRendererAction;

                _deviceManager.OnInitialize += _sisTarget1.Initialize;
                //_deviceManager.OnInitialize += _effectRenderer.Initialize;
                _deviceManager.OnInitialize += _initializeRendererAction;


                _deviceManager.Initialize(DisplayProperties.LogicalDpi);
                //_effectRenderer.InitializeUI(root, d2dRectangleBottom);
                if (_initializeUIRendererAction != null)
                {
                    _initializeUIRendererAction(root, d2dRectangleBottom);
                }


                //var fpsRenderer = new FpsRenderer();
                //fpsRenderer.Initialize(deviceManager);
                //d2dTarget.OnRender += fpsRenderer.Render;


                //if (_assetUri != null && _assetUri != string.Empty) _effectRenderer.LoadLocalAsset(_assetUri);
                if (_assetUri != null && _assetUri != string.Empty)
                {
                    _loadAssetUriRendererAction(_assetUri);
                }

                //this.Unloaded += DrawingSurfaceSIS_Unloaded;


                _hasInitializedSurface = true;
            }
        }
Пример #9
0
        /// <summary>
        /// Detaches the renderer.
        /// </summary>
        private void DetachRenderer()
        {
            lock (this.deviceManager)
            {
                if (this.terminalRenderer == null)
                {
                    return;
                }

                CompositionTarget.Rendering -= CompositionTarget_Rendering;
                this.d2dTarget.OnRender     -= terminalRenderer.Render;
                this.rectangle.Fill          = null;

                this.deviceManager.OnInitialize -= this.d2dTarget.Initialize;
                this.deviceManager.OnInitialize -= this.terminalRenderer.Initialize;

                this.d2dTarget.Dispose();
                this.d2dTarget = null;

                this.terminalRenderer.Dispose();
                this.terminalRenderer = null;
            }
        }
Пример #10
0
        /// <summary>
        /// Renders the screen.
        /// </summary>
        /// <param name="target">The Direct2D drawing target.</param>
        public virtual void Render(TargetBase target)
        {
            Point drawingPosition = new Point(0, 0);
            SurfaceImageSourceTarget surfaceImageSourceTarget = target as SurfaceImageSourceTarget;

            if (surfaceImageSourceTarget != null)
            {
                drawingPosition = surfaceImageSourceTarget.DrawingPosition;
            }

            IRenderableScreenCopy screenCopy = this.screen.GetScreenCopy();

            var context2D = target.DeviceManager.ContextDirect2D;

            context2D.BeginDraw();
            context2D.Transform = Matrix.Identity;
            context2D.Clear(this.GetColor(ScreenColor.DefaultBackground));

            // 1. Paint backgrounds
            {
                RectangleF rect  = new RectangleF();
                var        lines = screenCopy.Cells;
                for (int y = 0; y < lines.Length; y++)
                {
                    var cols = lines[y];

                    rect.Top    = drawingPosition.Y + (y * this.physicalFontMetrics.CellHeight);
                    rect.Bottom = rect.Top + this.physicalFontMetrics.CellHeight;

                    ScreenColor currentBackgroundColor = cols.Length > 0 ? cols[0].BackgroundColor : ScreenColor.DefaultBackground;
                    ScreenColor cellBackgroundColor;
                    int         blockStart = 0;
                    for (int x = 0; x <= cols.Length; x++) // loop once above the upper bound
                    {
                        var cell = cols[x < cols.Length ? x : x - 1];

                        bool isCursor = !screenCopy.CursorHidden && y == screenCopy.CursorRow && x == screenCopy.CursorColumn;
                        cellBackgroundColor = isCursor ? ScreenColor.CursorBackground : cell.BackgroundColor;
                        if (cellBackgroundColor != currentBackgroundColor || x == cols.Length)
                        {
                            rect.Left  = drawingPosition.X + (blockStart * this.physicalFontMetrics.CellWidth);
                            rect.Right = drawingPosition.X + (x * this.physicalFontMetrics.CellWidth);

                            Brush backgroundBrush = this.GetBrush(context2D, this.GetColor(currentBackgroundColor));
                            if (currentBackgroundColor == ScreenColor.CursorBackground && !screenCopy.HasFocus)
                            {
                                rect.Right = rect.Right - 1.0f;
                                context2D.DrawRectangle(rect, backgroundBrush);
                            }
                            else
                            {
                                context2D.FillRectangle(rect, backgroundBrush);
                            }

                            blockStart = x;

                            currentBackgroundColor = cellBackgroundColor;
                        }
                    }
                }
            }

            // 2. Paint foregrounds
            {
                RectangleF rect  = new RectangleF();
                var        lines = screenCopy.Cells;
                for (int y = 0; y < lines.Length; y++)
                {
                    var cols = lines[y];

                    rect.Top    = drawingPosition.Y + (y * this.physicalFontMetrics.CellHeight);
                    rect.Bottom = rect.Top + this.physicalFontMetrics.CellHeight;

                    ScreenColor             currentForegroundColor   = cols.Length > 0 ? cols[0].ForegroundColor : ScreenColor.DefaultForeground;
                    ScreenCellModifications currentCellModifications = cols.Length > 0 ? cols[0].Modifications : ScreenCellModifications.None;
                    bool        currentCellUCSWIDE = cols.Length > 0 ? cols[0].Character == CjkWidth.UCSWIDE : false;
                    ScreenColor cellForegroundColor;
                    int         blockStart = 0;
                    for (int x = 0; x <= cols.Length; x++) // loop once above the upper bound
                    {
                        var cell = cols[x < cols.Length ? x : x - 1];

                        bool isCursor = !screenCopy.CursorHidden && y == screenCopy.CursorRow && x == screenCopy.CursorColumn;
                        cellForegroundColor = isCursor && screenCopy.HasFocus ? ScreenColor.CursorForeground : cell.ForegroundColor;
                        if (currentCellUCSWIDE || cellForegroundColor != currentForegroundColor || cell.Modifications != currentCellModifications || x == cols.Length)
                        {
                            rect.Left  = drawingPosition.X + (blockStart * this.physicalFontMetrics.CellWidth);
                            rect.Right = drawingPosition.X + (x * this.physicalFontMetrics.CellWidth);

                            Brush      foregroundBrush = this.GetBrush(context2D, this.GetColor(currentForegroundColor));
                            TextFormat textFormat      = this.textFormatNormal;
                            if (currentCellModifications.HasFlag(ScreenCellModifications.Bold))
                            {
                                textFormat = this.textFormatBold;
                            }

                            string text = new string(cols.Skip(blockStart).Take(x - blockStart).Select(c => char.IsWhiteSpace(c.Character) ? ' ' : c.Character).Where(ch => ch != CjkWidth.UCSWIDE).ToArray()).TrimEnd();

                            if (text.Length > 0)
                            {
                                context2D.DrawText(text, textFormat, rect, foregroundBrush, DrawTextOptions.Clip);
                            }

                            if (currentCellModifications.HasFlag(ScreenCellModifications.Underline))
                            {
                                var point1 = new Vector2(rect.Left, rect.Bottom - 1.0f) + drawingPosition;
                                var point2 = new Vector2(rect.Right, rect.Bottom - 1.0f) + drawingPosition;
                                context2D.DrawLine(point1, point2, foregroundBrush);
                            }

                            blockStart = x;

                            currentForegroundColor = cellForegroundColor;
                        }
                        currentCellUCSWIDE = cell.Character == CjkWidth.UCSWIDE;
                    }
                }
            }

            context2D.EndDraw();
        }
Пример #11
0
        private void mainGrid_Loaded(object sender, RoutedEventArgs e)
        {
            if (!_hasInitializedSurface)
            {
                int pixelWidth  = (int)(d2dRectangleBottom.ActualWidth * DisplayProperties.LogicalDpi / 96.0);
                int pixelHeight = (int)(d2dRectangleBottom.ActualHeight * DisplayProperties.LogicalDpi / 96.0);

                _ibTarget1 = new ImageBrush();
                _ibTarget2 = new ImageBrush();

                //_rectEffect = new Windows.UI.Xaml.Shapes.Rectangle() { HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch, VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Stretch, Width = pixelWidth, Height = pixelHeight };
                //mainGrid.Children.Add(_rectEffect);

                //_rectMagic = new Windows.UI.Xaml.Shapes.Rectangle() { HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch, VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Stretch, Width = pixelWidth, Height = pixelHeight };
                //mainGrid.Children.Add(_rectMagic);

                d2dRectangleBottom.Opacity = 1.0f;
                d2dRectangleBottom.Fill    = _ibTarget1;

                d2dRectangleTop.Opacity = 1.0f;
                d2dRectangleTop.Fill    = _ibTarget2;

                //_rectEffect.Fill = _ibTarget1;
                //_rectMagic.Fill = _ibTarget2;

                _deviceManager = new DeviceManager();



                _sisTarget1 = new SurfaceImageSourceTarget(pixelWidth, pixelHeight);
                _sisTarget2 = new SurfaceImageSourceTarget(pixelWidth, pixelHeight);

                _ibTarget1.ImageSource = _sisTarget1.ImageSource;
                _ibTarget2.ImageSource = _sisTarget2.ImageSource;

                _sisTarget1.OnRender += _effectRenderer.Render;
                _sisTarget2.OnRender += _magicRenderer.Render;

                _deviceManager.OnInitialize += _sisTarget1.Initialize;
                _deviceManager.OnInitialize += _effectRenderer.Initialize;

                _deviceManager.OnInitialize += _sisTarget2.Initialize;
                _deviceManager.OnInitialize += _magicRenderer.Initialize;

                _deviceManager.Initialize(DisplayProperties.LogicalDpi);

                _effectRenderer.InitializeUI(root, _rectEffect);//d2dRectangleBottom);
                _magicRenderer.InitializeUI(root, _rectMagic);

                //var fpsRenderer = new FpsRenderer();
                //fpsRenderer.Initialize(deviceManager);
                //d2dTarget.OnRender += fpsRenderer.Render;


                if (_assetUri != null && _assetUri != string.Empty)
                {
                    _effectRenderer.LoadLocalAsset(_assetUri);
                }


                _hasInitializedSurface = true;
            }

            this.Unloaded += DrawingSurfaceSIS_Unloaded;
        }