From a4916e075b7460e791b1b40307188a43cb537a8c Mon Sep 17 00:00:00 2001 From: James Coliz Date: Tue, 9 Jul 2024 13:16:45 -0700 Subject: [PATCH] feat(ui): Display bounding boxes around 'boxes' in the definition --- LogoSlideMaker.WinUi/MainWindow.xaml.cs | 13 +++++- .../ViewModels/MainViewModel.cs | 45 ++++++++++--------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/LogoSlideMaker.WinUi/MainWindow.xaml.cs b/LogoSlideMaker.WinUi/MainWindow.xaml.cs index 8814128..d20c8e9 100644 --- a/LogoSlideMaker.WinUi/MainWindow.xaml.cs +++ b/LogoSlideMaker.WinUi/MainWindow.xaml.cs @@ -13,6 +13,7 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; +using System.Linq; using System.Runtime.InteropServices; using System.Threading.Tasks; using Windows.Foundation; @@ -357,7 +358,17 @@ private void CanvasControl_Draw(CanvasControl _, CanvasDrawEventArgs args) { try { - foreach (var p in viewModel.Primitives) + var primitives = viewModel.ShowBoundingBoxes ? + viewModel.Primitives.Concat(viewModel.BoxPrimitives) : + viewModel.Primitives; + + if (primitives is null) + { + logger.LogError("Draw: Primitives failed"); + return; + } + + foreach (var p in primitives) { Draw(p, args.DrawingSession); } diff --git a/LogoSlideMaker.WinUi/ViewModels/MainViewModel.cs b/LogoSlideMaker.WinUi/ViewModels/MainViewModel.cs index c9c8f69..3449cf6 100644 --- a/LogoSlideMaker.WinUi/ViewModels/MainViewModel.cs +++ b/LogoSlideMaker.WinUi/ViewModels/MainViewModel.cs @@ -49,6 +49,11 @@ public class MainViewModel(IGetImageAspectRatio bitmaps, ILogger /// public IReadOnlyList Primitives => _primitives; + /// + /// Drawing primitives needed to render bounding boxes for the current slide + /// + public IReadOnlyList BoxPrimitives => _boxPrimitives; + /// /// All the image paths we would need to render /// @@ -400,12 +405,14 @@ public void BackToPreviousSlide() /// public void GeneratePrimitives() { + _primitives.Clear(); + _boxPrimitives.Clear(); + if (_definition is null || _layout is null) { return; } - _primitives.Clear(); var config = _definition.Render; // Add primitives for a background @@ -436,29 +443,22 @@ public void GeneratePrimitives() var generator = new PrimitivesEngine(config, bitmaps); _primitives.AddRange(_layout.Logos.SelectMany(generator.ToPrimitives)); -#if false - // TODO: Need a new home for this! Elsewhere we are handling bounding boxes - // as a rendering operation NOT a primitive-generating operation. - // Add bounding boxes for any boxes with explicit outer dimensions - if (ShowBoundingBoxes) - { - _primitives.AddRange( - _definition.Boxes - .Where(x => x.Outer is not null) - .Select(x => new RectanglePrimitive() + // Add optional primitives to draw + _boxPrimitives.AddRange( + _definition.Boxes + .Where(x => x.Outer is not null) + .Select(x => new RectanglePrimitive() + { + Rectangle = x.Outer! with { - Rectangle = x.Outer! with - { - X = x.Outer.X * 96m, - Y = x.Outer.Y * 96m, - Width = x.Outer.Width * 96m, - Height = x.Outer.Height * 96m - } + X = x.Outer.X * 96m, + Y = x.Outer.Y * 96m, + Width = x.Outer.Width * 96m, + Height = x.Outer.Height * 96m } - ) - ); - } -#endif + } + ) + ); } /// @@ -635,6 +635,7 @@ private void OnPropertyChanged([CallerMemberName] string? propertyName = null) private Definition? _definition; private SlideLayout? _layout; private readonly List _primitives = []; + private readonly List _boxPrimitives = []; #endregion }