Peter Hansen is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againplaybook-air / studies / ca / microcode / menu / TopSwipeMenu.as
- Branch
- default
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | //--------------------------------------------------------------
// PlayBook top-swipe menu
//
// Copyright 2011 Peter Hansen
// Made available under the MIT License (http://www.opensource.org/licenses/mit-license)
//
// Please give credit in your "About" dialog, documentation, or similar place.
// See http://peterhansen.ca/blog/playbook-top-swipe-menu.html for background.
package ca.microcode.menu {
import caurina.transitions.Tweener;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import qnx.events.QNXApplicationEvent;
import qnx.system.QNXApplication;
import qnx.ui.text.Label;
public class TopSwipeMenu extends Sprite {
// minimize distance to swipe before menu slides open by itself
public var swipe_region:int = 21;
// duration in seconds for full slide (e.g. when closing)
public var slide_time:Number = 0.3;
public function TopSwipeMenu(width:int, height:int, bgcolor:int=0xcccccc) {
var bg:Shape = new Shape();
bg.graphics.beginFill(bgcolor, 1);
bg.graphics.drawRect(0, 0, width, height);
addChild(bg);
this.y = -height;
QNXApplication.qnxApplication.addEventListener(QNXApplicationEvent.SWIPE_START, onEvent);
}
private var state:int;
private const ST_IDLE:int = 0;
private const ST_SWIPE_START:int = 1;
private const ST_FIRST_DOWN:int = 2;
private const ST_SWIPING:int = 3;
private const ST_OPENING:int = 4;
private const ST_OPEN:int = 5;
private const ST_CLOSING:int = 6;
private var swipe_start_pos:Point;
private function onEvent(event:Event):void {
var prev_state:int = state;
var p:Point = new Point(stage.mouseX, stage.mouseY);
trace(state, event.type, "@", p);
switch (state) {
case ST_IDLE:
if (event.type == QNXApplicationEvent.SWIPE_START)
state = ST_SWIPE_START;
break;
case ST_SWIPE_START:
if (event.type == MouseEvent.MOUSE_MOVE) {
swipe_start_pos = new Point(stage.mouseX, stage.mouseY);
state = ST_FIRST_DOWN;
}
// anything but mouseMove
else
state = ST_IDLE;
break;
case ST_FIRST_DOWN:
if (event.type == MouseEvent.MOUSE_DOWN) {
// check whether pos is same as it was for first move
var pos:Point = new Point(stage.mouseX, stage.mouseY);
if (pos.equals(swipe_start_pos))
state = ST_SWIPING;
else // not a top-swipe if positions don't match
state = ST_IDLE;
}
else if (event.type == QNXApplicationEvent.SWIPE_START)
state = ST_SWIPE_START;
else
state = ST_IDLE;
break;
case ST_SWIPING:
if (event.type == MouseEvent.MOUSE_UP) {
if (stage.mouseY > swipe_region)
state = ST_OPENING;
else
state = ST_CLOSING;
}
break;
case ST_OPENING:
if (event.type == Event.COMPLETE)
state = ST_OPEN;
else if (event.type == Event.CLOSE)
state = ST_CLOSING;
break;
case ST_OPEN:
if (event.type == Event.CLOSE)
state = ST_CLOSING;
else if (event.type == QNXApplicationEvent.SWIPE_START)
state = ST_SWIPE_START;
break;
case ST_CLOSING:
if (event.type == Event.COMPLETE)
state = ST_IDLE;
else if (event.type == QNXApplicationEvent.SWIPE_START)
state = ST_SWIPE_START;
break;
}
// handle state change
if (state != prev_state) {
trace(prev_state, "-->", state);
var duration:Number;
var slide_closed:Boolean;
// leaving state
switch (prev_state) {
case ST_FIRST_DOWN:
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onEvent);
stage.removeEventListener(MouseEvent.MOUSE_DOWN, onEvent);
break;
case ST_SWIPING:
stage.removeEventListener(MouseEvent.MOUSE_UP, onEvent);
stage.removeEventListener(Event.ENTER_FRAME, onSwiping);
break;
case ST_OPEN:
stage.removeEventListener(MouseEvent.MOUSE_DOWN, outsideClick, true);
slide_closed = true;
break;
}
// entering state
switch (state) {
case ST_SWIPE_START:
stage.addEventListener(MouseEvent.MOUSE_MOVE, onEvent);
break;
case ST_FIRST_DOWN:
stage.addEventListener(MouseEvent.MOUSE_DOWN, onEvent);
break;
case ST_SWIPING:
stage.addEventListener(MouseEvent.MOUSE_UP, onEvent);
stage.addEventListener(Event.ENTER_FRAME, onSwiping);
break;
case ST_OPENING:
stage.addEventListener(MouseEvent.MOUSE_DOWN, outsideClick, true);
duration = slide_time * -this.y / height;
Tweener.addTween(this, {y: 0, time: duration, transition: "linear",
onComplete: onEvent,
onCompleteParams: [new Event(Event.COMPLETE)]
});
break;
case ST_CLOSING:
stage.removeEventListener(MouseEvent.MOUSE_DOWN, outsideClick, true);
slide_closed = true;
break;
}
if (slide_closed) {
duration = slide_time * (height + this.y) / height;
Tweener.addTween(this, {y: -height, time: duration, transition: "linear",
onComplete: onEvent,
onCompleteParams: [new Event(Event.COMPLETE)]
});
}
}
}
private function onSwiping(e:Event):void {
var y:int = stage.mouseY;
if (y >= height)
y = height;
this.y = y - height;
}
private function outsideClick(e:MouseEvent):void {
if (stage.mouseY > this.y + this.height)
onEvent(new Event(Event.CLOSE));
}
public function close():void {
onEvent(new Event(Event.CLOSE));
}
}
}
|