25
25
// implemented here, using [Bindable]). Call updateFromValue()
26
26
// explicitly.
27
27
28
+ // To change the graphics, you can either draw into the normalShape,
29
+ // hoverShape, draggingShape shape objects, or you can make a subclass
30
+ // that overrides draw().
31
+
28
32
// Enhancements that might be nice:
29
- // 1. Separate sprites for normal, hover, and dragging states.
30
- // 2. Control over the filters and alpha used in the normal, hover, and dragging states
31
- // 3. Figure out whether [Bindable] would work with Model.reference and other models.
33
+ // 1. Figure out whether [Bindable] would work with Model.reference and other models.
32
34
33
35
package {
34
36
import flash.display.* ;
@@ -44,14 +46,16 @@ package {
44
46
// the value of 'dragging' will be that edge position, in global
45
47
// coordinates.
46
48
public var dragging: Point = null ;
47
-
49
+ public var hovering: Boolean = false ;
50
+
48
51
// The mapping to and from the underlying value ("model"). This
49
52
// can be changed at any time; call updateFromValue() to update
50
53
// the drag handle position.
51
54
public var model: Model;
52
55
53
- public var draggingFilters: Array =
54
- [new GlowFilter(0xccffcc), new DropShadowFilter()] ;
56
+ public var normalShape: Shape = new Shape ();
57
+ public var hoverShape: Shape = new Shape ();
58
+ public var draggingShape: Shape = new Shape ();
55
59
56
60
public function Draggable (model :Model ) {
57
61
this . model = model;
@@ -61,27 +65,51 @@ package {
61
65
addEventListener (MouseEvent . MOUSE_OVER , onMouseOver);
62
66
addEventListener (MouseEvent . MOUSE_OUT , onMouseOut);
63
67
64
- // TODO: better way to decide on alpha
65
- alpha = 0.5 ;
68
+ hoverShape. visible = false ;
69
+ draggingShape. visible = false ;
70
+
71
+ addChild (normalShape);
72
+ addChild (hoverShape);
73
+ addChild (draggingShape);
74
+
75
+ draw ();
76
+ }
66
77
67
- // TODO: what about other shapes? What about changing shapes
68
- // during dragging or mouseover?
69
- graphics . beginFill (0xccff66 );
70
- graphics . drawCircle (0 , 0 , 10 );
71
- graphics . endFill ();
78
+ // Draw the drag handle onto the three shape objects
79
+ public function draw ():void {
80
+ normalShape. alpha = 0.5 ;
81
+ normalShape. graphics . beginFill (0xddeeee );
82
+ normalShape. graphics . drawCircle (0 , 0 , 7 );
83
+ normalShape. graphics . endFill ();
84
+ normalShape. filters = [ new BevelFilter (2 )];
85
+
86
+ hoverShape. graphics . beginFill (0xffffcc );
87
+ hoverShape. graphics . drawCircle (0 , 0 , 8 );
88
+ hoverShape. graphics . endFill ();
89
+ hoverShape. filters = [ new BevelFilter (2 ), new GlowFilter (0xff0000 , 0.0 , 2 , 2 , 1 )];
90
+
91
+ draggingShape. graphics . beginFill (0x77ff77 );
92
+ draggingShape. graphics . drawCircle (0 , 0 , 7 );
93
+ draggingShape. graphics . endFill ();
94
+ draggingShape. filters = [ new BevelFilter (2 ), new DropShadowFilter ()];
72
95
}
73
96
97
+ // Set the visibility of the layers
98
+ public function setVisibility ():void {
99
+ normalShape. visible = ! dragging && ! hovering;
100
+ hoverShape. visible = hovering && ! dragging;
101
+ draggingShape. visible = !! dragging;
102
+ }
103
+
74
104
// Begin a drag operation
75
105
public function onMouseDown (e :MouseEvent ):void {
76
106
// Calculate the relative position between the center of the
77
107
// drag handle and the mouse down point, in global
78
108
// coordinates. We will preserve that during the drag operation.
79
109
var p: Point = parent . localToGlobal (new Point (x , y ));
80
110
dragging = localToGlobal (new Point (e. localX , e. localY )). subtract (p);
111
+ setVisibility();
81
112
82
- filters = draggingFilters;
83
- alpha = 1.0 ;
84
-
85
113
// While dragging we "capture" the mouse by tracking it on the stage
86
114
stage . addEventListener (MouseEvent . MOUSE_MOVE , onMouseDrag);
87
115
stage . addEventListener (MouseEvent . MOUSE_UP , onMouseUp);
@@ -90,8 +118,8 @@ package {
90
118
// End a drag operation (only active while dragging)
91
119
public function onMouseUp (e :MouseEvent ):void {
92
120
dragging = null ;
93
- filters = [] ;
94
- alpha = 0.5 ;
121
+ setVisibility() ;
122
+
95
123
stage . removeEventListener (MouseEvent . MOUSE_MOVE , onMouseDrag);
96
124
stage . removeEventListener (MouseEvent . MOUSE_UP , onMouseUp);
97
125
}
@@ -119,17 +147,13 @@ package {
119
147
120
148
// TODO: Generalize mouse hover effect
121
149
public function onMouseOver (e :MouseEvent ):void {
122
- if (! dragging) {
123
- alpha = 1.0 ;
124
- filters = [ new DropShadowFilter ()];
125
- }
150
+ hovering = true ;
151
+ setVisibility();
126
152
}
127
153
128
154
public function onMouseOut (e :MouseEvent ):void {
129
- if (! dragging) {
130
- alpha = 0.5 ;
131
- filters = [];
132
- }
155
+ hovering = false ;
156
+ setVisibility();
133
157
}
134
158
135
159
}
0 commit comments