diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2019-05-28 08:36:59 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2019-05-28 08:36:59 +0300 |
| commit | 1616df43a043c24c9f788156234c04cf5305590a (patch) | |
| tree | 059f94eaf64b2a0f26316b35acda0a92b5cf0363 /Software/Visual_Studio/TEMP | |
| parent | 8fefb650921f35b2b72ae5d49d7adefe30bd8a6c (diff) | |
| download | Tango-1616df43a043c24c9f788156234c04cf5305590a.tar.gz Tango-1616df43a043c24c9f788156234c04cf5305590a.zip | |
Tango.Graphics2D BindingToEvent ...
Diffstat (limited to 'Software/Visual_Studio/TEMP')
15 files changed, 640 insertions, 141 deletions
diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/BindingEventArgs.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/BindingEventArgs.cs new file mode 100644 index 000000000..90299fe8f --- /dev/null +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/BindingEventArgs.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media; + +namespace Tango.Graphics2D +{ + public class BindingEventArgs<T> : EventArgs + { + public DrawingVisual Visual { get; set; } + public FrameworkElement Element { get; set; } + public T Value { get; set; } + } +} diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/BindingEventContainer.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/BindingEventContainer.cs new file mode 100644 index 000000000..6de81b02e --- /dev/null +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/BindingEventContainer.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Data; +using System.Windows.Media; + +namespace Tango.Graphics2D +{ + public class BindingEventContainer : DependencyObject + { + public static BindingEventContainer<T> Generate<T>(DrawingVisual visual, FrameworkElement element, DependencyProperty elementProperty) + { + BindingEventContainer<T> container = new BindingEventContainer<T>(visual, element); + + Binding binding = new Binding(); + binding.Mode = BindingMode.OneWay; + binding.Source = element; + binding.Path = new PropertyPath(elementProperty); + BindingOperations.SetBinding(container, BindingEventContainer<T>.ValueProperty, binding); + + return container; + } + } + + public class BindingEventContainer<T> : BindingEventContainer + { + private DrawingVisual _visual; + private FrameworkElement _element; + + public event EventHandler<BindingEventArgs<T>> ValueChanged; + + public T Value + { + get { return (T)GetValue(ValueProperty); } + set { SetValue(ValueProperty, value); } + } + public static readonly DependencyProperty ValueProperty = + DependencyProperty.Register("Value", typeof(T), typeof(BindingEventContainer<T>), new PropertyMetadata(default(T), (d, e) => (d as BindingEventContainer<T>).OnValueChanged())); + + public BindingEventContainer(DrawingVisual visual, FrameworkElement element) + { + _visual = visual; + _element = element; + } + + private void OnValueChanged() + { + ValueChanged?.Invoke(this, new BindingEventArgs<T>() + { + Element = _element, + Visual = _visual, + Value = Value, + }); + } + } +} diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DCanvas.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DCanvas.cs index 5693e132d..4625615f6 100644 --- a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DCanvas.cs +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DCanvas.cs @@ -11,7 +11,7 @@ namespace Tango.Graphics2D { public class Drawing2DCanvas : Drawing2DHost { - protected override Point GetElementLocation(FrameworkElement element) + public override Point GetElementLocation(FrameworkElement element) { return GetElementCanvasLocation(element); } diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DFrame.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DFrame.cs index 0c8e393a1..eec52829b 100644 --- a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DFrame.cs +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DFrame.cs @@ -9,7 +9,7 @@ namespace Tango.Graphics2D { public class Drawing2DFrame : Drawing2DHost { - protected override Point GetElementLocation(FrameworkElement element) + public override Point GetElementLocation(FrameworkElement element) { if (element.HorizontalAlignment == HorizontalAlignment.Left) { diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DHost.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DHost.cs index 965f7ea97..7d03689dc 100644 --- a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DHost.cs +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DHost.cs @@ -7,9 +7,11 @@ using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; +using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Shapes; +using Tango.Graphics2D.VisualBinders; namespace Tango.Graphics2D { @@ -17,6 +19,12 @@ namespace Tango.Graphics2D public abstract class Drawing2DHost : FrameworkElement { private readonly VisualCollection _children; + private Dictionary<DrawingVisual, FrameworkElement> _elements; + private List<BindingEventContainer> _containers; + private FrameworkElement _mouseEnterElement; + private Dictionary<Type, IVisualBinder> _binders; + + #region Properties public ObservableCollection<FrameworkElement> Elements { @@ -26,25 +34,6 @@ namespace Tango.Graphics2D public static readonly DependencyProperty ElementsProperty = DependencyProperty.Register("Elements", typeof(ObservableCollection<FrameworkElement>), typeof(Drawing2DHost), new PropertyMetadata(null, (d, e) => (d as Drawing2DHost).OnElementsChanged())); - private void OnElementsChanged() - { - if (Elements != null) - { - Elements.CollectionChanged += Elements_CollectionChanged; - } - } - - private void Elements_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) - { - if (!DesignMode) - { - foreach (var element in e.NewItems) - { - (element as FrameworkElement).DataContext = DataContext; - } - } - } - public Brush Foreground { get { return (Brush)this.GetValue(ForegroundProperty); } @@ -53,13 +42,30 @@ namespace Tango.Graphics2D public static readonly DependencyProperty ForegroundProperty = TextBlock.ForegroundProperty.AddOwner(typeof(Drawing2DHost)); + #endregion + + #region Constructors + public Drawing2DHost() { Elements = new ObservableCollection<FrameworkElement>(); + _elements = new Dictionary<DrawingVisual, FrameworkElement>(); + _containers = new List<BindingEventContainer>(); + _binders = new Dictionary<Type, IVisualBinder>(); _children = new VisualCollection(this); + + RegisterVisualBinder<TextBlock>(new TextBlockBinder()); + RegisterVisualBinder<Rectangle>(new RectangleBinder()); + RegisterVisualBinder<Ellipse>(new EllipseBinder()); + RegisterVisualBinder<Border>(new BorderBinder()); + Loaded += Drawing2DHost_Loaded; } + #endregion + + #region Loaded Event + private void Drawing2DHost_Loaded(object sender, RoutedEventArgs e) { DrawElements(); @@ -76,181 +82,216 @@ namespace Tango.Graphics2D } } - private void DrawElements() + #endregion + + #region Mouse Interactions + + protected override void OnMouseDown(MouseButtonEventArgs e) { - foreach (var element in Elements) + base.OnMouseDown(e); + + if (e.ChangedButton == MouseButton.Left) { - DrawElement(element); + Point pt = e.GetPosition(this); + VisualTreeHelper.HitTest(this, null, MouseDownCallback, new PointHitTestParameters(pt)); } } - private void DrawElement(FrameworkElement element) + private HitTestResultBehavior MouseDownCallback(HitTestResult result) { - DrawingVisual visual = null; - - if (element is TextBlock) - { - visual = DrawTextBlock(element as TextBlock); - } - else if (element is Rectangle) + if (result.VisualHit.GetType() == typeof(System.Windows.Media.DrawingVisual)) { - visual = DrawRectangle(element as Rectangle); + var visual = result.VisualHit as DrawingVisual; + + if (visual != null) + { + var element = _elements[visual]; + + if (element != null) + { + element.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) + { + RoutedEvent = Mouse.MouseDownEvent, + Source = element, + }); + } + } } - else if (element is Ellipse) + + // Stop the hit test enumeration of objects in the visual tree. + return HitTestResultBehavior.Stop; + } + + protected override void OnMouseUp(MouseButtonEventArgs e) + { + base.OnMouseUp(e); + + if (e.ChangedButton == MouseButton.Left) { - visual = DrawEllipse(element as Ellipse); + Point pt = e.GetPosition(this); + VisualTreeHelper.HitTest(this, null, MouseUpCallback, new PointHitTestParameters(pt)); } - else if (element is Border) + } + + private HitTestResultBehavior MouseUpCallback(HitTestResult result) + { + if (result.VisualHit.GetType() == typeof(System.Windows.Media.DrawingVisual)) { - visual = DrawBorder(element as Border); - } + var visual = result.VisualHit as DrawingVisual; - visual.Opacity = element.Opacity; + if (visual != null) + { + var element = _elements[visual]; + + if (element != null) + { + element.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) + { + RoutedEvent = Mouse.MouseUpEvent, + Source = element, + }); + } + } + } - AddVisual(visual); + // Stop the hit test enumeration of objects in the visual tree. + return HitTestResultBehavior.Stop; } - private DrawingVisual DrawTextBlock(TextBlock textBlock) + protected override void OnMouseMove(MouseEventArgs e) { - using (Drawing2DContext d2 = new Drawing2DContext()) - { - var x = d2.Context; + base.OnMouseMove(e); - double width = 0; - double height = 0; + Point pt = e.GetPosition(this); + VisualTreeHelper.HitTest(this, null, MouseMoveCallback, new PointHitTestParameters(pt)); + } - List<Tuple<FormattedText, double>> runs = new List<Tuple<FormattedText, double>>(); + private HitTestResultBehavior MouseMoveCallback(HitTestResult result) + { + if (result.VisualHit.GetType() == typeof(System.Windows.Media.DrawingVisual)) + { + var visual = result.VisualHit as DrawingVisual; - foreach (var run in textBlock.Inlines.OfType<Run>()) + if (visual != null) { - var typeface = new Typeface( - run.FontFamily, - run.FontStyle, - run.FontWeight, - run.FontStretch); + var element = _elements[visual]; - var formattedText = new FormattedText(run.Text, - CultureInfo.GetCultureInfo("en-us"), - run.FlowDirection, - typeface, - run.FontSize, - run.Foreground); + if (element != null) + { + element.RaiseEvent(new MouseEventArgs(Mouse.PrimaryDevice, 0) + { + RoutedEvent = Mouse.MouseMoveEvent, + Source = element, + }); - formattedText.MaxTextWidth = ActualWidth; + if (_mouseEnterElement != null && _mouseEnterElement != element) + { + _mouseEnterElement.RaiseEvent(new MouseEventArgs(Mouse.PrimaryDevice, 0) + { + RoutedEvent = Mouse.MouseLeaveEvent, + Source = _mouseEnterElement, + }); + } - double w = formattedText.WidthIncludingTrailingWhitespace; + if (_mouseEnterElement != element) + { + _mouseEnterElement = element; - runs.Add(new Tuple<FormattedText, double>(formattedText, w)); - - width += w; - height = Math.Max(formattedText.Height, height); + element.RaiseEvent(new MouseEventArgs(Mouse.PrimaryDevice, 0) + { + RoutedEvent = Mouse.MouseEnterEvent, + Source = element, + }); + } + } } + } - textBlock.Width = width; - textBlock.Height = height; + // Stop the hit test enumeration of objects in the visual tree. + return HitTestResultBehavior.Stop; + } - var location = GetElementLocation(textBlock); + #endregion - double position_x = location.X; + #region Elements Changed - foreach (var run in runs) + private void Elements_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + { + if (!IsDesignMode) + { + foreach (var element in e.NewItems) { - x.DrawText(run.Item1, new Point(position_x, location.Y)); - position_x += run.Item2; + (element as FrameworkElement).DataContext = DataContext; } - - return d2.Visual; } } - private DrawingVisual DrawRectangle(Rectangle rectangle) + private void OnElementsChanged() { - using (Drawing2DContext d2 = new Drawing2DContext()) + if (Elements != null) { - var x = d2.Context; - - var location = GetElementLocation(rectangle); + Elements.CollectionChanged += Elements_CollectionChanged; + } + } - Pen pen = null; + #endregion - 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)); + #region Drawing Executers - return d2.Visual; + private void DrawElements() + { + foreach (var element in Elements) + { + DrawElement(element); } } - private DrawingVisual DrawEllipse(Ellipse ellipse) + private void DrawElement(FrameworkElement element) { - using (Drawing2DContext d2 = new Drawing2DContext()) - { - var x = d2.Context; + IVisualBinder binder = _binders[element.GetType()]; - var location = GetElementLocation(ellipse); + var result = binder.CreateVisual(element, this); - Pen pen = null; + _containers.AddRange(result.BindingEventContainers); - if (ellipse.Stroke != null) - { - pen = new Pen(ellipse.Stroke, ellipse.StrokeThickness); - pen.DashCap = ellipse.StrokeDashCap; - //TODO: add some stroke features... - } + DrawingVisual visual = result.Visual; - 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; - } + AddVisual(visual, element); } - private DrawingVisual DrawBorder(Border border) + private void AddVisual(DrawingVisual visual, FrameworkElement element) { - 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; + visual.Opacity = element.Opacity; + visual.Transform = element.RenderTransform; - if (border.BorderBrush != null) - { - pen = new Pen(border.BorderBrush, border.BorderThickness.Top); - } + _children.Add(visual); + _elements.Add(visual, element); + } - x.DrawRoundedRectangle(border.Background, pen, new Rect(location.X, location.Y, border.Width, border.Height), border.CornerRadius.TopLeft, border.CornerRadius.BottomRight); + #endregion - return d2.Visual; - } - } + #region Abstract and Virtual Methods protected virtual Size OnMeasure() { return new Size(double.NaN, double.NaN); } - protected abstract Point GetElementLocation(FrameworkElement element); + public abstract Point GetElementLocation(FrameworkElement element); + + #endregion + + #region Binders - private void AddVisual(DrawingVisual visual) + public void RegisterVisualBinder<T>(IVisualBinder instance) { - _children.Add(visual); + _binders.Add(typeof(T), instance); } + #endregion + + #region Child Get/Count + protected override int VisualChildrenCount => _children != null ? _children.Count : 0; protected override Visual GetVisualChild(int index) @@ -263,9 +304,15 @@ namespace Tango.Graphics2D return _children[index]; } - public bool DesignMode + #endregion + + #region Helper Methods + + protected bool IsDesignMode { get { return (DesignerProperties.GetIsInDesignMode(new DependencyObject())); } } + + #endregion } } diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DStackPanel.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DStackPanel.cs index 4f39435e4..edcaa34f6 100644 --- a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DStackPanel.cs +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Drawing2DStackPanel.cs @@ -21,7 +21,7 @@ namespace Tango.Graphics2D public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(Drawing2DStackPanel), new PropertyMetadata(Orientation.Vertical)); - protected override Point GetElementLocation(FrameworkElement element) + public override Point GetElementLocation(FrameworkElement element) { if (Orientation == Orientation.Vertical) { diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/IVisualBinder.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/IVisualBinder.cs new file mode 100644 index 000000000..07ce9e338 --- /dev/null +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/IVisualBinder.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media; + +namespace Tango.Graphics2D +{ + public interface IVisualBinder + { + VisualBinderResult CreateVisual(FrameworkElement element, Drawing2DHost host); + } +} diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/MainWindow.xaml b/Software/Visual_Studio/TEMP/Tango.Graphics2D/MainWindow.xaml index c530ae62c..1c69bbfed 100644 --- a/Software/Visual_Studio/TEMP/Tango.Graphics2D/MainWindow.xaml +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/MainWindow.xaml @@ -27,7 +27,7 @@ </ListBox>--> <StackPanel> - <local:Drawing2DStackPanel VerticalAlignment="Top"> + <!--<local:Drawing2DStackPanel VerticalAlignment="Top"> <TextBlock HorizontalAlignment="Left" Margin="0 20 0 0"> <Run>Top</Run> <Run FontWeight="Bold" FontStyle="Italic">Left</Run> @@ -39,17 +39,62 @@ <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" Stroke="Black" StrokeThickness="2"></Rectangle> - <Ellipse Margin="20 0 0 0" Fill="Blue" Width="100" Height="100"></Ellipse> - </local:Drawing2DStackPanel> + <Rectangle Margin="100 0 0 0" MouseUp="Rectangle_MouseUp" Fill="Red" Width="100" Height="100" Stroke="Black" StrokeThickness="2"></Rectangle> + <Ellipse Margin="20 0 0 0" MouseUp="Ellipse_MouseUp" Fill="Blue" Width="100" Height="100"></Ellipse> + </local:Drawing2DStackPanel>--> <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> + <Border x:Name="border" MouseDown="Border_MouseDown" MouseUp="Border_MouseUp" Background="Gainsboro" BorderBrush="DimGray" BorderThickness="1" CornerRadius="5"> + + </Border> + <TextBlock x:Name="txt" MouseEnter="Txt_MouseEnter" MouseLeave="Txt_MouseLeave" MouseDown="Txt_MouseDown" Text="Roy Ben Shabat" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10"> + <TextBlock.Triggers> + <EventTrigger RoutedEvent="MouseEnter"> + <BeginStoryboard> + <Storyboard> + <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.5" Duration="00:00:0.3"></DoubleAnimation> + <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX" To="1.2" Duration="00:00:0.3"></DoubleAnimation> + </Storyboard> + </BeginStoryboard> + </EventTrigger> + <EventTrigger RoutedEvent="MouseLeave"> + <BeginStoryboard> + <Storyboard> + <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="00:00:0.3"></DoubleAnimation> + <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX" To="1" Duration="00:00:0.3"></DoubleAnimation> + </Storyboard> + </BeginStoryboard> + </EventTrigger> + </TextBlock.Triggers> + <TextBlock.RenderTransform> + <ScaleTransform ScaleX="1" ScaleY="1" CenterX="50" CenterY="0.5" /> + </TextBlock.RenderTransform> + </TextBlock> </local:Drawing2DFrame> </Grid> </StackPanel> + <TextBlock x:Name="ttt" Text="Roy Ben Shabat" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10" IsHitTestVisible="True" Background="Red"> + <TextBlock.Triggers> + <EventTrigger RoutedEvent="MouseEnter"> + <BeginStoryboard> + <Storyboard> + <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0" Duration="00:00:00"></DoubleAnimation> + </Storyboard> + </BeginStoryboard> + </EventTrigger> + <EventTrigger RoutedEvent="MouseLeave"> + <BeginStoryboard> + <Storyboard> + <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="00:00:00"></DoubleAnimation> + </Storyboard> + </BeginStoryboard> + </EventTrigger> + </TextBlock.Triggers> + <TextBlock.RenderTransform> + <ScaleTransform ScaleX="1" ScaleY="1" CenterX="50" CenterY="0.5" /> + </TextBlock.RenderTransform> + </TextBlock> </Grid> </Window> diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/MainWindow.xaml.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/MainWindow.xaml.cs index 7f4c5a3cb..5118b7c81 100644 --- a/Software/Visual_Studio/TEMP/Tango.Graphics2D/MainWindow.xaml.cs +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/MainWindow.xaml.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -43,5 +44,41 @@ namespace Tango.Graphics2D }); } } + + private void Rectangle_MouseUp(object sender, MouseButtonEventArgs e) + { + MessageBox.Show("Rectangle Clicked"); + txt.Opacity = 1; + } + + private void Ellipse_MouseUp(object sender, MouseButtonEventArgs e) + { + MessageBox.Show("Ellipse Clicked"); + } + + private void Txt_MouseDown(object sender, MouseButtonEventArgs e) + { + txt.Opacity = 1; + } + + private void Txt_MouseEnter(object sender, MouseEventArgs e) + { + Debug.WriteLine("Text Mouse Enter"); + } + + private void Txt_MouseLeave(object sender, MouseEventArgs e) + { + Debug.WriteLine("Text Mouse Leave"); + } + + private void Border_MouseDown(object sender, MouseButtonEventArgs e) + { + border.Background = Brushes.DimGray; + } + + private void Border_MouseUp(object sender, MouseButtonEventArgs e) + { + border.Background = Brushes.Gainsboro; + } } }
\ No newline at end of file diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Tango.Graphics2D.csproj b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Tango.Graphics2D.csproj index 2a4dd9026..1bbee5a09 100644 --- a/Software/Visual_Studio/TEMP/Tango.Graphics2D/Tango.Graphics2D.csproj +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/Tango.Graphics2D.csproj @@ -55,6 +55,11 @@ <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> </ApplicationDefinition> + <Compile Include="VisualBinderResult.cs" /> + <Compile Include="VisualBinders\BorderBinder.cs" /> + <Compile Include="VisualBinders\EllipseBinder.cs" /> + <Compile Include="VisualBinders\RectangleBinder.cs" /> + <Compile Include="VisualBinders\TextBlockBinder.cs" /> <Page Include="MainWindow.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> @@ -63,11 +68,14 @@ <DependentUpon>App.xaml</DependentUpon> <SubType>Code</SubType> </Compile> + <Compile Include="BindingEventContainer.cs" /> + <Compile Include="BindingEventArgs.cs" /> <Compile Include="Drawing2DCanvas.cs" /> <Compile Include="Drawing2DContext.cs" /> <Compile Include="Drawing2DFrame.cs" /> <Compile Include="Drawing2DHost.cs" /> <Compile Include="Drawing2DStackPanel.cs" /> + <Compile Include="IVisualBinder.cs" /> <Compile Include="MainWindow.xaml.cs"> <DependentUpon>MainWindow.xaml</DependentUpon> <SubType>Code</SubType> @@ -100,5 +108,6 @@ <ItemGroup> <None Include="App.config" /> </ItemGroup> + <ItemGroup /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> </Project>
\ No newline at end of file diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinderResult.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinderResult.cs new file mode 100644 index 000000000..28adab506 --- /dev/null +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinderResult.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media; + +namespace Tango.Graphics2D +{ + public class VisualBinderResult + { + public DrawingVisual Visual { get; set; } + + public List<BindingEventContainer> BindingEventContainers { get; set; } + + public VisualBinderResult(DrawingVisual visual) + { + Visual = visual; + BindingEventContainers = new List<BindingEventContainer>(); + } + + public void AddBasicContainers(FrameworkElement element) + { + AddOpacityContainer(element); + AddVisibilityContainer(element); + } + + private void AddOpacityContainer(FrameworkElement element) + { + var container = BindingEventContainer.Generate<double>(Visual, element, FrameworkElement.OpacityProperty); + container.ValueChanged += (sender, e) => + { + e.Visual.Opacity = e.Value; + }; + + BindingEventContainers.Add(container); + } + + private void AddVisibilityContainer(FrameworkElement element) + { + var container = BindingEventContainer.Generate<Visibility>(Visual, element, FrameworkElement.VisibilityProperty); + container.ValueChanged += (sender, e) => + { + e.Visual.Opacity = e.Value == Visibility.Visible ? 1 : 0; + }; + + BindingEventContainers.Add(container); + } + } +} diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinders/BorderBinder.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinders/BorderBinder.cs new file mode 100644 index 000000000..622e12b41 --- /dev/null +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinders/BorderBinder.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; + +namespace Tango.Graphics2D.VisualBinders +{ + public class BorderBinder : IVisualBinder + { + public VisualBinderResult CreateVisual(FrameworkElement element, Drawing2DHost host) + { + Border border = element as Border; + + using (Drawing2DContext d2 = new Drawing2DContext()) + { + if (double.IsNaN(border.Width)) + { + border.Width = host.ActualWidth; + } + if (double.IsNaN(border.Height)) + { + border.Height = host.ActualHeight; + } + + var x = d2.Context; + + var location = host.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); + + VisualBinderResult result = new VisualBinderResult(d2.Visual); + result.AddBasicContainers(element); + + //Background Binding + var backgroundContainer = BindingEventContainer.Generate<Brush>(result.Visual, element, Border.BackgroundProperty); + backgroundContainer.ValueChanged += (sender, e) => + { + var context = e.Visual.RenderOpen(); + context.DrawRoundedRectangle(border.Background, pen, new Rect(location.X, location.Y, border.Width, border.Height), border.CornerRadius.TopLeft, border.CornerRadius.BottomRight); + context.Close(); + }; + result.BindingEventContainers.Add(backgroundContainer); + + return result; + } + } + } +} diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinders/EllipseBinder.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinders/EllipseBinder.cs new file mode 100644 index 000000000..0b67e272c --- /dev/null +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinders/EllipseBinder.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media; +using System.Windows.Shapes; + +namespace Tango.Graphics2D.VisualBinders +{ + public class EllipseBinder : IVisualBinder + { + public VisualBinderResult CreateVisual(FrameworkElement element, Drawing2DHost host) + { + Ellipse ellipse = element as Ellipse; + + using (Drawing2DContext d2 = new Drawing2DContext()) + { + var x = d2.Context; + + var location = host.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); + + VisualBinderResult result = new VisualBinderResult(d2.Visual); + result.AddBasicContainers(element); + + return result; + } + } + } +} diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinders/RectangleBinder.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinders/RectangleBinder.cs new file mode 100644 index 000000000..b77f57679 --- /dev/null +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinders/RectangleBinder.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media; +using System.Windows.Shapes; + +namespace Tango.Graphics2D.VisualBinders +{ + public class RectangleBinder : IVisualBinder + { + public VisualBinderResult CreateVisual(FrameworkElement element, Drawing2DHost host) + { + Rectangle rectangle = element as Rectangle; + + using (Drawing2DContext d2 = new Drawing2DContext()) + { + var x = d2.Context; + + var location = host.GetElementLocation(rectangle); + + 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)); + + VisualBinderResult result = new VisualBinderResult(d2.Visual); + result.AddBasicContainers(element); + + return result; + } + } + } +} diff --git a/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinders/TextBlockBinder.cs b/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinders/TextBlockBinder.cs new file mode 100644 index 000000000..6e643e431 --- /dev/null +++ b/Software/Visual_Studio/TEMP/Tango.Graphics2D/VisualBinders/TextBlockBinder.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +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.Media; + +namespace Tango.Graphics2D.VisualBinders +{ + public class TextBlockBinder : IVisualBinder + { + public VisualBinderResult CreateVisual(FrameworkElement element, Drawing2DHost host) + { + TextBlock textBlock = element as TextBlock; + + using (Drawing2DContext d2 = new Drawing2DContext()) + { + var x = d2.Context; + + 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); + + formattedText.MaxTextWidth = host.ActualWidth; + + double w = formattedText.WidthIncludingTrailingWhitespace; + + runs.Add(new Tuple<FormattedText, double>(formattedText, w)); + + width += w; + height = Math.Max(formattedText.Height, height); + } + + textBlock.Width = width; + textBlock.Height = height; + + var location = host.GetElementLocation(textBlock); + + double position_x = location.X; + + foreach (var run in runs) + { + x.DrawText(run.Item1, new Point(position_x, location.Y)); + position_x += run.Item2; + } + + VisualBinderResult result = new VisualBinderResult(d2.Visual); + result.AddBasicContainers(element); + + + + return result; + } + } + } +} |
