permit GPS tracks to be cleared from the usual menu. Fixes #3492
[potlatch2.git] / net / systemeD / halcyon / Map.as
1 package net.systemeD.halcyon {
2
3         import flash.display.Loader;
4         import flash.display.Sprite;
5         import flash.events.*;
6         import flash.external.ExternalInterface;
7         import flash.geom.Rectangle;
8         import flash.net.*;
9         import flash.text.Font;
10         import flash.text.TextField;
11         import flash.ui.Keyboard;
12         
13         import net.systemeD.halcyon.connection.*;
14         import net.systemeD.halcyon.styleparser.*;
15
16 //      for experimental export function:
17 //      import flash.net.FileReference;
18 //      import com.adobe.images.JPGEncoder;
19
20     /** The representation of part of the map on the screen, including information about coordinates, background imagery, paint properties etc. */
21     public class Map extends Sprite {
22
23                 /** master map scale - how many Flash pixels in 1 degree longitude (for Landsat, 5120) */
24                 public const MASTERSCALE:Number=5825.4222222222; 
25                                                                                                 
26                 /** don't zoom out past this */
27                 public const MINSCALE:uint=13; 
28                 /** don't zoom in past this */
29                 public const MAXSCALE:uint=23; 
30
31                 /** sprite for ways and (POI/tagged) nodes in core layer */
32                 public var paint:MapPaint;                                               
33                 /** sprite for vector background layers */
34                 public var vectorbg:Sprite;
35
36                 /** map scale */
37                 public var scale:uint=14;                                                
38                 /** current scaling factor for lon/latp */
39                 public var scalefactor:Number=MASTERSCALE;
40                 public var bigedge_l:Number= 999999;                    // area of largest whichways
41                 public var bigedge_r:Number=-999999;                    //  |
42                 public var bigedge_b:Number= 999999;                    //  |
43                 public var bigedge_t:Number=-999999;                    //  |
44
45                 public var edge_l:Number;                                               // current bounding box
46                 public var edge_r:Number;                                               //  |
47                 public var edge_t:Number;                                               //  |
48                 public var edge_b:Number;                                               //  |
49                 public var centre_lat:Number;                                   // centre lat/lon
50                 public var centre_lon:Number;                                   //  |
51
52                 /** urllon-xradius/masterscale; */ 
53                 public var baselon:Number;
54                 /** lat2lat2p(urllat)+yradius/masterscale; */
55                 public var basey:Number; 
56                 /** width (Flash pixels) */
57                 public var mapwidth:uint; 
58                 /** height (Flash pixels) */
59                 public var mapheight:uint; 
60
61                 /** Is the map being panned */
62                 public var dragstate:uint=NOT_DRAGGING;                 // dragging map (panning)
63                 /** Can the map be panned */
64                 private var _draggable:Boolean=true;                    //  |
65                 private var lastxmouse:Number;                                  //  |
66                 private var lastymouse:Number;                                  //  |
67                 private var downX:Number;                                               //  |
68                 private var downY:Number;                                               //  |
69                 private var downTime:Number;                                    //  |
70                 public const NOT_DRAGGING:uint=0;                               //  |
71                 public const NOT_MOVED:uint=1;                                  //  |
72                 public const DRAGGING:uint=2;                                   //  |
73                 /** How far the map can be dragged without actually triggering a pan. */
74                 public const TOLERANCE:uint=7;                                  //  |
75                 
76                 /** object containing HTML page parameters */
77                 public var initparams:Object; 
78
79                 /** reference to backdrop sprite */
80                 public var backdrop:Object; 
81                 /** background tile object */
82                 public var tileset:TileSet; 
83                 /** background tile URL, name and scheme */
84                 private var tileparams:Object={ url:'' }; 
85                 /** internal style URL */
86                 private var styleurl:String=''; 
87                 /** show all objects, even if unstyled? */
88                 public var showall:Boolean=true; 
89                 
90                 /** server connection */
91                 public var connection:Connection; 
92                 /** VectorLayer objects */
93                 public var vectorlayers:Object={};  
94                 
95                 // ------------------------------------------------------------------------------------------
96                 /** Map constructor function */
97         public function Map(initparams:Object) {
98
99                         this.initparams=initparams;
100                         connection = Connection.getConnection(initparams);
101             connection.addEventListener(Connection.NEW_WAY, newWayCreated);
102             connection.addEventListener(Connection.NEW_POI, newPOICreated);
103             connection.addEventListener(Connection.WAY_RENUMBERED, wayRenumbered);
104             connection.addEventListener(Connection.NODE_RENUMBERED, nodeRenumbered);
105                         gotEnvironment(null);
106
107                         addEventListener(Event.ENTER_FRAME, everyFrame);
108                         scrollRect=new Rectangle(0,0,800,600);
109         }
110
111                 public function gotEnvironment(r:Object):void {
112                         var loader:Loader = new Loader();
113                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, gotFont);
114                         loader.load(new URLRequest("FontLibrary.swf"));
115                 }
116                 
117                 public function gotFont(r:Event):void {
118                         var FontLibrary:Class = r.target.applicationDomain.getDefinition("FontLibrary") as Class;
119                         Font.registerFont(FontLibrary.DejaVu);
120
121                         if (initparams['lat'] != null) {
122                                 // parameters sent from HTML
123                                 init(initparams['lat'],
124                                          initparams['lon'],
125                                          initparams['zoom']);
126
127                         } else {
128                                 // somewhere innocuous
129                                 init(53.09465,-2.56495,17);
130                         }
131                 }
132
133                 // ------------------------------------------------------------------------------------------
134                 /** Initialise map at a given lat/lon */
135         public function init(startlat:Number, startlon:Number, startscale:uint=0):void {
136                         while (numChildren) { removeChildAt(0); }
137
138                         tileset=new TileSet(this);                                      // 0 - 900913 background
139                         if (initparams['tileblocks']) {                         //   | option to block dodgy tile sources
140                                 tileset.blocks=initparams['tileblocks'];//   |
141                         }                                                                                       //   |
142                         addChild(tileset);                                                      //   |
143                         tileset.init(tileparams, false, 
144                                      initparams['background_dim']    ==null ? true  : initparams['background_dim'],
145                                      initparams['background_sharpen']==null ? false : initparams['background_sharpen']);
146
147                         vectorbg = new Sprite();                                        // 1 - vector background layers
148                         addChild(vectorbg);                                                     //   |
149
150                         paint = new MapPaint(this,-5,5);                        // 2 - core paint object
151                         addChild(paint);                                                        //   |
152                         paint.isBackground=false;                                       //   |
153
154                         if (styleurl) {                                                         // if we've only just set up paint, then setStyle won't have created the RuleSet
155                                 paint.ruleset=new RuleSet(MINSCALE,MAXSCALE,redraw,redrawPOIs);
156                                 paint.ruleset.loadFromCSS(styleurl);
157                         }
158                         if (startscale>0) {
159                                 scale=startscale;
160                                 this.dispatchEvent(new MapEvent(MapEvent.SCALE, {scale:scale}));
161                         }
162
163                         scalefactor=MASTERSCALE/Math.pow(2,13-scale);
164                         baselon    =startlon          -(mapwidth /2)/scalefactor;
165                         basey      =lat2latp(startlat)+(mapheight/2)/scalefactor;
166                         updateCoords(0,0);
167             this.dispatchEvent(new Event(MapEvent.INITIALISED));
168                         download();
169
170             if (ExternalInterface.available) {
171               ExternalInterface.addCallback("setPosition", function (lat:Number,lon:Number,zoom:uint):void {
172                   updateCoordsFromLatLon(lat, lon);
173                   changeScale(zoom);
174               });
175             }
176         }
177
178                 // ------------------------------------------------------------------------------------------
179                 /** Recalculate co-ordinates from new Flash origin */
180
181                 public function updateCoords(tx:Number,ty:Number):void {
182                         setScrollRectXY(tx,ty);
183
184                         edge_t=coord2lat(-ty          );
185                         edge_b=coord2lat(-ty+mapheight);
186                         edge_l=coord2lon(-tx          );
187                         edge_r=coord2lon(-tx+mapwidth );
188                         setCentre();
189
190                         tileset.update();
191                 }
192                 
193                 /** Move the map to centre on a given latitude/longitude. */
194                 public function updateCoordsFromLatLon(lat:Number,lon:Number):void {
195                         var cy:Number=-(lat2coord(lat)-mapheight/2);
196                         var cx:Number=-(lon2coord(lon)-mapwidth/2);
197                         updateCoords(cx,cy);
198                 }
199                 
200                 private function setScrollRectXY(tx:Number,ty:Number):void {
201                         var w:Number=scrollRect.width;
202                         var h:Number=scrollRect.height;
203                         scrollRect=new Rectangle(-tx,-ty,w,h);
204                 }
205                 private function setScrollRectSize(width:Number,height:Number):void {
206                         var sx:Number=scrollRect.x ? scrollRect.x : 0;
207                         var sy:Number=scrollRect.y ? scrollRect.y : 0;
208                         scrollRect=new Rectangle(sx,sy,width,height);
209                 }
210                 
211                 private function getX():Number { return -scrollRect.x; }
212                 private function getY():Number { return -scrollRect.y; }
213                 
214                 private function setCentre():void {
215                         centre_lat=coord2lat(-getY()+mapheight/2);
216                         centre_lon=coord2lon(-getX()+mapwidth/2);
217                         this.dispatchEvent(new MapEvent(MapEvent.MOVE, {lat:centre_lat, lon:centre_lon, scale:scale, minlon:edge_l, maxlon:edge_r, minlat:edge_b, maxlat:edge_t}));
218                 }
219                 
220                 /** Sets the offset between the background imagery and the map. */
221                 public function nudgeBackground(x:Number,y:Number):void {
222                         this.dispatchEvent(new MapEvent(MapEvent.NUDGE_BACKGROUND, { x: x, y: y }));
223                 }
224
225                 private function moveMap(dx:Number,dy:Number):void {
226                         updateCoords(getX()+dx,getY()+dy);
227                         updateEntityUIs(false, false);
228                         download();
229                 }
230                 
231                 /** Recentre map at given lat/lon, updating the UI and downloading entities. */
232                 public function moveMapFromLatLon(lat:Number,lon:Number):void {
233                         updateCoordsFromLatLon(lat,lon);
234                         updateEntityUIs(false,false);
235                         download();
236                 }
237                 
238                 /** Recentre map at given lat/lon, if that point is currently outside the visible area. */
239                 public function scrollIfNeeded(lat:Number,lon:Number): void{
240             if (lat> edge_t || lat < edge_b || lon < edge_l || lon > edge_r) {
241                 moveMapFromLatLon(lat, lon);
242             }
243
244                 }
245
246                 // Co-ordinate conversion functions
247
248                 public function latp2coord(a:Number):Number     { return -(a-basey)*scalefactor; }
249                 public function coord2latp(a:Number):Number     { return a/-scalefactor+basey; }
250                 public function lon2coord(a:Number):Number      { return (a-baselon)*scalefactor; }
251                 public function coord2lon(a:Number):Number      { return a/scalefactor+baselon; }
252
253                 public function latp2lat(a:Number):Number       { return 180/Math.PI * (2 * Math.atan(Math.exp(a*Math.PI/180)) - Math.PI/2); }
254                 public function lat2latp(a:Number):Number       { return 180/Math.PI * Math.log(Math.tan(Math.PI/4+a*(Math.PI/180)/2)); }
255
256                 public function lat2coord(a:Number):Number      { return -(lat2latp(a)-basey)*scalefactor; }
257                 public function coord2lat(a:Number):Number      { return latp2lat(a/-scalefactor+basey); }
258
259
260                 // ------------------------------------------------------------------------------------------
261                 /** Resize map size based on current stage and height */
262
263                 public function updateSize(w:uint, h:uint):void {
264                         mapwidth = w; centre_lon=coord2lon(-getX()+w/2);
265                         mapheight= h; centre_lat=coord2lat(-getY()+h/2);
266                         setScrollRectSize(w,h);
267
268                         this.dispatchEvent(new MapEvent(MapEvent.RESIZE, {width:w, height:h}));
269                         
270             if ( backdrop != null ) {
271                 backdrop.width=mapwidth;
272                 backdrop.height=mapheight;
273             }
274             if ( mask != null ) {
275                 mask.width=mapwidth;
276                 mask.height=mapheight;
277             }
278                 }
279
280         /** Download map data. Data is downloaded for the connection and the vector layers, where supported.
281         * The bounding box for the download is taken from the current map edges.
282         */
283                 public function download():void {
284                         this.dispatchEvent(new MapEvent(MapEvent.DOWNLOAD, {minlon:edge_l, maxlon:edge_r, maxlat:edge_t, minlat:edge_b} ));
285                         
286                         if (edge_l>=bigedge_l && edge_r<=bigedge_r &&
287                                 edge_b>=bigedge_b && edge_t<=bigedge_t) { return; }     // we have already loaded this area, so ignore
288                         bigedge_l=edge_l; bigedge_r=edge_r;
289                         bigedge_b=edge_b; bigedge_t=edge_t;
290                         if (connection.waycount>1000) {
291                                 connection.purgeOutside(edge_l,edge_r,edge_t,edge_b);
292                         }
293                         connection.loadBbox(edge_l,edge_r,edge_t,edge_b);
294
295             // Do the same for vector layers
296             for each (var layer:VectorLayer in vectorlayers) {
297               layer.loadBbox(edge_l,edge_r,edge_t,edge_b);
298             }
299                 }
300
301         private function newWayCreated(event:EntityEvent):void {
302             var way:Way = event.entity as Way;
303                         if (!way.loaded || !way.within(edge_l,edge_r,edge_t,edge_b)) { return; }
304                         paint.createWayUI(way);
305         }
306
307         private function newPOICreated(event:EntityEvent):void {
308             var node:Node = event.entity as Node;
309                         if (!node.within(edge_l,edge_r,edge_t,edge_b)) { return; }
310                         paint.createNodeUI(node);
311         }
312
313                 private function wayRenumbered(event:EntityRenumberedEvent):void {
314             var way:Way = event.entity as Way;
315                         paint.renumberWayUI(way,event.oldID);
316                 }
317
318                 private function nodeRenumbered(event:EntityRenumberedEvent):void {
319             var node:Node = event.entity as Node;
320                         paint.renumberNodeUI(node,event.oldID);
321                 }
322
323         /** Visually mark an entity as highlighted. */
324         public function setHighlight(entity:Entity, settings:Object):void {
325                         if      ( entity is Way  && paint.wayuis[entity.id] ) { paint.wayuis[entity.id].setHighlight(settings);  }
326                         else if ( entity is Node && paint.nodeuis[entity.id]) { paint.nodeuis[entity.id].setHighlight(settings); }
327         }
328
329         public function setHighlightOnNodes(way:Way, settings:Object):void {
330                         if (paint.wayuis[way.id]) paint.wayuis[way.id].setHighlightOnNodes(settings);
331         }
332
333                 public function protectWay(way:Way):void {
334                         if (paint.wayuis[way.id]) paint.wayuis[way.id].protectSprites();
335                 }
336
337                 public function unprotectWay(way:Way):void {
338                         if (paint.wayuis[way.id]) paint.wayuis[way.id].unprotectSprites();
339                 }
340                 
341                 public function limitWayDrawing(way:Way,except:Number=NaN,only:Number=NaN):void {
342                         if (!paint.wayuis[way.id]) return;
343                         paint.wayuis[way.id].drawExcept=except;
344                         paint.wayuis[way.id].drawOnly  =only;
345                         paint.wayuis[way.id].redraw();
346                 }
347
348                 /** Protect Entities and EntityUIs against purging. This prevents the currently selected items
349                    from being purged even though they're off-screen. */
350
351                 public function setPurgable(entities:Array, purgable:Boolean):void {
352                         for each (var entity:Entity in entities) {
353                                 entity.locked=!purgable;
354                                 if ( entity is Way  ) {
355                                         var way:Way=entity as Way;
356                                         if (paint.wayuis[way.id]) { paint.wayuis[way.id].purgable=purgable; }
357                                         for (var i:uint=0; i<way.length; i++) {
358                                                 var node:Node=way.getNode(i)
359                                                 node.locked=!purgable;
360                                                 if (paint.nodeuis[node.id]) { paint.nodeuis[node.id].purgable=purgable; }
361                                         }
362                                 } else if ( entity is Node && paint.nodeuis[entity.id]) { 
363                                         paint.nodeuis[entity.id].purgable=purgable;
364                                 }
365                         }
366                 }
367
368         // Handle mouse events on ways/nodes
369         private var mapController:MapController = null;
370
371         /** Assign map controller. */
372         public function setController(controller:MapController):void {
373             this.mapController = controller;
374         }
375
376         public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
377             if ( mapController != null )
378                 mapController.entityMouseEvent(event, entity);
379                                 
380         }
381
382                 // ------------------------------------------------------------------------------------------
383                 // Add vector layer
384                 
385                 public function addVectorLayer(layer:VectorLayer):void {
386                         vectorlayers[layer.name]=layer;
387                         vectorbg.addChild(layer.paint);
388                 }
389                 
390                 public function removeVectorLayer(layer:VectorLayer):void {
391                         if (!layer) return;
392                         layer.blank();
393                         vectorbg.removeChild(layer.paint);
394                         delete vectorlayers[layer.name];
395                 }
396                 
397                 public function findVectorLayer(name:String):VectorLayer {
398                         for each (var layer:VectorLayer in vectorlayers) {
399                                 if (layer.name==name) { return layer; }
400                         }
401                         return null;
402                 }
403
404                 // ------------------------------------------------------------------------------------------
405                 // Redraw all items, zoom in and out
406                 
407                 public function updateEntityUIs(redraw:Boolean,remove:Boolean):void {
408                         paint.updateEntityUIs(connection.getObjectsByBbox(edge_l, edge_r, edge_t, edge_b), redraw, remove);
409                         for each (var v:VectorLayer in vectorlayers) {
410                                 v.paint.updateEntityUIs(v.getObjectsByBbox(edge_l, edge_r, edge_t, edge_b), redraw, remove);
411                         }
412                 }
413                 /** Redraw everything, including in every vector layer. */
414                 public function redraw():void {
415                         paint.redraw();
416                         for each (var v:VectorLayer in vectorlayers) { v.paint.redraw(); }
417                 }
418                 /** Redraw POI's, including in every vector layer. */
419                 public function redrawPOIs():void { 
420                         paint.redrawPOIs();
421                         for each (var v:VectorLayer in vectorlayers) { v.paint.redrawPOIs(); }
422                 }
423                 
424                 /** Increase scale. */
425                 public function zoomIn():void {
426                         if (scale==MAXSCALE) { return; }
427                         changeScale(scale+1);
428                 }
429
430                 /** Decrease scale. */
431                 public function zoomOut():void {
432                         if (scale==MINSCALE) { return; }
433                         changeScale(scale-1);
434                 }
435
436                 private function changeScale(newscale:uint):void {
437                         scale=newscale;
438                         this.dispatchEvent(new MapEvent(MapEvent.SCALE, {scale:scale}));
439                         scalefactor=MASTERSCALE/Math.pow(2,13-scale);
440                         updateCoordsFromLatLon((edge_t+edge_b)/2,(edge_l+edge_r)/2);    // recentre
441                         tileset.changeScale(scale);
442                         updateEntityUIs(true,true);
443                         download();
444                 }
445
446                 /** Switch to new MapCSS. */
447                 public function setStyle(url:String):void {
448                         styleurl=url;
449                         if (paint) { 
450                                 paint.ruleset=new RuleSet(MINSCALE,MAXSCALE,redraw,redrawPOIs);
451                                 paint.ruleset.loadFromCSS(url);
452                         }
453         }
454
455                 /** Select a new background imagery. */
456                 public function setBackground(bg:Object):void {
457                         tileparams=bg;
458                         if (tileset) { tileset.init(bg, bg.url!=''); }
459                 }
460
461                 /** Set background dimming on/off. */
462                 public function setDimming(dim:Boolean):void {
463                         if (tileset) { tileset.setDimming(dim); }
464                 }
465                 
466                 /** Return background dimming. */
467                 public function getDimming():Boolean {
468                         if (tileset) { return tileset.getDimming(); }
469                         return true;
470                 }
471
472                 /** Set background sharpening on/off. */
473                 public function setSharpen(sharpen:Boolean):void {
474                         if (tileset) { tileset.setSharpen(sharpen); }
475                 }
476                 /** Return background sharpening. */
477                 public function getSharpen():Boolean {
478                         if (tileset) { return tileset.getSharpen(); }
479                         return false;
480                 }
481
482                 // ------------------------------------------------------------------------------------------
483                 // Export (experimental)
484                 // ** just a bit of fun for now!
485                 // really needs to take a bbox, and make sure that the image is correctly cropped/resized 
486                 // to that area (will probably require creating a new DisplayObject with a different origin
487                 // and mask)
488 /*              
489                 public function export():void {
490                         trace("size is "+this.width+","+this.height);
491                         var jpgSource:BitmapData = new BitmapData(800,800); // (this.width, this.height);
492                         jpgSource.draw(this);
493                         var jpgEncoder:JPGEncoder = new JPGEncoder(85);
494                         var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
495                         var fileRef:FileReference = new FileReference();
496 //                      fileRef.save(jpgStream,'map.jpeg');
497                 }
498
499 */
500
501                 // ==========================================================================================
502                 // Events
503                 
504                 // ------------------------------------------------------------------------------------------
505                 // Mouse events
506                 
507                 /** Should map be allowed to pan? */
508                 public function set draggable(draggable:Boolean):void {
509                         _draggable=draggable;
510                         dragstate=NOT_DRAGGING;
511                 }
512
513                 /** Prepare for being dragged by recording start time and location of mouse. */
514                 public function mouseDownHandler(event:MouseEvent):void {
515                         if (!_draggable) { return; }
516                         dragstate=NOT_MOVED;
517                         lastxmouse=stage.mouseX; downX=stage.mouseX;
518                         lastymouse=stage.mouseY; downY=stage.mouseY;
519                         downTime=new Date().getTime();
520                 }
521         
522                 /** Respond to mouse up by possibly moving map. */
523                 public function mouseUpHandler(event:MouseEvent=null):void {
524                         if (dragstate==DRAGGING) { moveMap(x,y); }
525                         dragstate=NOT_DRAGGING;
526                 }
527         
528                 /** Respond to mouse movement, dragging the map if tolerance threshold met. */
529                 public function mouseMoveHandler(event:MouseEvent):void {
530                         if (!_draggable) { return; }
531                         if (dragstate==NOT_DRAGGING) { 
532                            this.dispatchEvent(new MapEvent(MapEvent.MOUSE_MOVE, { x: coord2lon(mouseX), y: coord2lat(mouseY) }));
533                return; 
534             }
535                         
536                         if (dragstate==NOT_MOVED) {
537                                 if (new Date().getTime()-downTime<300) {
538                                         if (Math.abs(downX-stage.mouseX)<=TOLERANCE   && Math.abs(downY-stage.mouseY)<=TOLERANCE  ) return;
539                                 } else {
540                                         if (Math.abs(downX-stage.mouseX)<=TOLERANCE/2 && Math.abs(downY-stage.mouseY)<=TOLERANCE/2) return;
541                                 }
542                                 dragstate=DRAGGING;
543                         }
544                         
545                         setScrollRectXY(getX()+stage.mouseX-lastxmouse,getY()+stage.mouseY-lastymouse);
546                         lastxmouse=stage.mouseX; lastymouse=stage.mouseY;
547                         setCentre();
548                 }
549         
550                 // ------------------------------------------------------------------------------------------
551                 // Do every frame
552
553                 private function everyFrame(event:Event):void {
554                         if (tileset) { tileset.serviceQueue(); }
555                 }
556
557                 // ------------------------------------------------------------------------------------------
558                 // Miscellaneous events
559                 
560                 /** Respond to cursor movements and zoom in/out.*/
561                 public function keyUpHandler(event:KeyboardEvent):void {
562                         if (event.target is TextField) return;                          // not meant for us
563                         switch (event.keyCode) {
564                                 case Keyboard.PAGE_UP:  zoomIn(); break;                 // Page Up - zoom in
565                                 case Keyboard.PAGE_DOWN:        zoomOut(); break;            // Page Down - zoom out
566                                 case Keyboard.LEFT:     moveMap(mapwidth/2,0); break;        // left cursor
567                                 case Keyboard.UP:       moveMap(0,mapheight/2); break;           // up cursor
568                                 case Keyboard.RIGHT:    moveMap(-mapwidth/2,0); break;   // right cursor
569                                 case Keyboard.DOWN:     moveMap(0,-mapheight/2); break;      // down cursor
570                         }
571                 }
572
573                 // ------------------------------------------------------------------------------------------
574                 // Debugging
575                 
576                 public function clearDebug():void {
577                         if (!Globals.vars.hasOwnProperty('debug')) return;
578                         Globals.vars.debug.text='';
579                 }
580                         
581                 public function addDebug(text:String):void {
582                         trace(text);
583                         if (!Globals.vars.hasOwnProperty('debug')) return;
584                         if (!Globals.vars.debug.visible) return;
585                         Globals.vars.debug.appendText(text+"\n");
586                         Globals.vars.debug.scrollV=Globals.vars.debug.maxScrollV;
587                 }
588
589         }
590 }