/lib/com/modestmaps/extras/ZoomBox.as

https://github.com/pombredanne/Elastic-Lists
ActionScript | 106 lines | 86 code | 19 blank | 1 comment | 3 complexity | 1e7d0994d874b4dcf5d1125663d418f8 MD5 | raw file
  1. package com.modestmaps.extras
  2. {
  3. import com.modestmaps.Map;
  4. import com.modestmaps.core.MapExtent;
  5. import com.modestmaps.geo.Location;
  6. import flash.display.LineScaleMode;
  7. import flash.display.Shape;
  8. import flash.display.Sprite;
  9. import flash.events.Event;
  10. import flash.events.MouseEvent;
  11. import flash.geom.Point;
  12. import flash.geom.Rectangle;
  13. public class ZoomBox extends Sprite
  14. {
  15. protected var map:Map;
  16. protected var box:Shape;
  17. protected var p:Point;
  18. public function ZoomBox( map:Map,
  19. boxLineThickness:Number=0,
  20. boxLineColor:Number=0xff0000,
  21. boxFillColor:Number=0xffffff,
  22. boxFillAlpha:Number=0.2 )
  23. {
  24. this.map = map;
  25. box = new Shape();
  26. box.graphics.lineStyle(boxLineThickness, boxLineColor, 1, false, LineScaleMode.NONE);
  27. box.graphics.beginFill(boxFillColor, boxFillAlpha);
  28. box.graphics.drawRect(0,0,100,100);
  29. box.graphics.endFill();
  30. box.visible = false;
  31. addChild(box);
  32. addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  33. }
  34. protected function onAddedToStage(event:Event):void
  35. {
  36. removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  37. stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, true, -100);
  38. addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
  39. }
  40. protected function onRemovedFromStage(event:Event):void
  41. {
  42. removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
  43. stage.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, true);
  44. addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  45. }
  46. protected function onMouseDown(event:MouseEvent):void
  47. {
  48. if (event.shiftKey) {
  49. map.grid.mouseEnabled = false;
  50. p = new Point(stage.mouseX, stage.mouseY);
  51. p = map.globalToLocal(p);
  52. box.x = p.x;
  53. box.y = p.y;
  54. box.scaleX = box.scaleY = 0;
  55. stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  56. stage.addEventListener(Event.MOUSE_LEAVE, onMouseUp);
  57. stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  58. event.stopImmediatePropagation();
  59. }
  60. }
  61. protected function onMouseUp(event:Event):void
  62. {
  63. box.visible = false;
  64. if (Math.abs(box.scaleX) > 0 && Math.abs(box.scaleY) > 0) {
  65. var rect:Rectangle = box.getBounds(map);
  66. var nw:Location = map.pointLocation(rect.topLeft);
  67. var se:Location = map.pointLocation(rect.bottomRight);
  68. // TODO: what happens at the international date line?
  69. var extent:MapExtent = new MapExtent(nw.lat, se.lat, se.lon, nw.lon);
  70. map.setExtent(extent);
  71. }
  72. stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  73. stage.removeEventListener(Event.MOUSE_LEAVE, onMouseUp);
  74. stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  75. event.stopImmediatePropagation();
  76. map.grid.mouseEnabled = true;
  77. }
  78. protected function onMouseMove(event:MouseEvent):void
  79. {
  80. var mouseP:Point = map.globalToLocal(new Point(stage.mouseX, stage.mouseY));
  81. var movement:Point = p.subtract(mouseP);
  82. box.visible = true;
  83. box.scaleX = -movement.x / 100;
  84. box.scaleY = -movement.y / 100;
  85. }
  86. }
  87. }