My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* @author Nicholas Bilyk - April 2009
*/
package com.nbilyk.utils {
import com.nbilyk.events.KineticScrollManagerEvent;

import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.text.TextField;
import flash.utils.getTimer;

import spark.components.RichEditableText;

/**
* To use via mxml:
* <nbilyk:KineticScrollManager target="{targetViewPort}"/>
*
* To use via actionscript:
* create a non-temporary instance of KineticScrollManager, passing the UIComponent you wish to
* use kinetic scrolling on.
*
* Example:
* private var ksm:KineticScrollManager;
* ksm = new KineticScrollManager(component);
*/
[Event(name="tossStart", type="com.nbilyk.events.KineticScrollManagerEvent")]
[Event(name="tossEnd", type="com.nbilyk.events.KineticScrollManagerEvent")]
public class KineticScrollManager extends EventDispatcher {

private static const HISTORY_LENGTH:uint = 5; // The amount of mouse move events to keep track of

protected var velocity:Point = new Point();

private var _target:DisplayObject;

private var previousPoints:Vector.<Point>;
private var previousTimes:Vector.<int>;
private var _enabled:Boolean = true;

/**
* Dampening affects how quickly the toss velocity will slow to a stop.
* Make this number 0 < dampening < 1. Where 1 will go forever, and 0 will prevent any momentum.
*/
public var dampening:Number = .8;

public var horizontalScrollEnabled:Boolean = true;
public var verticalScrollEnabled:Boolean = true;

public function KineticScrollManager(targetVal:DisplayObject = null) {
target = targetVal;
}

/**
* The target DisplayObject for which to apply the kinetic scrolling.
* Must have the properties horizontalScrollPosition and verticalScrollPosition
*/
public function get target():DisplayObject {
return _target;
}

public function set target(value:DisplayObject):void {
if (_target) removeAllListeners();
_target = value;
if (value) {
target.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, false, 0, true);
target.addEventListener(MouseEvent.CLICK, mouseClickHandler, true, 100, true);
}
}

protected function mouseDownHandler(event:MouseEvent):void {
if (!enabled) return;
if (event.target is TextField && TextField(event.target).selectable) return;
if (event.target is RichEditableText && (RichEditableText(event.target).editable || RichEditableText(event.target).selectable)) return; // Contrib: Judah Frangipane
if (hasMouseEventListeners(DisplayObject(event.target))) return;

stop();
previousPoints = new <Point>[new Point(target.stage.mouseX, target.stage.mouseY)];
previousTimes = new <int>[getTimer()];
target.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler, false, 0, true);
target.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, 0, true);
}

private function hasMouseEventListeners(displayTarget:DisplayObject):Boolean {
if (displayTarget == target) return false;
if (displayTarget.hasEventListener(MouseEvent.MOUSE_DOWN) || displayTarget.hasEventListener(MouseEvent.MOUSE_UP)) return true;
if (displayTarget.parent) return hasMouseEventListeners(displayTarget.parent);
return false;
}

protected function mouseMoveHandler(event:MouseEvent):void {
if (!enabled) return;
if (!event.buttonDown) {
mouseUpHandler();
return;
}
var currPoint:Point = new Point(target.stage.mouseX, target.stage.mouseY);
var currTime:int = getTimer();
var previousPoint:Point = Point(previousPoints[previousPoints.length - 1]);
var diff:Point = currPoint.subtract(previousPoint);
diff = transformPointToLocal(diff);
moveScrollPosition(diff);

// Keep track of a set amount of positions and times so that on release, we can always look back a consistant amount.
previousPoints.push(currPoint);
previousTimes.push(currTime);
if (previousPoints.length >= HISTORY_LENGTH) {
previousPoints.shift();
previousTimes.shift();
}
}

protected function mouseUpHandler(event:MouseEvent = null):void {
target.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
target.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
if (!enabled) return;

var currPoint:Point = new Point(target.stage.mouseX, target.stage.mouseY);
var currTime:int = getTimer();
if (currTime - firstTime < 2) return;
var firstPoint:Point = previousPoints[0];
var firstTime:int = previousTimes[0];
var diff:Point = currPoint.subtract(firstPoint);
var time:Number = (currTime - firstTime) / (1000 / target.stage.frameRate);
velocity = new Point(diff.x / time, diff.y / time);
start();
}

protected function enterFrameHandler(event:Event):void {
velocity = new Point(velocity.x * dampening, velocity.y * dampening);
var localVelocity:Point = transformPointToLocal(velocity);
if (Math.abs(localVelocity.x) < .1) localVelocity.x = 0;
if (Math.abs(localVelocity.y) < .1) localVelocity.y = 0;
if (!localVelocity.x && !localVelocity.y) stop();
moveScrollPosition(localVelocity);
}

private function mouseClickHandler(event:MouseEvent):void {
if (velocity.length > 5) {
event.stopImmediatePropagation();
}
}

/**
* Starts the enter frame handler
*/
protected function start():void {
target.addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true);
dispatchEvent(new KineticScrollManagerEvent(KineticScrollManagerEvent.TOSS_START, velocity));
}

/**
* Starts the enter frame handler
*/
public function stop():void {
target.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
velocity = new Point();
dispatchEvent(new KineticScrollManagerEvent(KineticScrollManagerEvent.TOSS_END, velocity));
}

/**
* Sets the velocity of the scrolling.
*/
public function setVelocity(value:Point):void {
if (!value) value = new Point();
if (!target.stage) return;
target.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
target.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

velocity = value;
start();
}

/**
* Removes all listeners from the target and stage.
*/
protected function removeAllListeners():void {
target.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
target.removeEventListener(MouseEvent.CLICK, mouseClickHandler, true);
target.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
if (target.stage) {
target.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
target.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
}

/**
* If enabled is false, scrolling will stop and be disabled.
*/
public function get enabled():Boolean {
return _enabled;
}

public function set enabled(value:Boolean):void {
if (value == _enabled)
return; // no-op
_enabled = value;
if (!value) {
stop();
}
}

/**
* Moves scroller which moves viewport into position
* */
protected function moveScrollPosition(diff:Point):void {
if (horizontalScrollEnabled) {
Object(target).horizontalScrollPosition -= diff.x;
}
if (verticalScrollEnabled) {
Object(target).verticalScrollPosition -= diff.y;
}
}

private function transformPointToLocal(p:Point):Point {
var cM:Matrix = target.transform.concatenatedMatrix.clone();
cM.tx = 0;
cM.ty = 0;
cM.invert();
return cM.transformPoint(p);
}
}
}

Change log

r116 by nbilyk on Mar 4, 2011   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r113 by nbilyk on Feb 5, 2011   Diff
[No log message]
r111 by nbilyk on Feb 4, 2011   Diff
[No log message]
r98 by nbilyk on Nov 28, 2010   Diff
[No log message]
All revisions of this file

File info

Size: 7887 bytes, 226 lines
Powered by Google Project Hosting