0e9037930eb7a53a61f5699c40bd4d21099f1758
[potlatch2.git] / net / systemeD / halcyon / WayUI.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.TextField;
8         import flash.text.TextFormat;
9         import flash.events.*;
10         import net.systemeD.halcyon.styleparser.*;
11     import net.systemeD.halcyon.connection.*;
12         import net.systemeD.halcyon.Globals;
13
14         /** The graphical representation of a Way. */ 
15         public class WayUI extends EntityUI {
16
17                 /** Total length of way */
18                 public var pathlength:Number;
19                 /** Area of the way */
20                 public var patharea:Number;
21                 /** X coord of the centre of the area */
22                 public var centroid_x:Number;
23                 /** Y coord of the centre of the area */
24                 public var centroid_y:Number;                           //  |
25                 /** Angle at each node */
26                 public var heading:Array=new Array();
27                 /** vertex to draw exclusively, or not at all (used by DragWayNode) */ 
28                 public var drawExcept:Number;
29                 /** " */
30                 public var drawOnly:Number;
31                 private var indexStart:uint;
32                 private var indexEnd:uint;
33                 public var nameformat:TextFormat;
34                 private var recalculateDue:Boolean=false;
35                 // Store the temporary highlight settings applied to all nodes.
36                 private var nodehighlightsettings: Object={};
37
38                 private const NODESIZE:uint=6;
39
40                 public function WayUI(way:Way, paint:MapPaint) {
41                         super(way,paint);
42             entity.addEventListener(Connection.WAY_NODE_ADDED, wayNodeAdded, false, 0, true);
43             entity.addEventListener(Connection.WAY_NODE_REMOVED, wayNodeRemoved, false, 0, true);
44             entity.addEventListener(Connection.WAY_REORDERED, wayReordered, false, 0, true);
45             entity.addEventListener(Connection.ENTITY_DRAGGED, wayDragged, false, 0, true);
46             attachNodeListeners();
47             attachRelationListeners();
48             recalculate();
49                         redraw();
50                         redrawMultis();
51                 }
52                 
53                 public function removeEventListeners():void {
54                         removeGenericEventListeners();
55             entity.removeEventListener(Connection.WAY_NODE_ADDED, wayNodeAdded);
56             entity.removeEventListener(Connection.WAY_NODE_REMOVED, wayNodeRemoved);
57             entity.removeEventListener(Connection.WAY_REORDERED, wayReordered);
58             entity.removeEventListener(Connection.ENTITY_DRAGGED, wayDragged);
59                         removeNodeListeners();
60                         removeRelationListeners();
61                 }
62                 
63                 private function attachNodeListeners():void {
64                         var way:Way=entity as Way;
65             for (var i:uint = 0; i < way.length; i++ ) {
66                 way.getNode(i).addEventListener(Connection.NODE_MOVED, nodeMoved, false, 0, true);
67             }
68                 }
69                 
70                 private function removeNodeListeners():void {
71                         var way:Way=entity as Way;
72             for (var i:uint = 0; i < way.length; i++ ) {
73                 way.getNode(i).removeEventListener(Connection.NODE_MOVED, nodeMoved);
74             }
75                 }
76                 
77                 private function wayNodeAdded(event:WayNodeEvent):void {
78                     event.node.addEventListener(Connection.NODE_MOVED, nodeMoved, false, 0, true);
79             recalculate();
80                     redraw();
81                         redrawMultis();
82                 }
83                     
84                 private function wayNodeRemoved(event:WayNodeEvent):void {
85                     if (!event.node.hasParent(event.way)) {
86                                 event.node.removeEventListener(Connection.NODE_MOVED, nodeMoved);
87                         }
88                         if (paint.nodeuis[event.node.id]) {
89                                 paint.nodeuis[event.node.id].redraw();
90                         }
91             recalculate();
92                     redraw();
93                         redrawMultis();
94                 }
95                     
96         private function nodeMoved(event:NodeMovedEvent):void {
97                         recalculate();
98             redraw();
99                         redrawMultis();
100         }
101         private function wayReordered(event:EntityEvent):void {
102             redraw();
103                         redrawMultis();
104         }
105                 private function wayDragged(event:EntityDraggedEvent):void {
106                         offsetSprites(event.xDelta,event.yDelta);
107                 }
108
109                 override protected function relationAdded(event:RelationMemberEvent):void {
110                         super.relationAdded(event);
111                         redrawMultis();
112                 }
113                 override protected function relationRemoved(event:RelationMemberEvent):void {
114                         super.relationRemoved(event);
115                         redrawMultis();
116                 }
117                 override protected function relationTagChanged(event:TagEvent):void {
118                         super.relationTagChanged(event);
119                         redrawMultis();
120                 }
121
122                 /** Don't redraw until further notice. */
123                 override public function suspendRedraw(event:EntityEvent):void {
124                         super.suspendRedraw(event);
125                         recalculateDue=false;
126                 }
127                 
128                 /** Continue redrawing as normal. */
129                 override public function resumeRedraw(event:EntityEvent):void {
130                         suspended=false;
131                         if (recalculateDue) { recalculate(); }
132                         super.resumeRedraw(event);
133                 }
134
135                 /** Redraw other ways that are the "outer" part of a multipolygon of which we are the "inner" */
136                 public function redrawMultis():void {
137                         var multis:Array=entity.findParentRelationsOfType('multipolygon','inner');
138                         for each (var m:Relation in multis) {
139                                 var outers:Array=m.findMembersByRole('outer');
140                                 for each (var e:Entity in outers) { 
141                                         if (e is Way && paint.wayuis[e.id]) {
142                                                 paint.wayuis[e.id].redraw();
143                                         }
144                                 }
145                         }
146                 }
147         
148         /** Mark every node in this way as highlighted, and redraw it. 
149         * 
150         * @param settings Style state that it applies to. @see EntityUI#setStateClass()
151         *  */
152         public function setHighlightOnNodes(settings:Object):void {
153                         for (var i:uint = 0; i < Way(entity).length; i++) {
154                 var node:Node = Way(entity).getNode(i);
155                                 if (paint.nodeuis[node.id]) {
156                                         // Speed things up a bit by only setting the highlight if it's either:
157                                         // a) an "un-highlight" (so we don't leave mess behind when scrolling)
158                                         // b) currently onscreen
159                                         // Currently this means if you highlight an object then scroll, nodes will scroll
160                                         // into view that should be highlighted but aren't.
161                                         if (settings.hoverway==false || 
162                                             settings.selectedway==false || 
163                                             node.lat >=  paint.map.edge_b && node.lat <= paint.map.edge_t &&
164                                             node.lon >= paint.map.edge_l && node.lon <= paint.map.edge_r) {
165                                             paint.nodeuis[node.id].setHighlight(settings); // Triggers redraw if required
166                                         }
167                                         if (settings.selectedway  || settings.hoverway)
168                                                 nodehighlightsettings=settings;
169                                         else
170                                            nodehighlightsettings={}; 
171                                 }
172                         }
173         }
174         
175         // An ugly hack to allow nodes that have recently scrolled into view to get highlighted.
176         public function updateHighlights():void {
177                 if (nodehighlightsettings)
178                    setHighlightOnNodes(nodehighlightsettings);
179         }
180
181                 // ------------------------------------------------------------------------------------------
182                 /** Calculate pathlength, patharea, centroid_x, centroid_y, heading[]. 
183                 * ** this could be made scale-independent - would speed up redraw
184                 */
185                 public function recalculate():void {
186                         if (suspended) { recalculateDue=true; return; }
187                         
188                         var lx:Number, ly:Number, sc:Number;
189                         var node:Node, latp:Number, lon:Number;
190                         var cx:Number=0, cy:Number=0;
191                         var way:Way=entity as Way;
192                         
193                         pathlength=0;
194                         patharea=0;
195                         if (way.length==0) { return; }
196                         
197                         lx = way.getNode(way.length-1).lon;
198                         ly = way.getNode(way.length-1).latp;
199                         for ( var i:uint = 0; i < way.length; i++ ) {
200                 node = way.getNode(i);
201                 latp = node.latp;
202                 lon  = node.lon;
203
204                                 // length and area
205                                 if ( i>0 ) { pathlength += Math.sqrt( Math.pow(lon-lx,2)+Math.pow(latp-ly,2) ); }
206                                 sc = (lx*latp-lon*ly)*paint.map.scalefactor;
207                                 cx += (lx+lon)*sc;
208                                 cy += (ly+latp)*sc;
209                                 patharea += sc;
210                                 
211                                 // heading
212                                 if (i>0) { heading[i-1]=Math.atan2((lon-lx),(latp-ly)); }
213
214                                 lx=lon; ly=latp;
215                         }
216                         heading[way.length-1]=heading[way.length-2];
217
218                         pathlength*=paint.map.scalefactor;
219                         patharea/=2;
220                         if (patharea!=0 && way.isArea()) {
221                                 centroid_x=paint.map.lon2coord(cx/patharea/6);
222                                 centroid_y=paint.map.latp2coord(cy/patharea/6);
223                         } else if (pathlength>0) {
224                                 var c:Array=pointAt(0.5);
225                                 centroid_x=c[0];
226                                 centroid_y=c[1];
227                         }
228                 }
229
230                 // ------------------------------------------------------------------------------------------
231         
232                 /** Go through the complex process of drawing this way, including applying styles, casings, fills, fonts... */
233                 override public function doRedraw():Boolean {
234                         interactive=false;
235                         removeSprites();
236                         if (Way(entity).length==0) { return false; }
237                         if (!paint.ready) { return false; }
238
239             // Copy tags object, and add states
240             var tags:Object = entity.getTagsCopy();
241             setStateClass('area', Way(entity).isArea());
242             setStateClass('tiger', (entity.isUneditedTiger() && Globals.vars.highlightTiger));
243             tags=applyStateClasses(tags);
244
245                         // Keep track of maximum stroke width for hitzone
246                         var maxwidth:Number=4;
247
248                         // Create styleList if not already drawn
249                         if (!styleList || !styleList.isValidAt(paint.map.scale)) {
250                                 styleList=paint.ruleset.getStyles(entity, tags, paint.map.scale);
251                         }
252
253                         // Which layer?
254                         layer=styleList.layerOverride();
255                         if (isNaN(layer)) {
256                                 layer=0;
257                                 if (tags['layer']) { layer=Math.min(Math.max(tags['layer'],paint.minlayer),paint.maxlayer); }
258                         }
259
260                         // Do we have to draw all nodes in the way?
261                         var hasFills:Boolean=styleList.hasFills();
262                         if (isNaN(drawOnly) || hasFills) {
263                                 indexStart=0; indexEnd=Way(entity).length; 
264                         } else {
265                                 indexStart=Math.max(0,drawOnly-1);
266                                 indexEnd  =Math.min(drawOnly+2,Way(entity).length);
267                         }
268
269                         // Iterate through each sublayer, drawing any styles on that layer
270                         var drawn:Boolean;
271                         var multis:Array=entity.findParentRelationsOfType('multipolygon','outer');
272                         var inners:Array=[];
273                         for each (var m:Relation in multis) {
274                                 inners=inners.concat(m.findMembersByRole('inner',Way));
275                         }
276
277                         for each (var sublayer:Number in styleList.sublayers) {
278                                 if (styleList.shapeStyles[sublayer]) {
279                                         var s:ShapeStyle=styleList.shapeStyles[sublayer];
280                                         var stroke:Shape, fill:Shape, casing:Shape, roadname:Sprite;
281                                         var x0:Number=paint.map.lon2coord(Way(entity).getNode(0).lon);
282                                         var y0:Number=paint.map.latp2coord(Way(entity).getNode(0).latp);
283                                         interactive||=s.interactive;
284
285                                         // Stroke
286                                         if (s.width)  {
287                                                 stroke=new Shape(); addToLayer(stroke,STROKESPRITE,sublayer);
288                                                 stroke.graphics.moveTo(x0,y0);
289                                                 s.applyStrokeStyle(stroke.graphics);
290                                                 if (s.dashes && s.dashes.length>0) {
291                                                         var segments:Array=dashedLine(stroke.graphics,s.dashes); 
292                                                         if (s.line_style) { lineDecoration(stroke.graphics,s,segments); }
293                                                 } else { solidLines(stroke.graphics,inners); }
294                                                 drawn=true;
295                                                 if (s.interactive) { maxwidth=Math.max(maxwidth,s.width); }
296                                         }
297
298                                         // Fill
299                                         if ((!isNaN(s.fill_color) || s.fill_image) && entity.findParentRelationsOfType('multipolygon','inner').length==0 && isNaN(drawExcept)) {
300                                                 fill=new Shape(); addToLayer(fill,FILLSPRITE);
301                                                 fill.graphics.moveTo(x0,y0);
302                                                 if (s.fill_image) { new WayBitmapFiller(this,fill.graphics,s); }
303                                                                          else { s.applyFill(fill.graphics); }
304                                                 solidLines(fill.graphics,inners);
305                                                 fill.graphics.endFill();
306                                                 drawn=true;
307                                         }
308
309                                         // Casing
310                                         if (s.casing_width) { 
311                                                 casing=new Shape(); addToLayer(casing,CASINGSPRITE);
312                                                 casing.graphics.moveTo(x0,y0);
313                                                 s.applyCasingStyle(casing.graphics);
314                                                 if (s.casing_dashes && s.casing_dashes.length>0) { dashedLine(casing.graphics,s.casing_dashes); }
315                                                                                                                                         else { solidLines(casing.graphics,inners); }
316                                                 drawn=true;
317                                                 if (s.interactive) { maxwidth=Math.max(maxwidth,s.casing_width); }
318                                         }
319                                 }
320                                 
321                                 if (styleList.textStyles[sublayer] && isNaN(drawExcept)) {
322                                         var t:TextStyle=styleList.textStyles[sublayer];
323                                         interactive||=t.interactive;
324                                         roadname=new Sprite(); addToLayer(roadname,NAMESPRITE);
325                                         nameformat = t.getTextFormat();
326                                         var a:String=tags[t.text];
327                                         if (a) {
328                                                 if (t.font_caps) { a=a.toUpperCase(); }
329                                                 if (t.text_center && centroid_x) {
330                                                         t.writeNameLabel(roadname,a,centroid_x,centroid_y);
331                                                 } else {
332                                                         writeNameOnPath(roadname,a,t.text_offset ? t.text_offset : 0);
333                                                 }
334                                                 if (t.text_halo_radius>0) { roadname.filters=t.getHaloFilter(); }
335                                         }
336                                 }
337                                 
338                                 // ** ShieldStyle to do
339                         }
340
341                         // Draw icons
342                         var r:Number;
343                         var nodeSelected:int=stateClasses["nodeSelected"];
344                         for (var i:uint = indexStart; i < indexEnd; i++) {
345                 var node:Node = Way(entity).getNode(i);
346                                 var nodeStateClasses:Object={};
347 //                              if (i==0) { nodetags['_heading']= heading[i]; }
348 //                                   else { nodetags['_heading']=(heading[i]+heading[i-1])/2; }
349                                 // ** FIXME - heading isn't currently applied
350                                 nodeStateClasses['junction']=(node.numParentWays>1);
351                                 paint.createNodeUI(node,r,layer,nodeStateClasses);
352                         }
353                         if (!drawn) { return false; } // If not visible, no hitzone.
354                         
355             // create a generic "way" hitzone sprite
356                         if (interactive && drawn) {
357                     hitzone = new Sprite();
358                     hitzone.graphics.lineStyle(maxwidth, 0x000000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
359                     solidLines(hitzone.graphics,[]);
360                     hitzone.visible = false;
361                                 setListenSprite();
362                         }
363
364                         return true;
365                 }
366                 
367                 // ------------------------------------------------------------------------------------------
368                 // Drawing support functions
369
370                 /** Draw solid polyline */
371                 
372                 public function solidLines(g:Graphics,inners:Array):void {
373                         solidLine(g);
374                         for each (var w:Way in inners) { solidLineOtherWay(g,w); }
375                 }
376
377                 private function solidLine(g:Graphics):void {
378                         if (indexEnd==0) { return; }
379                         var way:Way=entity as Way;
380                         
381             var node:Node = way.getNode(indexStart);
382                         g.moveTo(paint.map.lon2coord(node.lon), paint.map.latp2coord(node.latp));
383                         for (var i:uint = indexStart+1; i < indexEnd; i++) {
384                 node = way.getNode(i);
385                                 if (!isNaN(drawExcept) && (i-1==drawExcept || i==drawExcept)) {
386                                         g.moveTo(paint.map.lon2coord(node.lon), paint.map.latp2coord(node.latp));
387                                 } else {
388                                         g.lineTo(paint.map.lon2coord(node.lon), paint.map.latp2coord(node.latp));
389                                 }
390                         }
391                 }
392
393                 private function solidLineOtherWay(g:Graphics,way:Way):void {
394                         if (way.length==0) { return; }
395                         
396                         var node:Node = way.getNode(0);
397                         g.moveTo(paint.map.lon2coord(node.lon), paint.map.latp2coord(node.latp));
398                         for (var i:uint = 1; i < way.length; i++) {
399                                 node = way.getNode(i);
400                                 g.lineTo(paint.map.lon2coord(node.lon), paint.map.latp2coord(node.latp));
401                         }
402                 }
403
404                 /** Draw dashed polyline */
405                 
406                 private function dashedLine(g:Graphics,dashes:Array):Array {
407                         var way:Way=entity as Way;
408                         var segments:Array=[];
409                         var draw:Boolean=false, dashleft:Number=0, dc:Array=new Array();
410                         var a:Number, xc:Number, yc:Number;
411                         var curx:Number, cury:Number;
412                         var dx:Number, dy:Number, segleft:Number=0;
413                         var i:int=indexStart;
414
415             var node:Node = way.getNode(i);
416             var nextNode:Node = way.getNode(i);
417                         g.moveTo(paint.map.lon2coord(node.lon), paint.map.latp2coord(node.latp));
418                         while (i < indexEnd-1 || segleft>0) {
419                                 if (dashleft<=0) {      // should be ==0
420                                         if (dc.length==0) { dc=dashes.slice(0); }
421                                         dashleft=dc.shift();
422                                         if (draw) { segments.push([curx,cury,dx,dy]); }
423                                         draw=!draw;
424                                 }
425                                 if (i==drawExcept || i==drawExcept+1) { draw=false; }
426                                 if (segleft<=0) {       // should be ==0
427                     node = way.getNode(i);
428                     nextNode = way.getNode(i+1);
429                                         curx=paint.map.lon2coord(node.lon);
430                     dx=paint.map.lon2coord(nextNode.lon)-curx;
431                                         cury=paint.map.latp2coord(node.latp);
432                     dy=paint.map.latp2coord(nextNode.latp)-cury;
433                                         a=Math.atan2(dy,dx); xc=Math.cos(a); yc=Math.sin(a);
434                                         segleft=Math.sqrt(dx*dx+dy*dy);
435                                         i++;
436                                 }
437
438                                 if (segleft<=dashleft) {
439                                         // the path segment is shorter than the dash
440                                         curx+=dx; cury+=dy;
441                                         moveLine(g,curx,cury,draw);
442                                         dashleft-=segleft; segleft=0;
443                                 } else {
444                                         // the path segment is longer than the dash
445                                         curx+=dashleft*xc; dx-=dashleft*xc;
446                                         cury+=dashleft*yc; dy-=dashleft*yc;
447                                         moveLine(g,curx,cury,draw);
448                                         segleft-=dashleft; dashleft=0;
449                                 }
450                         }
451                         return segments;
452                 }
453
454                 private function moveLine(g:Graphics,x:Number,y:Number,draw:Boolean):void {
455                         if (draw) { g.lineTo(x,y); }
456                                  else { g.moveTo(x,y); }
457                 }
458
459                 /** Draw decoration (arrows etc.) */
460                 
461                 private function lineDecoration(g:Graphics,s:ShapeStyle,segments:Array):void {
462                         var c:int=s.color ? s.color : 0;
463                         switch (s.line_style.toLowerCase()) {
464
465                                 case 'arrows':
466                                         var w:Number=s.width*1.5;       // width of arrow
467                                         var l:Number=s.width*2;         // length of arrow
468                                         var angle0:Number, angle1:Number, angle2:Number;
469                                         g.lineStyle(1,c);
470                                         for each (var seg:Array in segments) {
471                                                 g.beginFill(c);
472                                                 angle0= Math.atan2(seg[3],seg[2]);
473                                                 angle1=-Math.atan2(seg[3],seg[2]);
474                                                 angle2=-Math.atan2(seg[3],seg[2])-Math.PI;
475                                                 g.moveTo(seg[0]+l*Math.cos(angle0),
476                                                          seg[1]+l*Math.sin(angle0));
477                                                 g.lineTo(seg[0]+w*Math.sin(angle1),
478                                                          seg[1]+w*Math.cos(angle1));
479                                                 g.lineTo(seg[0]+w*Math.sin(angle2),
480                                                          seg[1]+w*Math.cos(angle2));
481                                                 g.endFill();
482                                         }
483                                         break;
484                 case 'triangle':
485                     w=s.width*10;   //triangle egde
486                     g.lineStyle(1,c);
487                     for each (seg in segments) {
488                         g.beginFill(c);
489                         angle0 = -Math.atan2(seg[3], seg[2]) + Math.PI / 2; //0
490                         angle1 = -Math.atan2(seg[3], seg[2]) - Math.PI/6;       //60
491                         g.moveTo(seg[0], seg[1]);//start 0,0
492                         g.lineTo(seg[0] - w * Math.sin(angle0), seg[1] - w * Math.cos(angle0));
493                         g.lineTo(seg[0] + w * Math.sin(angle1), seg[1] + w * Math.cos(angle1));
494                         g.endFill();
495                     }
496                     break;
497                                 }
498                 }
499
500                 
501                 /** Find point partway (0-1) along a path
502                   * @return (x,y,angle)
503                   * inspired by senocular's Path.as */
504                 
505                 private function pointAt(t:Number):Array {
506                         var way:Way=entity as Way;
507                         var totallen:Number = t*pathlength;
508                         var curlen:Number = 0;
509                         var dx:Number, dy:Number, seglen:Number;
510                         for (var i:int = 1; i < way.length; i++){
511                                 dx=paint.map.lon2coord(way.getNode(i).lon)-paint.map.lon2coord(way.getNode(i-1).lon);
512                                 dy=paint.map.latp2coord(way.getNode(i).latp)-paint.map.latp2coord(way.getNode(i-1).latp);
513                                 seglen=Math.sqrt(dx*dx+dy*dy);
514                                 if (totallen > curlen+seglen) { curlen+=seglen; continue; }
515                                 return new Array(paint.map.lon2coord(way.getNode(i-1).lon)+(totallen-curlen)/seglen*dx,
516                                                                  paint.map.latp2coord(way.getNode(i-1).latp)+(totallen-curlen)/seglen*dy,
517                                                                  Math.atan2(dy,dx));
518                         }
519                         return new Array(0, 0, 0);
520                 }
521
522                 /** Draw name along path
523                  * based on code by Tom Carden
524                  * */
525                 
526                 private function writeNameOnPath(s:Sprite,a:String,textOffset:Number=0):void {
527
528                         // make a dummy textfield so we can measure its width
529                         var tf:TextField = new TextField();
530                         tf.defaultTextFormat = nameformat;
531                         tf.text = a;
532                         tf.width = tf.textWidth+4;
533                         tf.height = tf.textHeight+4;
534                         if (pathlength<tf.width) { return; }    // no room for text?
535
536                         var t1:Number = (pathlength/2 - tf.width/2) / pathlength; var p1:Array=pointAt(t1);
537                         var t2:Number = (pathlength/2 + tf.width/2) / pathlength; var p2:Array=pointAt(t2);
538
539                         var angleOffset:Number; // so we can do a 180ยบ if we're running backwards
540                         var offsetSign:Number;  // -1 if we're starting at t2
541                         var tStart:Number;      // t1 or t2
542
543                         // make sure text doesn't run right->left or upside down
544                         if (p1[0] < p2[0] && 
545                                 p1[2] < Math.PI/2 &&
546                                 p1[2] > -Math.PI/2) {
547                                 angleOffset = 0; offsetSign = 1; tStart = t1;
548                         } else {
549                                 angleOffset = Math.PI; offsetSign = -1; tStart = t2;
550                         } 
551
552                         // make a textfield for each char, centered on the line,
553                         // using getCharBoundaries to rotate it around its center point
554                         var chars:Array = a.split('');
555                         for (var i:int = 0; i < chars.length; i++) {
556                                 var rect:Rectangle = tf.getCharBoundaries(i);
557                                 if (rect) {
558                                         s.addChild(rotatedLetter(chars[i],
559                                                                                          tStart + offsetSign*(rect.left+rect.width/2)/pathlength,
560                                                                                          rect.width, tf.height, angleOffset, textOffset));
561                                 }
562                         }
563                 }
564
565                 private function rotatedLetter(char:String, t:Number, w:Number, h:Number, a:Number, o:Number):TextField {
566                         var tf:TextField = new TextField();
567             tf.mouseEnabled = false;
568             tf.mouseWheelEnabled = false;
569                         tf.defaultTextFormat = nameformat;
570                         tf.embedFonts = true;
571                         tf.text = char;
572                         tf.width = tf.textWidth+4;
573                         tf.height = tf.textHeight+4;
574
575                         var p:Array=pointAt(t);
576                         var matrix:Matrix = new Matrix();
577                         matrix.translate(-w/2, -h/2-o);
578                         // ** add (say) -4 to the height to move it up by 4px
579                         matrix.rotate(p[2]+a);
580                         matrix.translate(p[0], p[1]);
581                         tf.transform.matrix = matrix;
582                         return tf;
583                 }
584                 
585                 public function getNodeAt(x:Number, y:Number):Node {
586                         var way:Way=entity as Way;
587                         for (var i:uint = 0; i < way.length; i++) {
588                 var node:Node = way.getNode(i);
589                 var nodeX:Number = paint.map.lon2coord(node.lon);
590                 var nodeY:Number = paint.map.latp2coord(node.latp);
591                 if ( nodeX >= x-NODESIZE && nodeX <= x+NODESIZE &&
592                      nodeY >= y-NODESIZE && nodeY <= y+NODESIZE )
593                     return node;
594             }
595             return null;
596                 }
597
598                 // ------------------------------------------------------------------------------------------
599                 /* Interaction */
600         // TODO: can this be sped up? Hit testing for long ways (that go off the screen) seems to be very slow. */
601                 public function hitTest(x:Number, y:Number):Way {
602                         if (hitzone.hitTestPoint(x,y,true)) { return entity as Way; }
603                         return null;
604                 }
605         }
606 }