Beispiel #1
0
        public CompositionDevice(Adapter1 adapter, Device2D device2D)
        {
            Contract.Requires(device2D != null);

            _DxgiFactory = new Lazy<Factory1>();

            Adapter1 newAdapter = adapter;

            if(adapter == null)
            {
                newAdapter = _DxgiFactory.Value.GetAdapter1(0);
            }
            #if DEBUG
            _Device3D = new Device1(
                newAdapter, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug, FeatureLevel.Level_10_0);
            #else
            _Device3D = new Device1(
                newAdapter,
                DeviceCreationFlags.BgraSupport,
                FeatureLevel.Level_10_0);
            #endif
            if(adapter == null)
            {
                newAdapter.Dispose();
            }

            _Compositor = new Compositor(_Device3D, device2D);

            device2D.Resources.RegisterEffect<ColorOutputEffect>();
            device2D.Resources.RegisterEffect<GaussianBlurEffect>();
            device2D.Resources.RegisterEffect<DropShadowEffect>();
            device2D.Resources.RegisterEffect<BoxBlurEffect>();
        }
Beispiel #2
0
        int GetColorArraySize2D(Device2D device)
        {
            int maxRow    = ChromaAnimationAPI.GetMaxRow(device);
            int maxColumn = ChromaAnimationAPI.GetMaxColumn(device);

            return(maxRow * maxColumn);
        }
Beispiel #3
0
 public void Dispose()
 {
     ImagingFactory.Dispose();
     Factory2D.Dispose();
     FontFactory.Dispose();
     SwapChain.Dispose();
     Device2D.Dispose();
     DeviceContext.Dispose();
 }
Beispiel #4
0
        protected Painter(Device2D device2D)
        {
            Contract.Requires(device2D != null);

            _BoundThread = Thread.CurrentThread;
            _Device2D = device2D;

            Contract.Assert(Device2D.Equals(device2D));
        }
Beispiel #5
0
        public ShaderCache(Device device3D, Device2D device2D)
        {
            Contract.Requires(device3D != null);
            Contract.Requires(device2D != null);

            device2D.Diagnostics.Register(_CachedShaderCount);

            _Shaders = new List<PixelShader>();

            _Device3D = device3D;
        }
Beispiel #6
0
        public void Postrender(RenderContext context)
        {
            //context.CommandList.ResourceTransition(Presenter.BackBuffer, ResourceState.RenderTarget, ResourceState.Present);

            Presenter.EndDraw(context.CommandList);

            Device2D.Draw(Window);

            context.CommandList.FinishFrame(Presenter);
            Presenter.Present();
        }
Beispiel #7
0
        void SetAmbientColor2D(Device2D device, int[] colors, int ambientColor)
        {
            int size = GetColorArraySize2D(device);

            for (int i = 0; i < size; ++i)
            {
                if (colors[i] == 0)
                {
                    colors[i] = ambientColor;
                }
            }
        }
Beispiel #8
0
        void SetupAnimation2D(string path, Device2D device)
        {
            int animationId = ChromaAnimationAPI.GetAnimation(path);

            if (animationId == -1)
            {
                animationId = ChromaAnimationAPI.CreateAnimationInMemory((int)DeviceType.DE_2D, (int)device);
                ChromaAnimationAPI.CopyAnimation(animationId, path);
                ChromaAnimationAPI.CloseAnimation(animationId);
                ChromaAnimationAPI.MakeBlankFramesName(path, 1, 0.1f, 0);
            }
        }
Beispiel #9
0
        public PaintingDevice(Device2D device2D, Device device3D)
        {
            Contract.Requires(device2D != null);
            Contract.Requires(device3D != null);

            _Factory2D = new Factory(FactoryType.SingleThreaded);

            _PainterContext = new Painter(_Factory2D, device2D, device3D);

            Contract.Assert(Factory2D == _Factory2D);
            Contract.Assert(Painter == _PainterContext);
        }
Beispiel #10
0
        public static IntPtr GetDeviceHandle(this Canvas canvas, Device2D device2D)
        {
            Contract.Requires(canvas != null);
            Contract.Requires(device2D != null);

            Canvas.ResolvedContext context = device2D.Resources.ResolveCanvas(canvas);

            if(context != null)
            {
                return ((Surface2D)context.Surface2D).DeviceHandle;
            }

            return IntPtr.Zero;
        }
Beispiel #11
0
        public static Canvas CreateIcon(Device2D device2D)
        {
            Contract.Requires(device2D != null);

            int width;
            int height;

            byte[] rgbaData;

            using(Icon icon = new Icon(Properties.Resources.frost_icon, 256, 256))
            {
                using(Bitmap bitmap = icon.ToBitmap())
                {
                    width = bitmap.Width;
                    height = bitmap.Height;

                    rgbaData = new byte[width * height * 4];

                    int index = 0;

                    for(int y = 0; y < height; ++y)
                    {
                        for(int x = 0; x < width; ++x)
                        {
                            System.Drawing.Color pixel = bitmap.GetPixel(x, y);

                            float alpha = pixel.A / 255.0f;

                            rgbaData[index + 0] = Convert.ToByte(pixel.R * alpha);
                            rgbaData[index + 1] = Convert.ToByte(pixel.G * alpha);
                            rgbaData[index + 2] = Convert.ToByte(pixel.B * alpha);
                            rgbaData[index + 3] = pixel.A;

                            index += 4;
                        }
                    }
                }
            }

            Canvas canvas = new Canvas(new Size(width, height), SurfaceUsage.Normal);

            device2D.Resources.Copy(rgbaData, canvas);

            return canvas;
        }
