Skip to content

Commit

Permalink
Print dialog with multiple sites. Togglable rectangles to preview the…
Browse files Browse the repository at this point in the history
… print area
  • Loading branch information
yNiklas committed May 13, 2021
1 parent 95c8904 commit 4d8855e
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 35 deletions.
21 changes: 12 additions & 9 deletions SyncBoard/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
Height="892"
Width="1500">
<Canvas x:Name="PrintCanvas" Opacity="0"/>
<Rectangle Stroke="#FF818181" HorizontalAlignment="Left" Width="794" Height="1123"
VerticalAlignment="Top"/>
<Grid x:Name="printBackgrounds"></Grid>
<InkCanvas x:Name="inkCanvas"
Height="1218"
Width="1843" VerticalAlignment="Top" HorizontalAlignment="Left"/>
Height="1218"
Width="1843" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</Grid>
</ScrollViewer>

Expand All @@ -48,14 +47,18 @@
x:Name="roomCodeBox" TextAlignment="Center" TextChanged="roomCodeBox_TextChanged" HorizontalAlignment="Right" Width="86"/>
<Button Content="Beitreten" Margin="0,0,120,0" RenderTransformOrigin="0.483,0.508" IsEnabled="False"
x:Name="connectButton" Click="connectButton_Click" HorizontalAlignment="Right" Width="76"/>
<Button Content="Exportieren" Margin="207,0,0,0" Click="exportBoard"/>
<Button Content="Laden" Margin="318,0,0,0" Click="importBoard"/>
<TextBlock HorizontalAlignment="Left" Margin="323,0,0,0" Text="A User joined your Board" TextWrapping="Wrap"
VerticalAlignment="Center" x:Name="userJoinedText" Visibility="Collapsed"/>
<Button Content="Exportieren" Margin="313,10,0,0" Click="exportBoard" VerticalAlignment="Top"/>
<Button Content="Laden" Margin="424,10,0,0" Click="importBoard" VerticalAlignment="Top"/>
<TextBlock Margin="0,15,317,0" Text="A User joined your Board" TextWrapping="Wrap"
VerticalAlignment="Top" x:Name="userJoinedText" Visibility="Collapsed" HorizontalAlignment="Right" Width="155"/>
<AppBarToggleButton Icon="Accept" Label="Vollbild"
Width="63"
Click="Button_Click_1" x:Name="fullscreenIcon"/>
<Button Content="Drucken" Margin="399,0,0,0" Click="Printer_Click"/>
<Button Content="Drucken" Margin="505,10,0,0" Click="Printer_Click" VerticalAlignment="Top"/>
<AppBarToggleButton x:Name="togglePrintBackground" Icon="Accept"
Label="Druckvorschau" Margin="171,0,0,0"
VerticalAlignment="Center" Width="91" IsChecked="True"
Tapped="TogglePrintSiteBackgrounds"/>
</Grid>
<InkToolbar x:Name="inkToolbar"
VerticalAlignment="Top"
Expand Down
154 changes: 128 additions & 26 deletions SyncBoard/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.ConstrainedExecution;
using System.Threading;
using System.Timers;
using Windows.Data.Json;
Expand Down Expand Up @@ -44,6 +45,12 @@ public sealed partial class MainPage : Page

private String roomCode = "";

private static int PAGE_HEIGHT = 1123, PAGE_WIDTH = 794,
PRINT_RECTANGLE_WIDTH = 794,
PRINT_RECTANGLE_HEIGHT = 1123,
AMOUNT_INITIAL_RECTANGLES = 2;
private int rectangleCounter = 0;

public MainPage()
{
this.InitializeComponent();
Expand All @@ -70,6 +77,9 @@ public MainPage()
inkCanvas.InkPresenter.StrokesErased += InkPresenter_StrokesErased;
inkCanvas.InkPresenter.StrokesCollected += InkPresenter_Drawed;

// Init background:
InitializePrintSiteBackground();

// Network and sync:
InitSocket();
}
Expand Down Expand Up @@ -178,11 +188,11 @@ private async void ListenIncome()
{
if (o.Value<float>("y") >= inkCanvas.Height - EXPAND_MARGIN)
{
expandBoard(true, o.Value<float>("y"));
expandBoard(o.Value<float>("y"));
}
else if (o.Value<float>("x") >= inkCanvas.Width - EXPAND_MARGIN)
{
expandBoard(false, o.Value<float>("x"));
expandBoard(o.Value<float>("x"));
}
});
}
Expand Down Expand Up @@ -410,15 +420,25 @@ private void expandBoard(bool bottom)
outputGrid.Width += 1200;
inkCanvas.Width += 1200;
}

if (inkCanvas.Height >= rectangleCounter * PRINT_RECTANGLE_HEIGHT)
{
CreateNewPrintSiteBackground();
}
}

private void expandBoard(bool bottom, float offset)
private void expandBoard(float offset)
{
outputGrid.Height = offset + 1200;
inkCanvas.Height = offset + 1200;

outputGrid.Width = offset + 1200;
inkCanvas.Width = offset + 1200;

if (inkCanvas.Height >= rectangleCounter * PRINT_RECTANGLE_HEIGHT)
{
CreateNewPrintSiteBackground();
}
}

private void SetOfflineMode(bool set)
Expand Down Expand Up @@ -594,42 +614,124 @@ private void Button_Click_1(object sender, RoutedEventArgs e)
fullscreenIcon.IsChecked = ApplicationView.GetForCurrentView().IsFullScreenMode;
}

