better still...
[potlatch2.git] / net / systemeD / halcyon / styleparser / TextStyle.as
1 package net.systemeD.halcyon.styleparser {
2
3         import flash.display.*;
4         import flash.text.TextField;
5         import flash.text.TextFormat;
6         import flash.filters.BitmapFilter;
7         import flash.filters.GlowFilter;
8
9         public class TextStyle extends Style {
10
11                 public var font_family:String;
12                 public var font_bold:Boolean;
13                 public var font_italic:Boolean;
14                 public var font_underline:Boolean;
15                 public var font_caps:Boolean;
16                 public var font_size:uint;
17                 public var text_color:uint;
18                 public var text_offset:Number;          // Y offset - i.e. within or outside casing?
19                 public var max_width:Number;            // maximum width of label
20                 public var text:String;
21                 public var text_halo_color:uint;
22                 public var text_halo_radius:uint=0;
23                 public var text_center:Boolean;
24                 public var letter_spacing:uint;
25
26                 override public function get properties():Array {
27                         return [
28                                 'font_family','font_bold','font_italic','font_caps','font_size',
29                                 'text_color','text_offset','max_width',
30                                 'text','text_halo_color','text_halo_radius','text_center',
31                                 'letter_spacing'
32                         ];
33                 }
34
35                 override public function get drawn():Boolean {
36                         return (text!=null);
37                 }
38
39                 public function getTextFormat():TextFormat {
40                         var tf:TextFormat=new TextFormat(font_family    ? font_family   : "DejaVu",
41                                                          font_size      ? font_size     : 8,
42                                                          text_color     ? text_color    : 0,
43                                                          font_bold      ? font_bold     : false,
44                                                          font_italic    ? font_italic   : false,
45                                                          font_underline ? font_underline: false);
46                         tf.letterSpacing=(letter_spacing ? letter_spacing : 0);
47                         return tf;
48                 }
49         
50                 public function getHaloFilter():Array {
51                         var filter:BitmapFilter=new GlowFilter(text_halo_color  ? text_halo_color  : 0xFFFFFF,1,
52                                                                                                    text_halo_radius ? text_halo_radius: 2,
53                                                                                                    text_halo_radius ? text_halo_radius: 2,255);
54                         return [filter];
55                 }
56                 
57                 public function writeNameLabel(d:DisplayObjectContainer,a:String,x:Number,y:Number):TextField {
58                         var tf:TextField = new TextField();
59                         var n:TextFormat = getTextFormat();
60                         n.align = "center";
61                         tf.embedFonts = true;
62                         tf.defaultTextFormat = n;
63                         tf.text = a;
64                         if (max_width) {
65                                 tf.width=max_width;
66                                 tf.wordWrap=true;
67                                 tf.height=tf.textHeight+4;
68                         } else {
69                                 tf.width = tf.textWidth+4;
70                                 tf.height = tf.textHeight+4;
71                         }
72                         if (text_halo_radius>0) { tf.filters=getHaloFilter(); }
73                         d.x=x-tf.width/2;
74                         d.y=y-tf.height/2; tf.y=text_offset ? text_offset : 0;
75                         d.addChild(tf);
76
77                         return tf;
78                 }
79
80         }
81
82 }