AltosSiteMap: ensure buffer around active tile
authorAnthony Towns <aj@erisian.com.au>
Sun, 21 Nov 2010 07:39:50 +0000 (17:39 +1000)
committerAnthony Towns <aj@erisian.com.au>
Sun, 21 Nov 2010 07:39:50 +0000 (17:39 +1000)
ao-tools/altosui/AltosSiteMap.java
ao-tools/altosui/AltosSiteMapTile.java

index b2d79043f45aff10c23dc7a53551f03ad2e7442d..802eb68ce6c5f5d4a5a524c998aaedafc30d09d6 100644 (file)
@@ -260,29 +260,57 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
                                if (0 <= ref.y && ref.y < px_size)
                                        in_any = true;
                }
-               if (!in_any) {
-                       final AltosSiteMapTile tile = new AltosSiteMapTile(px_size);
-                       final Point offset = tileOffset(pt);
-                       mapTiles.put(offset, tile);
 
+               Point offset = tileOffset(pt);
+               if (!in_any) {
                        Point2D.Double ref, lref;
                        ref = translatePoint(pt, tileCoordOffset(offset));
                        lref = translatePoint(last_pt, tileCoordOffset(offset));
-                       tile.show(state, crc_errors, lref, ref);
 
+                       AltosSiteMapTile tile = createTile(offset);
+                       tile.show(state, crc_errors, lref, ref);
                        initMap(tile, offset);
+                       finishTileLater(tile, offset);
+               }
 
-                       SwingUtilities.invokeLater( new Runnable() {
-                               public void run() {
-                                       addTileAt(tile, offset);
-                                       setViewportView(comp);
-                               }
-                       } );
+               if (offset != tileOffset(last_pt)) {
+                       ensureTilesAround(offset);
                }
+
                last_pt = pt;
                last_state = state.state;
        }
 
+       private AltosSiteMapTile createTile(final Point offset) {
+               final AltosSiteMapTile tile = new AltosSiteMapTile(px_size);
+               mapTiles.put(offset, tile);
+               return tile;
+       }
+       private void finishTileLater(final AltosSiteMapTile tile,
+                                    final Point offset)
+       {
+               SwingUtilities.invokeLater( new Runnable() {
+                       public void run() {
+                               addTileAt(tile, offset);
+                               tile.setScrollable();
+                       }
+               } );
+       }
+
+       private void ensureTilesAround(Point base_offset) {
+               for (int x = -1; x <= 1; x++) {
+                       for (int y = -1; y <= 1; y++) {
+                               Point offset = new Point(base_offset.x + x, base_offset.y + y);
+                               if (mapTiles.containsKey(offset))
+                                       continue;
+                               AltosSiteMapTile tile = createTile(offset);
+                               initMap(tile, offset);
+                               finishTileLater(tile, offset);
+                       }
+               }
+       }
+
+
        private void addTileAt(AltosSiteMapTile tile, Point offset) {
                if (Math.abs(offset.x) >= MAX_TILE_DELTA ||
                                Math.abs(offset.y) >= MAX_TILE_DELTA)
@@ -328,10 +356,11 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
 
                for (int x = -1; x <= 1; x++) {
                        for (int y = -1; y <= 1; y++) {
-                               AltosSiteMapTile t = new AltosSiteMapTile(px_size);
                                Point offset = new Point(x, y);
+                               AltosSiteMapTile t = new AltosSiteMapTile(px_size);
                                mapTiles.put(offset, t);
                                addTileAt(t, offset);
+                               t.setScrollable();
                        }
                }
                setViewportView(comp);
index ea8c8bd9eafb302d272020751e3ae4fc76d29dff..e09429862d505dd92a4b3111a5f6a7abc5586671 100644 (file)
@@ -52,10 +52,17 @@ public class AltosSiteMapTile extends JLayeredPane {
                Color.BLACK   // landed
        };
 
-       boolean drawn_landed_circle = false;
-       boolean drawn_boost_circle = false;
-       public void show(AltosState state, int crc_errors,
-                        Point2D.Double last_pt, Point2D.Double pt)
+       private boolean drawn_landed_circle = false;
+       private boolean drawn_boost_circle = false;
+       private boolean scrollable = false;
+       public synchronized void setScrollable() {
+               scrollable = true;
+       }
+       public synchronized boolean isScrollable() {
+               return scrollable;
+       }
+       public synchronized void show(AltosState state, int crc_errors,
+                                     Point2D.Double last_pt, Point2D.Double pt)
        {
                if (0 <= state.state && state.state < stateColors.length) {
                        g2d.setColor(stateColors[state.state]);
@@ -63,7 +70,7 @@ public class AltosSiteMapTile extends JLayeredPane {
                g2d.draw(new Line2D.Double(last_pt, pt));
 
                int px_size = getWidth();
-               if (0 <= pt.x && pt.x < px_size) {
+               if (isScrollable() && 0 <= pt.x && pt.x < px_size) {
                        if (0 <= pt.y && pt.y < px_size) {
                                int dx = 500, dy = 250;
                                if (state.state > 2) {