Unify selection ControllerStates so they can work on either background or editable...
[potlatch2.git] / net / systemeD / potlatch2 / controller / NoSelection.as
index cdfcc1a916054aaaa79751ee1a203fa41ddf973d..5c5072d2242f0fee477dc45f6368d5a89f00e279 100644 (file)
@@ -1,50 +1,53 @@
 package net.systemeD.potlatch2.controller {
        import flash.events.*;
+       import flash.display.*;
        import net.systemeD.potlatch2.EditController;
        import net.systemeD.halcyon.connection.*;
+    import net.systemeD.halcyon.connection.actions.*;
        import net.systemeD.halcyon.Map;
-       import net.systemeD.halcyon.Globals;
+       import net.systemeD.halcyon.MapPaint;
 
        public class NoSelection extends ControllerState {
 
                public function NoSelection() {
                }
+
+               override public function isSelectionState():Boolean {
+                       return false;
+               }
+               
                override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
+                       var cs:ControllerState = sharedMouseEvents(event, entity);
+                       if (cs) return cs;
+
+                       var paint:MapPaint = getMapPaint(DisplayObject(event.target));
                        var focus:Entity = getTopLevelFocusEntity(entity);
 
-                       if ( event.type == MouseEvent.CLICK ) {
-                               if ( focus is Way ) {
-                                       return new SelectedWay(focus as Way);
-                               } else if ( focus is Node ) {
-                                       // *** select node
-                                       Globals.vars.root.addDebug("- selected POI");
-                               }
-                       } else if (event.type==MouseEvent.MOUSE_UP && focus==null && map.dragstate!=map.DRAGGING) {
+                       if (event.type==MouseEvent.MOUSE_UP && (focus==null || (paint && paint.isBackground)) && map.dragstate!=map.DRAGGING) {
                                map.dragstate=map.NOT_DRAGGING;
-                               return new CreateWay(event);
-                       } else if ( event.type == MouseEvent.MOUSE_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 undo:CompositeUndoableAction = new BeginWayAction();
+                               var conn:Connection = layer.connection;
+                               var startNode:Node = conn.createNode(
+                                       {}, 
+                                       controller.map.coord2lat(event.localY),
+                                       controller.map.coord2lon(event.localX), undo.push);
+                               var way:Way = conn.createWay({}, [startNode], undo.push);
+                               MainUndoStack.getGlobalStack().addAction(undo);
+                               return new DrawWay(way, true, false);
                        }
                        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 processKeyboardEvent(event:KeyboardEvent):ControllerState {
+                       var cs:ControllerState = sharedKeyboardEvents(event);
+                       return cs ? cs : this;
                }
-
+               
+        override public function enterState():void {
+                       controller.map.mouseUpHandler();
+        }
+        override public function exitState(newState:ControllerState):void {
+        }
                override public function toString():String {
                        return "NoSelection";
                }