space-drag for backgrounds, plus D to dim (au revoir caps lock)
authorRichard Fairhurst <richard@systemed.net>
Tue, 1 Jun 2010 16:13:56 +0000 (16:13 +0000)
committerRichard Fairhurst <richard@systemed.net>
Tue, 1 Jun 2010 16:13:56 +0000 (16:13 +0000)
net/systemeD/halcyon/Map.as
net/systemeD/halcyon/MapEvent.as
net/systemeD/halcyon/TileSet.as
net/systemeD/potlatch2/BackgroundSelector.mxml
net/systemeD/potlatch2/EditController.as
net/systemeD/potlatch2/Yahoo.as
net/systemeD/potlatch2/controller/ControllerState.as
net/systemeD/potlatch2/controller/DragBackground.as [new file with mode: 0644]

index 7de3dba..652a151 100755 (executable)
@@ -53,6 +53,7 @@ package net.systemeD.halcyon {
                public var mapheight:uint;                                              // height (Flash pixels)
 
                public var dragstate:uint=NOT_DRAGGING;                 // dragging map
+               private var _draggable:Boolean=true;                    //  |
                private var lastxmouse:Number;                                  //  |
                private var lastymouse:Number;                                  //  |
                private var downX:Number;                                               //  |
@@ -173,6 +174,10 @@ package net.systemeD.halcyon {
                        centre_lon=coord2lon(-x+mapwidth/2);
                        this.dispatchEvent(new MapEvent(MapEvent.MOVE, {lat:centre_lat, lon:centre_lon, scale:scale}));
                }
+               
+               public function nudgeBackground(x:Number,y:Number):void {
+                       this.dispatchEvent(new MapEvent(MapEvent.NUDGE_BACKGROUND, { x: x, y: y }));
+               }
 
                // Co-ordinate conversion functions
 
@@ -349,7 +354,13 @@ package net.systemeD.halcyon {
                // ------------------------------------------------------------------------------------------
                // Mouse events
                
+               public function set draggable(draggable:Boolean):void {
+                       _draggable=draggable;
+                       dragstate=NOT_DRAGGING;
+               }
+
                public function mouseDownHandler(event:MouseEvent):void {
+                       if (!_draggable) { return; }
                        dragstate=NOT_MOVED;
                        lastxmouse=downX=mouseX;
                        lastymouse=downY=mouseY;
@@ -365,9 +376,8 @@ package net.systemeD.halcyon {
                }
         
                public function mouseMoveHandler(event:MouseEvent):void {
-                       if (dragstate==NOT_DRAGGING) {
-                               return;
-                       }
+                       if (!_draggable) { return; }
+                       if (dragstate==NOT_DRAGGING) { return; }
                        
                        if (dragstate==NOT_MOVED && Math.abs(downX - mouseX) < 3 && Math.abs(downY - mouseY) < 3) {
                                return;
index a6dfb31..9492b2e 100644 (file)
@@ -8,6 +8,7 @@ package net.systemeD.halcyon {
                public static const RESIZE:String = "resize";
                public static const MOVE:String = "move";
                public static const CLICK:String = "click";
+               public static const NUDGE_BACKGROUND:String = "nudge_background";
 
                public var params:Object;
 
index 54eb3e5..59eaaed 100755 (executable)
@@ -4,6 +4,7 @@ package net.systemeD.halcyon {
        import flash.events.*;
        import flash.net.*;
        import flash.system.LoaderContext;
+       import net.systemeD.halcyon.MapEvent;
        
     public class TileSet extends Sprite {
 
@@ -12,8 +13,8 @@ package net.systemeD.halcyon {
                public var tile_b:int;
                public var tile_t:int;
 
-               public var xoffset:Number=0;
-               public var yoffset:Number=0;
+               private var offset_lon:Number=0;
+               private var offset_lat:Number=0;
 
                private var requests:Array=[];
                private var tiles:Object={};            // key is "z,x,y"; value "true" (needed) or reference to sprite
@@ -27,11 +28,13 @@ package net.systemeD.halcyon {
                        this.map=map;
                        alpha=0.5;
                        createSprites();
+                       map.addEventListener(MapEvent.NUDGE_BACKGROUND, nudgeHandler);
                }
        
                public function init(url:String=null, update:Boolean=false):void {
                        baseurl=url;
                        tiles={};
+                       offset_lon=offset_lat=x=y=0;
                        while (numChildren) { removeChildAt(0); }
                        createSprites();
                        if (update) { this.update(); }
@@ -51,17 +54,18 @@ package net.systemeD.halcyon {
                        for (var i:uint=map.MINSCALE; i<=map.MAXSCALE; i++) {
                                this.getChildAt(i-map.MINSCALE).visible=(scale==i);
                        }
-                       // ** Should also double or halve xoffset/yoffset accordingly
+                       x=map.lon2coord(map.centre_lon+offset_lon)-map.lon2coord(map.centre_lon);
+                       y=map.lat2coord(map.centre_lat+offset_lat)-map.lat2coord(map.centre_lat);
                }
                        
                // Update bounds - called on every move
                
                public function update():void {
                        if (!baseurl) { return; }
-                       tile_l=lon2tile(map.edge_l+xoffset);
-                       tile_r=lon2tile(map.edge_r+xoffset);
-                       tile_t=lat2tile(map.edge_t+yoffset);
-                       tile_b=lat2tile(map.edge_b+yoffset);
+                       tile_l=lon2tile(map.edge_l-offset_lon);
+                       tile_r=lon2tile(map.edge_r-offset_lon);
+                       tile_t=lat2tile(map.edge_t-offset_lat);
+                       tile_b=lat2tile(map.edge_b-offset_lat);
                        for (var tx:int=tile_l; tx<=tile_r; tx++) {
                                for (var ty:int=tile_t; ty<=tile_b; ty++) {
                                        if (!tiles[map.scale+','+tx+','+ty]) { addRequest(tx,ty); }
@@ -119,6 +123,16 @@ package net.systemeD.halcyon {
                }
 
 
+               // Update offset
+               
+               public function nudgeHandler(event:MapEvent):void {
+                       if (!baseurl) { return; }
+                       this.x+=event.params.x; this.y+=event.params.y;
+                       offset_lat=map.centre_lat-map.coord2lat(map.lat2coord(map.centre_lat)-this.y);
+                       offset_lon=map.centre_lon-map.coord2lon(map.lon2coord(map.centre_lon)-this.x);
+                       update();
+               }
+
                
                // ------------------------------------------------------------------
                // Co-ordinate conversion functions
index 1503f30..0a28e3b 100644 (file)
@@ -13,6 +13,7 @@
             <mx:Object label="OSM - Osmarender" data="http://tah.openstreetmap.org/Tiles/tile/$z/$x/$y.png" />
             <mx:Object label="OSM - Maplint (errors)" data="http://tah.openstreetmap.org/Tiles/maplint/$z/$x/$y.png" />
             <mx:Object label="OpenCycleMap" data="http://andy.sandbox.cloudmade.com/tiles/cycle/$z/$x/$y.png" />
+                       <mx:Object label="UK: OS StreetView" data="http://os.openstreetmap.org/sv/$z/$x/$y.png" />
             <mx:Object label="UK historic: 1:25k" data="http://ooc.openstreetmap.org/os1/$z/$x/$y.png" />
             <mx:Object label="UK historic: 7th" data="http://ooc.openstreetmap.org/os7/$z/$x/$y.png" />
             <mx:Object label="UK historic: NPE" data="http://ooc.openstreetmap.org/npe/$z/$x/$y.png" />
index ea819b6..3ac6bd6 100644 (file)
@@ -15,6 +15,7 @@ package net.systemeD.potlatch2 {
         public var state:ControllerState;
         private var _connection:Connection;
         
+               private var keys:Object={};
 
         public function EditController(map:Map, tagViewer:TagViewer, toolbox:Toolbox) {
             this._map = map;
@@ -27,6 +28,7 @@ package net.systemeD.potlatch2 {
             map.parent.addEventListener(MouseEvent.MOUSE_UP, mapMouseEvent);
             map.parent.addEventListener(MouseEvent.MOUSE_DOWN, mapMouseEvent);
             map.parent.addEventListener(MouseEvent.CLICK, mapMouseEvent);
+            map.parent.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
             map.parent.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
         }
 
@@ -48,12 +50,21 @@ package net.systemeD.potlatch2 {
                        toolbox.setEntity(entity);
         }
         
+        private function keyDownHandler(event:KeyboardEvent):void {
+                       keys[event.keyCode]=true;
+               }
+
         private function keyUpHandler(event:KeyboardEvent):void {
             trace("key code "+event.keyCode);
+                       if (keys[event.keyCode]) { delete keys[event.keyCode]; }
             var newState:ControllerState = state.processKeyboardEvent(event);
             setState(newState);            
                }
 
+               public function keyDown(key:Number):Boolean {
+                       return Boolean(keys[key]);
+               }
+
         private function mapMouseEvent(event:MouseEvent):void {
             map.stage.focus = map.parent;
             
index 0470528..942696d 100644 (file)
@@ -13,6 +13,12 @@ package net.systemeD.potlatch2 {
                private static const token:String="f0a.sejV34HnhgIbNSmVHmndXFpijgGeun0fSIMG9428hW_ifF3pYKwbV6r9iaXojl1lU_dakekR";
                private static const MAXZOOM:int=17;
 
+               private var _lat:Number;
+               private var _lon:Number;
+               private var _scale:Number;
+               private var offset_lat:Number=0;
+               private var offset_lon:Number=0;
+
                public function Yahoo(w:Number, h:Number, map:Map) {
                        super();
                        this.init(token, w, h);  
@@ -23,12 +29,12 @@ package net.systemeD.potlatch2 {
                
                public function show():void {
                        this.visible=true;
-                       this.zoomLevel = 18-map.scale;
-                       this.centerLatLon = new LatLon(map.centre_lat, map.centre_lon);
+                       moveto(map.centre_lat, map.centre_lon, map.scale);
 
                        this.addEventListener(YahooMapEvent.MAP_INITIALIZE, initHandler);
                        map.addEventListener(MapEvent.MOVE, moveHandler);
                        map.addEventListener(MapEvent.RESIZE, resizeHandler);
+                       map.addEventListener(MapEvent.NUDGE_BACKGROUND, nudgeHandler);
                }
 
                public function hide():void {
@@ -37,6 +43,7 @@ package net.systemeD.potlatch2 {
                        this.removeEventListener(YahooMapEvent.MAP_INITIALIZE, initHandler);
                        map.removeEventListener(MapEvent.MOVE, moveHandler);
                        map.removeEventListener(MapEvent.RESIZE, resizeHandler);
+                       map.removeEventListener(MapEvent.NUDGE_BACKGROUND, nudgeHandler);
                }
                
                private function initHandler(event:YahooMapEvent):void {
@@ -49,13 +56,23 @@ package net.systemeD.potlatch2 {
 
                private function moveto(lat:Number,lon:Number,scale:uint):void {
                        if (scale>MAXZOOM) { this.visible=false; return; }
+                       _lat=lat; _lon=lon; _scale=scale;
+                       
                        this.visible=true;
                        this.zoomLevel=18-scale;
-                       this.centerLatLon=new LatLon(latlon);
+                       this.centerLatLon=new LatLon(lat+offset_lat, lon+offset_lon);
                }
 
                private function resizeHandler(event:MapEvent):void {
                        this.setSize(event.params.width, event.params.height);
                }
+               
+               private function nudgeHandler(event:MapEvent):void {
+                       var cx:Number=map.lon2coord(map.centre_lon);
+                       var cy:Number=map.lat2coord(map.centre_lat);
+                       offset_lon+=map.coord2lon(cx-event.params.x)-map.centre_lon;
+                       offset_lat+=map.coord2lat(cy-event.params.y)-map.centre_lat;
+                       moveto(_lat,_lon,_scale);
+               }
        }
-}
\ No newline at end of file
+}
index 8db5a01..e0e4a1f 100644 (file)
@@ -47,7 +47,8 @@ package net.systemeD.potlatch2.controller {
                
                protected function sharedKeyboardEvents(event:KeyboardEvent):ControllerState {
                        switch (event.keyCode) {
-                               case 90:        MainUndoStack.getGlobalStack().undo(); return null;             // Z
+                               case 68:        controller.map.paint.alpha=1.3-controller.map.paint.alpha; return null; // D
+                               case 90:        MainUndoStack.getGlobalStack().undo(); return null;                                             // Z
                        }
                        return null;
                }
@@ -72,6 +73,9 @@ package net.systemeD.potlatch2.controller {
                                } else if ( entity is Node && focus is Way ) {
                                        // select way node
                                        return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
+                               } else if ( controller.keyDown(32) ) {
+                                       // drag map
+                                       return new DragBackground(event);
                                }
                        } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING ) {
                                return new NoSelection();
diff --git a/net/systemeD/potlatch2/controller/DragBackground.as b/net/systemeD/potlatch2/controller/DragBackground.as
new file mode 100644 (file)
index 0000000..222e40f
--- /dev/null
@@ -0,0 +1,49 @@
+package net.systemeD.potlatch2.controller {
+       import flash.events.*;
+    import net.systemeD.potlatch2.EditController;
+    import net.systemeD.halcyon.connection.*;
+    import net.systemeD.halcyon.MapEvent;
+       import net.systemeD.halcyon.Globals;
+
+    public class DragBackground extends ControllerState {
+
+        private var downX:Number;
+        private var downY:Number;
+        
+        public function DragBackground(event:MouseEvent) {
+                       trace ("starting DragBackground");
+            downX = event.localX;
+            downY = event.localY;
+        }
+       override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
+
+            if (event.type==MouseEvent.MOUSE_UP) {
+                       return previousState;
+
+                       } else if ( event.type == MouseEvent.MOUSE_MOVE) {
+                               // dragging
+                               controller.map.nudgeBackground(event.localX-downX, event.localY-downY);
+                   downX = event.localX;
+                   downY = event.localY;
+                               return this;
+
+                       } else {
+                               // event not handled
+                return this;
+                       }
+        }
+
+        override public function enterState():void {
+                       controller.map.draggable=false;
+                       Globals.vars.root.addDebug("**** -> "+this);
+        }
+        override public function exitState():void {
+                       controller.map.draggable=true;
+                       Globals.vars.root.addDebug("**** <- "+this);
+        }
+        override public function toString():String {
+            return "DragBackground";
+        }
+    }
+}