Beispiel #12
0
        public GeometryNode(Shape geometry, float resolution, Device2D device2D)
        {
            Contract.Requires(geometry != null);
            Contract.Requires(resolution >= double.MinValue && resolution <= double.MaxValue);
            Contract.Requires(device2D != null);

            Rectangle region = device2D.Geometry.MeasureRegion(geometry);

            SetRegion(ref region);

            _Geometry = geometry;

            Shape simplified = device2D.Geometry.Simplify(geometry, 1.0f / resolution);

            simplified.ExtractTo(this);

            _Lines = _BuiltLines.ToArray();
        }
Beispiel #13
0
        public Painter(Factory factory2D, Device2D device2D, Device device3D)
            : base(device2D)
        {
            Contract.Requires(factory2D != null);
            Contract.Requires(device2D != null);
            Contract.Requires(device3D != null);

            _States = new Stack<State>();

            _Drawer = new Drawer(factory2D);

            _Device2D = device2D;
            _Device3D = device3D;
            _Factory2D = factory2D;

            device2D.Diagnostics.Register(_FrameDuration);

            _Watch = new Stopwatch();
        }
Beispiel #14
0
        private Sample ComputeTree(ref Rectangle region, Device2D device2D)
        {
            Sample.Location empty = new Sample.Location();

            return ComputeTree(ref region, device2D, ref empty, ref empty, ref empty, ref empty);
        }
Beispiel #15
0
        public void Render(Canvas canvas, Device2D device2D)
        {
            Contract.Requires(canvas != null);
            Contract.Requires(device2D != null);

            IntPtr sharedHandle = canvas.GetDeviceHandle(device2D);

            if(sharedHandle != _SharedHandle)
            {
                _SharedMutex.SafeDispose();
                _DependentView.SafeDispose();
                _SharedTexture.SafeDispose();

                _SharedMutex = null;
                _DependentView = null;
                _SharedTexture = null;

                if(sharedHandle != IntPtr.Zero)
                {
                    _SharedTexture = _Device3D.OpenSharedResource<Texture2D>(sharedHandle);

                    _SharedMutex = _SharedTexture.QueryInterface<KeyedMutex>();

                    _DependentView = new ShaderResourceView(_Device3D, _SharedTexture);

                    _SharedHandle = sharedHandle;
                }
            }

            if(_SharedMutex != null)
            {
                _SharedMutex.AcquireSync();

                try
                {
                    if(_SharedTexture != null)
                    {
                        var textureVariable = _Effect.GetVariableByName("tex2D");

                        Contract.Assert(textureVariable != null);

                        var shaderResource = textureVariable.AsShaderResource();

                        Contract.Assert(shaderResource != null);

                        shaderResource.SetResource(_DependentView);

                        _EffectPass.Apply();

                        _Device3D.Draw(_VertexCount, 0);
                    }
                }
                finally
                {
                    _SharedMutex.ReleaseSync();
                }
            }
        }
Beispiel #16
0
        public void Reset(Canvas target, Device2D device2D)
        {
            ShapedText shapedOutput = new ShapedText();

            const string plainText = "Hello world!";

            device2D.TextShaper.Begin(shapedOutput, plainText);

            device2D.TextShaper.AnalyzeScripts();

            device2D.TextShaper.SetPointSize(plainText, 95.0f);

            device2D.TextShaper.End();

            List<GlyphOutline> outlines = new List<GlyphOutline>();

            foreach(TextShaper.Span span in shapedOutput.Spans)
            {
                foreach(int clusterIndex in span.Clusters)
                {
                    TextShaper.Cluster cluster = shapedOutput.Clusters[clusterIndex];

                    outlines.Add(
                        device2D.Resources.GetGlyphOutline(
                            cluster.Glyphs,
                            false,
                            false,
                            span.FontMetrics.FontId,
                            shapedOutput.Glyphs));
                }
            }

            device2D.Painter.Begin(target);

            device2D.Painter.SetBrush(Resources.Foreground);

            foreach(TextShaper.Span span in shapedOutput.Spans)
            {
                float emSize = span.FontMetrics.MeasureEm(span.PointSize);

                foreach(int clusterIndex in span.Clusters)
                {
                    float advance = shapedOutput.Clusters[clusterIndex].Advance;

                    if (_AreRegionsDisplayed)
                    {
                        device2D.Painter.SaveState();

                        device2D.Painter.StrokeWidth = 0.5f;
                        device2D.Painter.LineStyle = LineStyle.Dash;

                        device2D.Painter.SetBrush(Color.Red);
                        device2D.Painter.StrokeRectangle(0, 0, advance, emSize);
                        device2D.Painter.SetBrush(Resources.Foreground);

                        device2D.Painter.RestoreState();
                    }

                    float baseline = outlines[clusterIndex].Baseline;

                    device2D.Painter.SaveState();

                    device2D.Painter.Scale(emSize, emSize);
                    device2D.Painter.Translate(0, baseline);

                    if(outlines[clusterIndex].Shape != null)
                    {
                        device2D.Painter.Fill(outlines[clusterIndex].Shape);
                    }

                    device2D.Painter.RestoreState();

                    device2D.Painter.Translate(advance, 0);
                }
            }

            device2D.Painter.End();
        }
