Merge branch 'master' of github.com:systemed/potlatch2
[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         /** The elastic is a visual indication of a way that the user is currently in the process of drawing. */
16         public class Elastic {
17
18                 public var map:Map;                                                     // reference to parent map
19                 public var sprites:Array=new Array();           // instances in display list
20                 private var editableLayer:MapPaint;
21         private var _start:Point;
22         private var _end:Point;
23
24                 /** Create and draw the elastic. */
25                 public function Elastic(map:Map, start:Point, end:Point) {
26                         this.map = map;
27                         editableLayer = map.editableLayer;
28                         _start = start;
29                         _end = end;
30                         redraw();
31                 }
32                 
33                 public function set start(start:Point):void {
34                     this._start = start;
35                     redraw();
36                 }
37
38                 public function set end(end:Point):void {
39                     this._end = end;
40                     redraw();
41                 }
42                 
43                 public function get start():Point {
44                     return _start;
45                 }
46                 
47                 public function get end():Point {
48                     return _end;
49                 }
50                 
51                 /** Remove all currently existing sprites */
52                 public function removeSprites():void {
53                         
54                         while (sprites.length>0) {
55                                 var d:DisplayObject=sprites.pop(); d.parent.removeChild(d);
56                         }
57         }
58         
59                 /** Draws the elastic - a dashed line on the highest paint layer. */
60                 public function redraw():void {
61                     removeSprites();
62
63                         // Create stroke object
64                         var stroke:Shape = new Shape();
65             stroke.graphics.lineStyle(1, 0xff0000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
66
67                         var l:DisplayObject=editableLayer.getPaintSpriteAt(editableLayer.maxlayer);
68                         var o:DisplayObject=Sprite(l).getChildAt(3);    // names layer
69                         (o as Sprite).addChild(stroke);
70                         sprites.push(stroke);
71
72                         dashedLine(stroke.graphics, [2,2]);
73
74
75                 }
76                 
77                 // ------------------------------------------------------------------------------------------
78                 // Drawing support functions
79
80                 // Draw dashed polyline
81                 
82                 private function dashedLine(g:Graphics,dashes:Array):void {
83                         var draw:Boolean=false, dashleft:Number=0, dc:Array=new Array();
84                         var a:Number, xc:Number, yc:Number;
85                         var curx:Number, cury:Number;
86                         var dx:Number, dy:Number, segleft:Number=0;
87                         var i:int=0;
88
89             var p0:Point = start;
90             var p1:Point = end;
91                         g.moveTo(map.lon2coord(p0.x), map.latp2coord(p0.y));
92                         while (i < 1 || segleft>0) {
93                                 if (dashleft<=0) {      // should be ==0
94                                         if (dc.length==0) { dc=dashes.slice(0); }
95                                         dashleft=dc.shift();
96                                         draw=!draw;
97                                 }
98                                 if (segleft<=0) {       // should be ==0
99                                         curx=map.lon2coord(p0.x);
100                     dx=map.lon2coord(p1.x)-curx;
101                                         cury=map.latp2coord(p0.y);
102                     dy=map.latp2coord(p1.y)-cury;
103                                         a=Math.atan2(dy,dx); xc=Math.cos(a); yc=Math.sin(a);
104                                         segleft=Math.sqrt(dx*dx+dy*dy);
105                                         i++;
106                                 }
107
108                                 if (segleft<=dashleft) {
109                                         // the path segment is shorter than the dash
110                                         curx+=dx; cury+=dy;
111                                         moveLine(g,curx,cury,draw);
112                                         dashleft-=segleft; segleft=0;
113                                 } else {
114                                         // the path segment is longer than the dash
115                                         curx+=dashleft*xc; dx-=dashleft*xc;
116                                         cury+=dashleft*yc; dy-=dashleft*yc;
117                                         moveLine(g,curx,cury,draw);
118                                         segleft-=dashleft; dashleft=0;
119                                 }
120                         }
121                 }
122
123                 private function moveLine(g:Graphics,x:Number,y:Number,draw:Boolean):void {
124                         if (draw) { g.lineTo(x,y); }
125                                  else { g.moveTo(x,y); }
126                 }
127
128         }
129 }