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 processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
33 public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
37 public function get map():Map {
38 return controller.map;
41 public function enterState():void {}
42 public function exitState():void {}
44 public function toString():String {
48 protected function sharedKeyboardEvents(event:KeyboardEvent):ControllerState {
49 switch (event.keyCode) {
50 case 90: MainUndoStack.getGlobalStack().undo(); return null; // Z
55 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
56 var paint:MapPaint = getMapPaint(DisplayObject(event.target));
57 var focus:Entity = getTopLevelFocusEntity(entity);
59 if ( event.type == MouseEvent.MOUSE_DOWN ) {
60 if ( entity is Way && event.altKey && paint.isBackground ) {
61 // pull way out of vector background layer
62 return new SelectedWay(paint.findSource().pullThrough(entity,controller.connection));
63 } else if ( entity is Way ) {
65 return new DragWay(focus as Way, event);
66 } else if ( focus is Node ) {
68 return new DragPOINode(entity as Node,event,false);
69 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
70 // select node within this way
71 return new DragWayNode(selectedWay, getNodeIndex(selectedWay,entity as Node), event, false);
72 } else if ( entity is Node && focus is Way ) {
74 return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
76 } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING ) {
77 return new NoSelection();
78 } else if ( event.type == MouseEvent.ROLL_OVER ) {
79 controller.map.setHighlight(focus, { hover: true });
80 } else if ( event.type == MouseEvent.MOUSE_OUT ) {
81 controller.map.setHighlight(focus, { hover: false });
86 public static function getTopLevelFocusEntity(entity:Entity):Entity {
87 if ( entity is Node ) {
88 for each (var parent:Entity in entity.parentWays) {
92 } else if ( entity is Way ) {
99 protected function getMapPaint(d:DisplayObject):MapPaint {
101 if (d is MapPaint) { return MapPaint(d); }
107 protected function getNodeIndex(way:Way,node:Node):uint {
108 for (var i:uint=0; i<way.length; i++) {
109 if (way.getNode(i)==node) { return i; }