| 1 | /** |
|---|
| 2 | * Functions for drawing commonly used elements. |
|---|
| 3 | **/ |
|---|
| 4 | package com.jeroenwijering.utils { |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import flash.display.*; |
|---|
| 8 | import flash.geom.Rectangle; |
|---|
| 9 | import flash.text.TextField; |
|---|
| 10 | import flash.text.TextFormat; |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | public class Draw { |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * Completely clear the contents of a displayobject. |
|---|
| 18 | * |
|---|
| 19 | * @param tgt Displayobject to clear. |
|---|
| 20 | **/ |
|---|
| 21 | public static function clear(tgt:Sprite) { |
|---|
| 22 | var len = tgt.numChildren; |
|---|
| 23 | for(var i=0; i<len; i++) { |
|---|
| 24 | tgt.removeChildAt(0); |
|---|
| 25 | } |
|---|
| 26 | tgt.scaleX = tgt.scaleY = 1; |
|---|
| 27 | }; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Clone a displayobject. |
|---|
| 32 | * |
|---|
| 33 | * @param tgt Displayobject to clone. |
|---|
| 34 | * |
|---|
| 35 | * @return The clone; not yet added to the displaystack. |
|---|
| 36 | **/ |
|---|
| 37 | public static function clone(tgt:MovieClip):MovieClip { |
|---|
| 38 | var cls:Class = Object(tgt).constructor; |
|---|
| 39 | var dup:MovieClip = new cls(); |
|---|
| 40 | dup.transform = tgt.transform; |
|---|
| 41 | if(tgt.scale9Grid) { |
|---|
| 42 | var rct:Rectangle = tgt.scale9Grid; |
|---|
| 43 | rct.x /= 20, rct.y /= 20, rct.width /= 20, rct.height /= 20; |
|---|
| 44 | dup.scale9Grid = rct; |
|---|
| 45 | } |
|---|
| 46 | return dup; |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Draw a rectangle on stage. |
|---|
| 52 | * |
|---|
| 53 | * @param tgt Displayobject to add the rectangle to. |
|---|
| 54 | * @param col Color of the rectangle. |
|---|
| 55 | * @param wid Width of the rectangle. |
|---|
| 56 | * @param hei Height of the rectangle. |
|---|
| 57 | * @param xps X offset of the rectangle, defaults to 0. |
|---|
| 58 | * @param yps Y offset of the rectangle, defaults to 0. |
|---|
| 59 | **/ |
|---|
| 60 | public static function rect(tgt:Sprite,col:String,wid:Number,hei:Number,xps:Number=0,yps:Number=0,alp:Number=1):Sprite { |
|---|
| 61 | var rct = new Sprite(); |
|---|
| 62 | rct.x = xps; |
|---|
| 63 | rct.y = yps; |
|---|
| 64 | rct.graphics.beginFill(col,alp); |
|---|
| 65 | rct.graphics.drawRect(0,0,wid,hei); |
|---|
| 66 | tgt.addChild(rct); |
|---|
| 67 | return rct; |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * Draw a textfield on stage. |
|---|
| 73 | * |
|---|
| 74 | * @param tgt Displayobject to add the textfield to. |
|---|
| 75 | * @param col Color of the text. |
|---|
| 76 | * @param xps X offset of the rectangle. |
|---|
| 77 | * @param yps Y offset of the rectangle. |
|---|
| 78 | * @param txt Text string to print. |
|---|
| 79 | * @param ats Textfield autosize direction, defaults to left. |
|---|
| 80 | * @param siz Font size, defaults to 12. |
|---|
| 81 | * |
|---|
| 82 | * @return A reference to the textfield. |
|---|
| 83 | **/ |
|---|
| 84 | public static function text(tgt:Sprite,col:String,xps:Number,yps:Number,txt:String,ats:String='left',siz:Number=12,fnt:String='Arial'):TextField { |
|---|
| 85 | var tfd = new TextField(); |
|---|
| 86 | var fmt = new TextFormat(); |
|---|
| 87 | tfd.autoSize = ats; |
|---|
| 88 | tfd.selectable = false; |
|---|
| 89 | fmt.font = fnt; |
|---|
| 90 | fmt.color = col; |
|---|
| 91 | fmt.size = siz; |
|---|
| 92 | fmt.underline = false; |
|---|
| 93 | tfd.defaultTextFormat = fmt; |
|---|
| 94 | tfd.x = xps; |
|---|
| 95 | tfd.y = yps; |
|---|
| 96 | tfd.text = txt; |
|---|
| 97 | tgt.addChild(tfd); |
|---|
| 98 | return tfd; |
|---|
| 99 | }; |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | } |
|---|