public
Description: Github flash badge
Homepage: http://spinach.andascarygoat.com/tags/badjo
Clone URL: git://github.com/zmack/badjo.git
zmack (author)
Sat May 03 11:09:34 -0700 2008
commit  d78f7e67afd085e5a2d721e31665499ac9b129a8
tree    cb66efed8615d0e68443d5eff89ee058cc6c200a
parent  82f4b90a3d58e194eda814c0bbcec0529e7b0515
badjo / src / PickleButton.as
100644 134 lines (109 sloc) 3.762 kb
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
package {
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.text.StyleSheet;
  
  public class PickleButton extends Sprite {
    public static const NORMAL_COLOR:uint = 0xFF00FF;
    public static const SELECTED_COLOR:uint = 0x0F00F0;
    public var text:String;
    public var itemWidth:Number;
    public var itemHeight:Number;
    public var padding:Number;
    public var parentItem:Object;
 
    private var _backgroundColor:uint;
    private var _image:Sprite;
    private var _textField:TextField;
    private var _style:StyleSheet;
    private var _resizeHeight:uint;
 
    public function PickleButton(options:Object) {
      this.itemWidth = options.width || 200;
      this.padding = options.padding || 5;
 
      this._backgroundColor = PickleButton.NORMAL_COLOR;
      this._image = options.image || new Sprite();
      this._textField = this.createTextField();
      this._textField.htmlText = '<p>' + (options.text || 'No text specified') + '</p>';
 
      this.resizeComponents();
      this.drawBackground();
      addChild(this._textField);
      addChild(this._image);
      this.addEvents();
    }
 
    public function drawBackground(color:uint = 0xFF00FF):void {
      color = this._backgroundColor;
      this.graphics.clear();
      this.graphics.beginFill(color, 0.4);
      this.graphics.drawRoundRect(0, 0, this.itemWidth, this.itemHeight, 15, 15);
    }
 
    private function addEvents():void {
      this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
      this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
      this.addEventListener(MouseEvent.CLICK, onClick);
    }
 
    private function onMouseOver(e:MouseEvent):void {
      this._backgroundColor = PickleButton.SELECTED_COLOR;
      this.drawBackground();
    }
 
    private function onMouseOut(e:MouseEvent):void {
      trace("And we're out");
      this._backgroundColor = PickleButton.NORMAL_COLOR;
      this.drawBackground();
    }
 
    private function onClick(e:MouseEvent):void {
      if ( this.itemHeight == 50 ) {
        resizeTo();
      } else {
        resizeTo(50);
      }
    }
 
    private function createTextField():TextField {
      var text:TextField = new TextField();
      
      text.y = this.padding;
      text.styleSheet = this.createStyleSheet();
      text.multiline = true;
      text.wordWrap = true;
      text.selectable = false;
 
      text.mouseEnabled = false;
      return text;
    }
 
    private function redraw():void {
      this.drawBackground();
    }
 
    private function resizeTo(height:uint = 0):void {
      var item:PickleButton = this;
      height ||= this.getBaseHeight();
      this._resizeHeight = height;
 
      if ( !this.hasEventListener(Event.ENTER_FRAME) ) {
        this.addEventListener(Event.ENTER_FRAME, this.resizer);
        trace("Added listener");
      }
    }
 
    private function resizer(e:Event):void {
      if ( this.itemHeight != this._resizeHeight ) {
        this.itemHeight += ( (itemHeight < this._resizeHeight)? 2 : -2 );
        this.drawBackground();
        (this.parentItem as ProjectList).redraw();
      } else {
        this.removeEventListener(Event.ENTER_FRAME, this.resizer);      
        trace("Removed listener");
      }
    }
 
    private function resizeComponents():void {
      this.itemHeight = this.getBaseHeight();
      this._textField.x = this._image.width + this.padding;
      this._textField.width = this.itemWidth - this._image.width - this.padding;
      this._textField.height = this.itemHeight - this.padding * 1;
 
      this._image.x = this.padding;
      this._image.y = this.padding;
    }
 
    private function getBaseHeight() {
      return this._image.height + this.padding * 2;
    }
 
    private function createStyleSheet():StyleSheet {
      var style:StyleSheet = new StyleSheet();
 
      style.parseCSS('p { font-family: "Trebuchet MS"; font-size: 11px; color: #000000; background-color: #FF00FF; }');
 
      return style;
    }
 
  }
}