Beispiel #17
0
        private void TestTest(Sample s, Device2D device2D)
        {
            Rectangle region = s.Region;

            device2D.Painter.StrokeRectangle(region);

            /*float mag = 50;

            Frost.Point tl =
                new Frost.Point(
                    region.Left + s.TopLeft.Intersection.X * mag,
                    region.Top + s.TopLeft.Intersection.Y * mag);
            Frost.Point tr =
                new Frost.Point(
                    region.Right + s.TopRight.Intersection.X * mag,
                    region.Top + s.TopRight.Intersection.Y * mag);
            Frost.Point bl =
                new Frost.Point(
                    region.Left + s.BottomLeft.Intersection.X * mag,
                    region.Bottom + s.BottomLeft.Intersection.Y * mag);
            Frost.Point br =
                new Frost.Point(
                    region.Right + s.BottomRight.Intersection.X * mag,
                    region.Bottom + s.BottomRight.Intersection.Y * mag);

            tl = s.TopLeft.Intersection;
            tr = s.TopRight.Intersection;
            bl = s.BottomLeft.Intersection;
            br = s.BottomRight.Intersection;

            mDevice2D.Painter.SetBrush(Color.Green);
            if(s.TopLeft.Distance > 0.0)
                mDevice2D.Painter.Stroke(region.Left, region.Top, tl.X, tl.Y);
            if(s.TopRight.Distance > 0.0)
                mDevice2D.Painter.Stroke(region.Right, region.Top, tr.X, tr.Y);
            if(s.BottomLeft.Distance > 0.0)
                mDevice2D.Painter.Stroke(region.Left, region.Bottom, bl.X, bl.Y);
            if(s.BottomRight.Distance > 0.0)
                mDevice2D.Painter.Stroke(region.Right, region.Bottom, br.X, br.Y);//*/
            device2D.Painter.SetBrush(Color.Red);

            foreach(Sample ss in s.Children)
            {
                TestTest(ss, device2D);
            }
        }
Beispiel #18
0
        public void Reset(Canvas target, Device2D device2D)
        {
            if(device2D.Resources.FindEffect<DistanceEffectSettings>() == null)
            {
                device2D.Resources.RegisterEffect<DistanceFieldEffect>();
            }

            ShapedText output = new ShapedText();

            const string text = "M";

            device2D.TextShaper.Begin(output, text);
            device2D.TextShaper.AnalyzeScripts();
            device2D.TextShaper.SetFamily(text, "Arno Pro");
            device2D.TextShaper.SetPointSize(text, 12.0f);
            device2D.TextShaper.SetFeatures(text, new FontFeatureCollection(new[] {new FontFeature("swsh")}));
            device2D.TextShaper.End();

            GlyphOutline outline =
                device2D.Resources.GetGlyphOutline(
                    new IndexedRange(0, 1),
                    false,
                    false,
                    output.Spans[0].FontMetrics.FontId,
                    output.Glyphs);

            GC.Collect(4);

            Canvas test2 = new Canvas(new Size(128, 128), SurfaceUsage.Normal);

            Stopwatch watch = new Stopwatch();
            watch.Start();
            Canvas test = _Distance.CreateField(
                outline.Shape, outline.Baseline, device2D);
            watch.Stop();

            //mForm.Text = string.Format("Time: {0}", watch.ElapsedMilliseconds);

            Debug.WriteLine("Time: {0}", watch.ElapsedMilliseconds);

            Rectangle reg = device2D.Geometry.MeasureRegion(outline.Shape);

            device2D.Painter.Begin(target);
            device2D.Painter.Translate(test.Region.X, test.Region.Y);

            device2D.Painter.Translate((0.5f - (reg.Width / 2.0f)) * 400, (0.5f - (reg.Height / 2.0f)) * 400);

            // translate the glyph to the left corner of the EM square
            device2D.Painter.Translate(-reg.X * 400, -reg.Y * 400);
            device2D.Painter.SetBrush(Color.Black);
            device2D.Painter.Scale(400, 400);
            device2D.Painter.Fill(outline.Shape);
            device2D.Painter.End(); //*/

            device2D.Compositor.Begin(target, Retention.RetainData);

            DistanceEffectSettings settings;
            device2D.Compositor.Translate(400, 0);
            device2D.Compositor.Scale(3.125f, 3.125f);
            device2D.Compositor.ApplyEffect(settings);
            device2D.Compositor.Composite(test);
            device2D.Compositor.End();

            device2D.Painter.Begin(target, Retention.RetainData);
            device2D.Painter.SetBrush(Color.IndianRed);
            device2D.Painter.IsAntialiased = Antialiasing.Aliased;
            device2D.Painter.StrokeWidth = DistanceField.EmLength / 400;
            device2D.Painter.Scale(1.0f / DistanceField.EmLength, 1.0f / DistanceField.EmLength);
            device2D.Painter.Scale(400, 400);
            TestTest(_Distance.Sample, device2D);
            device2D.Painter.End();

            //device2D.Resources.DumpToFiles(null, SurfaceUsage.Normal); //*/
        }
