Skip to content

Instantly share code, notes, and snippets.

@fljot fljot/gist:5befaff062292b6512c7 Secret

Created Sep 19, 2011
Embed
What would you like to do?
package com.inreflected.ui.managers
{
import com.inreflected.events.InactivityEvent;
import flash.display.InteractiveObject;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.utils.getTimer;
[Event(name="activated", type="com.inreflected.events.InactivityEvent")]
[Event(name="inactive", type="com.inreflected.events.InactivityEvent")]
/**
* @author Pavel fljot
*
* Inspired by
*
* org.casalib.time.Inactivity
* (http://svn.as3.casalib.org/releases/latest/code/org/casalib/time/Inactivity.as)
*
* com.reintroducing.mouse.MouseIdleMonitor
* (http://evolve.reintroducing.com/2008/07/11/as3/as3-mouseidlemonitor/)
*/
public class InactivityManager extends EventDispatcher
{
public static const DEFAULT_EVENT_TYPES:Array =
[
MouseEvent.MOUSE_MOVE,
MouseEvent.MOUSE_DOWN,
MouseEvent.MOUSE_UP,
MouseEvent.MOUSE_WHEEL,
KeyboardEvent.KEY_DOWN,
KeyboardEvent.KEY_UP
];
private var inactivityTimer:Timer;
private var lastActivityTime:uint;
private var eventTypes:Array;
public function InactivityManager(scope:InteractiveObject, delay:uint, eventTypes:Array = null)
{
super();
inactivityTimer = new Timer(delay, 1);
inactivityTimer.addEventListener(TimerEvent.TIMER_COMPLETE, userInactivityHandler, false, 0, true);
_scope = scope;
if (eventTypes)
{
this.eventTypes = eventTypes;
}
else
{
this.eventTypes = DEFAULT_EVENT_TYPES;
}
this.delay = delay;
}
/** @private */
private var _delay:uint;
/**
*
*/
public function get delay():uint
{
return _delay;
}
public function set delay(value:uint):void
{
if (_delay != value)
{
_delay = value;
inactivityTimer.delay = delay;
if (running)
{
userActivityHandler();
}
}
}
private var _scope:InteractiveObject;
public function get scope():InteractiveObject
{
return _scope;
}
private var _running:Boolean = false;
public function get running():Boolean
{
return _running;
}
public function get inactivityTime():uint
{
return running ? getTimer() - lastActivityTime : 0;
}
//--------------------------------------------------------------------------
//
// Public methods
//
//--------------------------------------------------------------------------
public function start():void
{
if (!running)
{
for each (var eventType:String in eventTypes)
{
scope.addEventListener(eventType, userActivityHandler, false, 0, true);
}
_running = true;
userActivityHandler();
}
}
public function stop():void
{
if (running)
{
for each (var eventType:String in eventTypes)
{
scope.removeEventListener(eventType, userActivityHandler);
}
_running = false;
inactivityTimer.reset();
inactivityTimer.stop();
}
}
public function restart():void
{
stop();
start();
}
//--------------------------------------------------------------------------
//
// Event handlers
//
//--------------------------------------------------------------------------
protected function userActivityHandler(event:Event = null):void
{
if (running)//to work correctly if stop was called while user-activity event processing
{
if (!inactivityTimer.running)
{
dispatchEvent(new InactivityEvent(InactivityEvent.ACTIVATED, false, false, inactivityTime));
}
lastActivityTime = getTimer();
inactivityTimer.reset();
inactivityTimer.start();
}
}
protected function userInactivityHandler(event:TimerEvent):void
{
dispatchEvent(new InactivityEvent(InactivityEvent.INACTIVE, false, false, inactivityTime));
}
}
}
package com.inreflected.events
{
import flash.events.Event;
/**
* @author Pavel fljot
*/
public class InactivityEvent extends Event
{
public static const ACTIVATED:String = "activated";
public static const INACTIVE:String = "inactive";
public function InactivityEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, inactivityTime:uint = 0)
{
super(type, bubbles, cancelable);
this._inactivityTime = inactivityTime;
}
private var _inactivityTime:uint;
public function get inactivityTime():uint
{
return _inactivityTime;
}
override public function clone():Event
{
return new InactivityEvent(type, bubbles, cancelable, inactivityTime);
}
override public function toString():String
{
return formatToString("InactivityEvent", "type", "bubbles", "cancelable", "eventPhase", "inactivityTime");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.