~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Analysis/Profiler/Frontend/Controls/PiePiece.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Text;
 
7
using System.Windows.Shapes;
 
8
using System.Windows.Media;
 
9
using System.Windows;
 
10
 
 
11
namespace ICSharpCode.Profiler.Controls
 
12
{
 
13
        /// <summary>
 
14
        /// A pie piece shape
 
15
        /// </summary>
 
16
        class PiePiece : Shape
 
17
        {
 
18
                #region dependency properties
 
19
 
 
20
                public static readonly DependencyProperty RadiusProperty =
 
21
                        DependencyProperty.Register("RadiusProperty", typeof(double), typeof(PiePiece),
 
22
                                                    new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
 
23
 
 
24
                /// <summary>
 
25
                /// The radius of this pie piece
 
26
                /// </summary>
 
27
                public double Radius
 
28
                {
 
29
                        get { return (double)GetValue(RadiusProperty); }
 
30
                        set { SetValue(RadiusProperty, value); }
 
31
                }
 
32
 
 
33
                public static readonly DependencyProperty InnerRadiusProperty =
 
34
                        DependencyProperty.Register("InnerRadiusProperty", typeof(double), typeof(PiePiece),
 
35
                                                    new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
 
36
 
 
37
                /// <summary>
 
38
                /// The inner radius of this pie piece
 
39
                /// </summary>
 
40
                public double InnerRadius
 
41
                {
 
42
                        get { return (double)GetValue(InnerRadiusProperty); }
 
43
                        set { SetValue(InnerRadiusProperty, value); }
 
44
                }
 
45
 
 
46
                public static readonly DependencyProperty WedgeAngleProperty =
 
47
                        DependencyProperty.Register("WedgeAngleProperty", typeof(double), typeof(PiePiece),
 
48
                                                    new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
 
49
 
 
50
                /// <summary>
 
51
                /// The wedge angle of this pie piece in degrees
 
52
                /// </summary>
 
53
                public double WedgeAngle
 
54
                {
 
55
                        get { return (double)GetValue(WedgeAngleProperty); }
 
56
                        set { SetValue(WedgeAngleProperty, value); }
 
57
                }
 
58
 
 
59
                public static readonly DependencyProperty RotationAngleProperty =
 
60
                        DependencyProperty.Register("RotationAngleProperty", typeof(double), typeof(PiePiece),
 
61
                                                    new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
 
62
 
 
63
                /// <summary>
 
64
                /// The rotation, in degrees, from the Y axis vector of this pie piece.
 
65
                /// </summary>
 
66
                public double RotationAngle
 
67
                {
 
68
                        get { return (double)GetValue(RotationAngleProperty); }
 
69
                        set { SetValue(RotationAngleProperty, value); }
 
70
                }
 
71
                #endregion
 
72
                
 
73
                protected override Size MeasureOverride(Size constraint)
 
74
                {
 
75
                        double size = 2 * Radius;
 
76
                        return new Size(size, size);
 
77
                }
 
78
                
 
79
                protected override Geometry DefiningGeometry
 
80
                {
 
81
                        get
 
82
                        {
 
83
                                // Create a StreamGeometry for describing the shape
 
84
                                StreamGeometry geometry = new StreamGeometry();
 
85
                                geometry.FillRule = FillRule.EvenOdd;
 
86
 
 
87
                                using (StreamGeometryContext context = geometry.Open())
 
88
                                {
 
89
                                        DrawGeometry(context);
 
90
                                }
 
91
 
 
92
                                // Freeze the geometry for performance benefits
 
93
                                geometry.Freeze();
 
94
 
 
95
                                return geometry;
 
96
                        }
 
97
                }
 
98
 
 
99
                /// <summary>
 
100
                /// Draws the pie piece
 
101
                /// </summary>
 
102
                private void DrawGeometry(StreamGeometryContext context)
 
103
                {
 
104
                        double centreX = Radius;
 
105
                        double centreY = Radius;
 
106
                        
 
107
                        Point startPoint = new Point(centreX, centreY);
 
108
 
 
109
                        Point innerArcStartPoint = Utils.ComputeCartesianCoordinate(RotationAngle, InnerRadius);
 
110
                        innerArcStartPoint.Offset(centreX, centreY);
 
111
 
 
112
                        Point innerArcEndPoint = Utils.ComputeCartesianCoordinate(RotationAngle + WedgeAngle, InnerRadius);
 
113
                        innerArcEndPoint.Offset(centreX, centreY);
 
114
 
 
115
                        Point outerArcStartPoint = Utils.ComputeCartesianCoordinate(RotationAngle, Radius);
 
116
                        outerArcStartPoint.Offset(centreX, centreY);
 
117
 
 
118
                        Point outerArcEndPoint = Utils.ComputeCartesianCoordinate(RotationAngle + WedgeAngle, Radius);
 
119
                        outerArcEndPoint.Offset(centreX, centreY);
 
120
 
 
121
                        bool largeArc = WedgeAngle>180.0;
 
122
 
 
123
                        Size outerArcSize = new Size(Radius, Radius);
 
124
                        Size innerArcSize = new Size(InnerRadius, InnerRadius);
 
125
 
 
126
                        context.BeginFigure(innerArcStartPoint, true, true);
 
127
                        context.LineTo(outerArcStartPoint, true, true);
 
128
                        context.ArcTo(outerArcEndPoint, outerArcSize, 0, largeArc, SweepDirection.Clockwise, true, true);
 
129
                        context.LineTo(innerArcEndPoint, true, true);
 
130
                        context.ArcTo(innerArcStartPoint, innerArcSize, 0, largeArc, SweepDirection.Counterclockwise, true, true);
 
131
                }
 
132
        }
 
133
 
 
134
        public static class Utils
 
135
        {
 
136
 
 
137
                /// <summary>
 
138
                /// Converts a coordinate from the polar coordinate system to the cartesian coordinate system.
 
139
                /// </summary>
 
140
                /// <param name="angle"></param>
 
141
                /// <param name="radius"></param>
 
142
                /// <returns></returns>
 
143
                public static Point ComputeCartesianCoordinate(double angle, double radius)
 
144
                {
 
145
                        // convert to radians
 
146
                        double angleRad = (Math.PI / 180.0) * (angle - 90);
 
147
 
 
148
                        double x = radius * Math.Cos(angleRad);
 
149
                        double y = radius * Math.Sin(angleRad);
 
150
 
 
151
                        return new Point(x, y);
 
152
                }
 
153
        }
 
154
}