public void UpdatePreview(/*Vixen.Preview.PreviewElementIntentStates elementStates*/)
        {
            if (!gdiControl.IsUpdating)
            {
                Vixen.Sys.Managers.ElementManager elements = VixenSystem.Elements;

                Element[] elementArray = elements.Where(
                    e => e.State.Any(i => ((i as IIntentState <LightingValue>) != null) ? ((i as IIntentState <LightingValue>).GetValue().Intensity > 0) :
                                     ((i as IIntentState <RGBValue>) != null) && ((i as IIntentState <RGBValue>).GetValue().Intensity > 0))
                    ).ToArray();

                if (elementArray.Length == 0)
                {
                    if (needsUpdate)
                    {
                        needsUpdate = false;
                        gdiControl.BeginUpdate();
                        gdiControl.EndUpdate();
                        gdiControl.Invalidate();
                    }

                    toolStripStatusFPS.Text = "0 fps";
                    return;
                }

                needsUpdate = true;

                CancellationTokenSource tokenSource = new CancellationTokenSource();
                gdiControl.BeginUpdate();

                try
                {
                    elementArray.AsParallel().WithCancellation(tokenSource.Token).ForAll(element =>
                    {
                        if (element != null)
                        {
                            ElementNode node = VixenSystem.Elements.GetElementNodeForElement(element);
                            if (node != null)
                            {
                                List <PreviewPixel> pixels;
                                if (NodeToPixel.TryGetValue(node, out pixels))
                                {
                                    foreach (PreviewPixel pixel in pixels)
                                    {
                                        pixel.Draw(gdiControl.FastPixel, element.State);
                                    }
                                }
                            }
                        }
                    });
                } catch (Exception e)
                {
                    Logging.ErrorException(e.Message, e);
                }

                gdiControl.EndUpdate();
                gdiControl.Invalidate();

                toolStripStatusFPS.Text = string.Format("{0} fps", gdiControl.FrameRate);
            }
        }
Esempio n. 2
0
        public void Update()
        {
            Vixen.Sys.Managers.ElementManager elements = VixenSystem.Elements;
            Element[] elementArray = elements.Where(e => e.State.Where(i => (i as IIntentState <LightingValue>) != null).Where(i => (i as IIntentState <LightingValue>).GetValue().Intensity > 0).Any()).ToArray();

            try
            {
                Stopwatch w = Stopwatch.StartNew();

                // Calculate our actual frame rate
                this.frameCount++;

                if (DateTime.UtcNow.Subtract(this.time).TotalSeconds >= 1)
                {
                    this.fps        = this.frameCount;
                    this.frameCount = 0;
                    this.time       = DateTime.UtcNow;
                }
                int iPixels = 0;

                // since we need to clear the display and re-draw it, it flickers if we do it live
                // so, create a temp, local, compatible bitmap render target for drawing
                // later we'll blt this to the real render target in one feel swoop
                using (var rt = this.RenderTarget.CreateCompatibleRenderTarget())
                {
                    rt.BeginDraw();

                    rt.Clear(new D2D.ColorF(0, 0, 0, 1f));
                    //rt.Clear();

                    if (Background != null)
                    {
                        rt.DrawBitmap(Background);
                    }
                    if (NodeToPixel.Count == 0)
                    {
                        Reload();
                    }

                    CancellationTokenSource tokenSource = new CancellationTokenSource();



                    try
                    {
                        elementArray.AsParallel().WithCancellation(tokenSource.Token).ForAll(element =>
                        {
                            ElementNode node;

                            if (!ElementNodeCache.TryGetValue(element.Id, out node))
                            {
                                if (element != null)
                                {
                                    node = VixenSystem.Elements.GetElementNodeForElement(element);
                                    if (node != null)
                                    {
                                        ElementNodeCache.TryAdd(element.Id, node);
                                    }
                                }
                            }

                            if (node != null)
                            {
                                Color pixColor;


                                //TODO: Discrete Colors
                                pixColor = IntentHelpers.GetAlphaRGBMaxColorForIntents(element.State);

                                if (pixColor.A > 0)
                                {
                                    using (var brush = rt.CreateSolidColorBrush(pixColor.ToColorF())) {
                                        List <PreviewPixel> pixels;
                                        if (NodeToPixel.TryGetValue(node, out pixels))
                                        {
                                            iPixels += pixels.Count;
                                            pixels.ForEach(p => RenderPixel(rt, /*channelIntentState, */ p, brush));
                                        }
                                    }
                                }
                            }
                        });
                    }
                    catch (Exception)
                    {
                        tokenSource.Cancel();
                        //Console.WriteLine(e.Message);
                    }


                    // done w/local bitmap render target
                    rt.EndDraw();

                    // now get the bitmap and write it to the real target
                    this.RenderTarget.BeginDraw();

                    this.RenderTarget.DrawBitmap(rt.Bitmap);

                    // Draw a little FPS in the top left corner
                    w.Stop();
                    var  ms = w.ElapsedMilliseconds;
                    long avgtime, maxtime;
                    AddUpdateTime(ms, out maxtime, out avgtime);

                    string text = string.Format("FPS {0} \nPoints {1} \nPixels {3} \nRender Time:{2} \nMax Render: {4} \nAvg Render: {5}", this.fps, elements.Count(), w.ElapsedMilliseconds, iPixels, avgtime.ToString("00"), avgtime.ToString("00"));

                    using (var textBrush = this.RenderTarget.CreateSolidColorBrush(Color.White.ToColorF())) {
                        this.RenderTarget.DrawText(text, this.textFormat, new D2D.RectF(10, 10, 100, 20), textBrush);
                    }

                    // All done!
                    this.RenderTarget.EndDraw();
                }
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }