1////////////////////////////////////////////////////////////////////////////////
2//
3//  ADOBE SYSTEMS INCORPORATED
4//  Copyright 2004-2007 Adobe Systems Incorporated
5//  All Rights Reserved.
6//
7//  NOTICE: Adobe permits you to use, modify, and distribute this file
8//  in accordance with the terms of the license agreement accompanying it.
9//
10////////////////////////////////////////////////////////////////////////////////
11
12package mx.graphics
13{
14
15import flash.display.CapsStyle;
16import flash.display.Graphics;
17import flash.display.GraphicsSolidFill;
18import flash.display.GraphicsStroke;
19import flash.display.JointStyle;
20import flash.events.EventDispatcher;
21import flash.geom.Point;
22import flash.geom.Rectangle;
23
24import mx.events.PropertyChangeEvent;
25
26/**
27 *  The SolidColorStroke class defines the properties for a line.
28 *
29 *  You can define a SolidColorStroke object in MXML, but you must attach that SolidColorStroke to
30 *  another object for it to appear in your application. The following example
31 *  defines two SolidColorStroke objects and then uses them in the horizontalAxisRenderer
32 *  of a LineChart control:
33 *
34 *  <pre>
35 *  ...
36 *  &lt;mx:SolidColorStroke id="ticks" color="0xFF0000" weight="1"/&gt;
37 *  &lt;mx:SolidColorStroke id="mticks" color="0x0000FF" weight="1"/&gt;
38 *
39 *  &lt;mx:LineChart id="mychart" dataProvider="{ndxa}"&gt;
40 *      &lt;mx:horizontalAxisRenderer&gt;
41 *          &lt;mx:AxisRenderer placement="bottom" canDropLabels="true"&gt;
42 *              &lt;mx:tickStroke&gt;{ticks}&lt;/mx:tickStroke&gt;
43 *              &lt;mx:minorTickStroke&gt;{mticks}&lt;/mx:minorTickStroke&gt;
44 *          &lt;/mx:AxisRenderer&gt;
45 *      &lt;/mx:horizontalAxisRenderer&gt;
46 *  &lt;/LineChart&gt;
47 *  ...
48 *  </pre>
49 *
50 *  @mxml
51 *
52 *  <p>The <code>&lt;mx:SolidColorStroke&gt;</code> tag inherits all the tag attributes
53 *  of its superclass, and adds the following tag attributes:</p>
54 *
55 *  <pre>
56 *  &lt;mx:SolidColorStroke
57 *    <b>Properties</b>
58 *    alpha="1.0"
59 *    caps="round|none|square"
60 *    color="0x000000"
61 *    joints="round|bevel|miter"
62 *    miterLimit="3"
63 *    pixelHinting="false|true"
64 *    scaleMode="normal|none|horizontal|vertical"
65 *    weight="1 (<i>in most cases</i>)"
66 *  /&gt;
67 *  </pre>
68 *
69 *  @see flash.display.Graphics
70 *
71 *  @langversion 3.0
72 *  @playerversion Flash 9
73 *  @playerversion AIR 1.1
74 *  @productversion Flex 3
75 */
76public class SolidColorStroke extends EventDispatcher implements IStroke
77{
78    include "../core/Version.as";
79
80    //--------------------------------------------------------------------------
81    //
82    //  Constructor
83    //
84    //--------------------------------------------------------------------------
85
86    /**
87     *  Constructor.
88     *
89     *  @param color Specifies the line color.
90     *  The default value is 0x000000 (black).
91     *
92     *  @param weight Specifies the line weight, in pixels.
93     *  The default value is 1.
94     *
95     *  @param alpha Specifies the alpha value in the range 0.0 to 1.0.
96     *  The default value is 1.0 (opaque).
97     *
98     *  @param pixelHinting Specifies whether to hint strokes to full pixels.
99     *  This value affects both the position of anchors of a curve
100     *  and the line stroke size itself.
101     *  The default value is false.
102     *
103     *  @param scaleMode A value from the LineScaleMode class
104     *  that specifies which scale mode to use.
105     *  Valid values are <code>LineScaleMode.HORIZONTAL</code>,
106     *  <code>LineScaleMode.NONE</code>, <code>LineScaleMode.NORMAL</code>,
107     *  and <code>LineScaleMode.VERTICAL</code>.
108     *  This parameter is optional,
109     *  with a default value of <code>LineScaleMode.NORMAL</code>.
110     *
111     *  @param caps Specifies the type of caps at the end of lines.
112     *  Valid values are <code>CapsStyle.ROUND</code>, <code>CapsStyle.SQUARE</code>,
113     *  and <code>CapsStyle.NONE</code>.
114     *  The default value is <code>CapsStyle.ROUND</code>.
115     *
116     *  @param joints Specifies the type of joint appearance used at angles.
117     *  Valid values are <code>JointStyle.ROUND</code>, <code>JointStyle.MITER</code>,
118     *  and <code>JointStyle.BEVEL</code>.
119     *  The default value is <code>JointStyle.ROUND</code>.
120     *
121     *  @param miterLimit Indicates the limit at which a miter is cut off.
122     *  Valid values range from 1 to 255.
123     *  The default value is 3.
124     *
125     *  @langversion 3.0
126     *  @playerversion Flash 9
127     *  @playerversion AIR 1.1
128     *  @productversion Flex 3
129     */
130    public function SolidColorStroke(color:uint = 0x000000,
131                                     weight:Number = 1,
132                                     alpha:Number = 1.0,
133                                     pixelHinting:Boolean = false,
134                                     scaleMode:String = "normal",
135                                     caps:String = "round",
136                                     joints:String = "round",
137                                     miterLimit:Number = 3)
138    {
139        super();
140
141        this.color = color;
142        this._weight = weight;
143        this.alpha = alpha;
144        this.pixelHinting = pixelHinting;
145        this.scaleMode = scaleMode;
146        this.caps = caps;
147        this.joints = joints;
148        this.miterLimit = miterLimit;
149    }
150
151    //--------------------------------------------------------------------------
152    //
153    //  Properties
154    //
155    //--------------------------------------------------------------------------
156
157    //----------------------------------
158    //  alpha
159    //----------------------------------
160
161    private var _alpha:Number = 0.0;
162
163    [Bindable("propertyChange")]
164    [Inspectable(category="General")]
165
166    /**
167     *  The transparency of a line.
168     *  Possible values are 0.0 (invisible) through 1.0 (opaque).
169     *
170     *  @default 1.0.
171     *
172     *  @langversion 3.0
173     *  @playerversion Flash 9
174     *  @playerversion AIR 1.1
175     *  @productversion Flex 3
176     */
177    public function get alpha():Number
178    {
179        return _alpha;
180    }
181
182    /**
183     *  @private
184     */
185    public function set alpha(value:Number):void
186    {
187        var oldValue:Number = _alpha;
188        if (value != oldValue)
189        {
190            _alpha = value;
191            dispatchStrokeChangedEvent("alpha", oldValue, value);
192        }
193    }
194
195    //----------------------------------
196    //  caps
197    //----------------------------------
198
199    private var _caps:String = CapsStyle.ROUND;
200
201    [Bindable("propertyChange")]
202    [Inspectable(category="General", enumeration="round,square,none", defaultValue="round")]
203
204    /**
205     *  Specifies the type of caps at the end of lines.
206     *  Valid values are: <code>CapsStyle.ROUND</code>, <code>CapsStyle.SQUARE</code>,
207     *  and <code>CapsStyle.NONE</code>.
208     *
209     *  @default CapsStyle.ROUND
210     *
211     *  @langversion 3.0
212     *  @playerversion Flash 9
213     *  @playerversion AIR 1.1
214     *  @productversion Flex 3
215     */
216    public function get caps():String
217    {
218        return _caps;
219    }
220
221    public function set caps(value:String):void
222    {
223        var oldValue:String = _caps;
224        if (value != oldValue)
225        {
226            _caps = value;
227            dispatchStrokeChangedEvent("caps", oldValue, value);
228        }
229    }
230
231    //----------------------------------
232    //  color
233    //----------------------------------
234
235    private var _color:uint = 0x000000;
236
237    [Bindable("propertyChange")]
238    [Inspectable(category="General", format="Color")]
239
240    /**
241     *  The line color.
242     *
243     *  @default 0x000000 (black).
244     *
245     *  @langversion 3.0
246     *  @playerversion Flash 9
247     *  @playerversion AIR 1.1
248     *  @productversion Flex 3
249     */
250    public function get color():uint
251    {
252        return _color;
253    }
254
255    public function set color(value:uint):void
256    {
257        var oldValue:uint = _color;
258        if (value != oldValue)
259        {
260            _color = value;
261            dispatchStrokeChangedEvent("color", oldValue, value);
262        }
263    }
264
265    //----------------------------------
266    //  joints
267    //----------------------------------
268
269    private var _joints:String = JointStyle.ROUND;
270
271    [Bindable("propertyChange")]
272    [Inspectable(category="General", enumeration="round,bevel,miter", defaultValue="round")]
273
274    /**
275     *  Specifies the type of joint appearance used at angles.
276     *  Valid values are <code>JointStyle.ROUND</code>, <code>JointStyle.MITER</code>,
277     *  and <code>JointStyle.BEVEL</code>.
278     *
279     *  @default JointStyle.ROUND
280     *
281     *  @langversion 3.0
282     *  @playerversion Flash 9
283     *  @playerversion AIR 1.1
284     *  @productversion Flex 3
285     */
286    public function get joints():String
287    {
288        return _joints;
289    }
290
291    public function set joints(value:String):void
292    {
293        var oldValue:String = _joints;
294        if (value != oldValue)
295        {
296            _joints = value;
297            dispatchStrokeChangedEvent("joints", oldValue, value);
298        }
299    }
300
301    //----------------------------------
302    //  miterLimit
303    //----------------------------------
304
305    private var _miterLimit:Number = 3;
306
307    [Bindable("propertyChange")]
308    [Inspectable(category="General", minValue="1.0", maxValue="255.0")]
309
310    /**
311     *  Indicates the limit at which a miter is cut off.
312     *  Valid values range from 1 to 255.
313     *
314     *  @default 3
315     *
316     *  @langversion 3.0
317     *  @playerversion Flash 9
318     *  @playerversion AIR 1.1
319     *  @productversion Flex 3
320     */
321    public function get miterLimit():Number
322    {
323        return _miterLimit;
324    }
325
326    public function set miterLimit(value:Number):void
327    {
328        var oldValue:Number = _miterLimit;
329        if (value != oldValue)
330        {
331            _miterLimit = value;
332            dispatchStrokeChangedEvent("miterLimit", oldValue, value);
333        }
334    }
335
336    //----------------------------------
337    //  pixelHinting
338    //----------------------------------
339
340    private var _pixelHinting:Boolean = false;
341
342    [Bindable("propertyChange")]
343    [Inspectable(category="General")]
344
345    /**
346     *  Specifies whether to hint strokes to full pixels.
347     *  This value affects both the position of anchors of a curve
348     *  and the line stroke size itself.
349     *
350     *  @default false
351     *
352     *  @langversion 3.0
353     *  @playerversion Flash 9
354     *  @playerversion AIR 1.1
355     *  @productversion Flex 3
356     */
357    public function get pixelHinting():Boolean
358    {
359        return _pixelHinting;
360    }
361
362    public function set pixelHinting(value:Boolean):void
363    {
364        var oldValue:Boolean = _pixelHinting;
365        if (value != oldValue)
366        {
367            _pixelHinting = value;
368            dispatchStrokeChangedEvent("pixelHinting", oldValue, value);
369        }
370    }
371
372    //----------------------------------
373    //  scaleMode
374    //----------------------------------
375
376    private var _scaleMode:String = "normal";
377
378    [Bindable("propertyChange")]
379    [Inspectable(category="General", enumeration="normal,vertical,horizontal,none", defaultValue="normal")]
380
381     /**
382     *  A value from the LineScaleMode class
383     *  that  specifies which scale mode to use.
384     *  Value valids are:
385     *
386     *  <ul>
387     *  <li>
388     *  <code>LineScaleMode.NORMAL</code>&#151;
389     *  Always scale the line thickness when the object is scaled  (the default).
390     *  </li>
391     *  <li>
392     *  <code>LineScaleMode.NONE</code>&#151;
393     *  Never scale the line thickness.
394     *  </li>
395     *  <li>
396     *  <code>LineScaleMode.VERTICAL</code>&#151;
397     *  Do not scale the line thickness if the object is scaled vertically
398     *  <em>only</em>.
399     *  </li>
400     *  <li>
401     *  <code>LineScaleMode.HORIZONTAL</code>&#151;
402     *  Do not scale the line thickness if the object is scaled horizontally
403     *  <em>only</em>.
404     *  </li>
405     *  </ul>
406     *
407     *  @default LineScaleMode.NORMAL
408     *
409     *  @langversion 3.0
410     *  @playerversion Flash 9
411     *  @playerversion AIR 1.1
412     *  @productversion Flex 3
413     */
414    public function get scaleMode():String
415    {
416        return _scaleMode;
417    }
418
419    public function set scaleMode(value:String):void
420    {
421        var oldValue:String = _scaleMode;
422        if (value != oldValue)
423        {
424            _scaleMode = value;
425            dispatchStrokeChangedEvent("scaleMode", oldValue, value);
426        }
427    }
428
429    //----------------------------------
430    //  weight
431    //----------------------------------
432
433    /**
434     *  @private
435     *  Storage for the weight property.
436     */
437    private var _weight:Number;
438
439    [Bindable("propertyChange")]
440    [Inspectable(category="General", minValue="0.0")]
441
442    /**
443     *  The line weight, in pixels.
444     *  For many charts, the default value is 1 pixel.
445     *
446     *  @langversion 3.0
447     *  @playerversion Flash 9
448     *  @playerversion AIR 1.1
449     *  @productversion Flex 3
450     */
451    public function get weight():Number
452    {
453        return _weight;
454    }
455
456    /**
457     *  @private
458     */
459    public function set weight(value:Number):void
460    {
461        var oldValue:Number = _weight;
462        if (value != oldValue)
463        {
464            _weight = value;
465            dispatchStrokeChangedEvent("weight", oldValue, value);
466        }
467    }
468
469    //--------------------------------------------------------------------------
470    //
471    //  Methods
472    //
473    //--------------------------------------------------------------------------
474
475    /**
476     *  @inheritDoc
477     *
478     *  @langversion 3.0
479     *  @playerversion Flash 9
480     *  @playerversion AIR 1.1
481     *  @productversion Flex 3
482     */
483    public function apply(graphics:Graphics, targetBounds:Rectangle, targetOrigin:Point):void
484    {
485        graphics.lineStyle(weight, color, alpha, pixelHinting,
486                    scaleMode, caps, joints, miterLimit);
487    }
488
489    /**
490     *  @inheritDoc
491     *
492     *  @langversion 3.0
493     *  @playerversion Flash 9
494     *  @playerversion AIR 1.1
495     *  @productversion Flex 3
496     */
497    public function createGraphicsStroke(targetBounds:Rectangle, targetOrigin:Point):GraphicsStroke
498    {
499        // Construct a new GraphicsStroke object and set all of
500        // its properties to match the SolidColorStroke's
501        // properties
502        var graphicsStroke:GraphicsStroke = new GraphicsStroke();
503        graphicsStroke.thickness = weight;
504        graphicsStroke.miterLimit = miterLimit;
505        graphicsStroke.pixelHinting = pixelHinting;
506        graphicsStroke.scaleMode = scaleMode;
507
508        // There is a bug in Drawing API-2 where if no caps is
509        // specified, a value of 'none' is used instead of 'round'
510        graphicsStroke.caps = (!caps) ? CapsStyle.ROUND : caps;
511
512        // Give the GraphicsStroke a GraphicsSolidFill corresponding to the
513        // SolidColorStroke's color and alpha values
514        var graphicsSolidFill:GraphicsSolidFill = new GraphicsSolidFill();
515        graphicsSolidFill.color = color;
516        graphicsSolidFill.alpha = alpha;
517        graphicsStroke.fill = graphicsSolidFill;
518
519        return graphicsStroke;
520    }
521
522    /**
523     *  @private
524     */
525    private function dispatchStrokeChangedEvent(prop:String, oldValue:*,
526                                                value:*):void
527    {
528        if (hasEventListener("propertyChange"))
529            dispatchEvent(PropertyChangeEvent.createUpdateEvent(this, prop,
530                oldValue, value));
531    }
532}
533
534}
535