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