~extremepopcorn/dhlib/dhlib_ep

« back to all changes in this revision

Viewing changes to lib/ui/CheckBox.mhx

  • Committer: edA-qa mort-ora-y
  • Date: 2010-02-16 05:36:32 UTC
  • Revision ID: eda-qa@disemia.com-20100216053632-60lt7fndfi3fgblw
first

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* <license>
 
2
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
 
3
 * For full copyright and license information please refer to doc/license.txt.
 
4
 * </license> 
 
5
 */
 
6
package ui;
 
7
 
 
8
import flash.display.Sprite;
 
9
import flash.events.MouseEvent;
 
10
 
 
11
import flash.filters.BitmapFilter;
 
12
import flash.filters.BevelFilter;
 
13
 
 
14
class CheckBox extends Sprite, implements Widget, implements Selectable
 
15
{
 
16
        var checked : Bool;     //state of checkbox
 
17
        var down : Bool;        //stage of mouse
 
18
        var tf : StaticText;
 
19
                
 
20
        var filtersUp : Array<BitmapFilter>;
 
21
        var filtersDown : Array<BitmapFilter>;
 
22
        
 
23
        var bevelSize : Float;
 
24
        var box : Sprite;
 
25
        
 
26
        var color : draw.Color;
 
27
        var circle : Bool;
 
28
        
 
29
        private function new( text : String, ?color : draw.Color )
 
30
        {
 
31
                super();
 
32
                MixinWidget();
 
33
                
 
34
                down = false;
 
35
                circle = false;
 
36
                checked = false;
 
37
                if( color == null )
 
38
                        color = draw.Color.int(0x8080A0);
 
39
                this.color = color;
 
40
                
 
41
                bevelSize = 2;
 
42
                filtersUp = new Array();
 
43
                filtersUp.push( new BevelFilter( bevelSize, 45) );
 
44
                filtersDown = new Array();
 
45
                var bf = new BevelFilter( bevelSize, 180 + 45);
 
46
                //bf.highlightAlpha = 0;        //Unlike a button we need to keep this obrder otherwise it is hard to see
 
47
                filtersDown.push( bf );
 
48
                
 
49
                tf = StaticText.singleLine( text, StaticTextAlign.Left );
 
50
                addChild( tf );
 
51
                
 
52
                //use a separate box for the box to avoid applying filter to text
 
53
                box = new Sprite();
 
54
                addChild( box );
 
55
                
 
56
                addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown,false, 0, true /*weak*/ );
 
57
                addEventListener( MouseEvent.MOUSE_UP, onMouseUp,false, 0, true /*weak*/ );
 
58
                addEventListener( MouseEvent.MOUSE_OVER, onMouseOver,false, 0, true /*weak*/ );
 
59
                addEventListener( MouseEvent.MOUSE_OUT, onMouseOut,false, 0, true /*weak*/ );
 
60
        }
 
61
        
 
62
        /**
 
63
         * Constructs a standard check box.
 
64
         */
 
65
        static public function plain( text : String, ?color : draw.Color )
 
66
        {
 
67
                return new CheckBox( text, color );
 
68
        }
 
69
        
 
70
        function onMouseDown( evt : MouseEvent )
 
71
        {
 
72
                down = true;
 
73
                update();
 
74
        }
 
75
        
 
76
        function onMouseUp( evt : MouseEvent )
 
77
        {
 
78
                down = false;
 
79
                setSelected( !checked );
 
80
        }
 
81
        
 
82
        function onMouseOver( evt : MouseEvent )
 
83
        {
 
84
                down = evt.buttonDown;
 
85
                update();
 
86
        }
 
87
        
 
88
        function onMouseOut( evt : MouseEvent )
 
89
        {
 
90
                down = false;
 
91
                update();
 
92
        }
 
93
        
 
94
        function _resize( w : Float, h : Float )
 
95
        {
 
96
                var ds : Bool = down != checked;
 
97
                
 
98
                tf.x = h + bevelSize;
 
99
                tf.y = 0;
 
100
                tf.resize( w - tf.x, h );
 
101
                
 
102
                box.graphics.clear();
 
103
                box.graphics.beginFill( ds ? color.darken().asInt() : color.asInt() );
 
104
                if( circle )
 
105
                        box.graphics.drawEllipse( h/5,h/5,3*h/5,3*h/5 );
 
106
                else
 
107
                        box.graphics.drawRect( h/5,h/5,3*h/5,3*h/5 );
 
108
                box.graphics.endFill();
 
109
                
 
110
                box.filters = if( ds ) filtersDown else filtersUp;
 
111
        }
 
112
        
 
113
        public function getSelected() : Bool
 
114
        {
 
115
                return checked;
 
116
        }
 
117
        
 
118
        public function setSelected( sel : Bool )
 
119
        {
 
120
                if( sel != checked )
 
121
                {
 
122
                        checked = sel;
 
123
                        update();
 
124
                        if( onSelectChange != null )
 
125
                                Action.handle( this, onSelectChange );
 
126
                }
 
127
        }
 
128
        
 
129
        var onSelectChange : Dynamic;
 
130
        public function setOnSelectChange( action : Dynamic )
 
131
        {
 
132
                onSelectChange = action;
 
133
        }
 
134
 
 
135
        public function getPrefWidth() : SizeType
 
136
        {
 
137
                return SizeType.Sum( 
 
138
                        tf.getPrefHeight(),     //box is as wide as is high
 
139
                        SizeType.Sum( 
 
140
                                SizeType.Fixed( bevelSize ), 
 
141
                                tf.getPrefWidth() 
 
142
                                )
 
143
                        );
 
144
        }
 
145
        
 
146
        public function getPrefHeight() : SizeType
 
147
        {
 
148
                return tf.getPrefHeight();
 
149
        }
 
150
 
 
151
        define(`NoSizing',`')
 
152
        include(`MixinWidget.ihx')
 
153
}
 
 
b'\\ No newline at end of file'