package utils
{
import com.google.maps.MapEvent;
import com.google.maps.controls.ControlBase;
import com.google.maps.controls.ControlPosition;
import com.google.maps.interfaces.IMap;
import flash.events.Event;
import flash.geom.Point;
import flash.utils.getTimer;
/**
* A basic timer control that shows elapsed time between TILES_LOADED_PENDING
* and TILES_LOADED events.
*/
public class TileLoadTimer extends ControlBase
{
/**
* Radius of the timer dial.
*/
private static const RADIUS:Number = 25;
/**
* getTimer()'s value when TILES_LOADED_PENDING was first received, or 0
* if we are not timing.
*/
private var startTimer:int;
/**
* getTimer()'s value when we most recently were timing. 3 seconds after
* timing we reset the max elapsed time indicator back to 0.
*/
private var activityTimer:int;
/**
* Elapsed milliseconds since we started timing.
*/
private var elapsed:int;
/**
* Maximum recent value of elapsed milliseconds.
*/
private var elapsedMax:int;
/**
* Constructs a new TileLoadTimer.
* @constructor
*/
public function TileLoadTimer()
{
super(new ControlPosition(ControlPosition.ANCHOR_BOTTOM_RIGHT, 45, 45));
}
/**
* Sets the instance of the map that the control operates on.
* Normally invoked from the call to Map.addControl().
* @param map Map interface
*/
public override function initControlWithMap(map:IMap):void
{
super.initControlWithMap(map);
map.addEventListener(MapEvent.TILES_LOADED_PENDING, onTilesLoadedPending);
map.addEventListener(MapEvent.TILES_LOADED, onTilesLoaded);
map.addEventListener(Event.ENTER_FRAME, onEnterFrame);
startTimer = 0;
}
/**
* Called on each frame. Updates the timing variables and draws the dial.
* @param event Event that triggered this call.
*/
private function onEnterFrame(event:Event):void
{
var timerNow:int = getTimer();
if (startTimer) {
activityTimer = timerNow;
elapsed = timerNow - startTimer;
if (elapsed > elapsedMax) {
elapsedMax = elapsed;
}
} else {
if (timerNow - activityTimer > 3000) {
elapsedMax = 0;
}
}
graphics.clear();
graphics.beginFill(0x006633);
graphics.drawRect(-RADIUS - 2, -RADIUS - 2,
RADIUS * 2 + 4, RADIUS * 2 + 4);
graphics.endFill();
drawDialArc(elapsed, 0x00CC99);
drawDialLine(elapsedMax, 0x009900);
}
/**
* Draws a filled arc on the dial.
* @param angle Angle (in units of 0.001 radians).
* @param color Fill color.
*/
private function drawDialArc(angle:Number, color:uint):void
{
graphics.beginFill(color);
graphics.moveTo(0, 0);
for (var i:int = 0; i <= angle; i += 10) {
var p:Point = getPointOnCircle(i);
graphics.lineTo(p.x, p.y);
}
graphics.endFill();
}
/**
* Draws a line on the dial.
* @param angle Angle (in units of 0.001 radians).
* @param color Line color.
*/
private function drawDialLine(angle:Number, color:uint):void
{
var end:Point = getPointOnCircle(angle);
graphics.lineStyle(2, color);
graphics.moveTo(0, 0);
graphics.lineTo(end.x, end.y);
}
/**
* Returns the location of a point on the edge of the dial.
* @param angle Angle (in units of 0.001 radians).
* @return Point on the edge of the dial.
*/
private static function getPointOnCircle(angle:Number):Point
{
var radians:Number = angle / 1000.0;
return new Point(RADIUS * Math.sin(radians),
-RADIUS * Math.cos(radians));
}
/**
* Handles TILES_LOADED_PENDING. Starts the timer if it is not already
* running.
* @param event Event that triggered this call.
*/
private function onTilesLoadedPending(event:MapEvent):void
{
if (!startTimer) {
startTimer = getTimer();
}
}
/**
* Handles TILES_LOADED. Stops the timer and resets the elapsed time.
* @param event Event that triggered this call.
*/
private function onTilesLoaded(event:MapEvent):void
{
startTimer = 0;
elapsed = 0;
}
}
}
No comments:
Post a Comment