Beispiel #19
0
        public static void ResetDemo(
			IDemoContext context, Canvas target, Device2D device2D)
        {
            Contract.Requires(context != null);
            Contract.Requires(target != null);
            Contract.Requires(device2D != null);

            Rectangle left = Rectangle.FromEdges(
                0, 0, 213, target.Region.Bottom);
            Rectangle middle = Rectangle.FromEdges(
                left.Right, 0, target.Region.Right - 213, target.Region.Bottom);
            Rectangle right = Rectangle.FromEdges(
                middle.Right, 0, target.Region.Right, target.Region.Bottom);

            Rectangle demoRegion = middle.Contract(25);

            // create a new canvas for the demo contents
            Canvas demoTarget = new Canvas(demoRegion.Size);

            // resolve the new resource to avoid timing problems
            device2D.Resources.ResolveCanvas(demoTarget);

            // track memory usage and reset the clock
            long oldMemoryUsage = GC.GetTotalMemory(true);

            _Watch.Reset();
            _Watch.Start();

            // execute the demo
            context.Reset(demoTarget, device2D);

            // stop the clock and determine the garbage produced
            _Watch.Stop();

            long memoryDelta = GC.GetTotalMemory(false) - oldMemoryUsage;

            // query the subsystem timers
            IDeviceCounter<TimeSpan> compositionFrameDuration;
            IDeviceCounter<TimeSpan> paintingFrameDuration;

            device2D.Diagnostics.Query(
                "Composition", "FrameDuration", out compositionFrameDuration);
            device2D.Diagnostics.Query(
                "Painting", "FrameDuration", out paintingFrameDuration);

            ////////////////////////////////
            string timeString = string.Format(
                "Time Taken: {0} ms", _Watch.ElapsedMilliseconds);

            Rectangle timeMetrics = MeasureText(timeString, device2D);

            Rectangle timeRegion = timeMetrics.AlignRelativeTo(
                middle, Alignment.Center, Axis.Horizontal);

            /////////////////////////////
            string subsystemString =
                string.Format(
                    "Painting: {0} ms \u2219 Composition: {1} ms",
                    paintingFrameDuration.Value.Milliseconds,
                    compositionFrameDuration.Value.Milliseconds);

            Rectangle subsystemMetrics = MeasureText(subsystemString, device2D);

            Rectangle subsystemRegion = subsystemMetrics.AlignRelativeTo(
                middle, Alignment.Center, Axis.Horizontal);

            subsystemRegion = subsystemRegion.Translate(
                0, middle.Bottom - subsystemRegion.Height);

            ////////////////////////////////
            string memoryString = string.Format(
                "Memory: {0:N0} KB", GC.GetTotalMemory(true) / 1024);

            string garbageString = string.Format(
                "Garbage: {0:N0} KB", memoryDelta / 1024);

            const string snapshotString = "SNAPSHOT";

            Rectangle snapshotMetrics = MeasureText(snapshotString, device2D);

            const string optionsString = "OPTIONS";

            Rectangle optionsMetrics = MeasureText(optionsString, device2D);

            snapshotMetrics = snapshotMetrics.AlignRelativeTo(
                right, Alignment.Center, Axis.Horizontal);
            optionsMetrics = optionsMetrics.AlignRelativeTo(
                left, Alignment.Center, Axis.Horizontal);

            device2D.Painter.Begin(target);

            // clear the background to the background color
            device2D.Painter.SetBrush(Resources.Background);
            device2D.Painter.FillRectangle(target.Region);

            device2D.Painter.SetBrush(Resources.UIColor);

            device2D.Painter.FillRectangle(left);
            device2D.Painter.FillRectangle(right);

            Rectangle timePanel = timeRegion.Expand(
                timeRegion.Height,
                timeRegion.Height,
                timeRegion.Height,
                timeRegion.Height / 4.0f);

            device2D.Painter.FillRectangle(
                timePanel, new Size(timeRegion.Height / 2.0f));

            Rectangle subsystemPanel = subsystemRegion.Expand(
                subsystemRegion.Height,
                subsystemRegion.Height / 4.0f,
                subsystemRegion.Height,
                subsystemRegion.Height);

            device2D.Painter.FillRectangle(
                subsystemPanel, new Size(subsystemRegion.Height / 2.0f));

            device2D.Painter.SetBrush(Resources.ActiveButton);

            device2D.Painter.SaveState();
            device2D.Painter.LineStyle = LineStyle.Dash;
            device2D.Painter.StrokeRectangle(demoRegion.Expand(1));
            device2D.Painter.RestoreState();

            device2D.Painter.StrokeLine(
                left.Right, left.Top, left.Right, left.Bottom);
            device2D.Painter.StrokeLine(
                right.Left, right.Top, right.Left, right.Bottom);

            device2D.Painter.SetBrush(Resources.Foreground);

            device2D.Painter.SaveState();
            device2D.Painter.Translate(timeRegion.X, timeRegion.Y);
            DrawText(Point.Empty, timeString, device2D);
            device2D.Painter.RestoreState();

            device2D.Painter.SaveState();
            device2D.Painter.Translate(subsystemRegion.X, subsystemRegion.Y);
            DrawText(Point.Empty, subsystemString, device2D);
            device2D.Painter.RestoreState();

            device2D.Painter.SaveState();

            device2D.Painter.Translate(0, timeRegion.Height);

            DrawText(snapshotMetrics.Location, snapshotString, device2D);

            device2D.Painter.StrokeLine(
                snapshotMetrics.Left,
                snapshotMetrics.Bottom,
                snapshotMetrics.Right,
                snapshotMetrics.Bottom);

            device2D.Painter.Translate(
                right.Left + (timeRegion.Height / 2.0f),
                (timeRegion.Height * 2) + (timeRegion.Height * 0.25f));

            DrawText(Point.Empty, memoryString, device2D);

            device2D.Painter.Translate(
                0, (timeRegion.Height * 2) + (timeRegion.Height * 0.25f));

            DrawText(Point.Empty, garbageString, device2D);

            device2D.Painter.RestoreState();

            device2D.Painter.SaveState();

            device2D.Painter.Translate(0, timeRegion.Height);

            DrawText(optionsMetrics.Location, optionsString, device2D);

            device2D.Painter.StrokeLine(
                optionsMetrics.Left,
                optionsMetrics.Bottom,
                optionsMetrics.Right,
                optionsMetrics.Bottom);

            device2D.Painter.RestoreState();

            GenerateMenu(
                context,
                target,
                device2D,
                (timeRegion.Height * 3) + (timeRegion.Height * 0.25f),
                left.Width);

            device2D.Painter.End();

            device2D.Compositor.Begin(target, Retention.RetainData);
            device2D.Compositor.Composite(demoTarget, demoRegion.Location);
            device2D.Compositor.End();
        }
