add undo for creating stuff
[potlatch2.git] / net / systemeD / potlatch2 / controller / NoSelection.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import net.systemeD.potlatch2.EditController;
4         import net.systemeD.halcyon.connection.*;
5         import net.systemeD.halcyon.Map;
6         import net.systemeD.halcyon.Globals;
7
8         public class NoSelection extends ControllerState {
9
10                 public function NoSelection() {
11                 }
12  
13                 override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
14                         var focus:Entity = getTopLevelFocusEntity(entity);
15
16                         if ( event.type == MouseEvent.MOUSE_DOWN ) {
17                                 if ( entity is Way ) {
18                                         return new SelectedWay(focus as Way);
19                 } else if ( focus is Node ) {
20                                         return new DragPOINode(entity as Node,event,false);
21                 } else if ( entity is Node && focus is Way ) {
22                                         return new DragWayNode(focus as Way,entity as Node,event,false);
23                                 }
24                         } else if (event.type==MouseEvent.MOUSE_UP && focus==null && map.dragstate!=map.DRAGGING) {
25                                 map.dragstate=map.NOT_DRAGGING;
26                                 var undo:CompositeUndoableAction = new CompositeUndoableAction("Begin way");
27                                 var startNode:Node = controller.connection.createNode(
28                                         {}, 
29                                         controller.map.coord2lat(event.localY),
30                                         controller.map.coord2lon(event.localX), undo.push);
31                                 var way:Way = controller.connection.createWay({}, [startNode], undo.push);
32                                 MainUndoStack.getGlobalStack().addAction(undo);
33                                 return new DrawWay(way, true, false);
34                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
35                                 controller.map.setHighlight(focus, { hover: true });
36                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
37                                 controller.map.setHighlight(focus, { hover: false });
38                         } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
39                         }
40                         return this;
41                 }
42                 
43                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
44                         if ( entity is Node ) {
45                                 for each (var parent:Entity in entity.parentWays) {
46                                         return parent;
47                                 }
48                                 return entity;
49                         } else if ( entity is Way ) {
50                                 return entity;
51                         } else {
52                                 return null;
53                         }
54                 }
55
56         override public function enterState():void {
57                         Globals.vars.root.addDebug("**** -> "+this);
58         }
59         override public function exitState():void {
60                         Globals.vars.root.addDebug("**** <- "+this);
61         }
62                 override public function toString():String {
63                         return "NoSelection";
64                 }
65
66         }
67 }