move "show lat/long" from a keypress to an option. Slightly keen that P2 should not...
[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                 // ------------------------------------------------------------------------------------------
391                 // Redraw all items, zoom in and out
392                 
393                 public function updateEntityUIs(redraw:Boolean,remove:Boolean):void {
394                         paint.updateEntityUIs(connection.getObjectsByBbox(edge_l, edge_r, edge_t, edge_b), redraw, remove);
395                         for each (var v:VectorLayer in vectorlayers) {
396                                 v.paint.updateEntityUIs(v.getObjectsByBbox(edge_l, edge_r, edge_t, edge_b), redraw, remove);
397                         }
398                 }
399                 /** Redraw everything, including in every vector layer. */
400                 public function redraw():void {
401                         paint.redraw();
402                         for each (var v:VectorLayer in vectorlayers) { v.paint.redraw(); }
403                 }
404                 /** Redraw POI's, including in every vector layer. */
405                 public function redrawPOIs():void { 
406                         paint.redrawPOIs();
407                         for each (var v:VectorLayer in vectorlayers) { v.paint.redrawPOIs(); }
408                 }
409                 
410                 /** Increase scale. */
411                 public function zoomIn():void {
412                         if (scale==MAXSCALE) { return; }
413                         changeScale(scale+1);
414                 }
415
416                 /** Decrease scale. */
417                 public function zoomOut():void {
418                         if (scale==MINSCALE) { return; }
419                         changeScale(scale-1);
420                 }
421
422                 private function changeScale(newscale:uint):void {
423                         scale=newscale;
424                         this.dispatchEvent(new MapEvent(MapEvent.SCALE, {scale:scale}));
425                         scalefactor=MASTERSCALE/Math.pow(2,13-scale);
426                         updateCoordsFromLatLon((edge_t+edge_b)/2,(edge_l+edge_r)/2);    // recentre
427                         tileset.changeScale(scale);
428                         updateEntityUIs(true,true);
429                         download();
430                 }
431
432                 /** Switch to new MapCSS. */
433                 public function setStyle(url:String):void {
434                         styleurl=url;
435                         if (paint) { 
436                                 paint.ruleset=new RuleSet(MINSCALE,MAXSCALE,redraw,redrawPOIs);
437                                 paint.ruleset.loadFromCSS(url);
438                         }
439         }
440
441                 /** Select a new background imagery. */
442                 public function setBackground(bg:Object):void {
443                         tileparams=bg;
444                         if (tileset) { tileset.init(bg, bg.url!=''); }
445                 }
446
447                 /** Set background dimming on/off. */
448                 public function setDimming(dim:Boolean):void {
449                         if (tileset) { tileset.setDimming(dim); }
450                 }
451                 
452                 /** Return background dimming. */
453                 public function getDimming():Boolean {
454                         if (tileset) { return tileset.getDimming(); }
455                         return true;
456                 }
457
458                 /** Set background sharpening on/off. */
459                 public function setSharpen(sharpen:Boolean):void {
460                         if (tileset) { tileset.setSharpen(sharpen); }
461                 }
462                 /** Return background sharpening. */
463                 public function getSharpen():Boolean {
464                         if (tileset) { return tileset.getSharpen(); }
465                         return false;
466                 }
467
468                 // ------------------------------------------------------------------------------------------
469                 // Export (experimental)
470                 // ** just a bit of fun for now!
471                 // really needs to take a bbox, and make sure that the image is correctly cropped/resized 
472                 // to that area (will probably require creating a new DisplayObject with a different origin
473                 // and mask)
474 /*              
475                 public function export():void {
476                         trace("size is "+this.width+","+this.height);
477                         var jpgSource:BitmapData = new BitmapData(800,800); // (this.width, this.height);
478                         jpgSource.draw(this);
479                         var jpgEncoder:JPGEncoder = new JPGEncoder(85);
480                         var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
481                         var fileRef:FileReference = new FileReference();
482 //                      fileRef.save(jpgStream,'map.jpeg');
483                 }
484
485 */
486
487                 // ==========================================================================================
488                 // Events
489                 
490                 // ------------------------------------------------------------------------------------------
491                 // Mouse events
492                 
493                 /** Should map be allowed to pan? */
494                 public function set draggable(draggable:Boolean):void {
495                         _draggable=draggable;
496                         dragstate=NOT_DRAGGING;
497                 }
498
499                 /** Prepare for being dragged by recording start time and location of mouse. */
500                 public function mouseDownHandler(event:MouseEvent):void {
501                         if (!_draggable) { return; }
502                         dragstate=NOT_MOVED;
503                         lastxmouse=stage.mouseX; downX=stage.mouseX;
504                         lastymouse=stage.mouseY; downY=stage.mouseY;
505                         downTime=new Date().getTime();
506                 }
507         
508                 /** Respond to mouse up by possibly moving map. */
509                 public function mouseUpHandler(event:MouseEvent=null):void {
510                         if (dragstate==DRAGGING) { moveMap(x,y); }
511                         dragstate=NOT_DRAGGING;
512                 }
513         
514                 /** Respond to mouse movement, dragging the map if tolerance threshold met. */
515                 public function mouseMoveHandler(event:MouseEvent):void {
516                         if (!_draggable) { return; }
517                         if (dragstate==NOT_DRAGGING) { 
518                            this.dispatchEvent(new MapEvent(MapEvent.MOUSE_MOVE, { x: coord2lon(mouseX), y: coord2lat(mouseY) }));
519                return; 
520             }
521                         
522                         if (dragstate==NOT_MOVED) {
523                                 if (new Date().getTime()-downTime<300) {
524                                         if (Math.abs(downX-stage.mouseX)<=TOLERANCE   && Math.abs(downY-stage.mouseY)<=TOLERANCE  ) return;
525                                 } else {
526                                         if (Math.abs(downX-stage.mouseX)<=TOLERANCE/2 && Math.abs(downY-stage.mouseY)<=TOLERANCE/2) return;
527                                 }
528                                 dragstate=DRAGGING;
529                         }
530                         
531                         setScrollRectXY(getX()+stage.mouseX-lastxmouse,getY()+stage.mouseY-lastymouse);
532                         lastxmouse=stage.mouseX; lastymouse=stage.mouseY;
533                         setCentre();
534                 }
535         
536                 // ------------------------------------------------------------------------------------------
537                 // Do every frame
538
539                 private function everyFrame(event:Event):void {
540                         if (tileset) { tileset.serviceQueue(); }
541                 }
542
543                 // ------------------------------------------------------------------------------------------
544                 // Miscellaneous events
545                 
546                 /** Respond to cursor movements and zoom in/out.*/
547                 public function keyUpHandler(event:KeyboardEvent):void {
548                         if (event.target is TextField) return;                          // not meant for us
549                         switch (event.keyCode) {
550                                 case Keyboard.PAGE_UP:  zoomIn(); break;                 // Page Up - zoom in
551                                 case Keyboard.PAGE_DOWN:        zoomOut(); break;            // Page Down - zoom out
552                                 case Keyboard.LEFT:     moveMap(mapwidth/2,0); break;        // left cursor
553                                 case Keyboard.UP:       moveMap(0,mapheight/2); break;           // up cursor
554                                 case Keyboard.RIGHT:    moveMap(-mapwidth/2,0); break;   // right cursor
555                                 case Keyboard.DOWN:     moveMap(0,-mapheight/2); break;      // down cursor
556                         }
557                 }
558
559                 // ------------------------------------------------------------------------------------------
560                 // Debugging
561                 
562                 public function clearDebug():void {
563                         if (!Globals.vars.hasOwnProperty('debug')) return;
564                         Globals.vars.debug.text='';
565                 }
566                         
567                 public function addDebug(text:String):void {
568                         trace(text);
569                         if (!Globals.vars.hasOwnProperty('debug')) return;
570                         if (!Globals.vars.debug.visible) return;
571                         Globals.vars.debug.appendText(text+"\n");
572                         Globals.vars.debug.scrollV=Globals.vars.debug.maxScrollV;
573                 }
574
575         }
576 }