Beispiel #20
0
        private static Rectangle MeasureText(string text, Device2D device2D)
        {
            ShapedText shapedOutput = new ShapedText();

            device2D.TextShaper.Begin(shapedOutput, text);
            device2D.TextShaper.AnalyzeScripts();
            device2D.TextShaper.SetFamily(text, "Lucida Console");
            device2D.TextShaper.SetPointSize(text, 12.0f);
            device2D.TextShaper.End();

            float maxWidth = 0.0f;
            float maxHeight = 0.0f;

            foreach(TextShaper.Span span in shapedOutput.Spans)
            {
                maxHeight = Math.Max(
                    maxHeight,
                    span.FontMetrics.MeasureEm(span.PointSize));

                foreach(int clusterIndex in span.Clusters)
                {
                    maxWidth += shapedOutput.Clusters[clusterIndex].Advance;
                }
            }

            return new Rectangle(Point.Empty, maxWidth, maxHeight);
        }
Beispiel #21
0
        private static void GenerateMenu(
			IDemoContext context,
			Canvas target,
			Device2D device2D,
			float listTop,
			float listWidth)
        {
            Contract.Requires(context != null);
            Contract.Requires(target != null);
            Contract.Requires(device2D != null);

            var items = new List<KeyValuePair<string, KeyValuePair<Rectangle, bool>>>();

            Rectangle region = target.Region;

            region = region.Translate(0, listTop);

            float itemHeight = 0.0f;
            float itemSpacing = 0.0f;

            Thickness itemExpansion = Thickness.Empty;

            foreach(var item in context.Settings)
            {
                string currentText = item.IsActive ? item.ActiveText : item.InactiveText;

                string finalText = string.Format(
                    "[{0}] {1}", items.Count + 1, currentText);

                var metrics = MeasureText(finalText, device2D);

                metrics = metrics.Translate(region.Location);

                if(itemHeight.Equals(0.0f) && itemSpacing.Equals(0.0f))
                {
                    itemHeight = metrics.Height * 2.00f;
                    itemSpacing = metrics.Height * 0.25f;
                    itemExpansion = new Thickness(itemHeight / 4.0f);
                }

                items.Add(
                    new KeyValuePair<string, KeyValuePair<Rectangle, bool>>(
                        finalText, new KeyValuePair<Rectangle, bool>(metrics, item.IsActive)));

                region = region.Translate(0, itemHeight + itemSpacing);
            }

            for(int index = 0; index < items.Count; index++)
            {
                var item = items[index];

                Rectangle settingRegion = item.Value.Key.Expand(itemExpansion);

                settingRegion = settingRegion.Resize(listWidth, settingRegion.Height);
                settingRegion = settingRegion.Expand(itemHeight, 0, 0, 0);

                device2D.Painter.SetBrush(
                    item.Value.Value ? Resources.ActiveButton : Resources.UIColor);
                device2D.Painter.FillRectangle(settingRegion, new Size(itemHeight / 3.0f));

                device2D.Painter.SetBrush(Resources.ActiveButton);
                device2D.Painter.StrokeRectangle(
                    settingRegion, new Size(itemHeight / 3.0f));

                device2D.Painter.SetBrush(
                    item.Value.Value ? Resources.InactiveButton : Resources.Foreground);

                DrawText(item.Value.Key.Location, item.Key, device2D);
            }
        }
Beispiel #22
0
        private static void DrawText(Point location, string text, Device2D device2D)
        {
            ShapedText shapedOutput = new ShapedText();

            device2D.TextShaper.Begin(shapedOutput, text);
            device2D.TextShaper.AnalyzeScripts();
            device2D.TextShaper.SetFamily(text, "Lucida Console");
            device2D.TextShaper.SetPointSize(text, 12.0f);
            device2D.TextShaper.End();

            List<GlyphOutline> outlines = new List<GlyphOutline>();

            foreach(TextShaper.Span span in shapedOutput.Spans)
            {
                foreach(int clusterIndex in span.Clusters)
                {
                    TextShaper.Cluster cluster = shapedOutput.Clusters[clusterIndex];

                    outlines.Add(
                        device2D.Resources.GetGlyphOutline(
                            cluster.Glyphs,
                            false,
                            false,
                            span.FontMetrics.FontId,
                            shapedOutput.Glyphs));
                }
            }

            device2D.Painter.SaveState();
            device2D.Painter.Translate(location);

            foreach(TextShaper.Span span in shapedOutput.Spans)
            {
                float emSize = span.FontMetrics.MeasureEm(span.PointSize);

                foreach(int clusterIndex in span.Clusters)
                {
                    device2D.Painter.SaveState();

                    device2D.Painter.Scale(emSize, emSize);
                    device2D.Painter.Translate(0, outlines[clusterIndex].Baseline);

                    if(outlines[clusterIndex].Shape != null)
                    {
                        device2D.Painter.Fill(outlines[clusterIndex].Shape);
                    }

                    device2D.Painter.RestoreState();

                    device2D.Painter.Translate(
                        shapedOutput.Clusters[clusterIndex].Advance, 0);
                }
            }

            device2D.Painter.RestoreState();
        }
