Parcourir la source

Better distinction between font and cell size

smelc il y a 6 ans
Parent
commit
b0433826c8

+ 2 - 2
squidlib/src/main/java/squidpony/squidgrid/gui/gdx/IPanelBuilder.java

@@ -78,7 +78,7 @@ public interface IPanelBuilder {
 	 * @return Whether there's a font available for a cell of size
 	 * @return Whether there's a font available for a cell of size
 	 *         {@code cellSize}.
 	 *         {@code cellSize}.
 	 */
 	 */
-	boolean hasFontForCellOfSize(int cellSize);
+	boolean acceptsCellOfSize(int cellSize);
 
 
 	/**
 	/**
 	 * @param cellSize
 	 * @param cellSize
@@ -102,7 +102,7 @@ public interface IPanelBuilder {
 	abstract class Skeleton implements IPanelBuilder {
 	abstract class Skeleton implements IPanelBuilder {
 
 
 		@Override
 		@Override
-		public boolean hasFontForCellOfSize(int cellSize) {
+		public boolean acceptsCellOfSize(int cellSize) {
 			return hasFontOfSize(fontSizeForCellSize(cellSize));
 			return hasFontOfSize(fontSizeForCellSize(cellSize));
 		}
 		}
 
 

+ 40 - 20
squidlib/src/main/java/squidpony/squidgrid/gui/gdx/SquidPanelBuilder.java

@@ -21,32 +21,37 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion;
  * 
  * 
  * @author smelC
  * @author smelC
  */
  */
+// FIXME MOVE ME to com.hgames.core
 public abstract class SquidPanelBuilder extends IPanelBuilder.Skeleton {
 public abstract class SquidPanelBuilder extends IPanelBuilder.Skeleton {
 
 
 	public final /* @Nullable */ AssetManager assetManager;
 	public final /* @Nullable */ AssetManager assetManager;
 
 
-	protected final int smallestFont;
-	protected final int largestFont;
+	protected final int smallestCellSize;
+	protected final int largestCellSize;
 
 
 	protected final int fontOffset;
 	protected final int fontOffset;
 
 
 	/**
 	/**
-	 * @param smallestFont
-	 *            The smallest font size available.
-	 * @param largestFont
-	 *            The largest font size available.
+	 * There MUST be font for [smallestCellSize, smallestCellSize + 2, ...,
+	 * largestCellSize]
+	 * 
+	 * @param smallestCellSize
+	 *            The smallest cell size available.
+	 * @param largestCellSize
+	 *            The largest cell size available.
 	 * @param fontOffset
 	 * @param fontOffset
 	 *            This offset is added to the cell size when computing the font size
 	 *            This offset is added to the cell size when computing the font size
-	 *            for a given cell size.
-	 *            not to call this method.
+	 *            for a given cell size. not to call this method.
 	 * @param assetManager
 	 * @param assetManager
 	 */
 	 */
-	public SquidPanelBuilder(int smallestFont, int largestFont, int fontOffset,
+	public SquidPanelBuilder(int smallestCellSize, int largestCellSize, int fontOffset,
 			/* @Nullable */ AssetManager assetManager) {
 			/* @Nullable */ AssetManager assetManager) {
+		assert smallestCellSize <= largestCellSize;
+		assert 0 <= fontOffset;
 		this.assetManager = assetManager;
 		this.assetManager = assetManager;
 
 
-		this.smallestFont = smallestFont;
-		this.largestFont = largestFont;
+		this.smallestCellSize = smallestCellSize;
+		this.largestCellSize = largestCellSize;
 		this.fontOffset = fontOffset;
 		this.fontOffset = fontOffset;
 	}
 	}
 
 
@@ -108,22 +113,29 @@ public abstract class SquidPanelBuilder extends IPanelBuilder.Skeleton {
 	@Override
 	@Override
 	public int adjustCellSize(int sz) {
 	public int adjustCellSize(int sz) {
 		int result = sz % 2 == 0 ? sz : sz - 1;
 		int result = sz % 2 == 0 ? sz : sz - 1;
-		if (hasFontForCellOfSize(result))
+		if (acceptsCellOfSize(result))
 			return result;
 			return result;
 		if (cellSizeTooBig(sz)) {
 		if (cellSizeTooBig(sz)) {
+			/*
+			 * Note that we don't want the cell increment (2 on ascii, 12 on pixels) here,
+			 * because 'result' may not be valid at all (hence we need to correct it).
+			 */
 			final int offset = -2;
 			final int offset = -2;
 			while (0 < result) {
 			while (0 < result) {
 				result += offset;
 				result += offset;
-				if (hasFontForCellOfSize(result))
+				if (acceptsCellOfSize(result))
 					return result;
 					return result;
 			}
 			}
 			throw new IllegalStateException("There's a bug in the computation of the cell size");
 			throw new IllegalStateException("There's a bug in the computation of the cell size");
 		} else if (cellSizeTooSmall(result)) {
 		} else if (cellSizeTooSmall(result)) {
-			final int offset;
-			offset = 2;
+			/*
+			 * Note that we don't want the cell increment (2 on ascii, 12 on pixels) here,
+			 * because 'result' may not be valid at all (hence we need to correct it).
+			 */
+			final int offset = 2;
 			while (/* It's better to stop one day */ result < 512) {
 			while (/* It's better to stop one day */ result < 512) {
 				result += offset;
 				result += offset;
-				if (hasFontForCellOfSize(result))
+				if (acceptsCellOfSize(result))
 					return result;
 					return result;
 			}
 			}
 			throw new IllegalStateException("There's a bug in the computation of the cell size");
 			throw new IllegalStateException("There's a bug in the computation of the cell size");
@@ -134,16 +146,24 @@ public abstract class SquidPanelBuilder extends IPanelBuilder.Skeleton {
 
 
 	@Override
 	@Override
 	public boolean cellSizeTooBig(int cellSize) {
 	public boolean cellSizeTooBig(int cellSize) {
-		return largestFont < fontSizeForCellSize(cellSize);
+		return largestCellSize < fontSizeForCellSize(cellSize);
 	}
 	}
 
 
 	@Override
 	@Override
 	public boolean cellSizeTooSmall(int cellSize) {
 	public boolean cellSizeTooSmall(int cellSize) {
-		return fontSizeForCellSize(cellSize) < smallestFont;
+		return fontSizeForCellSize(cellSize) < smallestCellSize;
+	}
+
+	public int getSmallestCellSize() {
+		return smallestCellSize;
+	}
+
+	public int getLargestCellSize() {
+		return largestCellSize;
 	}
 	}
 
 
 	@Override
 	@Override
-	public boolean hasFontForCellOfSize(int cellSize) {
+	public boolean acceptsCellOfSize(int cellSize) {
 		return hasFontOfSize(fontSizeForCellSize(cellSize));
 		return hasFontOfSize(fontSizeForCellSize(cellSize));
 	}
 	}
 
 
@@ -154,7 +174,7 @@ public abstract class SquidPanelBuilder extends IPanelBuilder.Skeleton {
 
 
 	@Override
 	@Override
 	public boolean hasFontOfSize(int sz) {
 	public boolean hasFontOfSize(int sz) {
-		return sz % 2 == 0 && smallestFont <= sz && sz <= largestFont;
+		return sz % 2 == 0 && smallestCellSize <= sz && sz <= largestCellSize;
 	}
 	}
 
 
 	/**
 	/**