add node drawing + dragging & highlight selected way using stylesheet
[potlatch2.git] / net / systemeD / potlatch2 / EditController.as
1 package net.systemeD.potlatch2 {
2     import net.systemeD.halcyon.Map;
3     import net.systemeD.halcyon.MapController;
4     import net.systemeD.halcyon.connection.*;
5         import flash.events.*;
6         import flash.geom.*;
7
8     public class EditController implements MapController {
9
10         private var map:Map;
11         private var tagViewer:TagViewer;
12         private var selectedEntity:Entity;
13         
14         private var draggingNode:Node = null;
15         
16
17         public function EditController(map:Map, tagViewer:TagViewer) {
18             this.map = map;
19             this.tagViewer = tagViewer;
20             
21             map.parent.addEventListener(MouseEvent.MOUSE_MOVE, mapMouseEvent);
22             map.parent.addEventListener(MouseEvent.MOUSE_UP, mapMouseEvent);
23             map.parent.addEventListener(MouseEvent.MOUSE_DOWN, mapMouseEvent);
24         }
25
26         public function setActive():void {
27             map.setController(this);
28         }
29
30         private function mapMouseEvent(event:MouseEvent):void {
31             if ( draggingNode != null ) {
32                 var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
33                 event.localX = mapLoc.x;
34                 event.localY = mapLoc.y;
35
36                 processNodeEvent(event, null);
37             }
38         }
39         
40         public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
41             if ( event.type == MouseEvent.MOUSE_DOWN )
42                 event.stopPropagation();
43
44             if ( entity is Node || draggingNode != null ) {
45                 processNodeEvent(event, entity);
46                 return;
47             }
48             
49             if ( event.type == MouseEvent.CLICK ) {
50                 if ( selectedEntity != null ) {
51                     map.setHighlight(selectedEntity, "selected", false);
52                     map.setHighlight(selectedEntity, "showNodes", false);
53                 }
54                 tagViewer.setEntity(entity);
55                 map.setHighlight(entity, "selected", true);
56                 map.setHighlight(entity, "showNodes", true);
57                 selectedEntity = entity;
58             } else if ( event.type == MouseEvent.MOUSE_OVER )
59                 map.setHighlight(entity, "hover", true);
60             else if ( event.type == MouseEvent.MOUSE_OUT )
61                 map.setHighlight(entity, "hover", false);
62
63         }
64
65         private function processNodeEvent(event:MouseEvent, entity:Entity):void {
66             if ( draggingNode != null ) {
67                 if ( event.type == MouseEvent.MOUSE_UP ) {
68                     draggingNode = null;
69                 } else if ( event.type == MouseEvent.MOUSE_MOVE ) {
70                     draggingNode.lat = map.coord2lat(event.localY);
71                     draggingNode.lon = map.coord2lon(event.localX);
72                 }
73             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
74                 draggingNode = entity as Node;
75             }
76         }
77         
78     }
79
80 }
81