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