1 package net.systemeD.potlatch2.controller {
3 import flash.geom.Point;
4 import flash.ui.Keyboard;
5 import net.systemeD.potlatch2.EditController;
6 import net.systemeD.halcyon.connection.*;
7 import net.systemeD.halcyon.connection.actions.*;
9 /** The state of moving a selection around with the mouse. */
10 public class DragSelection extends ControllerState {
11 private var isDraggingStarted:Boolean = false;
12 private var enterTime:Number;
14 private var downX:Number;
15 private var downY:Number;
16 private var dragstate:uint=NOT_MOVED;
18 private const NOT_DRAGGING:uint=0;
20 /** "Dragging" but hasn't actually moved yet. */
21 private const NOT_MOVED:uint=1;
24 private const DRAGGING:uint=2;
26 /** Start the drag by recording the dragged way, where it started, and when. */
27 public function DragSelection(sel:Array, event:MouseEvent) {
28 selection = sel.concat();
31 enterTime = (new Date()).getTime();
34 /** Handle dragging and end drag events. Filters out very short or quick drags. */
35 override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
36 if (event.type==MouseEvent.MOUSE_UP) {
37 if (dragstate==DRAGGING) {
38 var undo:CompositeUndoableAction = new CompositeUndoableAction("Move items");
39 var lonDelta:Number = controller.map.coord2lon(downX)-controller.map.coord2lon(event.localX);
40 var latDelta:Number = controller.map.coord2lat(downY)-controller.map.coord2lat(event.localY);
41 var moved:Object = {};
42 for each (var entity:Entity in selection) {
44 var node:Node=Node(entity);
45 node.setLatLon(node.lat-latDelta, node.lon-lonDelta, undo.push);
47 } else if (entity is Way) {
48 undo.push(new MoveWayAction(Way(entity), latDelta, lonDelta, moved));
51 MainUndoStack.getGlobalStack().addAction(undo);
53 return controller.findStateForSelection(selection);
55 } else if ( event.type == MouseEvent.MOUSE_MOVE) {
57 if (dragstate==NOT_DRAGGING) {
59 } else if (dragstate==NOT_MOVED &&
60 ((Math.abs(downX - event.localX) < 3 && Math.abs(downY - event.localY) < 3) ||
61 (new Date().getTime()-enterTime)<300)) {
73 /** Abort dragging if ESC pressed. */
74 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
75 if (event.keyCode==Keyboard.ESCAPE) {
76 for each (var entity:Entity in selection) {
77 entity.dispatchEvent(new EntityDraggedEvent(Connection.ENTITY_DRAGGED, entity, 0, 0));
79 return controller.findStateForSelection(selection);
84 private function dragTo(event:MouseEvent):ControllerState {
85 for each (var entity:Entity in selection) {
86 entity.dispatchEvent(new EntityDraggedEvent(Connection.ENTITY_DRAGGED, entity, event.localX-downX, event.localY-downY));
91 public function forceDragStart():void {
95 /** Highlight the dragged selection. */
96 override public function enterState():void {
97 for each (var entity:Entity in selection) {
98 editableLayer.setHighlight(entity, { selected: true });
102 /** Un-highlight the dragged selection. */
103 override public function exitState(newState:ControllerState):void {
104 for each (var entity:Entity in selection) {
105 editableLayer.setHighlight(entity, { selected: false });
108 /** "DragSelection" */
109 override public function toString():String {
110 return "DragSelection";