Beispiel #23
0
 public void Reset(Canvas target, Device2D device2D)
 {
     Contract.Requires(device2D != null);
     Contract.Requires(target != null);
 }
Beispiel #24
0
        private Sample ComputeTree(
			ref Rectangle region,
			Device2D device2D,
			ref Sample.Location parentTopLeft,
			ref Sample.Location parentTopRight,
			ref Sample.Location parentBottomLeft,
			ref Sample.Location parentBottomRight,
			double level = 1.0)
        {
            Sample sample = new Sample(ref region);

            // define the points on the rectangle to test
            Point topLeft = new Point(region.Left, region.Top);
            Point topRight = new Point(region.Right, region.Top);
            Point bottomLeft = new Point(region.Left, region.Bottom);
            Point bottomRight = new Point(region.Right, region.Bottom);

            // the method was not called recursively, so compute the distances to each point
            if(level <= 1.0)
            {
                mDecomposer.Query(
                    ref topLeft, device2D, out sample.TopLeft.Distance, out sample.TopLeft.Intersection);
                mDecomposer.Query(
                    ref topRight, device2D, out sample.TopRight.Distance, out sample.TopRight.Intersection);
                mDecomposer.Query(
                    ref bottomLeft, device2D, out sample.BottomLeft.Distance, out sample.BottomLeft.Intersection);
                mDecomposer.Query(
                    ref bottomRight, device2D, out sample.BottomRight.Distance, out sample.BottomRight.Intersection);
            }
            else
            {
                // use distances already computed by the caller
                sample.TopLeft = parentTopLeft;
                sample.TopRight = parentTopRight;
                sample.BottomLeft = parentBottomLeft;
                sample.BottomRight = parentBottomRight;
            }

            // do not subdivide lower than a resolved resolution unit
            if(level > MaxLevel)
            {
                return sample;
            }

            int boundaryContains = 0;

            Thickness thick = new Thickness(MaximumError);

            Rectangle boundary = region.Expand(thick);

            boundaryContains += boundary.Contains(sample.TopLeft.Intersection) ? 1 : 0;
            boundaryContains += boundary.Contains(sample.TopRight.Intersection) ? 1 : 0;
            boundaryContains += boundary.Contains(sample.BottomLeft.Intersection) ? 1 : 0;
            boundaryContains += boundary.Contains(sample.BottomRight.Intersection) ? 1 : 0;

            // compute the intersection of the region with the shape
            int boundaryIntersection = 0;

            boundaryIntersection += sample.TopLeft.Distance >= 0.0 ? 1 : -1;
            boundaryIntersection += sample.TopRight.Distance >= 0.0 ? 1 : -1;
            boundaryIntersection += sample.BottomLeft.Distance >= 0.0 ? 1 : -1;
            boundaryIntersection += sample.BottomRight.Distance >= 0.0 ? 1 : -1;

            double s = Math.Abs(boundaryIntersection) != 4 || boundaryContains > 0 ? 0.0 : 1.0;

            double error = MinimumError + ((MaximumError - MinimumError) * s);

            float halfWidth = region.Width / 2.0f;
            float halfHeight = region.Height / 2.0f;

            Point leftCenter = new Point(region.Left, region.Top + halfHeight);
            Point topCenter = new Point(region.Left + halfWidth, region.Top);
            Point rightCenter = new Point(region.Right, region.Top + halfHeight);
            Point bottomCenter = new Point(region.Left + halfWidth, region.Bottom);
            Point center = new Point(region.Left + halfWidth, region.Top + halfHeight);

            Sample.Location centerLocation = new Sample.Location();

            mDecomposer.Query(
                ref center, device2D, out centerLocation.Distance, out centerLocation.Intersection);

            boundaryContains += boundary.Contains(centerLocation.Intersection) ? 1 : 0;

            if(boundaryContains == 0)
            {
                return sample;
            }

            Sample.Location leftCenterLocation = new Sample.Location();
            Sample.Location topCenterLocation = new Sample.Location();
            Sample.Location rightCenterLocation = new Sample.Location();
            Sample.Location bottomCenterLocation = new Sample.Location();

            mDecomposer.Query(
                ref leftCenter, device2D, out leftCenterLocation.Distance, out leftCenterLocation.Intersection);
            mDecomposer.Query(
                ref topCenter, device2D, out topCenterLocation.Distance, out topCenterLocation.Intersection);
            mDecomposer.Query(
                ref rightCenter,
                device2D,
                out rightCenterLocation.Distance,
                out rightCenterLocation.Intersection);
            mDecomposer.Query(
                ref bottomCenter,
                device2D,
                out bottomCenterLocation.Distance,
                out bottomCenterLocation.Intersection);

            double reconstructedLeftCenter = (sample.TopLeft.Distance + sample.BottomLeft.Distance) / 2.0;
            double reconstructedTopCenter = (sample.TopLeft.Distance + sample.TopRight.Distance) / 2.0;
            double reconstructedRightCenter = (sample.TopRight.Distance + sample.BottomRight.Distance) / 2.0;
            double reconstructedBottomCenter = (sample.BottomLeft.Distance + sample.BottomRight.Distance) /
                                               2.0;
            double reconstructedCenter = (reconstructedTopCenter + reconstructedBottomCenter) / 2.0;

            bool isLeftCenterInvalid = Math.Abs(reconstructedLeftCenter - leftCenterLocation.Distance) >
                                       error;
            bool isTopCenterInvalid = Math.Abs(reconstructedTopCenter - topCenterLocation.Distance) > error;
            bool isRightCenterInvalid = Math.Abs(reconstructedRightCenter - rightCenterLocation.Distance) >
                                        error;
            bool isBottomCenterInvalid =
                Math.Abs(reconstructedBottomCenter - bottomCenterLocation.Distance) > error;
            bool isCenterInvalid = Math.Abs(reconstructedCenter - centerLocation.Distance) > error;

            if(isLeftCenterInvalid || isTopCenterInvalid || isRightCenterInvalid || isBottomCenterInvalid ||
               isCenterInvalid)
            {
                Rectangle topLeftRegion = new Rectangle(
                    topLeft.X, topLeft.Y, center.X - topLeft.X, center.Y - topLeft.Y);

                sample.Children.Add(
                    ComputeTree(
                        ref topLeftRegion,
                        device2D,
                        ref sample.TopLeft,
                        ref topCenterLocation,
                        ref leftCenterLocation,
                        ref centerLocation,
                        level + 1.0));

                Rectangle topRightRegion = new Rectangle(
                    center.X, topRight.Y, topRight.X - center.X, center.Y - topRight.Y);

                sample.Children.Add(
                    ComputeTree(
                        ref topRightRegion,
                        device2D,
                        ref topCenterLocation,
                        ref sample.TopRight,
                        ref centerLocation,
                        ref rightCenterLocation,
                        level + 1.0));

                Rectangle bottomRightRegion = new Rectangle(
                    center.X, center.Y, bottomRight.X - center.X, bottomRight.Y - center.Y);

                sample.Children.Add(
                    ComputeTree(
                        ref bottomRightRegion,
                        device2D,
                        ref centerLocation,
                        ref rightCenterLocation,
                        ref bottomCenterLocation,
                        ref sample.BottomRight,
                        level + 1.0));

                Rectangle bottomLeftRegion = new Rectangle(
                    bottomLeft.X, center.Y, center.X - bottomLeft.X, bottomLeft.Y - center.Y);

                sample.Children.Add(
                    ComputeTree(
                        ref bottomLeftRegion,
                        device2D,
                        ref leftCenterLocation,
                        ref centerLocation,
                        ref sample.BottomLeft,
                        ref bottomCenterLocation,
                        level + 1.0));
            }

            return sample;
        }