private Polyline CreatePolyLineFromStroke(InkStroke stroke)
{
var polyLine = new Polyline();
polyLine.Stroke = new SolidColorBrush(stroke.DrawingAttributes.Color);
if (stroke.DrawingAttributes.Kind.Equals(InkDrawingAttributesKind.Pencil))
{
polyLine.StrokeDashArray = new DoubleCollection();
polyLine.StrokeDashArray.Add(1);
polyLine.StrokeDashArray.Add(0.3);
}
if (stroke.DrawingAttributes.DrawAsHighlighter)
{
polyLine.Opacity = 0.5;
}
polyLine.StrokeThickness = stroke.DrawingAttributes.Size.Height;
var points = new PointCollection();
foreach (var point in stroke.GetInkPoints())
{
points.Add(point.Position);
}
polyLine.Points = points;

return polyLine;
}

private Polyline TranslatePolyLineToPage(Polyline polyLine, int page)
{
polyLine.Translation = new Vector3(0, -PAGE_HEIGHT * page, 0);
return polyLine;
}

// Print PDF
private async void Printer_Click(object sender, RoutedEventArgs e)
{
// Create a Bitmap from the strokes.
var inkStream = new InMemoryRandomAccessStream();
await inkCanvas.InkPresenter.StrokeContainer.SaveAsync(inkStream.GetOutputStreamAt(0));
var inkBitmap = new BitmapImage();
await inkBitmap.SetSourceAsync(inkStream);

// Adjust Margin to layout the image properly in the print-page.
var inkBounds = inkCanvas.InkPresenter.StrokeContainer.BoundingRect;
var inkMargin = new Thickness(inkBounds.Left, inkBounds.Top, inkCanvas.ActualWidth - inkBounds.Right, inkCanvas.ActualHeight - inkBounds.Bottom);

// Prepare Viewbox+Image to be printed.
var inkViewbox = new Viewbox()
// Calculate amount of required pages
int pageCount = (int)inkCanvas.ActualHeight / PAGE_HEIGHT + 1;
System.Diagnostics.Debug.WriteLine("Creating print with " + pageCount + " page(s)");

// Clear the print-canvas
PrintCanvas.Children.Clear();

// Setup the required pages
List<Panel> pagePanels = new List<Panel>();
for (int i = 0; i < pageCount; i++)
{
pagePanels.Add(new ItemsStackPanel());
}

// Paint the strokes to the pages
foreach (var stroke in inkCanvas.InkPresenter.StrokeContainer.GetStrokes())
{
Child = new Image()
int page = (int)(stroke.BoundingRect.Top / PAGE_HEIGHT);
int pageOffset = 0;

while (stroke.BoundingRect.Bottom > PAGE_HEIGHT * (page + pageOffset))
{
Source = inkBitmap,
Margin = inkMargin
},
Width = inkCanvas.ActualWidth,
Height = inkCanvas.ActualHeight
};
var polyLine = this.CreatePolyLineFromStroke(stroke);
//polyLine2.Translation = new Vector3(0, -PAGE_HEIGHT * (page + 1), 0);
this.TranslatePolyLineToPage(polyLine, page + pageOffset);
pagePanels[page + pageOffset].Children.Add(polyLine);
pageOffset++;
}
}

PrintCanvas.Children.Clear();
PrintCanvas.Children.Add(inkViewbox);
// Add all pages to the output (except blanks)
for (int i = 0; i < pageCount; i++)
{
if (pagePanels[i].Children.Count > 0)
{
PrintCanvas.Children.Add(pagePanels[i]);
}
}

// Open print-GUI
var _printHelper = new PrintHelper(PrintCanvas);
var printHelperOptions = new PrintHelperOptions();
printHelperOptions.AddDisplayOption(StandardPrintTaskOptions.Orientation);
printHelperOptions.Orientation = PrintOrientation.Portrait;

await _printHelper.ShowPrintUIAsync("printing InkPen", printHelperOptions, true);
await _printHelper.ShowPrintUIAsync("SyncBoard Print", printHelperOptions, true);
}
}




// Background rectangles to indicate where the print area is
private void InitializePrintSiteBackground()
{
for (int i = 0; i < AMOUNT_INITIAL_RECTANGLES; i++)
{
CreateNewPrintSiteBackground();
}
}

private void CreateNewPrintSiteBackground()
{
Rectangle rectangle = new Rectangle();
rectangle.Width = PRINT_RECTANGLE_WIDTH;
rectangle.Height = PRINT_RECTANGLE_HEIGHT;
rectangle.Margin = new Thickness(0, PRINT_RECTANGLE_HEIGHT * rectangleCounter, 0, 0);
rectangle.Stroke = new SolidColorBrush(Color.FromArgb(255, 81, 81, 81));
rectangle.VerticalAlignment = VerticalAlignment.Top;
rectangle.HorizontalAlignment = HorizontalAlignment.Left;

printBackgrounds.Children.Add(rectangle);
rectangleCounter++;
}

private void TogglePrintSiteBackgrounds(object sender, RoutedEventArgs e)
{
if (togglePrintBackground.IsChecked == true)
{
printBackgrounds.Visibility = Visibility.Visible;
}
else
{
printBackgrounds.Visibility = Visibility.Collapsed;
}
}
}
}

0 comments on commit 4d8855e

Please sign in to comment.