slight refactoring of controller states
authorRichard Fairhurst <richard@systemed.net>
Sun, 2 May 2010 12:19:51 +0000 (12:19 +0000)
committerRichard Fairhurst <richard@systemed.net>
Sun, 2 May 2010 12:19:51 +0000 (12:19 +0000)
net/systemeD/potlatch2/controller/ControllerState.as
net/systemeD/potlatch2/controller/DragPOINode.as
net/systemeD/potlatch2/controller/DragWay.as
net/systemeD/potlatch2/controller/DragWayNode.as
net/systemeD/potlatch2/controller/DrawWay.as
net/systemeD/potlatch2/controller/NoSelection.as
net/systemeD/potlatch2/controller/SelectedPOINode.as
net/systemeD/potlatch2/controller/SelectedWay.as
net/systemeD/potlatch2/controller/SelectedWayNode.as

index 8642377..8d586ec 100644 (file)
@@ -12,6 +12,9 @@ package net.systemeD.potlatch2.controller {
         protected var controller:EditController;
         protected var previousState:ControllerState;
 
+        protected var selectedNode:Node;
+        public var selectedWay:Way;
+
         public function ControllerState() {}
  
         public function setController(controller:EditController):void {
@@ -42,6 +45,50 @@ package net.systemeD.potlatch2.controller {
                        return "(No state)";
                }
                
+               protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
+                       var paint:MapPaint = getMapPaint(DisplayObject(event.target));
+            var focus:Entity = getTopLevelFocusEntity(entity);
+
+                       if ( event.type == MouseEvent.MOUSE_DOWN ) {
+                               if ( entity is Way && event.altKey && paint.isBackground ) {
+                                       // pull way out of vector background layer
+                                       return new SelectedWay(paint.findSource().pullThrough(entity,controller.connection));
+                               } else if ( entity is Way ) {
+                                       // click way
+                                       return new DragWay(focus as Way, event);
+                               } else if ( focus is Node ) {
+                                       // select POI node
+                                       return new DragPOINode(entity as Node,event,false);
+//                             } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
+//                                     // select node within this way
+//                  return new DragWayNode(selectedWay, Node(entity), event, false);
+                               } else if ( entity is Node && focus is Way ) {
+                                       // select way node
+                                       return new DragWayNode(focus as Way,entity as Node,event,false);
+                               }
+                       } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING ) {
+                               return new NoSelection();
+                       } else if ( event.type == MouseEvent.ROLL_OVER ) {
+                               controller.map.setHighlight(focus, { hover: true });
+                       } else if ( event.type == MouseEvent.MOUSE_OUT ) {
+                               controller.map.setHighlight(focus, { hover: false });
+                       }
+                       return null;
+               }
+
+               public static function getTopLevelFocusEntity(entity:Entity):Entity {
+                       if ( entity is Node ) {
+                               for each (var parent:Entity in entity.parentWays) {
+                                       return parent;
+                               }
+                               return entity;
+                       } else if ( entity is Way ) {
+                               return entity;
+                       } else {
+                               return null;
+                       }
+               }
+
                protected function getMapPaint(d:DisplayObject):MapPaint {
                        while (d) {
                                if (d is MapPaint) { return MapPaint(d); }
@@ -49,6 +96,7 @@ package net.systemeD.potlatch2.controller {
                        }
                        return null;
                }
+               
 
     }
 }
