Exemple #1
0
        void ParseAndRenderSvgFile(string svgFile)
        {
            var docBuilder = new VgDocBuilder();
            var svgParser  = new SvgParser(docBuilder);

            //TODO: ask file content from host,

            string svgContent = System.IO.File.ReadAllText(svgFile);

            WebLexer.TextSnapshot textSnapshot = new WebLexer.TextSnapshot(svgContent);
            svgParser.ParseDocument(textSnapshot);
            //
#if DEBUG
            System.Diagnostics.Debug.WriteLine(svgFile);
#endif
            var             vgDocBuilder = new VgVisualDocBuilder();
            VgVisualElement vgVisElem    = vgDocBuilder.CreateVgVisualDoc(docBuilder.ResultDocument, _vgDocHost).VgRootElem;

            var uiSprite   = new UISprite(10, 10);
            var evListener = new GeneralEventListener();
            uiSprite.AttachExternalEventListener(evListener);
            evListener.MouseDown += (s, e) =>
            {
                //hit on svg color- area
                VgHitInfo hitInfo = uiSprite.FindRenderElementAtPos(e.X, e.Y, false);
#if DEBUG
                if (hitInfo.hitElem != null)
                {
                    SvgElement domElem = hitInfo.hitElem.DomElem;
                    if (domElem != null && domElem.ElemId != null)
                    {
                        Console.WriteLine(domElem.ElemId);
                    }
                }
#endif
            };


            uiSprite.LoadVg(vgVisElem);

            _backBoard.ClearChildren();
            _backBoard.Add(uiSprite);
        }
        protected override void OnStart(AppHost host, IGameHTMLUI pcx)
        {
            _vgVisualDocHost = new VgVisualDocHost();
            _vgVisualDocHost.SetInvalidateDelegate(vgElem =>
            {
                //invalidate graphic here
            });

            _backBoard           = new BackDrawBoardUI(800, 600);
            _backBoard.BackColor = Color.White;
            host.AddChild(_backBoard);

            //load lion svg

            string svgfile = "../Test8_HtmlRenderer.Demo/Samples/Svg/others/tiger.svg";
            //string svgfile = "1f30b.svg";
            //string svgfile = "../Data/Svg/twemoji/1f30b.svg";
            //string svgfile = "../Data/1f30b.svg";
            //string svgfile = "../Data/Svg/twemoji/1f370.svg";

            VgVisualDoc     vgDocRoot = ReadSvgFile(svgfile);
            VgVisualElement vgVisElem = vgDocRoot.VgRootElem;
            var             uiSprite  = new UISprite(10, 10);

            uiSprite.LoadVg(vgVisElem);
            _backBoard.Add(uiSprite);


            //
            _redBox           = new LayoutFarm.CustomWidgets.Box(50, 50); //visual rect box
            _redBox.BackColor = Color.Red;
            _redBox.SetLocation(10, 10);
            //box1.dbugTag = 1;
            SetupActiveBoxProperties(_redBox);
            _backBoard.Add(_redBox);
            //----------------------


            //--------
            _rectBoxController = new CustomWidgets.RectBoxController();
            _rectBoxController.Init();
            _backBoard.Add(_rectBoxController);

            //foreach (var ui in rectBoxController.GetControllerIter())
            //{
            //    viewport.AddContent(ui);
            //}

            //--------
            var svgEvListener = new GeneralEventListener();

            //uiSprite.AttachExternalEventListener(svgEvListener);
            svgEvListener.MouseDown += (s, e) =>
            {
                //e.MouseCursorStyle = MouseCursorStyle.Pointer;
                ////--------------------------------------------
                //e.SetMouseCapture(rectBoxController.ControllerBoxMain);
                _rectBoxController.UpdateControllerBoxes(_redBox);
                _rectBoxController.Focus();
                //System.Console.WriteLine("click :" + (count++));
            };


            GeneralEventListener controllBoxMainListener = new GeneralEventListener();

            controllBoxMainListener.KeyDown += (s1, e1) =>
            {
                if (e1.KeyCode == UIKeys.C && e1.Ctrl)
                {
                    //test copy back image buffer from current rect area

#if DEBUG
                    //int left = rectBoxController.ControllerBoxMain.Left;
                    //int top = rectBoxController.ControllerBoxMain.Top;
                    //int width = rectBoxController.ControllerBoxMain.Width;
                    //int height = rectBoxController.ControllerBoxMain.Height;

                    //using (DrawBoard drawBoard = DrawBoardCreator.CreateNewDrawBoard(1, width, height))
                    //{

                    //    //create new draw board
                    //    drawBoard.OffsetCanvasOrigin(left, top);
                    //    _backBoard.CurrentPrimaryRenderElement.CustomDrawToThisCanvas(drawBoard, new Rectangle(0, 0, width, height));
                    //    using (var img2 = new PixelFarm.CpuBlit.ActualBitmap(width, height))
                    //    {
                    //        //copy content from drawboard to target image and save
                    //        drawBoard.RenderTo(img2, 0, 0, width, height);

                    //        PixelFarm.CpuBlit.Imaging.PngImageWriter.SaveImgBufferToPngFile(
                    //            PixelFarm.CpuBlit.ActualBitmap.GetBufferPtr(img2),
                    //            img2.Stride,
                    //            img2.Width,
                    //            img2.Height,
                    //            "d:\\WImageTest\\tiger.png");
                    //    }
                    //    //copy content from drawboard to target image and save
                    //}
#endif
                }
            };

            _rectBoxController.ControllerBoxMain.AttachExternalEventListener(controllBoxMainListener);
        }