My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2008 Tink Ltd | http://www.tink.ws
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

package ws.tink.mx.skins
{

import flash.display.Graphics;
import flash.display.Sprite;
import flash.geom.Matrix;

import mx.core.IFlexDisplayObject;
import mx.core.IInvalidating;
import mx.core.IProgrammaticSkin;
import mx.core.UIComponentGlobals;
import mx.core.mx_internal;
import mx.managers.ILayoutManagerClient;
import mx.styles.ISimpleStyleClient;
import mx.styles.IStyleClient;
import mx.utils.GraphicsUtil;

use namespace mx_internal;

/**
* This class is the base class for skin elements
* which draw themselves programmatically.
*/
public class SpriteProgrammaticSkin extends Sprite
implements IFlexDisplayObject, IInvalidating,
ILayoutManagerClient, ISimpleStyleClient, IProgrammaticSkin
{

//--------------------------------------------------------------------------
//
// Class variables
//
//--------------------------------------------------------------------------

/**
* @private
* Set by horizontalGradientMatrix() or verticalGradientMatrix().
*/
private static var tempMatrix:Matrix = new Matrix();

//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------

/**
* Constructor.
*/
public function SpriteProgrammaticSkin()
{
super();

// If nobody explicitly sets a size for this object,
// then set its width and height to be its measured size.
_width = measuredWidth;
_height = measuredHeight;
}

//--------------------------------------------------------------------------
//
// Variables
//
//--------------------------------------------------------------------------

/**
* @private
*/
private var invalidateDisplayListFlag:Boolean = false;

//--------------------------------------------------------------------------
//
// Overridden properties
//
//--------------------------------------------------------------------------

//----------------------------------
// height
//----------------------------------

/**
* @private
* Storage for the height property.
*/
private var _height:Number;

/**
* @private
*/
override public function get height():Number
{
return _height;
}

/**
* @private
*/
override public function set height(value:Number):void
{
_height = value;

invalidateDisplayList();
}

//----------------------------------
// width
//----------------------------------

/**
* @private
* Storage for the width property.
*/
private var _width:Number;

/**
* @private
*/
override public function get width():Number
{
return _width;
}

/**
* @private
*/
override public function set width(value:Number):void
{
_width = value;

invalidateDisplayList();
}

//--------------------------------------------------------------------------
//
// Properties: IFlexDisplayObject
//
//--------------------------------------------------------------------------

//----------------------------------
// measuredHeight
//----------------------------------

/**
* The measured height of this object.
* This should be overridden by subclasses to return the preferred height for
* the skin.
*
* @return The measured height of the object, in pixels.
*/
public function get measuredHeight():Number
{
return 0;
}

//----------------------------------
// measuredWidth
//----------------------------------

/**
* The measured width of this object.
* This should be overridden by subclasses to return the preferred width for
* the skin.
*
* @return The measured width of the object, in pixels.
*/
public function get measuredWidth():Number
{
return 0;
}

//--------------------------------------------------------------------------
//
// Properties: ILayoutManagerClient
//
//--------------------------------------------------------------------------

//----------------------------------
// initialized
//----------------------------------

/**
* @private
* Storage for the initialized property.
*/
private var _initialized:Boolean = false;

/**
* @copy mx.core.UIComponent#initialized
*/
public function get initialized():Boolean
{
return _initialized;
}

/**
* @private
*/
public function set initialized(value:Boolean):void
{
_initialized = value;
}

//----------------------------------
// nestLevel
//----------------------------------

/**
* @private
* Storage for the nestLevel property.
*/
private var _nestLevel:int = 0;

/**
* @copy mx.core.UIComponent#nestLevel
*/
public function get nestLevel():int
{
return _nestLevel;
}

/**
* @private
*/
public function set nestLevel(value:int):void
{
_nestLevel = value;

// After nestLevel is initialized, add this object to the
// LayoutManager's queue, so that it is drawn at least once
invalidateDisplayList();
}

//----------------------------------
// processedDescriptors
//----------------------------------

/**
* @private
* Storage for the processedDescriptors property.
*/
private var _processedDescriptors:Boolean = false;

/**
* @copy mx.core.UIComponent#processedDescriptors
*/
public function get processedDescriptors():Boolean
{
return _processedDescriptors;
}

/**
* @private
*/
public function set processedDescriptors(value:Boolean):void
{
_processedDescriptors = value;
}

//----------------------------------
// updateCompletePendingFlag
//----------------------------------

/**
* @private
* Storage for the updateCompletePendingFlag property.
*/
private var _updateCompletePendingFlag:Boolean = true;

/**
* A flag that determines if an object has been through all three phases
* of layout validation (provided that any were required).
*/
public function get updateCompletePendingFlag():Boolean
{
return _updateCompletePendingFlag;
}

/**
* @private
*/
public function set updateCompletePendingFlag(value:Boolean):void
{
_updateCompletePendingFlag = value;
}

//--------------------------------------------------------------------------
//
// Properties: ISimpleStyleClient
//
//--------------------------------------------------------------------------

//----------------------------------
// styleName
//----------------------------------

/**
* @private
* Storage for the styleName property.
* For skins, it is always a UIComponent.
*/
private var _styleName:IStyleClient;

/**
* A parent component used to obtain style values. This is typically set to the
* component that created this skin.
*/
public function get styleName():Object
{
return _styleName;
}

/**
* @private
*/
public function set styleName(value:Object):void
{
if (_styleName != value)
{
_styleName = value as IStyleClient;
invalidateDisplayList();
}
}

//--------------------------------------------------------------------------
//
// Methods: IFlexDisplayObject
//
//--------------------------------------------------------------------------

/**
* Moves this object to the specified x and y coordinates.
*
* @param x The horizontal position, in pixels.
*
* @param y The vertical position, in pixels.
*/
public function move(x:Number, y:Number):void
{
this.x = x;
this.y = y;
}

/**
* Sets the height and width of this object.
*
* @param newWidth The width, in pixels, of this object.
*
* @param newHeight The height, in pixels, of this object.
*/
public function setActualSize(newWidth:Number, newHeight:Number):void
{
var changed:Boolean = false;

if (_width != newWidth)
{
_width = newWidth;
changed = true;
}

if (_height != newHeight)
{
_height = newHeight;
changed = true;
}

if (changed)
invalidateDisplayList();
}

//--------------------------------------------------------------------------
//
// Methods: ILayoutManagerClient
//
//--------------------------------------------------------------------------

/**
* This function is an empty stub so that ProgrammaticSkin
* can implement the ILayoutManagerClient interface.
* Skins do not call <code>LayoutManager.invalidateProperties()</code>,
* which would normally trigger a call to this method.
*/
public function validateProperties():void
{
}

/**
* This function is an empty stub so that ProgrammaticSkin
* can implement the ILayoutManagerClient interface.
* Skins do not call <code>LayoutManager.invalidateSize()</code>,
* which would normally trigger a call to this method.
*
* @param recursive Determines whether children of this skin are validated.
*/
public function validateSize(recursive:Boolean = false):void
{
}

/**
* This function is called by the LayoutManager
* when it's time for this control to draw itself.
* The actual drawing happens in the <code>updateDisplayList</code>
* function, which is called by this function.
*/
public function validateDisplayList():void
{
invalidateDisplayListFlag = false;

updateDisplayList(width, height);
}

//--------------------------------------------------------------------------
//
// Methods: ISimpleStyleClient
//
//--------------------------------------------------------------------------

/**
* Whenever any style changes, redraw this skin.
* Subclasses can override this method
* and perform a more specific test before calling invalidateDisplayList().
*
* @param styleProp The name of the style property that changed, or null
* if all styles have changed.
*/
public function styleChanged(styleProp:String):void
{
invalidateDisplayList();
}

//--------------------------------------------------------------------------
//
// Methods: Other
//
//--------------------------------------------------------------------------

/**
* @copy mx.core.UIComponent#invalidateDisplayList()
*/
public function invalidateDisplayList():void
{
// Don't try to add the object to the display list queue until we've
// been assigned a nestLevel, or we'll get added at the wrong place in
// the LayoutManager's priority queue.
if (!invalidateDisplayListFlag && nestLevel > 0)
{
invalidateDisplayListFlag = true;
UIComponentGlobals.layoutManager.invalidateDisplayList(this);
}
}

/**
* Programmatically draws the graphics for this skin.
*
* <p>Subclasses should override this method and include calls
* to methods such as <code>graphics.moveTo()</code> and
* <code>graphics.lineTo()</code>.</p>
*
* <p>This occurs before any scaling from sources
* such as user code or zoom effects.
* The component is unaware of the scaling that takes place later.</p>
*
* @param unscaledWidth
* The width, in pixels, of this object before any scaling.
*
* @param unscaledHeight
* The height, in pixels, of this object before any scaling.
*/
protected function updateDisplayList(unscaledWidth:Number,
unscaledHeight:Number):void
{
}

/**
* @inheritDoc
*/
public function invalidateSize():void
{
}

/**
* @inheritDoc
*/
public function invalidateProperties():void
{
}

/**
* Validate and update the properties and layout of this object
* and redraw it, if necessary.
*/
public function validateNow():void
{
// Since we don't have commit/measure/layout phases,
// all we need to do here is the draw phase
if (invalidateDisplayListFlag)
validateDisplayList();
}

/**
* Returns the value of the specified style property.
*
* @param styleProp Name of the style property.
*
* @return The style value. This can be any type of object that style properties can be, such as
* int, Number, String, etc.
*/
public function getStyle(styleProp:String):*
{
return _styleName.getStyle(styleProp);
}

/**
* Utility function to create a horizontal gradient matrix.
*
* @param x The left edge of the gradient.
*
* @param y The top edge of the gradient.
*
* @param width The width of the gradient.
*
* @param height The height of the gradient.
*
* @return The horizontal gradient matrix. This is a temporary
* object that should only be used for a single subsequent call
* to the <code>drawRoundRect()</code> method.
*/
protected function horizontalGradientMatrix(x:Number, y:Number,
width:Number,
height:Number):Matrix
{
return rotatedGradientMatrix(x, y, width, height, 0);
}

/**
* Utility function to create a vertical gradient matrix.
*
* @param x The left edge of the gradient.
*
* @param y The top edge of the gradient.
*
* @param width The width of the gradient.
*
* @param height The height of the gradient.
*
* @return The horizontal gradient matrix. This is a temporary
* object that should only be used for a single subsequent call
* to the <code>drawRoundRect()</code> method.
*/
protected function verticalGradientMatrix(x:Number, y:Number,
width:Number,
height:Number):Matrix
{
return rotatedGradientMatrix(x, y, width, height, 90);
}

/**
* Utility function to create a rotated gradient matrix.
*
* @param x The left edge of the gradient.
*
* @param y The top edge of the gradient.
*
* @param width The width of the gradient.
*
* @param height The height of the gradient.
*
* @param rotation The amount to rotate, in degrees.
*
* @return The horizontal gradient matrix. This is a temporary
* object that should only be used for a single subsequent call
* to the <code>drawRoundRect()</code> method.
*/
protected function rotatedGradientMatrix(x:Number, y:Number,
width:Number,
height:Number,
rotation:Number):Matrix
{
tempMatrix.createGradientBox(width, height,
rotation * Math.PI / 180, x, y);
return tempMatrix;
}

/**
* Programatically draws a rectangle into this skin's Graphics object.
*
* <p>The rectangle can have rounded corners.
* Its edges are stroked with the current line style
* of the Graphics object.
* It can have a solid color fill, a gradient fill, or no fill.
* A solid fill can have an alpha transparency.
* A gradient fill can be linear or radial. You can specify
* up to 15 colors and alpha values at specified points along
* the gradient, and you can specify a rotation angle
* or transformation matrix for the gradient.
* Finally, the rectangle can have a rounded rectangular hole
* carved out of it.</p>
*
* <p>This versatile rectangle-drawing routine is used by many skins.
* It calls the <code>drawRect()</code> or
* <code>drawRoundRect()</code>
* methods (in the flash.display.Graphics class) to draw into this
* skin's Graphics object.</p>
*
* @param x Horizontal position of upper-left corner
* of rectangle within this skin.
*
* @param y Vertical position of upper-left corner
* of rectangle within this skin.
*
* @param width Width of rectangle, in pixels.
*
* @param height Height of rectangle, in pixels.
*
* @param cornerRadius Corner radius/radii of rectangle.
* Can be <code>null</code>, a Number, or an Object.
* If it is <code>null</code>, it specifies that the corners should be square
* rather than rounded.
* If it is a Number, it specifies the same radius, in pixels,
* for all four corners.
* If it is an Object, it should have properties named
* <code>tl</code>, <code>tr</code>, <code>bl</code>, and
* <code>br</code>, whose values are Numbers specifying
* the radius, in pixels, for the top left, top right,
* bottom left, and bottom right corners.
* For example, you can pass a plain Object such as
* <code>{ tl: 5, tr: 5, bl: 0, br: 0 }</code>.
* The default value is null (square corners).
*
* @param color The RGB color(s) for the fill.
* Can be <code>null</code>, a uint, or an Array.
* If it is <code>null</code>, the rectangle not filled.
* If it is a uint, it specifies an RGB fill color.
* For example, pass <code>0xFF0000</code> to fill with red.
* If it is an Array, it should contain uints
* specifying the gradient colors.
* For example, pass <code>[ 0xFF0000, 0xFFFF00, 0x0000FF ]</code>
* to fill with a red-to-yellow-to-blue gradient.
* You can specify up to 15 colors in the gradient.
* The default value is null (no fill).
*
* @param alpha Alpha value(s) for the fill.
* Can be null, a Number, or an Array.
* This argument is ignored if <code>color</code> is null.
* If <code>color</code> is a uint specifying an RGB fill color,
* then <code>alpha</code> should be a Number specifying
* the transparency of the fill, where 0.0 is completely transparent
* and 1.0 is completely opaque.
* You can also pass null instead of 1.0 in this case
* to specify complete opaqueness.
* If <code>color</code> is an Array specifying gradient colors,
* then <code>alpha</code> should be an Array of Numbers, of the
* same length, that specifies the corresponding alpha values
* for the gradient.
* In this case, the default value is <code>null</code> (completely opaque).
*
* @param gradientMatrix Matrix object used for the gradient fill.
* The utility methods <code>horizontalGradientMatrix()</code>,
* <code>verticalGradientMatrix()</code>, and
* <code>rotatedGradientMatrix()</code> can be used to create the value for
* this parameter.
*
* @param gradientType Type of gradient fill. The possible values are
* <code>GradientType.LINEAR</code> or <code>GradientType.RADIAL</code>.
* (The GradientType class is in the package flash.display.)
*
* @param gradientRatios (optional default [0,255])
* Specifies the distribution of colors. The number of entries must match
* the number of colors defined in the <code>color</code> parameter.
* Each value defines the percentage of the width where the color is
* sampled at 100%. The value 0 represents the left-hand position in
* the gradient box, and 255 represents the right-hand position in the
* gradient box.
*
* @param hole (optional) A rounded rectangular hole
* that should be carved out of the middle
* of the otherwise solid rounded rectangle
* { x: #, y: #, w: #, h: #, r: # or { br: #, bl: #, tl: #, tr: # } }
*
* @see flash.display.Graphics#beginGradientFill()
*/
protected function drawRoundRect(
x:Number, y:Number, width:Number, height:Number,
cornerRadius:Object = null,
color:Object = null,
alpha:Object = null,
gradientMatrix:Matrix = null,
gradientType:String = "linear",
gradientRatios:Array /* of Number */ = null,
hole:Object = null):void
{
var g:Graphics = graphics;

// Quick exit if weight or height is zero.
// This happens when scaling a component to a very small value,
// which then gets rounded to 0.
if (width == 0 || height == 0)
return;

// If color is an object then allow for complex fills.
if (color !== null)
{
if (color is uint)
{
g.beginFill(uint(color), Number(alpha));
}
else if (color is Array)
{
var alphas:Array = alpha is Array ?
alpha as Array :
[ alpha, alpha ];

if (!gradientRatios)
gradientRatios = [ 0, 0xFF ];

g.beginGradientFill(gradientType,
color as Array, alphas,
gradientRatios, gradientMatrix);
}
}

var ellipseSize:Number;

// Stroke the rectangle.
if (!cornerRadius)
{
g.drawRect(x, y, width, height);
}
else if (cornerRadius is Number)
{
ellipseSize = Number(cornerRadius) * 2;
g.drawRoundRect(x, y, width, height,
ellipseSize, ellipseSize);
}
else
{
GraphicsUtil.drawRoundRectComplex(g,
x, y, width, height,
cornerRadius.tl, cornerRadius.tr,
cornerRadius.bl, cornerRadius.br);
}

// Carve a rectangular hole out of the middle of the rounded rect.
if (hole)
{
var holeR:Object = hole.r;
if (holeR is Number)
{
ellipseSize = Number(holeR) * 2;
g.drawRoundRect(hole.x, hole.y, hole.w, hole.h,
ellipseSize, ellipseSize);
}
else
{
GraphicsUtil.drawRoundRectComplex(g,
hole.x, hole.y, hole.w, hole.h,
holeR.tl, holeR.tr, holeR.bl, holeR.br);
}
}

if (color !== null)
g.endFill();
}
}

}

Change log

r230 by sdowns on Oct 15, 2010   Diff
commenting and open sourced further
components
Go to: 
Project members, sign in to write a code review

Older revisions

r224 by sdowns on Oct 15, 2010   Diff
Separation of code
r222 by sdowns on Oct 15, 2010   Diff
Separation of code
All revisions of this file

File info

Size: 22781 bytes, 785 lines
Powered by Google Project Hosting