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.EditController;
8 import net.systemeD.halcyon.Globals;
10 public class ControllerState {
12 protected var controller:EditController;
13 protected var previousState:ControllerState;
15 protected var selectedNode:Node;
16 public var selectedWay:Way;
18 public function ControllerState() {}
20 public function setController(controller:EditController):void {
21 this.controller = controller;
24 public function setPreviousState(previousState:ControllerState):void {
25 if ( this.previousState == null )
26 this.previousState = previousState;
29 public function isSelectionState():Boolean {
33 public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
37 public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
41 public function get map():Map {
42 return controller.map;
45 public function enterState():void {}
46 public function exitState(newState:ControllerState):void {}
48 public function toString():String {
52 protected function sharedKeyboardEvents(event:KeyboardEvent):ControllerState {
53 switch (event.keyCode) {
54 case 68: controller.map.paint.alpha=1.3-controller.map.paint.alpha; return null; // D
55 case 87: if (selectedWay) { return new SelectedWay(selectedWay); }; return null; // W
56 case 90: MainUndoStack.getGlobalStack().undo(); return null; // Z
61 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
62 var paint:MapPaint = getMapPaint(DisplayObject(event.target));
63 var focus:Entity = getTopLevelFocusEntity(entity);
65 if ( paint && paint.isBackground ) {
66 if ( event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
67 // alt-click to pull data out of vector background layer
68 var newEntity:Entity=paint.findSource().pullThrough(entity,controller.connection);
69 if (entity is Way) { return new SelectedWay(newEntity as Way); }
70 else if (entity is Node) { return new SelectedPOINode(newEntity as Node); }
71 } else if ( event.type == MouseEvent.MOUSE_UP ) {
72 return (this is NoSelection) ? null : new NoSelection();
73 } else { return null; }
76 if ( event.type == MouseEvent.MOUSE_DOWN ) {
77 if ( entity is Way ) {
79 return new DragWay(focus as Way, event);
80 } else if ( focus is Node ) {
82 return new DragPOINode(entity as Node,event,false);
83 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
84 // select node within this way
85 return new DragWayNode(selectedWay, getNodeIndex(selectedWay,entity as Node), event, false);
86 } else if ( entity is Node && focus is Way ) {
88 return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
89 } else if ( controller.keyDown(32) ) {
91 return new DragBackground(event);
93 } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING) {
94 return (this is NoSelection) ? null : new NoSelection();
95 } else if ( event.type == MouseEvent.ROLL_OVER ) {
96 controller.map.setHighlight(focus, { hover: true });
97 } else if ( event.type == MouseEvent.MOUSE_OUT ) {
98 controller.map.setHighlight(focus, { hover: false });
103 public static function getTopLevelFocusEntity(entity:Entity):Entity {
104 if ( entity is Node ) {
105 for each (var parent:Entity in entity.parentWays) {
109 } else if ( entity is Way ) {
116 protected function getMapPaint(d:DisplayObject):MapPaint {
118 if (d is MapPaint) { return MapPaint(d); }
124 protected function getNodeIndex(way:Way,node:Node):uint {
125 for (var i:uint=0; i<way.length; i++) {
126 if (way.getNode(i)==node) { return i; }
131 protected function repeatTags(object:Entity):void {
132 if (!controller.clipboards[object.getType()]) { return; }
135 var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
136 for (var k:String in controller.clipboards[object.getType()]) {
137 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
139 MainUndoStack.getGlobalStack().addAction(undo);