remove errors that seem to have been introduced in recent days
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedMultiple.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         
4         import net.systemeD.halcyon.AttentionEvent;
5         import net.systemeD.halcyon.Globals;
6         import net.systemeD.halcyon.connection.*;
7         import net.systemeD.halcyon.connection.actions.MergeWaysAction;
8     
9
10         public class SelectedMultiple extends ControllerState {
11                 protected var initSelection:Array;
12                 
13                 public function SelectedMultiple(sel:Array) {
14                         initSelection=sel.concat();
15                 }
16
17                 override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
18                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
19                         var focus:Entity = getTopLevelFocusEntity(entity);
20
21                         if ( event.type == MouseEvent.MOUSE_DOWN && entity && event.ctrlKey ) {
22                                 // modify selection
23                                 controller.map.setHighlight(entity, { selected: toggleSelection(entity) });
24                                 controller.updateSelectionUI();
25
26                                 if (selectCount>1) { return this; }
27                                 return controller.findStateForSelection(selection);
28                         }
29                         var cs:ControllerState = sharedMouseEvents(event, entity);
30                         return cs ? cs : this;
31                 }
32
33                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
34                         if (event.keyCode==74) return mergeWays();      // 'J'
35                         var cs:ControllerState = sharedKeyboardEvents(event);
36                         return cs ? cs : this;
37                 }
38                 
39                 public function mergeWays():ControllerState {
40                         var changed:Boolean;
41                         var waylist:Array=selectedWays;
42                         var conflictTags:Object={}; 
43                         var mergers:uint=0;
44                         do {
45                                 // ** FIXME - we should have one CompositeUndoableAction for the whole caboodle,
46                                 // but that screws up the execution order and can make the merge not work
47                                 var undo:CompositeUndoableAction = new CompositeUndoableAction("Merge ways");
48                                 changed=tryMerge(waylist, undo);
49                                 if (changed)
50                                     mergers ++;
51                 MainUndoStack.getGlobalStack().addAction(undo);
52                 
53                 if (MergeWaysAction.lastProblemTags) {
54                         for each (var t:String in MergeWaysAction.lastProblemTags) {
55                                 conflictTags[t]=t;
56                         }
57                 }
58                                         
59                         } while (changed==true);
60
61             if (mergers>0) {                                    
62                             var msg:String = 1 + mergers + " ways merged."
63                 var conflictTags2:Array = new Array();
64                 // there must be a better way of avoiding duplicates...
65                 for each (var conflict:String in conflictTags) conflictTags2.push(conflict);
66                 if (conflictTags2.length>0)
67                     msg += " *Warning* The following tags conflicted and need attention: " + conflictTags2;
68                 map.connection.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, msg));
69             }
70
71                         return controller.findStateForSelection(waylist);
72                 }
73                 
74                 private function tryMerge(waylist:Array, undo:CompositeUndoableAction):Boolean {
75                         var way1:Way, way2:Way, del:uint;
76                         for (var i:uint=0; i<waylist.length; i++) {
77                                 for (var j:uint=0; j<waylist.length; j++) {
78                                         if (waylist[i]!=waylist[j]) {
79
80                                                 // Preserve positive IDs if we can
81                                                 if (waylist[i].id < waylist[j].id && waylist[i].id >= 0) {
82                                                         way1=waylist[i]; way2=waylist[j]; del=j;
83                                                 } else {
84                                                         way1=waylist[j]; way2=waylist[i]; del=i;
85                                                 }
86
87                                                 // Merge as appropriate
88                                                 if (way1.getNode(0)==way2.getNode(0)) {
89                                                         waylist.splice(del,1);
90                                                         undo.push(new MergeWaysAction(way1,way2,0,0));
91                                                         return true;
92                                                 } else if (way1.getNode(0)==way2.getLastNode()) { 
93                                                         waylist.splice(del,1);
94                                                         undo.push(new MergeWaysAction(way1,way2,0,way2.length-1));
95                                                         return true;
96                                                 } else if (way1.getLastNode()==way2.getNode(0)) {
97                                                         waylist.splice(del,1);
98                                                         undo.push(new MergeWaysAction(way1,way2,way1.length-1,0));
99                                                         return true;
100                                                 } else if (way1.getLastNode()==way2.getLastNode()) { 
101                                                         waylist.splice(del,1);
102                                                         undo.push(new MergeWaysAction(way1,way2,way1.length-1,way2.length-1));
103                                                         return true;
104                                                 }
105                                         }
106                                 }
107                         }
108                         return false;
109                 }
110
111                 override public function enterState():void {
112                         selection=initSelection.concat();
113                         for each (var entity:Entity in selection) {
114                                 controller.map.setHighlight(entity, { selected: true, hover: false });
115                         }
116                         controller.updateSelectionUI();
117                         controller.map.setPurgable(selection,false);
118                         Globals.vars.root.addDebug("**** -> "+this+" "+selection);
119                 }
120
121                 override public function exitState(newState:ControllerState):void {
122                         controller.map.setPurgable(selection,true);
123                         for each (var entity:Entity in selection) {
124                                 controller.map.setHighlight(entity, { selected: false, hover: false });
125                         }
126                         selection = [];
127                         if (!newState.isSelectionState()) { controller.updateSelectionUI(); }
128                         Globals.vars.root.addDebug("**** <- "+this);
129                 }
130
131                 override public function toString():String {
132                         return "SelectedMultiple";
133                 }
134
135         }
136 }