private static void OnFaceChanged(DependencyObject d)
        {
            RadialGauge radialGauge = (RadialGauge)d;

            var container = radialGauge.GetTemplateChild(ContainerPartName) as Grid;

            if (container == null || DesignTimeHelpers.IsRunningInLegacyDesignerMode)
            {
                // Bad template.
                return;
            }

            radialGauge._root = container.GetVisual();
            radialGauge._root.Children.RemoveAll();
            radialGauge._compositor = radialGauge._root.Compositor;

            if (radialGauge.TickSpacing > 0)
            {
                // Ticks.
                SpriteVisual tick;
                for (double i = radialGauge.Minimum; i <= radialGauge.Maximum; i += radialGauge.TickSpacing)
                {
                    tick                        = radialGauge._compositor.CreateSpriteVisual();
                    tick.Size                   = new Vector2((float)radialGauge.TickWidth, (float)radialGauge.TickLength);
                    tick.Brush                  = radialGauge._compositor.CreateColorBrush(radialGauge.TickBrush.Color);
                    tick.Opacity                = (float)radialGauge.TickBrush.Opacity;
                    tick.Offset                 = new Vector3(100 - ((float)radialGauge.TickWidth / 2), 0.0f, 0);
                    tick.CenterPoint            = new Vector3((float)radialGauge.TickWidth / 2, 100.0f, 0);
                    tick.RotationAngleInDegrees = (float)radialGauge.ValueToAngle(i);
                    radialGauge._root.Children.InsertAtTop(tick);
                }

                // Scale Ticks.
                for (double i = radialGauge.Minimum; i <= radialGauge.Maximum; i += radialGauge.TickSpacing)
                {
                    tick                        = radialGauge._compositor.CreateSpriteVisual();
                    tick.Size                   = new Vector2((float)radialGauge.ScaleTickWidth, (float)radialGauge.ScaleWidth);
                    tick.Brush                  = radialGauge._compositor.CreateColorBrush(radialGauge.ScaleTickBrush.Color);
                    tick.Opacity                = (float)radialGauge.ScaleTickBrush.Opacity;
                    tick.Offset                 = new Vector3(100 - ((float)radialGauge.ScaleTickWidth / 2), (float)radialGauge.ScalePadding, 0);
                    tick.CenterPoint            = new Vector3((float)radialGauge.ScaleTickWidth / 2, 100 - (float)radialGauge.ScalePadding, 0);
                    tick.RotationAngleInDegrees = (float)radialGauge.ValueToAngle(i);
                    radialGauge._root.Children.InsertAtTop(tick);
                }
            }

            // Needle.
            radialGauge._needle             = radialGauge._compositor.CreateSpriteVisual();
            radialGauge._needle.Size        = new Vector2((float)radialGauge.NeedleWidth, (float)radialGauge.NeedleLength);
            radialGauge._needle.Brush       = radialGauge._compositor.CreateColorBrush(radialGauge.NeedleBrush.Color);
            radialGauge._needle.Opacity     = (float)radialGauge.NeedleBrush.Opacity;
            radialGauge._needle.CenterPoint = new Vector3((float)radialGauge.NeedleWidth / 2, (float)radialGauge.NeedleLength, 0);
            radialGauge._needle.Offset      = new Vector3(100 - ((float)radialGauge.NeedleWidth / 2), 100 - (float)radialGauge.NeedleLength, 0);
            radialGauge._root.Children.InsertAtTop(radialGauge._needle);

            OnValueChanged(radialGauge);
        }
