Made Gesture weak-referencing target

This commit is contained in:
Pavel fljot 2012-03-14 15:32:51 +02:00
parent e14bbd11bb
commit 82742a8465
2 changed files with 64 additions and 26 deletions

View file

@ -26,7 +26,7 @@ package org.gestouch.core
protected const _frameTickerShape:Shape = new Shape();
protected var _inputAdapters:Vector.<IInputAdapter> = new Vector.<IInputAdapter>();
protected var _stage:Stage;
protected var _gestures:Vector.<Gesture> = new Vector.<Gesture>();
protected var _gesturesMap:Dictionary = new Dictionary(true);
protected var _gesturesForTouchMap:Array = [];
protected var _gesturesForTargetMap:Dictionary = new Dictionary(true);
protected var _dirtyGestures:Vector.<Gesture> = new Vector.<Gesture>();
@ -157,19 +157,20 @@ package org.gestouch.core
{
throw new ArgumentError("Argument 'gesture' must be not null.");
}
if (_gestures.indexOf(gesture) > -1)
if (_gesturesMap[gesture])
{
throw new Error("This gesture is already registered.. something wrong.");
}
var targetGestures:Vector.<Gesture> = _gesturesForTargetMap[gesture.target] as Vector.<Gesture>;
var target:Object = gesture.target;
var targetGestures:Vector.<Gesture> = _gesturesForTargetMap[target] as Vector.<Gesture>;
if (!targetGestures)
{
targetGestures = _gesturesForTargetMap[gesture.target] = new Vector.<Gesture>();
}
targetGestures.push(gesture);
_gestures.push(gesture);
_gesturesMap[gesture] = true;
if (GesturesManager.initDefaultInputAdapter)
{
@ -195,19 +196,17 @@ package org.gestouch.core
var target:InteractiveObject = gesture.target;
var targetGestures:Vector.<Gesture> = _gesturesForTargetMap[target] as Vector.<Gesture>;
targetGestures.splice(targetGestures.indexOf(gesture), 1);
if (targetGestures.length == 0)
if (targetGestures.length > 1)
{
targetGestures.splice(targetGestures.indexOf(gesture), 1);
}
else
{
delete _gesturesForTargetMap[target];
target.removeEventListener(Event.ADDED_TO_STAGE, gestureTarget_addedToStageHandler);
}
var index:int = _gestures.indexOf(gesture);
if (index > -1)
{
_gestures.splice(index, 1);
}
delete _gesturesMap[gesture];
//TODO: decide about gesture state and _dirtyGestures
}
@ -226,24 +225,30 @@ package org.gestouch.core
gestouch_internal function onGestureRecognized(gesture:Gesture):void
{
for each (var otherGesture:Gesture in _gestures)
for (var key:Object in _gesturesMap)
{
var otherGesture:Gesture = key as Gesture;
var target:DisplayObject = gesture.target;
var otherTarget:DisplayObject = otherGesture.target;
// conditions for otherGesture "own properties"
if (otherGesture != gesture &&
target && otherTarget &&//in case GC worked half way through
otherGesture.enabled &&
otherGesture.state == GestureState.POSSIBLE)
{
// conditions for otherGesture target
if (otherGesture.target == gesture.target ||
(gesture.target is DisplayObjectContainer && (gesture.target as DisplayObjectContainer).contains(otherGesture.target)) ||
(otherGesture.target is DisplayObjectContainer && (otherGesture.target as DisplayObjectContainer).contains(gesture.target))
)
if (otherTarget == target ||
(target is DisplayObjectContainer && (target as DisplayObjectContainer).contains(otherTarget)) ||
(otherTarget is DisplayObjectContainer && (otherTarget as DisplayObjectContainer).contains(target)))
{
var gestureDelegate:IGestureDelegate = gesture.delegate;
var otherGestureDelegate:IGestureDelegate = otherGesture.delegate;
// conditions for gestures relations
if (gesture.canPreventGesture(otherGesture) &&
otherGesture.canBePreventedByGesture(gesture) &&
(!gesture.delegate || !gesture.delegate.gesturesShouldRecognizeSimultaneously(gesture, otherGesture)) &&
(!otherGesture.delegate || !otherGesture.delegate.gesturesShouldRecognizeSimultaneously(otherGesture, gesture)))
(!gestureDelegate || !gestureDelegate.gesturesShouldRecognizeSimultaneously(gesture, otherGesture)) &&
(!otherGestureDelegate || !otherGestureDelegate.gesturesShouldRecognizeSimultaneously(otherGesture, gesture)))
{
otherGesture.gestouch_internal::setState_internal(GestureState.FAILED);
}

View file

@ -37,8 +37,6 @@ package org.gestouch.gestures
public static const DEFAULT_SLOP:uint = Math.round(20 / 252 * flash.system.Capabilities.screenDPI);
public var delegate:IGestureDelegate;
protected const _touchesManager:ITouchesManager = TouchesManager.getInstance();
protected const _gesturesManager:IGesturesManager = GesturesManager.getInstance();
/**
@ -66,7 +64,7 @@ package org.gestouch.gestures
/** @private */
protected var _target:InteractiveObject;
private var _targetWeekStorage:Dictionary;
/**
* InteractiveObject (DisplayObject) which this gesture is tracking the actual gesture motion on.
@ -81,16 +79,28 @@ package org.gestouch.gestures
*/
public function get target():InteractiveObject
{
return _target;
for (var key:Object in _targetWeekStorage)
{
return key as InteractiveObject;
}
return null;
}
public function set target(value:InteractiveObject):void
{
if (_target == value)
var target:InteractiveObject = this.target;
if (target == value)
return;
uninstallTarget(target);
_target = value;
installTarget(target);
for (var key:Object in _targetWeekStorage)
{
delete _targetWeekStorage[key];
}
if (value)
{
(_targetWeekStorage ||= new Dictionary(true))[value] = true;
}
installTarget(value);
}
@ -119,6 +129,28 @@ package org.gestouch.gestures
}
private var _delegateWeekStorage:Dictionary;
public function get delegate():IGestureDelegate
{
for (var key:Object in _delegateWeekStorage)
{
return key as IGestureDelegate;
}
return null;
}
public function set delegate(value:IGestureDelegate):void
{
for (var key:Object in _delegateWeekStorage)
{
delete _delegateWeekStorage[key];
}
if (value)
{
(_delegateWeekStorage ||= new Dictionary(true))[value] = true;
}
}
protected var _state:uint = GestureState.IDLE;
public function get state():uint
{
@ -211,6 +243,7 @@ package org.gestouch.gestures
//TODO
reset();
target = null;
delegate = null;
_gesturesToFail = null;
}