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