TiledPatternGenerator.java

00001 package edu.stanford.hci.r3.pattern;
00002 
00003 import java.io.File;
00004 import java.util.ArrayList;
00005 import java.util.List;
00006 import java.util.Map;
00007 
00008 import edu.stanford.hci.r3.PaperToolkit;
00009 import edu.stanford.hci.r3.units.Units;
00010 import edu.stanford.hci.r3.util.DebugUtils;
00011 
00028 public class TiledPatternGenerator {
00029 
00034         private static final int BUFFER = 30;
00035 
00039         public static final String DEFAULT_PATTERN_PACKAGE_NAME = "default";
00040 
00044         private Map<String, PatternPackage> availablePackages;
00045 
00049         private int lastDotUsedX = 0;
00050 
00054         private int lastDotUsedY = 0;
00055 
00056         private int maxOfRecentHeightsInDots = 0;
00057 
00061         private int numTimesGetPatternCalled = 0;
00062 
00067         private int patternFileNumber = 0;
00068 
00072         private PatternPackage patternPackage;
00073 
00077         private File patternPath;
00078 
00084         public TiledPatternGenerator() {
00085                 this(PaperToolkit.getPatternPath());
00086         }
00087 
00091         public TiledPatternGenerator(File patternPathLocation) {
00092                 patternPath = patternPathLocation;
00093                 availablePackages = PatternPackage.getAvailablePatternPackages(patternPath);
00094                 setPackage(DEFAULT_PATTERN_PACKAGE_NAME);
00095         }
00096 
00100         public void displayTilingInformation(Units horizontal, Units vertical) {
00101                 long numDotsX = Math.round(horizontal.getValueInPatternDots());
00102                 long numDotsY = Math.round(vertical.getValueInPatternDots());
00103 
00104                 // System.out.println(numDotsX + " " + numDotsY);
00105 
00106                 int numTilesNeededX = 0;
00107                 int numTilesNeededY = 0;
00108 
00109                 int numDotsRemainingX = (int) numDotsX;
00110                 int numDotsRemainingY = (int) numDotsY;
00111 
00112                 final int numPatternColsPerFile = patternPackage.getNumPatternColsPerFile();
00113                 final int numPatternRowsPerFile = patternPackage.getNumPatternRowsPerFile();
00114 
00115                 while (numDotsRemainingX > 0) {
00116                         // use up one tile, and subtract an appropriate number of columns...
00117                         numDotsRemainingX -= numPatternColsPerFile;
00118                         numTilesNeededX++;
00119                 }
00120                 final int numDotsXFromRightMostTiles = numDotsRemainingX + numPatternColsPerFile;
00121 
00122                 while (numDotsRemainingY > 0) {
00123                         // use up one tile, and subtract an appropriate number of rows...
00124                         numDotsRemainingY -= numPatternRowsPerFile;
00125                         numTilesNeededY++;
00126                 }
00127                 final int numDotsYFromBottomMostTiles = numDotsRemainingY + numPatternRowsPerFile;
00128 
00129                 // the tiling is...
00130                 // numTilesNeededX, numTilesNeededY
00131                 // numDotsXFromRightMostTiles, numDotsYFromBottomMostTiles
00132                 System.out.println("Tiling Information (" + horizontal + ", " + vertical + ") {");
00133                 System.out.println("\t" + numTilesNeededX + " Tile(s) in X, with " + numDotsXFromRightMostTiles
00134                                 + " horizontal dots from the rightmost tiles.");
00135                 System.out.println("\t" + numTilesNeededY + " Tile(s) in Y, with " + numDotsYFromBottomMostTiles
00136                                 + " vertical dots from the bottommost tiles.");
00137                 System.out.println("}");
00138         }
00139 
00143         public PatternPackage getCurrentPatternPackage() {
00144                 return patternPackage;
00145         }
00146 
00158         public TiledPattern getPattern(Units width, Units height) {
00159                 DebugUtils.println("getPattern Called " + ++numTimesGetPatternCalled + " times...");
00160 
00161                 final long numDotsX = Math.round(width.getValueInPatternDots());
00162                 final long numDotsY = Math.round(height.getValueInPatternDots());
00163 
00164                 // System.out.println(numDotsX + " " + numDotsY);
00165 
00166                 int numTilesNeededX = 0;
00167                 int numTilesNeededY = 0;
00168 
00169                 int numDotsRemainingX = (int) numDotsX;
00170                 int numDotsRemainingY = (int) numDotsY;
00171 
00172                 final int numPatternColsPerFile = patternPackage.getNumPatternColsPerFile();
00173                 final int numPatternRowsPerFile = patternPackage.getNumPatternRowsPerFile();
00174 
00175                 // figure out how many horizontal dots we need
00176                 while (numDotsRemainingX > 0) {
00177                         // use up one tile, and subtract an appropriate number of columns...
00178                         numDotsRemainingX -= numPatternColsPerFile;
00179                         numTilesNeededX++;
00180                 }
00181                 final int numDotsXFromRightMostTiles = numDotsRemainingX + numPatternColsPerFile;
00182 
00183                 // figure out how many vertical dots we need
00184                 while (numDotsRemainingY > 0) {
00185                         // use up one tile, and subtract an appropriate number of rows...
00186                         numDotsRemainingY -= numPatternRowsPerFile;
00187                         numTilesNeededY++;
00188                 }
00189                 final int numDotsYFromBottomMostTiles = numDotsRemainingY + numPatternRowsPerFile;
00190 
00191                 // if we only need one tile....
00192                 // we would like to stay on the same pattern file if we still have space
00193                 // calculate this from numDotsXFromRightMostTiles and numDotsYFromBottomMostTiles...
00194                 // DebugUtils.println("NumTilesNeeded: " + numTilesNeededX + ", " + numTilesNeededY);
00195                 if (numTilesNeededX == 1 && numTilesNeededY == 1) {
00196                         // DebugUtils.println("DotsNeeded: x: " + numDotsXFromRightMostTiles + ", "
00197                         // + numDotsYFromBottomMostTiles);
00198                         // if we WILL exceed the horizontal bounds....
00199                         if (lastDotUsedX + numDotsXFromRightMostTiles + BUFFER >= numPatternColsPerFile) {
00200 
00201                                 // wrap back to the left side of the page
00202                                 lastDotUsedX = 0;
00203                                 lastDotUsedY += maxOfRecentHeightsInDots + BUFFER;
00204                         }
00205 
00206                         // if we WILL exceed the height bounds...
00207                         if (lastDotUsedY + numDotsYFromBottomMostTiles + BUFFER >= numPatternRowsPerFile) {
00208                                 lastDotUsedY = 0;
00209                                 // next time, get pattern from a new file!
00210                                 patternFileNumber++;
00211                         }
00212                 } else {
00213                         // we need more than one tile...
00214                         // CROSS OUR FINGERS for now. =)
00215                 }
00216 
00217                 // create and return the tiled pattern
00218                 final TiledPattern pattern = new TiledPattern(patternPackage, patternFileNumber, //
00219                                 lastDotUsedX, lastDotUsedY, //
00220                                 numTilesNeededX, numTilesNeededY, //
00221                                 numDotsXFromRightMostTiles, numDotsYFromBottomMostTiles); //
00222                 patternFileNumber = pattern.getLastPatternFileUsed();
00223 
00224                 // try to shift everything right
00225                 // we are still tiling horizontally
00226                 // maintain our tallest Y to date
00227                 lastDotUsedX += numDotsXFromRightMostTiles + BUFFER;
00228                 maxOfRecentHeightsInDots = Math.max(maxOfRecentHeightsInDots, numDotsYFromBottomMostTiles + BUFFER);
00229 
00230                 return pattern;
00231         }
00232 
00237         public PatternPackage getPatternPackageByName(String name) {
00238                 return availablePackages.get(name);
00239         }
00240 
00245         public List<String> listAvailablePatternPackageNames() {
00246                 return new ArrayList<String>(availablePackages.keySet());
00247         }
00248 
00253         public void resetUniquePatternTracker() {
00254                 patternFileNumber = 0;
00255                 lastDotUsedY = 0;
00256                 lastDotUsedX = 0;
00257         }
00258 
00264         private void setPackage(String packageName) {
00265                 PatternPackage pkg = availablePackages.get(packageName);
00266                 if (pkg == null) {
00267                         pkg = new ArrayList<PatternPackage>(availablePackages.values()).get(0);
00268                         System.err.println("Warning: " + packageName
00269                                         + " does not exist. Setting Pattern Package to the first one available (" + pkg.getName()
00270                                         + ").");
00271                 }
00272                 patternPackage = pkg;
00273         }
00274 
00281         public void setPatternFileNumber(int num) {
00282                 resetUniquePatternTracker();
00283                 patternFileNumber = num;
00284         }
00285 
00286 }

Generated on Sat Apr 14 18:21:39 2007 for R3 Paper Toolkit by  doxygen 1.4.7