From 56c56d443d7ebd5a9fbab098e8500390bfc0c7cc Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Mon, 27 May 2019 15:07:22 +0300 Subject: Implemented Drawing of Text Runs, Border and Ellipse. Implemented Drawing2DFrame. --- .../TEMP/Tango.Graphics2D/Drawing2DFrame.cs | 61 ++++++++++ .../TEMP/Tango.Graphics2D/Drawing2DHost.cs | 128 ++++++++++++++++++--- .../TEMP/Tango.Graphics2D/MainWindow.xaml | 33 ++++-- .../TEMP/Tango.Graphics2D/Tango.Graphics2D.csproj | 1 + 4 files changed, 194 insertions(+), 29 deletions(-) create mode 100644 Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DFrame.cs (limited to 'Software/Visual_Studio') diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DFrame.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DFrame.cs new file mode 100644 index 000000000..0c8e393a1 --- /dev/null +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DFrame.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; + +namespace Tango.Graphics2D +{ + public class Drawing2DFrame : Drawing2DHost + { + protected override Point GetElementLocation(FrameworkElement element) + { + if (element.HorizontalAlignment == HorizontalAlignment.Left) + { + if (element.VerticalAlignment == VerticalAlignment.Top) + { + return new Point(element.Margin.Left, element.Margin.Top); + } + else if (element.VerticalAlignment == VerticalAlignment.Bottom) + { + return new Point(element.Margin.Left, ActualHeight - element.Height - element.Margin.Bottom); + } + else + { + return new Point(element.Margin.Left, (ActualHeight / 2) - (element.Height / 2) + element.Margin.Top - element.Margin.Bottom); + } + } + else if (element.HorizontalAlignment == HorizontalAlignment.Right) + { + if (element.VerticalAlignment == VerticalAlignment.Top) + { + return new Point(ActualWidth - element.Width - element.Margin.Right, element.Margin.Top); + } + else if (element.VerticalAlignment == VerticalAlignment.Bottom) + { + return new Point(ActualWidth - element.Width - element.Margin.Right, ActualHeight - element.Height - element.Margin.Bottom); + } + else + { + return new Point(ActualWidth - element.Width - element.Margin.Right, (ActualHeight / 2) - (element.Height / 2) + element.Margin.Top - element.Margin.Bottom); + } + } + else + { + if (element.VerticalAlignment == VerticalAlignment.Top) + { + return new Point((ActualWidth / 2) - (element.Width / 2) + element.Margin.Left - element.Margin.Right, element.Margin.Top); + } + else if (element.VerticalAlignment == VerticalAlignment.Bottom) + { + return new Point((ActualWidth / 2) - (element.Width / 2) + element.Margin.Left - element.Margin.Right, ActualHeight - element.Height - element.Margin.Bottom); + } + else + { + return new Point((ActualWidth / 2) - (element.Width / 2) + element.Margin.Left - element.Margin.Right, (ActualHeight / 2) - (element.Height / 2) + element.Margin.Top - element.Margin.Bottom); + } + } + } + } +} diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DHost.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DHost.cs index 4cc314f6c..965f7ea97 100644 --- a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DHost.cs +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DHost.cs @@ -4,10 +4,9 @@ using System.Collections.ObjectModel; using System.ComponentModel; using System.Globalization; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; +using System.Windows.Documents; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Shapes; @@ -97,6 +96,16 @@ namespace Tango.Graphics2D { visual = DrawRectangle(element as Rectangle); } + else if (element is Ellipse) + { + visual = DrawEllipse(element as Ellipse); + } + else if (element is Border) + { + visual = DrawBorder(element as Border); + } + + visual.Opacity = element.Opacity; AddVisual(visual); } @@ -107,27 +116,48 @@ namespace Tango.Graphics2D { var x = d2.Context; - var typeface = new Typeface( - textBlock.FontFamily, - textBlock.FontStyle, - textBlock.FontWeight, - textBlock.FontStretch); + double width = 0; + double height = 0; + + List> runs = new List>(); + + foreach (var run in textBlock.Inlines.OfType()) + { + var typeface = new Typeface( + run.FontFamily, + run.FontStyle, + run.FontWeight, + run.FontStretch); + + var formattedText = new FormattedText(run.Text, + CultureInfo.GetCultureInfo("en-us"), + run.FlowDirection, + typeface, + run.FontSize, + run.Foreground); - var formattedText = new FormattedText(textBlock.Text, - CultureInfo.GetCultureInfo("en-us"), - textBlock.FlowDirection, - typeface, - textBlock.FontSize, - textBlock.Foreground); + formattedText.MaxTextWidth = ActualWidth; - formattedText.MaxTextWidth = ActualWidth; + double w = formattedText.WidthIncludingTrailingWhitespace; - textBlock.Width = formattedText.Width; - textBlock.Height = formattedText.Height; + runs.Add(new Tuple(formattedText, w)); + + width += w; + height = Math.Max(formattedText.Height, height); + } + + textBlock.Width = width; + textBlock.Height = height; var location = GetElementLocation(textBlock); - x.DrawText(formattedText, new Point(location.X, location.Y)); + double position_x = location.X; + + foreach (var run in runs) + { + x.DrawText(run.Item1, new Point(position_x, location.Y)); + position_x += run.Item2; + } return d2.Visual; } @@ -141,7 +171,69 @@ namespace Tango.Graphics2D var location = GetElementLocation(rectangle); - x.DrawRectangle(rectangle.Fill, null, new Rect(location.X, location.Y, rectangle.Width, rectangle.Height)); + Pen pen = null; + + if (rectangle.Stroke != null) + { + pen = new Pen(rectangle.Stroke, rectangle.StrokeThickness); + pen.DashCap = rectangle.StrokeDashCap; + //TODO: add some stroke features... + } + + x.DrawRectangle(rectangle.Fill, pen, new Rect(location.X, location.Y, rectangle.Width, rectangle.Height)); + + return d2.Visual; + } + } + + private DrawingVisual DrawEllipse(Ellipse ellipse) + { + using (Drawing2DContext d2 = new Drawing2DContext()) + { + var x = d2.Context; + + var location = GetElementLocation(ellipse); + + Pen pen = null; + + if (ellipse.Stroke != null) + { + pen = new Pen(ellipse.Stroke, ellipse.StrokeThickness); + pen.DashCap = ellipse.StrokeDashCap; + //TODO: add some stroke features... + } + + x.DrawEllipse(ellipse.Fill, pen, new Point(location.X + ellipse.Width / 2, location.Y + ellipse.Height / 2), ellipse.Width / 2, ellipse.Height / 2); + + return d2.Visual; + } + } + + private DrawingVisual DrawBorder(Border border) + { + using (Drawing2DContext d2 = new Drawing2DContext()) + { + if (double.IsNaN(border.Width)) + { + border.Width = ActualWidth; + } + if (double.IsNaN(border.Height)) + { + border.Height = ActualHeight; + } + + var x = d2.Context; + + var location = GetElementLocation(border); + + Pen pen = null; + + if (border.BorderBrush != null) + { + pen = new Pen(border.BorderBrush, border.BorderThickness.Top); + } + + x.DrawRoundedRectangle(border.Background, pen, new Rect(location.X, location.Y, border.Width, border.Height), border.CornerRadius.TopLeft, border.CornerRadius.BottomRight); return d2.Visual; } diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/MainWindow.xaml b/Software/Visual_Studio/TEMP/Tango.Graphics2D/MainWindow.xaml index 569469239..c530ae62c 100644 --- a/Software/Visual_Studio/TEMP/Tango.Graphics2D/MainWindow.xaml +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/MainWindow.xaml @@ -5,40 +5,51 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Tango.Graphics2D" mc:Ignorable="d" - Title="MainWindow" Height="450" Width="800" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"> + Title="MainWindow" Height="500" Width="800" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"> - + - + + + + + + + + diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Tango.Graphics2D.csproj b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Tango.Graphics2D.csproj index 8ff401703..2a4dd9026 100644 --- a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Tango.Graphics2D.csproj +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Tango.Graphics2D.csproj @@ -65,6 +65,7 @@ + -- cgit v1.3.1