slight refactoring of controller states
[potlatch2.git] / net / systemeD / potlatch2 / controller / ControllerState.as
index 864237719872e1a181c3778aa13941232a170cc3..8d586ec59348b1129c0ebecca32168aec1ca641c 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;
                }
+               
 
     }
 }