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 sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
49 var paint:MapPaint = getMapPaint(DisplayObject(event.target));
50 var focus:Entity = getTopLevelFocusEntity(entity);
52 if ( event.type == MouseEvent.MOUSE_DOWN ) {
53 if ( entity is Way && event.altKey && paint.isBackground ) {
54 // pull way out of vector background layer
55 return new SelectedWay(paint.findSource().pullThrough(entity,controller.connection));
56 } else if ( entity is Way ) {
58 return new DragWay(focus as Way, event);
59 } else if ( focus is Node ) {
61 return new DragPOINode(entity as Node,event,false);
62 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
63 // select node within this way
64 return new DragWayNode(selectedWay, getNodeIndex(selectedWay,entity as Node), event, false);
65 } else if ( entity is Node && focus is Way ) {
67 return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
69 } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING ) {
70 return new NoSelection();
71 } else if ( event.type == MouseEvent.ROLL_OVER ) {
72 controller.map.setHighlight(focus, { hover: true });
73 } else if ( event.type == MouseEvent.MOUSE_OUT ) {
74 controller.map.setHighlight(focus, { hover: false });
79 public static function getTopLevelFocusEntity(entity:Entity):Entity {
80 if ( entity is Node ) {
81 for each (var parent:Entity in entity.parentWays) {
85 } else if ( entity is Way ) {
92 protected function getMapPaint(d:DisplayObject):MapPaint {
94 if (d is MapPaint) { return MapPaint(d); }
100 protected function getNodeIndex(way:Way,node:Node):uint {
101 for (var i:uint=0; i<way.length; i++) {
102 if (way.getNode(i)==node) { return i; }