Skip to content

Commit 0507cb3

Browse files
committed
Use separate Shape objects for normal, hover, and dragging states.
1 parent b6ab6d0 commit 0507cb3

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

Draggable.as

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
// implemented here, using [Bindable]). Call updateFromValue()
2626
// explicitly.
2727

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+
2832
// 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.
3234

3335
package {
3436
import flash.display.*;
@@ -44,14 +46,16 @@ package {
4446
// the value of 'dragging' will be that edge position, in global
4547
// coordinates.
4648
public var dragging:Point = null;
47-
49+
public var hovering:Boolean = false;
50+
4851
// The mapping to and from the underlying value ("model"). This
4952
// can be changed at any time; call updateFromValue() to update
5053
// the drag handle position.
5154
public var model:Model;
5255

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();
5559

5660
public function Draggable(model:Model) {
5761
this.model = model;
@@ -61,27 +65,51 @@ package {
6165
addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
6266
addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
6367

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+
}
6677

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()];
7295
}
7396

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+
74104
// Begin a drag operation
75105
public function onMouseDown(e:MouseEvent):void {
76106
// Calculate the relative position between the center of the
77107
// drag handle and the mouse down point, in global
78108
// coordinates. We will preserve that during the drag operation.
79109
var p:Point = parent.localToGlobal(new Point(x, y));
80110
dragging = localToGlobal(new Point(e.localX, e.localY)).subtract(p);
111+
setVisibility();
81112

82-
filters = draggingFilters;
83-
alpha = 1.0;
84-
85113
// While dragging we "capture" the mouse by tracking it on the stage
86114
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseDrag);
87115
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
@@ -90,8 +118,8 @@ package {
90118
// End a drag operation (only active while dragging)
91119
public function onMouseUp(e:MouseEvent):void {
92120
dragging = null;
93-
filters = [];
94-
alpha = 0.5;
121+
setVisibility();
122+
95123
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseDrag);
96124
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
97125
}
@@ -119,17 +147,13 @@ package {
119147

120148
// TODO: Generalize mouse hover effect
121149
public function onMouseOver(e:MouseEvent):void {
122-
if (!dragging) {
123-
alpha = 1.0;
124-
filters = [new DropShadowFilter()];
125-
}
150+
hovering = true;
151+
setVisibility();
126152
}
127153

128154
public function onMouseOut(e:MouseEvent):void {
129-
if (!dragging) {
130-
alpha = 0.5;
131-
filters = [];
132-
}
155+
hovering = false;
156+
setVisibility();
133157
}
134158

135159
}

0 commit comments

Comments
 (0)