Exemplo n.º 2
0
        private static void OnValueChanged(DependencyObject d)
        {
            RadialGauge radialGauge = (RadialGauge)d;

            if (!double.IsNaN(radialGauge.Value))
            {
                if (radialGauge.StepSize != 0)
                {
                    radialGauge.Value = radialGauge.RoundToMultiple(radialGauge.Value, radialGauge.StepSize);
                }

                var middleOfScale = 100 - radialGauge.ScalePadding - (radialGauge.ScaleWidth / 2);
                var valueText     = radialGauge.GetTemplateChild(ValueTextPartName) as TextBlock;
                radialGauge.ValueAngle = radialGauge.ValueToAngle(radialGauge.Value);

                // Needle
                if (radialGauge._needle != null)
                {
                    radialGauge._needle.RotationAngleInDegrees = (float)radialGauge.ValueAngle;
                }

                // Trail
                var trail = radialGauge.GetTemplateChild(TrailPartName) as Path;
                if (trail != null)
                {
                    if (radialGauge.ValueAngle > radialGauge.MinAngle)
                    {
                        trail.Visibility = Visibility.Visible;
                        var pg = new PathGeometry();
                        var pf = new PathFigure();
                        pf.IsClosed   = false;
                        pf.StartPoint = radialGauge.ScalePoint(radialGauge.MinAngle, middleOfScale);
                        var seg = new ArcSegment();
                        seg.SweepDirection = SweepDirection.Clockwise;
                        seg.IsLargeArc     = radialGauge.ValueAngle > (180 + radialGauge.MinAngle);
                        seg.Size           = new Size(middleOfScale, middleOfScale);
                        seg.Point          = radialGauge.ScalePoint(Math.Min(radialGauge.ValueAngle, radialGauge.MaxAngle), middleOfScale); // On overflow, stop trail at MaxAngle.
                        pf.Segments.Add(seg);
                        pg.Figures.Add(pf);
                        trail.Data = pg;
                    }
                    else
                    {
                        trail.Visibility = Visibility.Collapsed;
                    }
                }

                // Value Text
                if (valueText != null)
                {
                    valueText.Text = radialGauge.Value.ToString(radialGauge.ValueStringFormat);
                }
            }
        }
Exemplo n.º 3
0
        private static void OnValueChanged(DependencyObject d)
        {
            RadialGauge radialGauge = (RadialGauge)d;

            if (!double.IsNaN(radialGauge.Value))
            {
                if (radialGauge.StepSize != 0)
                {
                    radialGauge.Value = radialGauge.RoundToMultiple(radialGauge.Value, radialGauge.StepSize);
                }

                var middleOfScale = 100 - radialGauge.ScalePadding - (radialGauge.ScaleWidth / 2);
                var valueText     = radialGauge.GetTemplateChild(ValueTextPartName) as TextBlock;
                radialGauge.ValueAngle = radialGauge.ValueToAngle(radialGauge.Value);

                // Needle
                if (radialGauge._needle != null)
                {
                    radialGauge._needle.RotationAngleInDegrees = (float)radialGauge.ValueAngle;
                }

                // Trail
                var trail = radialGauge.GetTemplateChild(TrailPartName) as Path;
                if (trail != null)
                {
                    if (radialGauge.ValueAngle > radialGauge.NormalizedMinAngle)
                    {
                        trail.Visibility = Visibility.Visible;

                        if (radialGauge.ValueAngle - radialGauge.NormalizedMinAngle == 360)
                        {
                            // Draw full circle.
                            var eg = new EllipseGeometry();
                            eg.Center  = new Point(100, 100);
                            eg.RadiusX = 100 - radialGauge.ScalePadding - (radialGauge.ScaleWidth / 2);
                            eg.RadiusY = eg.RadiusX;
                            trail.Data = eg;
                        }
                        else
                        {
                            // Draw arc.
                            var pg = new PathGeometry();
                            var pf = new PathFigure();
                            pf.IsClosed   = false;
                            pf.StartPoint = radialGauge.ScalePoint(radialGauge.NormalizedMinAngle, middleOfScale);
                            var seg = new ArcSegment();
                            seg.SweepDirection = SweepDirection.Clockwise;
                            seg.IsLargeArc     = radialGauge.ValueAngle > (180 + radialGauge.NormalizedMinAngle);
                            seg.Size           = new Size(middleOfScale, middleOfScale);
                            seg.Point          = radialGauge.ScalePoint(Math.Min(radialGauge.ValueAngle, radialGauge.NormalizedMaxAngle), middleOfScale); // On overflow, stop trail at MaxAngle.
                            pf.Segments.Add(seg);
                            pg.Figures.Add(pf);
                            trail.Data = pg;
                        }
                    }
                    else
                    {
                        trail.Visibility = Visibility.Collapsed;
                    }
                }

                // Value Text
                if (valueText != null)
                {
                    valueText.Text = radialGauge.Value.ToString(radialGauge.ValueStringFormat);
                }

                // Raise the ValueChanged event if the Value is different than before
                // (this is done to prevent "noise" since the RadialGauge code sets
                // Value in several places)
                if (radialGauge._oldValue != radialGauge.Value)
                {
                    radialGauge.ValueChanged?.Invoke(
                        radialGauge,
                        new GaugeValueChangedEventArgs(
                            radialGauge._oldValue,
                            radialGauge.Value,
                            radialGauge._oldValueAngle,
                            radialGauge._needle.RotationAngleInDegrees));

                    radialGauge._oldValue      = radialGauge.Value;
                    radialGauge._oldValueAngle = radialGauge.ValueAngle;
                }
            }
        }