root/trunk/as3/com/jeroenwijering/utils/Draw.as @ 2

Revision 2, 2.6 kB (checked in by jeroen, 18 months ago)

fixes in skinning and controlbar

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