1 package net.systemeD.potlatch2.controller {
3 import flash.display.*;
4 import net.systemeD.halcyon.Map;
5 import net.systemeD.halcyon.MapPaint;
6 import net.systemeD.halcyon.connection.*;
7 import net.systemeD.potlatch2.collections.Imagery;
8 import net.systemeD.potlatch2.EditController;
9 import net.systemeD.halcyon.Globals;
10 import net.systemeD.potlatch2.save.SaveManager;
11 import flash.ui.Keyboard;
12 /** Represents a particular state of the controller, such as "dragging a way" or "nothing selected". Key methods are
13 * processKeyboardEvent and processMouseEvent which take some action, and return a new state for the controller.
15 * This abstract class has some behaviour that applies in most states, and lots of 'null' behaviour.
17 public class ControllerState {
19 protected var controller:EditController;
20 protected var previousState:ControllerState;
22 protected var _selection:Array=[];
24 public function ControllerState() {}
26 public function setController(controller:EditController):void {
27 this.controller = controller;
30 public function setPreviousState(previousState:ControllerState):void {
31 if ( this.previousState == null )
32 this.previousState = previousState;
35 public function isSelectionState():Boolean {
39 /** When triggered by a mouse action such as a click, perform an action on the given entity, then move to a new state. */
40 public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
44 /** When triggered by a keypress, perform an action on the given entity, then move to a new state. */
45 public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
49 public function get map():Map {
50 return controller.map;
53 public function enterState():void {}
54 public function exitState(newState:ControllerState):void {}
56 /** Represent the state in text for debugging. */
57 public function toString():String {
60 /** Default behaviour for the current state that should be called if state-specific action has been taken care of or ruled out. */
61 protected function sharedKeyboardEvents(event:KeyboardEvent):ControllerState {
62 switch (event.keyCode) {
63 case 66: setSourceTag(); break; // B - set source tag for current object
64 case 67: controller.connection.closeChangeset(); break; // C - close changeset
65 case 68: controller.map.paint.alpha=1.3-controller.map.paint.alpha; return null; // D - dim
66 case 83: SaveManager.saveChanges(); break; // S - save
67 case 84: controller.tagViewer.togglePanel(); return null; // T - toggle tags panel
68 case 90: MainUndoStack.getGlobalStack().undo(); return null; // Z - undo
69 case Keyboard.NUMPAD_ADD: // + - add tag
70 case 187: controller.tagViewer.selectAdvancedPanel(); // |
71 controller.tagViewer.addNewTag(); return null; // |
76 /** Default behaviour for the current state that should be called if state-specific action has been taken care of or ruled out. */
77 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
78 var paint:MapPaint = getMapPaint(DisplayObject(event.target));
79 var focus:Entity = getTopLevelFocusEntity(entity);
81 if ( paint && paint.isBackground ) {
82 if ( event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
83 // alt-click to pull data out of vector background layer
84 var newEntity:Entity=paint.findSource().pullThrough(entity,controller.connection);
85 if (entity is Way) { return new SelectedWay(newEntity as Way); }
86 else if (entity is Node) { return new SelectedPOINode(newEntity as Node); }
87 } else if (event.type == MouseEvent.MOUSE_DOWN && entity is Marker) {
88 return new SelectedMarker(entity as Marker, paint.findSource());
89 } else if ( event.type == MouseEvent.MOUSE_UP ) {
90 return (this is NoSelection) ? null : new NoSelection();
91 } else { return null; }
94 if ( event.type == MouseEvent.MOUSE_DOWN ) {
95 if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
96 // select node within this way
97 return new DragWayNode(selectedWay, getNodeIndex(selectedWay,entity as Node), event, false);
98 } else if ( entity is Node && focus is Way ) {
100 return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
101 } else if ( controller.keyDown(Keyboard.SPACE) ) {
102 // drag the background imagery to compensate for poor alignment
103 return new DragBackground(event);
104 } else if (entity && selection.indexOf(entity)>-1) {
105 return new DragSelection(selection, event);
107 return new DragSelection([entity], event);
109 } else if ( event.type == MouseEvent.CLICK && focus == null && map.dragstate!=map.DRAGGING && this is SelectedMarker) {
110 // this is identical to the below, but needed for unselecting markers on vector background layers.
111 // Deselecting a POI or way on the main layer emits both CLICK and MOUSE_UP, but markers only CLICK
112 // I'll leave it to someone who understands to decide whether they are the same thing and should be
113 // combined with a (CLICK || MOUSE_UP)
115 // "&& this is SelectedMarker" added by Steve Bennett. The CLICK event being processed for SelectedWay state
116 // causes way to get unselected...so restrict the double processing as much as possible.
118 return (this is NoSelection) ? null : new NoSelection();
119 } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING) {
120 return (this is NoSelection) ? null : new NoSelection();
121 } else if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
122 map.mouseUpHandler(); // in case the end-drag is over an EntityUI
123 } else if ( event.type == MouseEvent.ROLL_OVER ) {
124 controller.map.setHighlight(focus, { hover: true });
125 } else if ( event.type == MouseEvent.MOUSE_OUT ) {
126 controller.map.setHighlight(focus, { hover: false });
127 } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
128 if (event.delta > 0) {
130 } else if (event.delta < 0) {
137 /** Gets the way that the selected node is part of, if that makes sense. If not, return the node, or the way, or nothing. */
138 public static function getTopLevelFocusEntity(entity:Entity):Entity {
139 if ( entity is Node ) {
140 for each (var parent:Entity in entity.parentWays) {
144 } else if ( entity is Way ) {
151 /** Find the MapPaint object that this DisplayObject belongs to. */
152 protected function getMapPaint(d:DisplayObject):MapPaint {
154 if (d is MapPaint) { return MapPaint(d); }
160 protected function getNodeIndex(way:Way,node:Node):uint {
161 for (var i:uint=0; i<way.length; i++) {
162 if (way.getNode(i)==node) { return i; }
167 /** Create a "repeat tags" action on the current entity, if possible. */
168 protected function repeatTags(object:Entity):void {
169 if (!controller.clipboards[object.getType()]) { return; }
172 var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
173 for (var k:String in controller.clipboards[object.getType()]) {
174 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
176 MainUndoStack.getGlobalStack().addAction(undo);
177 controller.updateSelectionUI();
183 /** Create an action to add "source=*" tag to current entity based on background imagery. This is a convenient shorthand for users. */
184 protected function setSourceTag():void {
185 if (selectCount!=1) { return; }
186 if (Imagery.instance().selected && Imagery.instance().selected.sourcetag) {
187 firstSelected.setTag('source',Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
189 controller.updateSelectionUI();
194 public function get selectCount():uint {
195 return _selection.length;
198 public function get selection():Array {
202 public function get firstSelected():Entity {
203 if (_selection.length==0) { return null; }
204 return _selection[0];
207 public function get selectedWay():Way {
208 if (firstSelected is Way) { return firstSelected as Way; }
212 public function get selectedWays():Array {
213 var selectedWays:Array=[];
214 for each (var item:Entity in _selection) {
215 if (item is Way) { selectedWays.push(item); }
220 public function hasSelectedWays():Boolean {
221 for each (var item:Entity in _selection) {
222 if (item is Way) { return true; }
227 public function hasSelectedAreas():Boolean {
228 for each (var item:Entity in _selection) {
229 if (item is Way && Way(item).isArea()) { return true; }
234 public function hasSelectedUnclosedWays():Boolean {
235 for each (var item:Entity in _selection) {
236 if (item is Way && !Way(item).isArea()) { return true; }
241 public function hasAdjoiningWays():Boolean {
242 if (_selection.length<2) { return false; }
243 var endNodes:Object={};
244 for each (var item:Entity in _selection) {
245 if (item is Way && !Way(item).isArea()) {
246 if (endNodes[Way(item).getNode(0).id]) return true;
247 if (endNodes[Way(item).getLastNode().id]) return true;
248 endNodes[Way(item).getNode(0).id]=true;
249 endNodes[Way(item).getLastNode().id]=true;
257 public function set selection(items:Array):void {
261 public function addToSelection(items:Array):void {
262 for each (var item:Entity in items) {
263 if (_selection.indexOf(item)==-1) { _selection.push(item); }
267 public function removeFromSelection(items:Array):void {
268 for each (var item:Entity in items) {
269 if (_selection.indexOf(item)>-1) {
270 _selection.splice(_selection.indexOf(item),1);
275 public function toggleSelection(item:Entity):Boolean {
276 if (_selection.indexOf(item)==-1) {
277 _selection.push(item); return true;
279 _selection.splice(_selection.indexOf(item),1); return false;