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.halcyon.AttentionEvent;
8 import net.systemeD.potlatch2.collections.Imagery;
9 import net.systemeD.potlatch2.EditController;
10 import net.systemeD.potlatch2.FunctionKeyManager;
11 import net.systemeD.potlatch2.history.HistoryDialog;
12 import net.systemeD.potlatch2.save.SaveManager;
13 import net.systemeD.potlatch2.utils.SnapshotConnection;
14 import net.systemeD.halcyon.AttentionEvent;
15 import flash.ui.Keyboard;
16 import flash.geom.Point;
17 import mx.controls.Alert;
18 import mx.events.CloseEvent;
19 import mx.core.FlexGlobals;
20 import mx.core.IChildList;
21 import mx.core.UIComponent;
23 /** Represents a particular state of the controller, such as "dragging a way" or "nothing selected". Key methods are
24 * processKeyboardEvent and processMouseEvent which take some action, and return a new state for the controller.
26 * This abstract class has some behaviour that applies in most states, and lots of 'null' behaviour.
28 public class ControllerState {
30 protected var controller:EditController;
31 public var layer:MapPaint;
33 protected var _selection:Array=[];
35 public function ControllerState() {}
37 public function setController(controller:EditController):void {
38 this.controller=controller;
39 if (!layer) layer=controller.map.editableLayer;
42 public function isSelectionState():Boolean {
46 /** When triggered by a mouse action such as a click, perform an action on the given entity, then move to a new state. */
47 public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
51 /** When triggered by a keypress, perform an action on the given entity, then move to a new state. */
52 public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
56 /** Retrieves the map associated with the current EditController */
57 public function get map():Map {
58 return controller.map;
61 /** This is called when the EditController sets this ControllerState as the active state.
62 * Override this with whatever is needed, such as adding highlights to entities
64 public function enterState():void {}
66 /** This is called by the EditController as the current controllerstate is exiting.
67 * Override this with whatever cleanup is needed, such as removing highlights from entities
69 public function exitState(newState:ControllerState):void {}
71 /** Mark that the state should process a shift-click on entry (because ZoomArea rejected it). */
72 public function handleShiftClickOnEntry(event:MouseEvent):void {}
74 /** Represent the state in text for debugging. */
75 public function toString():String {
79 /** Return contextual help string for this state. */
80 public function contextualHelpId():String {
84 /** Default behaviour for the current state that should be called if state-specific action has been taken care of or ruled out. */
85 protected function sharedKeyboardEvents(event:KeyboardEvent):ControllerState {
86 if (popupsAreOpen()) { return null; }
87 var editableLayer:MapPaint=controller.map.editableLayer; // shorthand for this method
88 if (event.keyCode>=112 && event.keyCode<=126 && event.shiftKey) { memoriseTags(event.keyCode); return null; }
89 switch (event.keyCode) {
90 case 48: removeTags(); break; // 0 - remove all tags
91 case 66: setSourceTag(); break; // B - set source tag for current object
92 case 67: editableLayer.connection.closeChangeset(); break; // C - close changeset
93 case 68: editableLayer.alpha=1.3-editableLayer.alpha; return null; // D - dim
94 case 69: return new Measurement(this); // E - measurement
95 case 71: FlexGlobals.topLevelApplication.trackLoader.load(); break; // G - GPS tracks **FIXME: move from Application to Map
96 case 72: showHistory(); break; // H - History
97 case 83: SaveManager.saveChanges(editableLayer.connection); break; // S - save
98 case 84: controller.tagViewer.togglePanel(); return null; // T - toggle tags panel
99 case 90: if (!event.shiftKey) { MainUndoStack.getGlobalStack().undo(); return null;}// Z - undo
100 else { MainUndoStack.getGlobalStack().redo(); return null; } // Shift-Z - redo
101 case Keyboard.ESCAPE: revertSelection(); break; // ESC - revert to server version
102 case Keyboard.NUMPAD_ADD: // + - add tag
103 case 187: controller.tagViewer.selectAdvancedPanel(); // |
104 controller.tagViewer.addNewTag(); return null; // |
109 protected function popupsAreOpen():Boolean {
110 var rawChildren:IChildList = FlexGlobals.topLevelApplication.systemManager.rawChildren;
111 for (var i: int = 0; i < rawChildren.numChildren; i++) {
112 var currRawChild:DisplayObject = rawChildren.getChildAt(i);
113 if (!(currRawChild is UIComponent)) { continue; }
114 if (!UIComponent(currRawChild).visible) { continue; }
115 if (!UIComponent(currRawChild).enabled) { continue; }
116 if (UIComponent(currRawChild).isPopUp) {
117 if (!('allowControllerKeyboardEvents' in currRawChild)) { return true; }
123 /** Default behaviour for the current state that should be called if state-specific action has been taken care of or ruled out. */
124 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
125 var paint:MapPaint = getMapPaint(DisplayObject(event.target));
126 var focus:Entity = getTopLevelFocusEntity(entity);
128 if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
129 map.mouseUpHandler(); // in case the end-drag is over an EntityUI
130 } else if ( event.type == MouseEvent.ROLL_OVER && paint && paint.interactive ) {
131 paint.setHighlight(focus, { hover: true });
132 } else if ( event.type == MouseEvent.MOUSE_OUT && paint && paint.interactive ) {
133 paint.setHighlight(focus, { hover: false });
134 } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
135 if (event.delta > 0) { map.zoomIn(); }
136 else if (event.delta < 0) { map.zoomOut(); }
139 if ( paint && paint.isBackground ) {
140 if (event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
141 // alt-click to pull data out of vector background layer
142 // extend the current selection (alt-ctrl) or create a new one (alt)?
143 var newSelection:Array=(event.altKey && event.ctrlKey) ? _selection : [];
144 // create a list of the alt-clicked item, plus anything else already selected (assuming it's in the same layer!)
145 var itemsToPullThrough:Array=[]
146 if (_selection.length && firstSelected.connection==entity.connection) itemsToPullThrough=_selection.slice();
147 if (itemsToPullThrough.indexOf(entity)==-1) itemsToPullThrough.push(entity);
148 // make sure they're unhighlighted, and pull them through
149 for each (var entity:Entity in itemsToPullThrough) {
150 paint.setHighlight(entity, { hover:false, selected: false });
151 if (entity is Way) paint.setHighlightOnNodes(Way(entity), { selectedway: false });
152 newSelection.push(paint.pullThrough(entity,controller.map.editableLayer));
154 return controller.findStateForSelection(newSelection);
155 } else if (!paint.interactive) {
157 } else if (event.type == MouseEvent.MOUSE_DOWN && paint.interactive) {
158 if (entity is Way ) { return new SelectedWay(entity as Way, paint); }
159 else if (entity is Node ) { if (!entity.hasParentWays) return new SelectedPOINode(entity as Node, paint); }
160 else if (entity is Marker) { return new SelectedMarker(entity as Marker, paint); }
161 } else if ( event.type == MouseEvent.MOUSE_UP && !event.ctrlKey) {
162 return (this is NoSelection) ? null : new NoSelection();
163 } else if ( event.type == MouseEvent.CLICK && focus == null && map.dragstate!=map.DRAGGING && !event.ctrlKey) {
164 return (this is NoSelection) ? null : new NoSelection();
167 } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
168 if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
169 // select node within this way
170 return new DragWayNode(selectedWay, getNodeIndex(selectedWay,entity as Node), event, false);
171 } else if ( controller.spaceHeld ) {
172 // drag the background imagery to compensate for poor alignment
173 return new DragBackground(event, this);
174 } else if (entity && selection.indexOf(entity)>-1) {
175 return new DragSelection(selection, event);
177 return controller.findStateForSelection([entity]);
178 } else if (!entity && event.shiftKey) {
179 return new ZoomArea(event.localX,event.localY,this);
180 } else if (event.ctrlKey && !layer.isBackground) {
181 return new SelectArea(event.localX,event.localY,selection);
184 } else if ( event.type==MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING && !event.ctrlKey) {
185 return (this is NoSelection) ? null : new NoSelection();
188 if (entity && event.type==MouseEvent.MOUSE_MOVE && map.dragstate!=map.DRAGGING) {
189 // send mouse-move events up to the map, for co-ordinate display and floating map listeners
190 var mapLoc:Point = controller.map.globalToLocal(new Point(event.stageX, event.stageY));
191 controller.map.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_MOVE, true, false, mapLoc.x, mapLoc.y));
196 /** Gets the way that the selected node is part of, if that makes sense. If not, return the node, or the way, or nothing. */
197 public static function getTopLevelFocusEntity(entity:Entity):Entity {
198 if ( entity is Node ) {
199 for each (var parent:Entity in entity.parentWays) {
203 } else if ( entity is Way ) {
210 /** Find the MapPaint object that this DisplayObject belongs to. */
211 protected function getMapPaint(d:DisplayObject):MapPaint {
213 if (d is MapPaint) { return MapPaint(d); }
219 protected function getNodeIndex(way:Way,node:Node):uint {
220 for (var i:uint=0; i<way.length; i++) {
221 if (way.getNode(i)==node) { return i; }
226 /** Create a "repeat tags" action on the current entity, if possible. */
227 protected function repeatTags(object:Entity):void {
228 if (!controller.clipboards[object.getType()]) { return; }
231 var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
232 for (var k:String in controller.clipboards[object.getType()]) {
233 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
235 MainUndoStack.getGlobalStack().addAction(undo);
236 controller.updateSelectionUI();
240 /** Create a "repeat relations" action on the current entity, if possible. */
241 protected function repeatRelations(object:Entity):void {
242 if (!controller.relationClipboards[object.getType()]) { return; }
245 var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat relations");
246 var relationsadded:uint;
247 for each (var rr:Object in controller.relationClipboards[object.getType()]) {
248 if (rr.relation.findEntityMemberIndex(object)==-1) {
249 rr.relation.appendMember(new RelationMember(object, rr.role), undo.push);
253 MainUndoStack.getGlobalStack().addAction(undo);
254 controller.updateSelectionUI();
256 if (relationsadded > 0) {
257 var msg:String=relationsadded.toString() + " relation(s) added to " + object.getType() + ".";
258 controller.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, msg));
262 /** Copy list of relations from current object, for future repeatRelation() call. */
263 protected function copyRelations(object: Entity):void {
264 // Leave existing relations alone if it doesn't have any
265 if (object.parentRelations.length == 0) return;
266 controller.relationClipboards[object.getType()]=[];
267 for each (var rm:Object in object.getRelationMemberships() ) {
268 var rr:Object = { relation: rm.relation, role: rm.role };
269 controller.relationClipboards[object.getType()].push(rr);
273 /** Remove all tags from current selection. */
274 protected function removeTags():void {
275 if (selectCount==0) return;
276 var undo:CompositeUndoableAction = new CompositeUndoableAction("Remove tags");
277 for each (var item:Entity in _selection) {
279 var tags:Array=item.getTagArray();
280 for each (var tag:Tag in tags) item.setTag(tag.key,null,undo.push);
282 MainUndoStack.getGlobalStack().addAction(undo);
283 controller.updateSelectionUI();
284 for each (item in _selection) item.resume();
288 Should ideally do relations too. */
289 protected function memoriseTags(keycode:uint):void {
290 if (selectCount!=1) return;
291 var str:String=firstSelected.getTagList().toString();
292 FunctionKeyManager.instance().setKey(keycode-111,'Saved tags',str);
293 controller.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Tags memorised on F"+(keycode-111)));
296 /** Recall memorised tags. */
297 public function recallTags(str:String):void {
298 if (selectCount==0) return;
299 var kv:Object=TagList.fromString(str);
300 var undo:CompositeUndoableAction = new CompositeUndoableAction("Recall tags");
301 for each (var item:Entity in _selection) {
303 for (var k:String in kv) item.setTag(k, kv[k], undo.push)
305 MainUndoStack.getGlobalStack().addAction(undo);
306 controller.updateSelectionUI();
307 for each (item in _selection) item.resume();
310 /** Recall memorised relation by ID. */
311 public function recallRelation(str:String):void {
312 if (selectCount==0) return;
313 var rel:Relation = controller.map.editableLayer.connection.getRelation(Number(str));
315 controller.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "That relation hasn't been loaded"));
318 var undo:CompositeUndoableAction = new CompositeUndoableAction("Recall relation");
319 for each (var item:Entity in _selection) {
321 rel.appendMember(new RelationMember(item, ''), undo.push);
323 MainUndoStack.getGlobalStack().addAction(undo);
324 controller.updateSelectionUI();
325 for each (item in _selection) item.resume();
326 controller.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Added to relation"));
329 /** Show the history dialog, if only one object is selected. */
330 protected function showHistory():void {
331 if (selectCount == 1 && firstSelected.id>0) {
332 new HistoryDialog().init(firstSelected);
333 } else if (selectCount == 1) {
334 controller.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Can't show history of an unsaved object"));
335 } else if (selectCount == 0) {
336 controller.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Can't show history, nothing selected"));
338 controller.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Can't show history, multiple objects selected"));
342 /** Create an action to add "source=*" tag to current entity based on background imagery. This is a convenient shorthand for users. */
343 protected function setSourceTag():void {
344 if (selectCount!=1) { return; }
345 var bg:Object=controller.map.tileset.selected;
347 var sourceTag:String = bg.sourcetag || bg.id || bg.name;
348 if (sourceTag=='None') { return; }
349 if ("sourcekey" in Imagery.instance().selected) {
350 firstSelected.setTag(bg.sourcekey, sourceTag, MainUndoStack.getGlobalStack().addAction);
352 firstSelected.setTag('source', sourceTag, MainUndoStack.getGlobalStack().addAction);
354 controller.updateSelectionUI();
357 /** Revert all selected items to previously saved state, via a dialog box. */
358 protected function revertSelection():void {
359 var revertable:Boolean=false;
360 for each (var item:Entity in _selection)
361 if (item.id>0) revertable=true;
363 Alert.show("Revert selected items to the last saved version, discarding your changes?","Are you sure?",Alert.YES | Alert.CANCEL,null,revertHandler,null,Alert.CANCEL);
365 protected function revertHandler(event:CloseEvent):void {
366 if (event.detail==Alert.CANCEL) return;
367 for each (var item:Entity in _selection) {
368 if (item.id>0) item.connection.loadEntity(item);
374 public function get selectCount():uint {
375 return _selection.length;
378 public function get selection():Array {
382 public function get firstSelected():Entity {
383 if (_selection.length==0) { return null; }
384 return _selection[0];
387 public function get selectedWay():Way {
388 if (firstSelected is Way) { return firstSelected as Way; }
392 public function get selectedWays():Array {
393 var selectedWays:Array=[];
394 for each (var item:Entity in _selection) {
395 if (item is Way) { selectedWays.push(item); }
400 public function get selectedNodes():Array {
401 var selectedNodes:Array=[];
402 for each (var item:Entity in _selection) {
403 if (item is Node) { selectedNodes.push(item); }
405 return selectedNodes;
408 public function hasSelectedWays():Boolean {
409 for each (var item:Entity in _selection) {
410 if (item is Way) { return true; }
415 public function hasSelectedAreas():Boolean {
416 for each (var item:Entity in _selection) {
417 if (item is Way && Way(item).isArea()) { return true; }
422 public function hasSelectedUnclosedWays():Boolean {
423 for each (var item:Entity in _selection) {
424 if (item is Way && !Way(item).isArea()) { return true; }
429 /** Determine whether or not any nodes are selected, and if so whether any of them belong to areas. */
430 public function hasSelectedWayNodesInAreas():Boolean {
431 for each (var item:Entity in _selection) {
433 var parentWays:Array = Node(item).parentWays;
434 for each (var way:Entity in parentWays) {
435 if (Way(way).isArea()) { return true; }
442 public function hasAdjoiningWays():Boolean {
443 if (_selection.length<2) { return false; }
444 var endNodes:Object={};
445 for each (var item:Entity in _selection) {
446 if (item is Way && !Way(item).isArea() && Way(item).length>0) {
447 var startNode:int=Way(item).getNode(0).id;
448 var finishNode:int=Way(item).getLastNode().id;
449 if (endNodes[startNode ]) return true;
450 if (endNodes[finishNode]) return true;
451 endNodes[startNode ]=true;
452 endNodes[finishNode]=true;
458 /** Identify the inners and outer from the current selection for making a multipolygon. */
460 public function multipolygonMembers():Object {
461 if (_selection.length<2) { return {}; }
464 var relation:Relation;
468 // If there's an existing outer in the selection, use that
469 for each (entity in selection) {
470 if (!(entity is Way)) return {};
471 var r:Array=entity.findParentRelationsOfType('multipolygon','outer');
472 if (r.length) { outer=Way(entity); relation=r[0]; }
475 // Otherwise, find the way with the biggest area
476 var largest:Number=0;
478 for each (entity in selection) {
479 if (!(entity is Way)) return {};
480 if (!Way(entity).isArea()) return {};
481 var props:Object=layer.wayUIProperties(entity as Way);
482 if (props.patharea>largest) { outer=Way(entity); largest=props.patharea; }
485 if (!outer) return {};
487 // Identify the inners
488 for each (entity in selection) {
489 if (entity==outer) continue;
490 if (!(entity is Way)) return {};
491 if (!Way(entity).isArea()) return {};
492 var node:Node=Way(entity).getFirstNode();
493 if (outer.pointWithin(node.lon,node.lat)) inners.push(entity);
495 if (inners.length==0) return {};
497 return { outer: outer,
505 public function set selection(items:Array):void {
509 public function addToSelection(items:Array):void {
510 for each (var item:Entity in items) {
511 if (_selection.indexOf(item)==-1) { _selection.push(item); }
515 public function removeFromSelection(items:Array):void {
516 for each (var item:Entity in items) {
517 if (_selection.indexOf(item)>-1) {
518 _selection.splice(_selection.indexOf(item),1);
523 public function toggleSelection(item:Entity):Boolean {
524 if (_selection.indexOf(item)==-1) {
525 _selection.push(item); return true;
527 _selection.splice(_selection.indexOf(item),1); return false;