Beispiel #25
0
        void BlendAnimation2D(FChromaSDKSceneEffect effect, FChromaSDKDeviceFrameIndex deviceFrameIndex, int device, Device2D device2D, string animationName,
                              int[] colors, int[] tempColors)
        {
            int size       = GetColorArraySize2D(device2D);
            int frameId    = deviceFrameIndex._mFrameIndex[device];
            int frameCount = ChromaAnimationAPI.GetFrameCountName(animationName);

            if (frameId < frameCount)
            {
                //cout << animationName << ": " << (1 + frameId) << " of " << frameCount << endl;
                float duration;
                int   animationId = ChromaAnimationAPI.GetAnimation(animationName);
                ChromaAnimationAPI.GetFrame(animationId, frameId, out duration, tempColors, size);
                for (int i = 0; i < size; ++i)
                {
                    int color1    = colors[i];                  //target
                    int tempColor = tempColors[i];              //source

                    // BLEND
                    int color2;
                    switch (effect._mBlend)
                    {
                    case EChromaSDKSceneBlend.SB_None:
                        color2 = tempColor;                                 //source
                        break;

                    case EChromaSDKSceneBlend.SB_Invert:
                        if (tempColor != 0)                                 //source
                        {
                            color2 = InvertColor(tempColor);                //source inverted
                        }
                        else
                        {
                            color2 = 0;
                        }
                        break;

                    case EChromaSDKSceneBlend.SB_Threshold:
                        color2 = Thresh(effect._mPrimaryColor, effect._mSecondaryColor, tempColor);                                 //source
                        break;

                    case EChromaSDKSceneBlend.SB_Lerp:
                    default:
                        color2 = MultiplyNonZeroTargetColorLerp(effect._mPrimaryColor, effect._mSecondaryColor, tempColor);                                 //source
                        break;
                    }

                    // MODE
                    switch (effect._mMode)
                    {
                    case EChromaSDKSceneMode.SM_Max:
                        colors[i] = MaxColor(color1, color2);
                        break;

                    case EChromaSDKSceneMode.SM_Min:
                        colors[i] = MinColor(color1, color2);
                        break;

                    case EChromaSDKSceneMode.SM_Average:
                        colors[i] = AverageColor(color1, color2);
                        break;

                    case EChromaSDKSceneMode.SM_Multiply:
                        colors[i] = MultiplyColor(color1, color2);
                        break;

                    case EChromaSDKSceneMode.SM_Add:
                        colors[i] = AddColor(color1, color2);
                        break;

                    case EChromaSDKSceneMode.SM_Subtract:
                        colors[i] = SubtractColor(color1, color2);
                        break;

                    case EChromaSDKSceneMode.SM_Replace:
                    default:
                        if (color2 != 0)
                        {
                            colors[i] = color2;
                        }
                        break;
                    }
                }
                deviceFrameIndex._mFrameIndex[device] = (frameId + frameCount + effect._mSpeed) % frameCount;
            }
        }
