Two more files documented.
[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
65                         var l:DisplayObject=map.paint.getPaintSpriteAt(map.paint.maxlayer);
66                         var o:DisplayObject=Sprite(l).getChildAt(3);    // names layer
67                         (o as Sprite).addChild(stroke);
68                         sprites.push(stroke);
69
70                         dashedLine(stroke.graphics, [2,2]);
71
72
73                 }
74                 
75                 // ------------------------------------------------------------------------------------------
76                 // Drawing support functions
77
78                 // Draw dashed polyline
79                 
80                 private function dashedLine(g:Graphics,dashes:Array):void {
81                         var draw:Boolean=false, dashleft:Number=0, dc:Array=new Array();
82                         var a:Number, xc:Number, yc:Number;
83                         var curx:Number, cury:Number;
84                         var dx:Number, dy:Number, segleft:Number=0;
85                         var i:int=0;
86
87             var p0:Point = start;
88             var p1:Point = end;
89                         g.moveTo(map.lon2coord(p0.x), map.latp2coord(p0.y));
90                         while (i < 1 || segleft>0) {
91                                 if (dashleft<=0) {      // should be ==0
92                                         if (dc.length==0) { dc=dashes.slice(0); }
93                                         dashleft=dc.shift();
94                                         draw=!draw;
95                                 }
96                                 if (segleft<=0) {       // should be ==0
97                                         curx=map.lon2coord(p0.x);
98                     dx=map.lon2coord(p1.x)-curx;
99                                         cury=map.latp2coord(p0.y);
100                     dy=map.latp2coord(p1.y)-cury;
101                                         a=Math.atan2(dy,dx); xc=Math.cos(a); yc=Math.sin(a);
102                                         segleft=Math.sqrt(dx*dx+dy*dy);
103                                         i++;
104                                 }
105
106                                 if (segleft<=dashleft) {
107                                         // the path segment is shorter than the dash
108                                         curx+=dx; cury+=dy;
109                                         moveLine(g,curx,cury,draw);
110                                         dashleft-=segleft; segleft=0;
111                                 } else {
112                                         // the path segment is longer than the dash
113                                         curx+=dashleft*xc; dx-=dashleft*xc;
114                                         cury+=dashleft*yc; dy-=dashleft*yc;
115                                         moveLine(g,curx,cury,draw);
116                                         segleft-=dashleft; dashleft=0;
117                                 }
118                         }
119                 }
120
121                 private function moveLine(g:Graphics,x:Number,y:Number,draw:Boolean):void {
122                         if (draw) { g.lineTo(x,y); }
123                                  else { g.moveTo(x,y); }
124                 }
125
126         }
127 }