slight refactoring of controller states
[potlatch2.git] / net / systemeD / potlatch2 / controller / ControllerState.as
index 7d6c5ca9dfbbe817cf93d1a081de0c6d45ebe52d..8d586ec59348b1129c0ebecca32168aec1ca641c 100644 (file)
@@ -1,14 +1,20 @@
 package net.systemeD.potlatch2.controller {
        import flash.events.*;
+       import flash.display.*;
     import net.systemeD.halcyon.Map;
+    import net.systemeD.halcyon.MapPaint;
     import net.systemeD.halcyon.connection.*;
     import net.systemeD.potlatch2.EditController;
+       import net.systemeD.halcyon.Globals;
 
     public class ControllerState {
 
         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 {
@@ -38,6 +44,59 @@ package net.systemeD.potlatch2.controller {
                public function toString():String {
                        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); }
+                               d=d.parent;
+                       }
+                       return null;
+               }
+               
 
     }
 }