Exemplo n.º 1
0
    public void Run()
    {
        RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;

        var visualizer = new MenuViewMessageInterpreter();
        var state      = new MenuViewMessage {
            Items =
            {
                MenuItemViewMessage.MakeRange("[-1, 1]: 0.5", -1, +1, 0.5, false),
                MenuItemViewMessage.MakeRange("[-1, 1]: -1",  -1, +1,  -1, true),
                MenuItemViewMessage.MakeRange("[-1, 1]: 0",   -1, +1,   0, false),
                MenuItemViewMessage.MakeRange("[-1, 1]: +1",  -1, +1,  +1, false),
                MenuItemViewMessage.MakeRange("[0, 1]: 0.5",   0,  1, 0.5, false),
                MenuItemViewMessage.MakeRange("[0, 1]: 0",     0,  1,   0, false),
                MenuItemViewMessage.MakeRange("[0, 1]: 1",     0,  1,   1, false)
            }
        };
        var visual = visualizer.Interpret(state);

        var window = new Window {
            Background = Brushes.Pink,
            Content    = visual
        };

        window.Show();

        Dispatcher.Run();
    }
    private void MenuRenderProc()
    {
        var cancellationToken = cancellationTokenSource.Token;

        var interpreter = new MenuViewMessageInterpreter();
        var bitmap      = new RenderTargetBitmap(PixelSize, PixelSize, 96 * ResolutionMultiplier, 96 * ResolutionMultiplier, PixelFormats.Pbgra32);

        while (!cancellationToken.IsCancellationRequested)
        {
            StagingTexture  stagingTexture;
            MenuViewMessage message;
            try {
                stagingTexture = readyToRenderToQueue.Take(cancellationToken);
                message        = messageQueue.Take(cancellationToken);
            } catch (OperationCanceledException) {
                break;
            }

            //if there are multiple pending messages, keep taking them until the queue is drained
            while (messageQueue.TryTake(out var secondMessage))
            {
                message = secondMessage;
            }

            var visual = interpreter.Interpret(message);
            visual.Arrange(new Rect(0, 0, WpfSize, WpfSize));
            visual.UpdateLayout();

            bitmap.Clear();
            bitmap.Render(visual);

            var dataBox = stagingTexture.dataBox;
            bitmap.CopyPixels(new Int32Rect(0, 0, PixelSize, PixelSize), dataBox.DataPointer, dataBox.SlicePitch, dataBox.RowPitch);
            readyToCopyFromQueue.Enqueue(stagingTexture);
        }
    }