2010-07-25 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing.Drawing2D / PathGradientBrush.jvm.cs
1
2
3 using System;
4 using System.Drawing;
5 using System.Runtime.InteropServices;
6 using awt = java.awt;
7
8 namespace System.Drawing.Drawing2D {
9         /// <summary>
10         /// Summary description for PathGradientBrush.
11         /// </summary>
12         [MonoTODO]
13         public sealed class PathGradientBrush : Brush {
14                 Brush _nativeObject;
15                 Blend _blend;
16                 Color _centerColor;
17                 PointF _center;
18                 PointF _focus;
19                 RectangleF _rectangle;
20                 Color [] _surroundColors;
21                 ColorBlend _interpolationColors;
22                 WrapMode _wrapMode;
23                 GraphicsPath _texturePath;
24                 bool _triangularShape = false;
25
26                 protected override java.awt.Paint NativeObject {
27                         get {
28                                 return _nativeObject;
29                         }
30                 }
31
32                 #region initialize
33
34                 void Initialize(GraphicsPath path, WrapMode wrapMode, bool initColors, bool calcCenter) {
35                         
36                         _texturePath = path;
37                         _wrapMode = wrapMode;
38                         _rectangle = path.GetBounds();
39
40                         if (initColors) {
41                                 _centerColor = Color.Black;
42                                 _surroundColors = new Color []{ Color.White };
43                         }
44                         
45                         Bitmap texture = new Bitmap( (int)_rectangle.Width, (int)_rectangle.Height );
46                         Graphics g = Graphics.FromImage( texture );
47                         PointF [] pathPoints = path.PathPoints;
48
49                         if (calcCenter) {
50                                 for (int i=0; i < pathPoints.Length; i++) {
51                                         _center.X += pathPoints[i].X;
52                                         _center.Y += pathPoints[i].Y;
53                                 }
54                                 _center.X /= pathPoints.Length;
55                                 _center.Y /= pathPoints.Length;
56                         }
57
58                         int outerColor = 0;
59                         DrawSector( g, CenterPoint, pathPoints[pathPoints.Length-1], pathPoints[0], CenterColor, SurroundColors[outerColor] );
60                         for(int i=0; i < pathPoints.Length - 1; i++) {
61                                 if (outerColor < SurroundColors.Length - 1)
62                                         outerColor++;
63                                 DrawSector( g, CenterPoint, pathPoints[i], pathPoints[i+1], CenterColor, SurroundColors[outerColor] );
64                         }
65
66                         _nativeObject = new TextureBrush( texture );
67                 }
68                 private void DrawSector(Graphics g, PointF center, PointF p1, PointF p2, Color innerColor, Color outerColor) {
69                         GraphicsPath pt = new GraphicsPath();
70                         pt.AddLine(p1, center);
71                         pt.AddLine(center, p2);
72                         LinearGradientBrush lgb = new LinearGradientBrush( GetVertical(center, p1, p2) , center, outerColor, innerColor );
73                         if (_triangularShape)
74                                 lgb.SetBlendTriangularShape(0.5f);
75                         g.FillPath( lgb, pt );
76                 }
77                 private PointF GetVertical(PointF c, PointF p1, PointF p2) {\r
78                         if (p1.X == p2.X)\r
79                                 return new PointF(p1.X, c.Y);\r
80                         if (p1.Y == p2.Y)\r
81                                 return new PointF(c.X, p2.Y);\r
82 \r
83                         float a = (float)(p2.Y - p1.Y) / (p2.X - p1.X);\r
84                         float av = - 1 / a;\r
85 \r
86                         float b1 = p1.Y - a * p1.X;\r
87                         float b2 = c.Y - av * c.X;\r
88 \r
89                         float ox = (b1 - b2) / (av - a);\r
90                         float oy = av * ox + b2;\r
91 \r
92                         return new PointF(ox, oy);\r
93                 }\r
94 \r
95                 #endregion\r
96 \r
97                 #region ctors\r
98
99                 public PathGradientBrush (GraphicsPath path) {
100                         Initialize( path, WrapMode.Clamp, true, true );
101                 }
102
103                 public PathGradientBrush (Point [] points) : this (points, WrapMode.Clamp) {
104                 }
105
106                 public PathGradientBrush (PointF [] points) : this (points, WrapMode.Clamp) {
107                 }
108
109                 public PathGradientBrush (Point [] points, WrapMode wrapMode) {
110                         GraphicsPath path = new GraphicsPath();
111                         path.AddLines( points );
112                         Initialize( path, wrapMode, true, true );
113                 }
114
115                 public PathGradientBrush (PointF [] points, WrapMode wrapMode) {
116                         GraphicsPath path = new GraphicsPath();
117                         path.AddLines( points );
118                         Initialize( path, wrapMode, true, true );
119                 }
120
121                 #endregion
122
123                 #region Properties
124
125                 [MonoTODO]
126                 public Blend Blend {
127                         get {
128                                 return _blend;
129                         }
130                         set {
131                                 _blend = value;
132                         }
133                 }
134
135                 public Color CenterColor {
136                         get {
137                                 return _centerColor;
138                         }
139                         set {
140                                 _centerColor = value;
141                                 Initialize(_texturePath, _wrapMode, false, false );
142                         }
143                 }
144
145                 public PointF CenterPoint {
146                         get {
147                                 return _center;
148                         }
149                         set {
150                                 _center = value;
151                                 Initialize(_texturePath, _wrapMode, false, false );
152                         }
153                 }
154
155                 public PointF FocusScales {
156                         get {
157                                 return _focus;
158                         }
159                         set {
160                                 _focus = value;
161                         }
162                 }
163
164                 public ColorBlend InterpolationColors {
165                         get {
166                                 return _interpolationColors;
167                         }
168                         set {
169                                 _interpolationColors = value;
170                         }
171                 }
172
173                 public RectangleF Rectangle {
174                         get {
175                                 return _rectangle;
176                         }
177                 }
178
179                 public Color [] SurroundColors {
180                         get {
181                                 return _surroundColors;
182                         }
183                         set {
184                                 _surroundColors = value;
185                                 Initialize(_texturePath, _wrapMode, false, false );
186                         }
187                 }
188
189                 public Matrix Transform {
190                         get {
191                                 return BrushTransform;
192                         }
193                         set {
194                                 BrushTransform = value;
195                         }
196                 }
197
198                 public WrapMode WrapMode {
199                         get {
200                                 return _wrapMode;
201                         }
202                         set {
203                                 _wrapMode = value;
204                         }
205                 }
206
207                 #endregion
208
209                 #region Methods
210
211                 public void MultiplyTransform (Matrix matrix) {
212                         base.BrushMultiplyTransform( matrix );
213                 }
214
215                 public void MultiplyTransform (Matrix matrix, MatrixOrder order) {
216                         base.BrushMultiplyTransform( matrix, order );
217                 }
218
219                 public void ResetTransform () {
220                         base.BrushResetTransform();
221                 }
222
223                 public void RotateTransform (float angle) {
224                         base.BrushRotateTransform( angle );
225                 }
226
227                 public void RotateTransform (float angle, MatrixOrder order) {
228                         base.BrushRotateTransform( angle, order );
229                 }
230
231                 public void ScaleTransform (float sx, float sy) {
232                         base.BrushScaleTransform( sx, sy );
233                 }
234
235                 public void ScaleTransform (float sx, float sy, MatrixOrder order) {
236                         base.BrushScaleTransform( sx, sy, order );
237                 }
238
239                 public void SetBlendTriangularShape (float focus) {
240                         SetBlendTriangularShape (focus, 1.0F);
241                 }
242
243                 public void SetBlendTriangularShape (float focus, float scale) {
244                         _triangularShape = true;
245                         Initialize( _texturePath, _wrapMode, false, false );
246                 }
247
248                 public void SetSigmaBellShape (float focus) {
249                         SetSigmaBellShape (focus, 1.0F);
250                 }
251
252                 [MonoTODO]
253                 public void SetSigmaBellShape (float focus, float scale) {
254                         // FALLBACK: Triangle shape used
255                         SetBlendTriangularShape (focus, scale);
256                 }
257
258                 public void TranslateTransform (float dx, float dy) {
259                         base.BrushTranslateTransform( dx, dy );
260                 }
261
262                 public void TranslateTransform (float dx, float dy, MatrixOrder order) {
263                         base.BrushTranslateTransform( dx, dy, order );
264                 }
265
266                 public override object Clone () {
267                         PathGradientBrush copy = (PathGradientBrush)InternalClone();
268
269                         if (copy._nativeObject != null)
270                                 copy._nativeObject = (Brush)copy._nativeObject.Clone();
271                         
272                         if (copy._surroundColors != null)
273                                 copy._surroundColors = (Color[])copy._surroundColors.Clone();
274                         
275                         if (copy._texturePath != null)
276                                 copy._texturePath = (GraphicsPath)copy._texturePath.Clone();
277
278                         //TBD: clone _blend, _interpolationColors
279                         //copy._blend = copy._blend
280                         
281                         return copy;
282                 }
283
284                 #endregion
285         }
286 }