Example #1
0
        public void Draw()
        {
            if (SelectedItem == null) return;
            if (gArcs == null) return;
            gArcs.Children.Clear();
            var a = 360.0/Segments;

            if (SelectedItem.Element != null)
            {
                ccCenter.Content = SelectedItem.Element;
                pBack.Visibility = Visibility.Collapsed;
                //ccCenter.Visibility = Visibility.Visible;
            }
            else if (SelectedItem.Icon != null && SelectedItem == RootItem)
            {
                iCenterIcon.Source = new BitmapImage(new Uri(SelectedItem.Icon, UriKind.RelativeOrAbsolute));
                iCenterIcon.Visibility = Visibility.Visible;
                pBack.Visibility = Visibility.Collapsed;
            }
            else
            {
                iCenterIcon.Visibility = Visibility.Collapsed;
                pBack.Visibility = Visibility.Visible;
                //ccCenter.Visibility = Visibility.Collapsed;
            }

            if (!Open)
            {
                BackgroundBrush = null;
                return;
            }
            BackgroundBrush = Brushes.White;

            for (var i = 0; i < Segments; i++)
            {
                var mi = SelectedItem.Items.FirstOrDefault(k => k.Position == i);

                var s = new Arc
                {
                    Width = Size,
                    Height = Size,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center,
                    Stretch = Stretch.None,
                    StartAngle = i*a,
                    StrokeThickness = 0,
                    Stroke = null,
                    EndAngle = (i + 1)*a - 1
                };
                if (mi != null && mi.Items != null && mi.Items.Count > 0)
                {
                    s.Fill = AccentBrush;
                    s.MouseDown += (e, si) => SelectItem(mi);
                    s.TouchDown += (e, si) => SelectItem(mi);
                }
                else
                {
                    s.Fill = SecondAccentBrush;
                }
                s.ArcThickness = 0;
                s.ArcThicknessUnit = UnitType.Pixel;

                gArcs.Children.Add(s);
                s.BeginAnimation(Arc.ArcThicknessProperty,
                    new DoubleAnimation(ArrowArcSize, new Duration(new TimeSpan(0, 0, 0, 0, 200))));
                const double dDegToRad = Math.PI/180.0;

                if (mi == null) continue;
                var f = new Arc
                {
                    Width = Size - (ArrowArcSize*2) - 3,
                    Height = Size - (ArrowArcSize*2) - 3,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center,
                    Stretch = Stretch.None,
                    StartAngle = i*a,
                    StrokeThickness = 0,
                    Stroke = null,
                    EndAngle = (i + 1)*a - 1,
                    Tag =  i
                };
                if (mi.Fill == null) mi.Fill = Brushes.Transparent;
                f.Fill = mi.Fill;
                f.ArcThickness = ItemSize;
                f.ArcThicknessUnit = UnitType.Pixel;
                f.MouseDown += (sender, e) => SelectItem(mi);

                //var eventAsObservable = Observable.FromEventPattern<TouchEventArgs>(f, "TouchDown");
                //eventAsObservable.Throttle(TimeSpan.FromMilliseconds(200)).Subscribe(k => Execute.OnUIThread(() => SelectItem(mi)));

                // Only subscribe to TouchDown on Windows 7: On Windows 8, it causes 
                var win8Version = new Version(6, 2, 9200, 0);
                if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
                    Environment.OSVersion.Version < win8Version)
                {
                    f.TouchDown += (e, si) => SelectItem(mi);
                }
                //f.TouchDown += (sender, e) => SelectItem(mi);

                gArcs.Children.Add(f);
                f.UpdateLayout();


                var sp = new StackPanel {Opacity = 0, IsHitTestVisible = false};
                if (mi.Element != null)
                {
                    var vb = new Viewbox {Width = 20, Height = 20, Stretch = Stretch.Uniform};
                    var pa = new Path {Data = Geometry.Parse(mi.Element), Fill = Brushes.Black};
                    vb.Child = pa;

                    sp.Children.Add(vb);
                }
                else if (!string.IsNullOrEmpty(mi.Icon))
                {
                    var img = new Image {Width = 20, Height = 20, Stretch = Stretch.UniformToFill};
                    var binding = new Binding {Source = mi, Path = new PropertyPath("Icon"), Mode = BindingMode.OneWay};
                    //img.Source = new BitmapImage(new Uri(mi.Icon, UriKind.RelativeOrAbsolute));
                    img.SetBinding(Image.SourceProperty, binding);
                    sp.Children.Add(img);
                }
                else
                {
                    var b = new Border {Background = null, Width = 20, Height = 20};
                    sp.Children.Add(b);
                }
                var tb = new TextBlock {Text = mi.Title};
                var bind = new Binding {Source = mi, Path = new PropertyPath("Title"), Mode = BindingMode.OneWay};
                tb.SetBinding(TextBlock.TextProperty, bind);
                var r = MenuCenterSize/2 + ItemSize - 10;
                var dX = r*Math.Sin((i + 0.5)*a*dDegToRad);

                // We invert the Y coordinate, because the origin in controls 
                // is the upper left corner, rather than the lower left
                var dY = -r*Math.Cos((i + 0.5)*a*dDegToRad);
                tb.Foreground = Brushes.Black;
                tb.FontSize = 9;
                f.TranslatePoint(new Point(ItemSize/2, ItemSize/2), gArcs);
                tb.TextAlignment = TextAlignment.Center;
                tb.HorizontalAlignment = HorizontalAlignment.Center;
                sp.RenderTransform = new TranslateTransform(dX, dY + Size/2 - 15);
                sp.Tag = i;
                sp.Children.Add(tb);
                gArcs.Children.Add(sp);

                sp.BeginAnimation(OpacityProperty,
                    new DoubleAnimation(1.0, new Duration(new TimeSpan(0, 0, 0, 0, 200))));


                if (mi.Items == null || mi.Items.Count <= 0) continue;
                var rp = new RegularPolygon
                {
                    IsHitTestVisible = false,
                    PointCount = 3,
                    Width = 8,
                    Height = 8,
                    Fill = Brushes.White,
                    Margin = new Thickness(-4, 0, 0, 0)
                };
                var r2 = Size/2 - (ArrowArcSize/2) + (rp.Width/2);
                rp.RenderTransform = new TransformGroup
                {
                    Children = new TransformCollection
                    {
                        new RotateTransform((i + 0.5)*a),
                        new TranslateTransform(
                            r2*Math.Sin((i + 0.5)*a*dDegToRad) + 5,
                            -r2*Math.Cos((i + 0.5)*a*dDegToRad) + 5
                            )
                    }
                };
                gArcs.Children.Add(rp);
            }

            //if (SelectedItem.Items == null || SelectedItem.Items.Count <= 0) return;
            //foreach (var i in SelectedItem.Items)
            //    if (i.Fill != null)
            //    {
            //    }
        }