Beispiel #26
0
        public Canvas CreateField(Shape geometry, double normalizedBaseline, Device2D device2D)
        {
            Rectangle normalizedRegion = device2D.Geometry.MeasureRegion(geometry);

            Matrix3X2 transform = Matrix3X2.Identity;

            transform.Translate(
                (0.5f - (normalizedRegion.Width / 2.0f)) * EmLength,
                (0.5f - (normalizedRegion.Height / 2.0f)) * EmLength,
                out transform);

            transform.Translate(
                -normalizedRegion.X * EmLength, -normalizedRegion.Y * EmLength, out transform);

            transform.Scale(EmLength, EmLength, out transform);

            mDecomposer.Decompose(geometry, ref transform, device2D);

            Rectangle newReg = new Rectangle(0, 0, EmLength, EmLength);

            mSample = ComputeTree(ref newReg, device2D);

            float maxNegative = float.MinValue;

            float pixel = EmLength / ResolvedLength;

            for(int y = 0; y < ResolvedLength; ++y)
            {
                for(int x = 0; x < ResolvedLength; ++x)
                {
                    float distance = 0.0f;

                    Point point = new Point((pixel * x), (pixel * y));

                    distance += mSample.SampleSample(ref point);

                    maxNegative = Math.Max(maxNegative, distance);

                    mField[y, x] = distance;
                }
            }

            int rgbaIndex = 0;

            for(int y = 0; y < ResolvedLength; ++y)
            {
                for(int x = 0; x < ResolvedLength; ++x)
                {
                    double distance = mField[y, x];

                    const double test = 0.5;

                    if(distance < 0.0)
                    {
                        distance = Math.Abs(distance) / Math.Abs(maxNegative);

                        distance = test + ((1.0 - test) * distance);
                    }
                    else
                    {
                        distance = Math.Abs(distance) / Math.Abs(maxNegative);

                        distance = test - (test * distance);
                    }

                    distance = Math.Min(1.0, distance);
                    distance = Math.Max(0.0, distance);

                    byte value = Convert.ToByte(255 - (distance * 255));

                    //byte[] bytes = BitConverter.GetBytes(value);

                    mRgbaData[rgbaIndex + 0] = 0;
                    mRgbaData[rgbaIndex + 1] = 0;
                    mRgbaData[rgbaIndex + 2] = 0;
                    mRgbaData[rgbaIndex + 3] = value;

                    rgbaIndex += 4;
                }
            }

            Canvas newCanvas = new Canvas(new Size(ResolvedLength, ResolvedLength), Frost.Surfacing.SurfaceUsage.Normal);

            device2D.Resources.Copy(mRgbaData, newCanvas);

            return newCanvas;
        }
        void BlendAnimation2D(Effect effect, DeviceFrameIndex deviceFrameIndex, int device, Device2D device2D, string animationName,
                              int[] colors, int[] tempColors)
        {
            int size       = GetColorArraySize2D(device2D);
            int frameId    = deviceFrameIndex._mFrameIndex[device];
            int frameCount = ChromaAnimationAPI.GetFrameCountName(animationName);

            if (frameId < frameCount)
            {
                //cout << animationName << ": " << (1 + frameId) << " of " << frameCount << endl;
                float duration;
                int   animationId = ChromaAnimationAPI.GetAnimation(animationName);
                ChromaAnimationAPI.GetFrame(animationId, frameId, out duration, tempColors, size);
                for (int i = 0; i < size; ++i)
                {
                    int color1    = colors[i];                  //target
                    int tempColor = tempColors[i];              //source

                    // BLEND
                    int color2;
                    if (effect._mBlend == "none")
                    {
                        color2 = tempColor;                         //source
                    }
                    else if (effect._mBlend == "invert")
                    {
                        if (tempColor != 0)                         //source
                        {
                            color2 = InvertColor(tempColor);        //source inverted
                        }
                        else
                        {
                            color2 = 0;
                        }
                    }
                    else if (effect._mBlend == "thresh")
                    {
                        color2 = Thresh(effect._mPrimaryColor, effect._mSecondaryColor, tempColor);                         //source
                    }
                    else                                                                                                    // if (effect._mBlend == "lerp") //default
                    {
                        color2 = MultiplyNonZeroTargetColorLerp(effect._mPrimaryColor, effect._mSecondaryColor, tempColor); //source
                    }

                    // MODE
                    if (effect._mMode == "max")
                    {
                        colors[i] = MaxColor(color1, color2);
                    }
                    else if (effect._mMode == "min")
                    {
                        colors[i] = MinColor(color1, color2);
                    }
                    else if (effect._mMode == "average")
                    {
                        colors[i] = AverageColor(color1, color2);
                    }
                    else if (effect._mMode == "multiply")
                    {
                        colors[i] = MultiplyColor(color1, color2);
                    }
                    else if (effect._mMode == "add")
                    {
                        colors[i] = AddColor(color1, color2);
                    }
                    else if (effect._mMode == "subtract")
                    {
                        colors[i] = SubtractColor(color1, color2);
                    }
                    else                     // if (effect._mMode == "replace") //default
                    {
                        if (color2 != 0)
                        {
                            colors[i] = color2;
                        }
                    }
                }
                deviceFrameIndex._mFrameIndex[device] = (frameId + frameCount + effect._mSpeed) % frameCount;
            }
        }