public void Draw(FastPixel.FastPixel fp, IIntentStates states)
        {
            if (_isDiscreteColored)
            {
                int       col      = 1;
                Rectangle drawRect = new Rectangle(drawArea.X, drawArea.Y, drawArea.Width, drawArea.Height);
                // Get states for each color
                IEnumerable <Color> colors = IntentHelpers.GetAlphaAffectedDiscreteColorsForIntents(states);
                foreach (Color c in colors)
                {
                    if (c != Color.Transparent && c.A > byte.MinValue)
                    {
                        fp.DrawCircle(drawRect, c);

                        if (col % 2 == 0)
                        {
                            drawRect.Y += PixelSize;
                            drawRect.X  = drawArea.X;
                        }
                        else
                        {
                            drawRect.X = drawArea.X + PixelSize;
                        }

                        col++;
                    }
                }
            }
            else
            {
                Color intentColor = IntentHelpers.GetAlphaRGBMaxColorForIntents(states);
                if (intentColor != Color.Transparent && intentColor.A > 0)
                {
                    fp.DrawCircle(drawArea, intentColor);
                }
            }
        }
Exemple #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);
            }
        }