diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-05-27 15:07:22 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-05-27 15:07:22 +0300 |
| commit | 56c56d443d7ebd5a9fbab098e8500390bfc0c7cc (patch) | |
| tree | db1a7a6cfdb662201fd41fa265c09b37ddd43549 /Software/Visual_Studio | |
| parent | a34089adc9e46e75d68a46f12fe41746a3970490 (diff) | |
| download | Tango-56c56d443d7ebd5a9fbab098e8500390bfc0c7cc.tar.gz Tango-56c56d443d7ebd5a9fbab098e8500390bfc0c7cc.zip | |
Implemented Drawing of Text Runs, Border and Ellipse.
Implemented Drawing2DFrame.
Diffstat (limited to 'Software/Visual_Studio')
4 files changed, 194 insertions, 29 deletions
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<Tuple<FormattedText, double>> runs = new List<Tuple<FormattedText, double>>(); + + foreach (var run in textBlock.Inlines.OfType<Run>()) + { + 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, double>(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}}"> <Window.Resources> <SolidColorBrush x:Key="RRR" Color="Blue"></SolidColorBrush> </Window.Resources> <Grid> - <ListBox ItemsSource="{Binding Persons}" VirtualizingPanel.IsVirtualizing="False" ScrollViewer.CanContentScroll="False"> + <!--<ListBox ItemsSource="{Binding Persons}" VirtualizingPanel.IsVirtualizing="False" ScrollViewer.CanContentScroll="False"> <ListBox.ItemTemplate> <DataTemplate> - <Canvas> + <local:Drawing2DCanvas Height="50" x:Name="host"> <Rectangle Width="30" Height="30"> <Rectangle.Fill> <SolidColorBrush Color="{Binding Color}" /> </Rectangle.Fill> </Rectangle> - <TextBlock Canvas.Left="40" Text="{Binding Name}"></TextBlock> - </Canvas> + <TextBlock Canvas.Left="40" Text="{Binding Age}"></TextBlock> + </local:Drawing2DCanvas> </DataTemplate> </ListBox.ItemTemplate> - </ListBox> + </ListBox>--> - <!--<StackPanel> + <StackPanel> <local:Drawing2DStackPanel VerticalAlignment="Top"> - <TextBlock Text="Left Top" HorizontalAlignment="Left" Margin="0 20 0 0"></TextBlock> + <TextBlock HorizontalAlignment="Left" Margin="0 20 0 0"> + <Run>Top</Run> + <Run FontWeight="Bold" FontStyle="Italic">Left</Run> + </TextBlock> <TextBlock Text="Center Center" HorizontalAlignment="Center" Margin="0 20 0 0"></TextBlock> - <TextBlock Text="RIght Bottom" HorizontalAlignment="Right" Margin="0 20 0 0"></TextBlock> + <TextBlock Text="Right Bottom" HorizontalAlignment="Right" Margin="0 20 0 0"></TextBlock> </local:Drawing2DStackPanel> <local:Drawing2DStackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0 100 0 0" Height="100"> <TextBlock VerticalAlignment="Top">Left Top</TextBlock> <TextBlock Margin="100 0 0 0" VerticalAlignment="Center">Center Center</TextBlock> <TextBlock Margin="100 0 0 0" VerticalAlignment="Bottom">Right Bottom</TextBlock> - <Rectangle Margin="100 0 0 0" Fill="Red" Width="100" Height="100"></Rectangle> + <Rectangle Margin="100 0 0 0" Fill="Red" Width="100" Height="100" Stroke="Black" StrokeThickness="2"></Rectangle> + <Ellipse Margin="20 0 0 0" Fill="Blue" Width="100" Height="100"></Ellipse> </local:Drawing2DStackPanel> - </StackPanel>--> + + <Grid Width="200" Height="100" Margin="0 10 0 0"> + <local:Drawing2DFrame> + <Border Background="Gainsboro" BorderBrush="DimGray" BorderThickness="1" CornerRadius="5"/> + <TextBlock Text="Roy Ben Shabat" Opacity="0.5" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10"></TextBlock> + </local:Drawing2DFrame> + </Grid> + </StackPanel> </Grid> </Window> 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 @@ </Compile> <Compile Include="Drawing2DCanvas.cs" /> <Compile Include="Drawing2DContext.cs" /> + <Compile Include="Drawing2DFrame.cs" /> <Compile Include="Drawing2DHost.cs" /> <Compile Include="Drawing2DStackPanel.cs" /> <Compile Include="MainWindow.xaml.cs"> |