index ad32f63..59e693e 100644 (file)
@@ -5,7 +5,6 @@ package net.systemeD.potlatch2.controller {
        import net.systemeD.halcyon.Globals;
 
     public class DragPOINode extends ControllerState {
-        private var selectedNode:Node;
         private var isDraggingStarted:Boolean = false;
                private var isNew:Boolean = false;
 
index e51e5bf..bae7d52 100644 (file)
@@ -5,7 +5,6 @@ package net.systemeD.potlatch2.controller {
        import net.systemeD.halcyon.Globals;
 
     public class DragWay extends ControllerState {
-        private var selectedWay:Way;
         private var isDraggingStarted:Boolean = false;
                private var enterTime:Number;
 
index f237c30..8c14fc0 100644 (file)
@@ -5,7 +5,6 @@ package net.systemeD.potlatch2.controller {
        import net.systemeD.halcyon.Globals;
 
     public class DragWayNode extends ControllerState {
-        private var selectedWay:Way;
         private var draggingNode:Node;
         private var isDraggingStarted:Boolean = false;
                private var isNew:Boolean = false;
index b034673..5517952 100644 (file)
@@ -27,7 +27,7 @@ package net.systemeD.potlatch2.controller {
                override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
                        var mouse:Point;
                        var node:Node;
-                       var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
+                       var focus:Entity = getTopLevelFocusEntity(entity);
 
                        if ( event.type == MouseEvent.MOUSE_UP ) {
                                if ( entity == null ) {
index e4300f2..83bdb05 100644 (file)
@@ -16,18 +16,7 @@ package net.systemeD.potlatch2.controller {
                override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
                        var focus:Entity = getTopLevelFocusEntity(entity);
 
-                       if ( event.type == MouseEvent.MOUSE_DOWN ) {
-                               var paint:MapPaint = getMapPaint(DisplayObject(event.target));
-                               if ( entity is Way && event.altKey && paint.isBackground ) {
-                                       return new SelectedWay(paint.findSource().pullThrough(entity,controller.connection));
-                               } else if ( entity is Way ) {
-                                       return new DragWay(focus as Way,event);
-                } else if ( focus is Node ) {
-                                       return new DragPOINode(entity as Node,event,false);
-                } else if ( entity is Node && focus is Way ) {
-                                       return new DragWayNode(focus as Way,entity as Node,event,false);
-                               }
-                       } else if (event.type==MouseEvent.MOUSE_UP && focus==null && map.dragstate!=map.DRAGGING) {
+                       if (event.type==MouseEvent.MOUSE_UP && focus==null && map.dragstate!=map.DRAGGING) {
                                map.dragstate=map.NOT_DRAGGING;
                                var undo:CompositeUndoableAction = new CompositeUndoableAction("Begin way");
                                var startNode:Node = controller.connection.createNode(
@@ -37,28 +26,12 @@ package net.systemeD.potlatch2.controller {
                                var way:Way = controller.connection.createWay({}, [startNode], undo.push);
                                MainUndoStack.getGlobalStack().addAction(undo);
                                return new DrawWay(way, true, false);
-                       } else if ( event.type == MouseEvent.ROLL_OVER ) {
-                               controller.map.setHighlight(focus, { hover: true });
-                       } else if ( event.type == MouseEvent.MOUSE_OUT ) {
-                               controller.map.setHighlight(focus, { hover: false });
-                       } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
                        }
+                       var cs:ControllerState = sharedMouseEvents(event, entity);
+                       if (cs) return cs;
                        return this;
                }
                
-               public static function getTopLevelFocusEntity(entity:Entity):Entity {
-                       if ( entity is Node ) {
-                               for each (var parent:Entity in entity.parentWays) {
-                                       return parent;
-                               }
-                               return entity;
-                       } else if ( entity is Way ) {
-                               return entity;
-                       } else {
-                               return null;
-                       }
-               }
-
         override public function enterState():void {
                        Globals.vars.root.addDebug("**** -> "+this);
         }
index 571e4fd..8472563 100644 (file)
@@ -6,7 +6,6 @@ package net.systemeD.potlatch2.controller {
        import net.systemeD.halcyon.Globals;
 
     public class SelectedPOINode extends ControllerState {
-        protected var selectedNode:Node;
         protected var initNode:Node;
         
         public function SelectedPOINode(node:Node) {
@@ -33,24 +32,9 @@ package net.systemeD.potlatch2.controller {
         }
         
         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
-                       if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
-            var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
-
-            if ( event.type == MouseEvent.MOUSE_UP ) {
-                               if ( entity is Way ) {
-                    return new SelectedWay(entity as Way);
-                               // ** do we need 'entity is Node && focus is Way' for POIs in ways?
-                } else if ( focus == null && map.dragstate!=map.DRAGGING ) {
-                    return new NoSelection();
-                               }
-            } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
-                if ( focus is Node ) {
-                                       return new DragPOINode(entity as Node,event,false);
-                } else if ( entity is Node && focus is Way ) {
-                                       return new DragWayNode(focus as Way,entity as Node,event,false);
-                               }
-            }
-
+                       if (event.type==MouseEvent.MOUSE_MOVE) { return this; }
+                       var cs:ControllerState = sharedMouseEvents(event, entity);
+                       if (cs) return cs;
             return this;
         }
 
index 3e4ff8e..f0b2c70 100644 (file)
@@ -9,7 +9,6 @@ package net.systemeD.potlatch2.controller {
        import net.systemeD.halcyon.Globals;
 
     public class SelectedWay extends ControllerState {
-        public var selectedWay:Way;
         protected var initWay:Way;
         
         public function SelectedWay(way:Way) {
@@ -37,46 +36,25 @@ package net.systemeD.potlatch2.controller {
         
         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
                        if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
-            var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
+            var focus:Entity = getTopLevelFocusEntity(entity);
 
-            if ( event.type == MouseEvent.MOUSE_UP ) {
-                               if ( entity is Node && event.shiftKey ) {
-                                       // start new way
-                    var way:Way = controller.connection.createWay({}, [entity],
-                        MainUndoStack.getGlobalStack().addAction);
-                    return new DrawWay(way, true, false);
-                               } else if ( entity is Way && event.ctrlKey ) {
-                                       // merge way
-                                       mergeWith(entity as Way);
-                } else if ( focus == null && map.dragstate!=map.DRAGGING ) {
-                    return new NoSelection();
-                               }
-
-            } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
-                               var paint:MapPaint = getMapPaint(DisplayObject(event.target));
-                               if ( entity is Way && event.altKey && paint.isBackground ) {
-                                       // pull way out of vector background layer
-                                       return new SelectedWay(paint.findSource().pullThrough(entity,controller.connection));
-                               } else if ( entity is Way && focus==selectedWay && event.shiftKey) {
-                                       // insert node within way (shift-click)
-                    var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
-                                       d.forceDragStart();
-                                       return d;
-                               } else if ( entity is Way ) {
-                                       return new DragWay(focus as Way, event);
-                               } else if ( entity is Node && entity.hasParent(selectedWay) ) {
-                                       // select node within this way
-                    return new DragWayNode(selectedWay, Node(entity), event, false);
-                } else if ( focus is Node ) {
-                                       // select POI node
-                                       return new DragPOINode(entity as Node,event,false);
-                } else if ( entity is Node && focus is Way ) {
-                                       // select way node
-                                       return new DragWayNode(focus as Way,entity as Node,event,false);
-                               }
-            }
-
-            return this;
+            if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
+                               // start new way
+                               var way:Way = controller.connection.createWay({}, [entity], MainUndoStack.getGlobalStack().addAction);
+                               return new DrawWay(way, true, false);
+                       } else if ( event.type == MouseEvent.MOUSE_UP && entity is Way && event.ctrlKey ) {
+                               // merge way
+                               mergeWith(entity as Way);
+                               return this;
+                       } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==selectedWay && event.shiftKey) {
+                               // insert node within way (shift-click)
+                var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
+                               d.forceDragStart();
+                               return d;
+                       }
+                       var cs:ControllerState = sharedMouseEvents(event, entity);
+                       if (cs) return cs;
+                       return this;
         }
         
                override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
index 13f4c57..872f12d 100644 (file)
@@ -6,7 +6,6 @@ package net.systemeD.potlatch2.controller {
        import net.systemeD.halcyon.Globals;
 
     public class SelectedWayNode extends SelectedWay {
-        protected var selectedNode:Node;
         protected var initNode:Node;
         
         public function SelectedWayNode(way:Way,node:Node) {
@@ -36,37 +35,25 @@ package net.systemeD.potlatch2.controller {
         
         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
                        if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
-            var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
+            var focus:Entity = getTopLevelFocusEntity(entity);
 
-            if ( event.type == MouseEvent.MOUSE_UP ) {
-                               if ( entity is Node && event.shiftKey ) {
-                                       // start new way
-                    var way:Way = controller.connection.createWay({}, [entity],
-                        MainUndoStack.getGlobalStack().addAction);
-                    return new DrawWay(way, true, false);
-                               } else if ( entity is Node && focus == selectedWay ) {
-                                       // select node within way
-                                       return selectOrEdit(selectedWay, Node(entity));
-                } else if ( entity is Way ) {
-                                       // select way
-                                       return new SelectedWay(Way(entity));
-                } else if ( focus == null && map.dragstate!=map.DRAGGING ) {
-                    return new NoSelection();
-                               }
-            } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
-                               if ( entity is Way && focus==selectedWay && event.shiftKey) {
-                                       // insert node within way (shift-click)
-                       var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
-                                       d.forceDragStart();
-                                       return d;
-                               } else if ( entity is Node && entity.hasParent(selectedWay) ) {
-                    return new DragWayNode(selectedWay, Node(entity), event, false);
-                               } else if ( focus is Node ) {
-                                       return new DragPOINode(entity as Node,event,false);
-                               }
-            }
-
-            return this;
+            if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
+                               // start new way
+                var way:Way = controller.connection.createWay({}, [entity],
+                    MainUndoStack.getGlobalStack().addAction);
+                return new DrawWay(way, true, false);
+                       } else if ( event.type == MouseEvent.MOUSE_UP && entity is Node && focus == selectedWay ) {
+                               // select node within way
+                               return selectOrEdit(selectedWay, Node(entity));
+            } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==selectedWay && event.shiftKey) {
+                               // insert node within way (shift-click)
+                       var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
+                               d.forceDragStart();
+                               return d;
+                       }
+                       var cs:ControllerState = sharedMouseEvents(event, entity);
+                       if (cs) return cs;
+                       return this;
         }
 
                override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {