bfaa05769221973f60ddc56edddc2669deb46b9b
[potlatch2.git] / net / systemeD / halcyon / Elastic.as
1 package net.systemeD.halcyon {
2
3         import flash.display.*;
4         import flash.geom.Matrix;
5         import flash.geom.Point;
6         import flash.geom.Rectangle;
7         import flash.text.AntiAliasType;
8         import flash.text.GridFitType;
9         import flash.text.TextField;
10         import flash.text.TextFormat;
11         import flash.events.*;
12         import net.systemeD.halcyon.styleparser.*;
13     import net.systemeD.halcyon.connection.*;
14
15         public class Elastic {
16
17                 public var map:Map;                                                     // reference to parent map
18                 public var sprites:Array=new Array();           // instances in display list
19         private var _start:Point;
20         private var _end:Point;
21
22                 public function Elastic(map:Map, start:Point, end:Point) {
23                         this.map = map;
24                         this._start = start;
25                         this._end = end;
26                         redraw();
27                 }
28                 
29                 public function set start(start:Point):void {
30                     this._start = start;
31                     redraw();
32                 }
33
34                 public function set end(end:Point):void {
35                     this._end = end;
36                     redraw();
37                 }
38                 
39                 public function get start():Point {
40                     return _start;
41                 }
42                 
43                 public function get end():Point {
44                     return _end;
45                 }
46                 
47                 public function removeSprites():void {
48                         // Remove all currently existing sprites
49                         while (sprites.length>0) {
50                                 var d:DisplayObject=sprites.pop(); d.parent.removeChild(d);
51                         }
52         }
53         
54                 public function redraw():void {
55                     removeSprites();
56
57                         // Iterate through each sublayer, drawing any styles on that layer
58                         var p0:Point = start;
59                         var p1:Point = end;
60
61                         // Create stroke object
62                         var stroke:Shape = new Shape();
63             stroke.graphics.lineStyle(1, 0xff0000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
64                         addToLayer(stroke,3);
65                         dashedLine(stroke.graphics, [2,2]);
66                 }
67                 
68                 // ------------------------------------------------------------------------------------------
69                 // Drawing support functions
70
71                 // Draw dashed polyline
72                 
73                 private function dashedLine(g:Graphics,dashes:Array):void {
74                         var draw:Boolean=false, dashleft:Number=0, dc:Array=new Array();
75                         var a:Number, xc:Number, yc:Number;
76                         var curx:Number, cury:Number;
77                         var dx:Number, dy:Number, segleft:Number=0;
78                         var i:int=0;
79
80             var p0:Point = start;
81             var p1:Point = end;
82                         g.moveTo(map.lon2coord(p0.x), map.latp2coord(p0.y));
83                         while (i < 1 || segleft>0) {
84                                 if (dashleft<=0) {      // should be ==0
85                                         if (dc.length==0) { dc=dashes.slice(0); }
86                                         dashleft=dc.shift();
87                                         draw=!draw;
88                                 }
89                                 if (segleft<=0) {       // should be ==0
90                                         curx=map.lon2coord(p0.x);
91                     dx=map.lon2coord(p1.x)-curx;
92                                         cury=map.latp2coord(p0.y);
93                     dy=map.latp2coord(p1.y)-cury;
94                                         a=Math.atan2(dy,dx); xc=Math.cos(a); yc=Math.sin(a);
95                                         segleft=Math.sqrt(dx*dx+dy*dy);
96                                         i++;
97                                 }
98
99                                 if (segleft<=dashleft) {
100                                         // the path segment is shorter than the dash
101                                         curx+=dx; cury+=dy;
102                                         moveLine(g,curx,cury,draw);
103                                         dashleft-=segleft; segleft=0;
104                                 } else {
105                                         // the path segment is longer than the dash
106                                         curx+=dashleft*xc; dx-=dashleft*xc;
107                                         cury+=dashleft*yc; dy-=dashleft*yc;
108                                         moveLine(g,curx,cury,draw);
109                                         segleft-=dashleft; dashleft=0;
110                                 }
111                         }
112                 }
113
114                 private function moveLine(g:Graphics,x:Number,y:Number,draw:Boolean):void {
115                         if (draw) { g.lineTo(x,y); }
116                                  else { g.moveTo(x,y); }
117                 }
118
119                 
120                 // Add object (stroke/fill/roadname) to layer sprite
121                 
122                 private function addToLayer(s:DisplayObject,t:uint,sublayer:int=-1):void {
123                         var l:DisplayObject=Map(map).getChildAt(map.WAYSPRITE+5);
124                         var o:DisplayObject=Sprite(l).getChildAt(t);
125                         if (sublayer!=-1) { o=Sprite(o).getChildAt(sublayer); }
126                         Sprite(o).addChild(s);
127                         sprites.push(s);
128             if ( s is Sprite ) {
129                 Sprite(s).mouseEnabled = false;
130                 Sprite(s).mouseChildren = false;
131             }
132                 }
133         }
134 }