From 995ec0c293fbb95087bf18f498617cbe2258084e Mon Sep 17 00:00:00 2001 From: ewaldbenes Date: Sat, 16 Jun 2018 00:06:34 +0200 Subject: [PATCH 1/7] Use hash map to store settler images instead of 4-dim array to save memory. Add unit tests for settler map. --- jsettlers.graphics/build.gradle | 5 + .../panel/selection/SelectionRow.java | 11 +- .../graphics/map/draw/ImagePreloadTask.java | 9 +- .../graphics/map/draw/ImageProvider.java | 14 +- .../graphics/map/draw/MapObjectDrawer.java | 9 +- .../settlerimages/SettlerImageFlavor.java | 61 ++++++ .../draw/settlerimages/SettlerImageMap.java | 189 +++++------------- .../settlerimages/SettlerImageMapItem.java | 35 +++- .../settlerimages/SettlerImageMapTest.java | 73 +++++++ 9 files changed, 249 insertions(+), 157 deletions(-) create mode 100644 jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageFlavor.java create mode 100644 jsettlers.graphics/src/test/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapTest.java diff --git a/jsettlers.graphics/build.gradle b/jsettlers.graphics/build.gradle index c4981313e8..d173139900 100644 --- a/jsettlers.graphics/build.gradle +++ b/jsettlers.graphics/build.gradle @@ -19,6 +19,11 @@ dependencies { implementation project(':jsettlers.common') implementation project(':go.graphics') implementation 'com.google.code.gson:gson:2.8.2' + + testCompile 'org.mockito:mockito-core:2.18.3' + testCompile 'org.hamcrest:hamcrest-library:1.3' + + } diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SelectionRow.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SelectionRow.java index a00f3c6a28..54b7201e98 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SelectionRow.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SelectionRow.java @@ -24,7 +24,10 @@ import jsettlers.common.movable.EMovableType; import jsettlers.graphics.image.Image; import jsettlers.graphics.localization.Labels; +import jsettlers.graphics.map.draw.ImageProvider; +import jsettlers.graphics.map.draw.settlerimages.SettlerImageFlavor; import jsettlers.graphics.map.draw.settlerimages.SettlerImageMap; +import jsettlers.graphics.map.draw.settlerimages.SettlerImageMapItem; import jsettlers.graphics.ui.UIPanel; public class SelectionRow extends UIPanel { @@ -48,10 +51,10 @@ public SelectionRow(EMovableType type, int count) { @Override public void drawAt(GLDrawContext gl) { float width = getPosition().getWidth(); - Image image = - SettlerImageMap.getInstance().getImageForSettler(type, - EMovableAction.NO_ACTION, EMaterialType.NO_MATERIAL, - EDirection.SOUTH_EAST, 0); + Image image = SettlerImageMap.getInstance().getImageForSettler( + new SettlerImageFlavor(type, EMovableAction.NO_ACTION, EMaterialType.NO_MATERIAL, EDirection.SOUTH_EAST), + 0.0f + ); Color color = getColor(); float bottomy = getPosition() diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImagePreloadTask.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImagePreloadTask.java index 3173d737c6..d23458c916 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImagePreloadTask.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImagePreloadTask.java @@ -17,9 +17,16 @@ import jsettlers.graphics.map.draw.settlerimages.SettlerImageMap; public class ImagePreloadTask implements Runnable { + + private final SettlerImageMap settlerImageMap; + + public ImagePreloadTask(SettlerImageMap settlerImageMap) { + this.settlerImageMap = settlerImageMap; + } + @Override public void run() { - SettlerImageMap.getInstance(); + settlerImageMap.loadFromMoveablesTextFile(); Background.preloadTexture(); diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImageProvider.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImageProvider.java index bef6e864b8..3f6c75431a 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImageProvider.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImageProvider.java @@ -35,6 +35,8 @@ import jsettlers.graphics.image.reader.versions.SettlersVersionMapping; import jsettlers.graphics.image.sequence.ArraySequence; import jsettlers.graphics.image.sequence.Sequence; +import jsettlers.graphics.map.draw.settlerimages.SettlerImageMap; +import jsettlers.graphics.map.draw.settlerimages.SettlerImageMapItem; import java.io.File; import java.util.Arrays; @@ -51,7 +53,8 @@ * * @author michael */ -public final class ImageProvider { +// TODO: removed final for testing. introduce it again when some supertype could be extracted +public class ImageProvider { private static final String FILE_PREFIX = "siedler3_"; private static final int LAST_SEQUENCE_NUMBER = 2; private static final List HIGHRES_IMAGE_FILE_NUMBERS = Arrays.asList(3, 14); @@ -73,7 +76,7 @@ public final class ImageProvider { private Thread preloadingThread; private ImageIndexFile indexFile = null; - private ImageProvider() { + public ImageProvider() { } /** @@ -305,7 +308,7 @@ private DatFileReader createFileReader(int fileIndex) { */ public void startPreloading() { if (lookupPath != null && preloadingThread == null) { - preloadingThread = new Thread(new ImagePreloadTask(), "image preloader"); + preloadingThread = new Thread(new ImagePreloadTask(SettlerImageMap.getInstance()), "image preloader"); preloadingThread.start(); } } @@ -327,4 +330,9 @@ public void waitForPreloadingFinish() { public void addPreloadTask(GLPreloadTask task) { tasks.add(task); } + + public Image getImageSafe(SettlerImageMapItem item, float progress) { + return getSettlerSequence(item.getFile(), item.getSequenceIndex()) + .getImageSafe(item.imageIndex(progress)); + } } diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/MapObjectDrawer.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/MapObjectDrawer.java index 02352a1a3c..b65612992e 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/MapObjectDrawer.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/MapObjectDrawer.java @@ -50,6 +50,7 @@ import jsettlers.graphics.image.SingleImage; import jsettlers.graphics.image.sequence.Sequence; import jsettlers.graphics.map.MapDrawContext; +import jsettlers.graphics.map.draw.settlerimages.SettlerImageFlavor; import jsettlers.graphics.map.draw.settlerimages.SettlerImageMap; import jsettlers.graphics.map.geometry.MapCoordinateConverter; import jsettlers.graphics.sound.SoundManager; @@ -375,8 +376,8 @@ private void drawShip(IMovable ship, int x, int y) { if (yShift >= 0) { float xShift = PASSENGER_POSITION_TO_FRONT[j] * xShiftForward + PASSENGER_POSITION_TO_RIGHT[j] * xShiftRight; IMovable passenger = passengerList.get(j); - Image image = this.imageMap.getImageForSettler(passenger.getMovableType(), EMovableAction.NO_ACTION, - EMaterialType.NO_MATERIAL, getPassengerDirection(direction, shipPosition, i), 0 + Image image = this.imageMap.getImageForSettler( + new SettlerImageFlavor(passenger.getMovableType(), EMovableAction.NO_ACTION, EMaterialType.NO_MATERIAL, getPassengerDirection(direction, shipPosition, i)), 0 ); image.drawAt(glDrawContext, drawBuffer, viewX + xShift, viewY + yShift + PASSENGER_DECK_HEIGHT, color, shade); } @@ -408,8 +409,8 @@ EMaterialType.NO_MATERIAL, getPassengerDirection(direction, shipPosition, i), 0 if (yShift < 0) { float xShift = PASSENGER_POSITION_TO_FRONT[j] * xShiftForward + PASSENGER_POSITION_TO_RIGHT[j] * xShiftRight; IMovable passenger = passengerList.get(j); - Image image = this.imageMap.getImageForSettler(passenger.getMovableType(), EMovableAction.NO_ACTION, - EMaterialType.NO_MATERIAL, getPassengerDirection(direction, shipPosition, i), 0 + Image image = this.imageMap.getImageForSettler( + new SettlerImageFlavor(passenger.getMovableType(), EMovableAction.NO_ACTION, EMaterialType.NO_MATERIAL, getPassengerDirection(direction, shipPosition, i)), 0 ); image.drawAt(glDrawContext, drawBuffer, viewX + xShift, viewY + yShift + PASSENGER_DECK_HEIGHT, color, shade); } diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageFlavor.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageFlavor.java new file mode 100644 index 0000000000..32f85fde2e --- /dev/null +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageFlavor.java @@ -0,0 +1,61 @@ +package jsettlers.graphics.map.draw.settlerimages; + +import jsettlers.common.material.EMaterialType; +import jsettlers.common.movable.EDirection; +import jsettlers.common.movable.EMovableAction; +import jsettlers.common.movable.EMovableType; +import jsettlers.common.movable.IMovable; + +import java.util.Objects; + +public final class SettlerImageFlavor { + + public static final SettlerImageFlavor NONE = new SettlerImageFlavor(null, null, null, null); + private final EMovableType type; + private final EMovableAction action; + private final EMaterialType material; + private final EDirection direction; + + public SettlerImageFlavor(EMovableType type, EMovableAction action, EMaterialType material, EDirection direction) { + this.type = type; + this.action = action; + this.material = material; + this.direction = direction; + } + + static SettlerImageFlavor createFromMovable(IMovable movable) { + return new SettlerImageFlavor(movable.getMovableType(), movable.getAction(), movable.getMaterial(), movable.getDirection()); + } + + public EMovableType getType() { + return type; + } + + public EMovableAction getAction() { + return action; + } + + public EMaterialType getMaterial() { + return material; + } + + public EDirection getDirection() { + return direction; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SettlerImageFlavor that = (SettlerImageFlavor) o; + return type == that.type && + action == that.action && + material == that.material && + direction == that.direction; + } + + @Override + public int hashCode() { + return Objects.hash(type, action, material, direction); + } +} diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMap.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMap.java index c62eb53a3a..ceb333421a 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMap.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMap.java @@ -14,10 +14,8 @@ *******************************************************************************/ package jsettlers.graphics.map.draw.settlerimages; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; +import java.util.HashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -38,40 +36,31 @@ */ public final class SettlerImageMap { - private static final SettlerImageMapItem DEFAULT_ITEM = new SettlerImageMapItem(10, 0, 0, 1); + static final SettlerImageMapItem DEFAULT_ITEM = new SettlerImageMapItem(10, 0, 0, 1); private static SettlerImageMap instance; - private final ImageProvider imageProvider = ImageProvider.getInstance(); + private final ImageProvider imageProvider; - private final SettlerImageMapItem[][][][] map; + final HashMap map = new HashMap<>(); private final Pattern linePattern = Pattern.compile("\\s*([\\w\\*]+)\\s*," + "\\s*([\\w\\*]+)\\s*," + "\\s*([\\w\\*]+)\\s*," + "\\s*([\\w\\*]+)\\s*" + "=\\s*(\\d+)\\s*," + "\\s*(\\d+)\\s*," + "\\s*(\\d+)\\s*," + "\\s*(-?\\d+)\\s*"); - private final int types; - - private final int actions; - - private final int materials; - - private final int directions; - - /** - * Creates a new settler image map. - */ private SettlerImageMap() { - this.types = EMovableType.NUMBER_OF_MOVABLETYPES; - this.actions = EMovableAction.values().length; - this.materials = EMaterialType.NUMBER_OF_MATERIALS; - this.directions = EDirection.VALUES.length; - this.map = new SettlerImageMapItem[this.types][this.actions][this.materials][this.directions]; + imageProvider = ImageProvider.getInstance(); + } + + SettlerImageMap(ImageProvider imageProvider){ + this.imageProvider = imageProvider; + } + public void loadFromMoveablesTextFile() { try { InputStream file = getClass().getResourceAsStream("movables.txt"); - readFromFile(file); + readFromStream(file); } catch (IOException e) { System.err.println("Error reading image file. " + "Settler images might not work."); @@ -84,23 +73,18 @@ private SettlerImageMap() { * @param file * The file to read from. */ - private void readFromFile(InputStream file) throws IOException { - int[][][][] priorities = new int[this.types][this.actions][this.materials][this.directions]; - - // add pseudo entry. - addEntryToMap(priorities, null, null, null, null, DEFAULT_ITEM, -1); - - readFromFile(file, priorities); - } - - private void readFromFile(InputStream file, int[][][][] priorities) throws IOException { + private void readFromStream(InputStream file) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(file)); + readFromReader(reader); + } + private void readFromReader(BufferedReader reader) throws IOException { + HashMap priorities = new HashMap<>(); String line = reader.readLine(); while (line != null) { if (!line.isEmpty() && !line.startsWith("#")) { try { - addByLine(priorities, line); + addByLine(line, priorities); } catch (IllegalArgumentException e) { e.printStackTrace(); } @@ -113,14 +97,13 @@ private void readFromFile(InputStream file, int[][][][] priorities) throws IOExc /** * Adds a line to the map * - * @param priorities - * The priority table to use. * @param line * The line. + * @param priorities * @throws IllegalArgumentException * if the line is not correct. */ - private void addByLine(int[][][][] priorities, String line) { + private void addByLine(String line, HashMap priorities) { final Matcher matcher = parseLine(line); final String typeString = matcher.group(1); final String actionString = matcher.group(2); @@ -132,14 +115,17 @@ private void addByLine(int[][][][] priorities, String line) { EMaterialType material = parseMaterial(materialString); EDirection direction = parseDirection(directionString); - int priority = calculatePriority(type, action, material, direction); - final int fileIndex = Integer.parseInt(matcher.group(5)); final int sequence = Integer.parseInt(matcher.group(6)); final int start = Integer.parseInt(matcher.group(7)); final int duration = Integer.parseInt(matcher.group(8)); - addEntryToMap(priorities, type, action, material, direction, new SettlerImageMapItem(fileIndex, sequence, start, duration), priority); + SettlerImageFlavor flavor = new SettlerImageFlavor(type, action, material, direction); + int priority = calculatePriority(flavor); + if(!priorities.containsKey(flavor) || priorities.get(flavor) < priority) { + map.put(flavor, new SettlerImageMapItem(fileIndex, sequence, start, duration)); + priorities.put(flavor, priority); + } } private EMovableType parseType(final String typeString) { @@ -182,18 +168,18 @@ private EDirection parseDirection(final String directionString) { return direction; } - private int calculatePriority(EMovableType type, EMovableAction action, EMaterialType material, EDirection direction) { + private int calculatePriority(SettlerImageFlavor settlerImageFlavor) { int priority = 1;// more than 0. - if (type != null) { + if (settlerImageFlavor.getType() != null) { priority += 10; } - if (action != null) { + if (settlerImageFlavor.getAction() != null) { priority += 100; } - if (material != null) { + if (settlerImageFlavor.getMaterial() != null) { priority += 1000; } - if (direction != null) { + if (settlerImageFlavor.getDirection() != null) { priority += 10000; } return priority; @@ -217,76 +203,13 @@ private Matcher parseLine(String line) { return matcher; } - /** - * Adds an entry to the map. Overrides cells with lower priorities. - * - * @param priorities - * The priority table to use. - * @param type - * @param action - * @param material - * @param direction - * @param item - * @param priority - */ - private void addEntryToMap(int[][][][] priorities, EMovableType type, EMovableAction action, EMaterialType material, EDirection direction, SettlerImageMapItem item, int priority) { - int minType, maxType; - if (type == null) { - minType = 0; - maxType = this.types; - } else { - minType = type.ordinal(); - maxType = minType + 1; - } - - int minAction, maxAction; - if (action == null) { - minAction = 0; - maxAction = this.actions; - } else { - minAction = action.ordinal(); - maxAction = minAction + 1; - } - - int minMaterial, maxMaterial; - if (material == null) { - minMaterial = 0; - maxMaterial = this.materials; - } else { - minMaterial = material.ordinal(); - maxMaterial = minMaterial + 1; - } - - int minDirection, maxDirection; - if (direction == null) { - minDirection = 0; - maxDirection = this.directions; - } else { - minDirection = direction.ordinal(); - maxDirection = minDirection + 1; - } - - for (int typeIndex = minType; typeIndex < maxType; typeIndex++) { - for (int actionIndex = minAction; actionIndex < maxAction; actionIndex++) { - for (int materialIndex = minMaterial; materialIndex < maxMaterial; materialIndex++) { - for (int direcitonIndex = minDirection; direcitonIndex < maxDirection; direcitonIndex++) { - if (priorities[typeIndex][actionIndex][materialIndex][direcitonIndex] < priority) { - this.map[typeIndex][actionIndex][materialIndex][direcitonIndex] = item; - priorities[typeIndex][actionIndex][materialIndex][direcitonIndex] = priority; - } - } - } - } - } - } - /** * Gets an image for a given settler. * * @param movable * The settler to get the image for - * @return The image or an null-image. - * @see SettlerImageMap#getImageForSettler(EMovableType, EMovableAction, EMaterialType, EDirection, float) + * @return The image or a null-image. + * @see SettlerImageMap#getImageForSettler(SettlerImageFlavor, float) */ public Image getImageForSettler(IMovable movable, float progress) { if (movable.getAction() == EMovableAction.WALKING) { @@ -295,51 +218,31 @@ public Image getImageForSettler(IMovable movable, float progress) { progress += .5f; } } - return getImageForSettler(movable.getMovableType(), - movable.getAction(), movable.getMaterial(), - movable.getDirection(), progress); + return getImageForSettler(SettlerImageFlavor.createFromMovable(movable), progress); } /** * Gets an image for a given settler. * - * @param movableType - * The type of the settler. - * @param action - * The action the settler is doing. - * @param material - * The material that is assigned to the settler. - * @param direction - * Its direction. + * + * @param settlerImageFlavor * @param progress * The progress. * @return The image. */ - public Image getImageForSettler(EMovableType movableType, EMovableAction action, EMaterialType material, EDirection direction, float progress) { - SettlerImageMapItem item = getMapItem(movableType, action, material, direction); - - int duration = item.getDuration(); - int imageIndex; - if (duration >= 0) { - imageIndex = item.getStart() + Math.min((int) (progress * duration), duration - 1); - } else { - imageIndex = item.getStart() + Math.max((int) (progress * duration), duration + 1); - } - return this.imageProvider.getSettlerSequence(item.getFile(), item.getSequenceIndex()).getImageSafe(imageIndex); + public Image getImageForSettler(SettlerImageFlavor settlerImageFlavor, float progress) { + SettlerImageMapItem item = getMapItem(settlerImageFlavor); + return imageProvider.getImageSafe(item, progress); } /** * Gets a map item. * - * @param movableType - * @param action - * @param material - * @param direction - * @param progress - * @return The item of the map at the given position. Is not null. + * + * @param settlerImageFlavor@return The item of the map at the given position. Is not null. */ - private SettlerImageMapItem getMapItem(EMovableType movableType, EMovableAction action, EMaterialType material, EDirection direction) { - SettlerImageMapItem item = this.map[movableType.ordinal()][action.ordinal()][material.ordinal][direction.ordinal]; + private SettlerImageMapItem getMapItem(SettlerImageFlavor settlerImageFlavor) { + SettlerImageMapItem item = map.get(settlerImageFlavor); if (item == null) { return DEFAULT_ITEM; } else { @@ -353,4 +256,8 @@ public static SettlerImageMap getInstance() { } return instance; } + + void loadFromMovablesText(String text) throws IOException { + readFromReader(new BufferedReader(new StringReader(text))); + } } diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapItem.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapItem.java index ae5df2309c..f86d0c873a 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapItem.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapItem.java @@ -14,19 +14,19 @@ *******************************************************************************/ package jsettlers.graphics.map.draw.settlerimages; +import java.util.Objects; + /** * This is a map item of settler images. * * @author michael * */ -public class SettlerImageMapItem { - private final int file; +public final class SettlerImageMapItem { + private final int file; private final int sequenceIndex; - private final int start; - private final int duration; public SettlerImageMapItem(int file, int sequenceIndex, int start, @@ -52,4 +52,31 @@ public int getStart() { public int getDuration() { return this.duration; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SettlerImageMapItem that = (SettlerImageMapItem) o; + return file == that.file && + sequenceIndex == that.sequenceIndex && + start == that.start && + duration == that.duration; + } + + @Override + public int hashCode() { + return Objects.hash(file, sequenceIndex, start, duration); + } + + public int imageIndex(float progress) { + int duration = getDuration(); + int imageIndex; + if (duration >= 0) { + imageIndex = getStart() + Math.min((int) (progress * duration), duration - 1); + } else { + imageIndex = getStart() + Math.max((int) (progress * duration), duration + 1); + } + return imageIndex; + } } diff --git a/jsettlers.graphics/src/test/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapTest.java b/jsettlers.graphics/src/test/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapTest.java new file mode 100644 index 0000000000..1a464c27b6 --- /dev/null +++ b/jsettlers.graphics/src/test/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapTest.java @@ -0,0 +1,73 @@ +package jsettlers.graphics.map.draw.settlerimages; + +import jsettlers.common.material.EMaterialType; +import jsettlers.common.movable.EDirection; +import jsettlers.common.movable.EMovableAction; +import jsettlers.common.movable.EMovableType; +import jsettlers.graphics.image.Image; +import jsettlers.graphics.map.draw.ImageProvider; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; + +import static org.hamcrest.collection.IsMapContaining.hasEntry; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +public class SettlerImageMapTest { + + private static final SettlerImageFlavor TEST_IMAGE_FLAVOR = new SettlerImageFlavor( + EMovableType.BEARER, + EMovableAction.WALKING, + EMaterialType.NO_MATERIAL, + EDirection.SOUTH_WEST); + private SettlerImageMap settlerImages; + private Image dummyImage; + + @Before + public void setUp(){ + dummyImage = mock(Image.class); + ImageProvider imageProvider = mock(ImageProvider.class); + when(imageProvider.getImageSafe(any(SettlerImageMapItem.class), anyFloat())).thenReturn(dummyImage); + + settlerImages = new SettlerImageMap(imageProvider); + } + + @Test + public void givenTextWithComment_whenLoad_thenContainsNoEntry() throws IOException { + String text = "# some comment"; + settlerImages.loadFromMovablesText(text); + assertTrue(settlerImages.map.isEmpty()); + //assertSame(dummyImage, settlerImages.getImageForSettler(SettlerImageFlavor.NONE, 1.0f)); + } + + @Test + public void givenTextWithDefaultEntry_whenLoad_thenContainsDefaultEntry() throws IOException { + String text = "*,*,*,*=10, 0, 0, 1"; + settlerImages.loadFromMovablesText(text); + assertEquals(1, settlerImages.map.size()); + assertThat(settlerImages.map, hasEntry(SettlerImageFlavor.NONE, SettlerImageMap.DEFAULT_ITEM)); + } + + @Test + public void givenTextWithTestEntry_whenLoad_thenContainsTestEntry() throws IOException { + String text = "BEARER, WALKING, NO_MATERIAL, SOUTH_WEST = 10, 0, 0, 12"; + settlerImages.loadFromMovablesText(text); + assertOneEntryInMap(); + } + + private void assertOneEntryInMap() { + assertEquals(1, settlerImages.map.size()); + assertThat(settlerImages.map, hasEntry(TEST_IMAGE_FLAVOR, + new SettlerImageMapItem(10, 0, 0, 12)) + ); + } + + @Test + public void givenTextWithDuplicateEntry_whenLoad_thenContainsOnlyOneEntry() throws IOException { + String text = "BEARER, WALKING, NO_MATERIAL, SOUTH_WEST = 10, 0, 0, 12\nBEARER, WALKING, NO_MATERIAL, SOUTH_WEST = 10, 0, 0, 12"; + settlerImages.loadFromMovablesText(text); + assertOneEntryInMap(); + } +} \ No newline at end of file From c968349786477284433d5af26e3b57e94c1dac3e Mon Sep 17 00:00:00 2001 From: ewaldbenes Date: Sat, 16 Jun 2018 09:30:18 +0200 Subject: [PATCH 2/7] Change Gradle testCompile to testImplementation. --- jsettlers.graphics/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jsettlers.graphics/build.gradle b/jsettlers.graphics/build.gradle index d173139900..31febf4c3a 100644 --- a/jsettlers.graphics/build.gradle +++ b/jsettlers.graphics/build.gradle @@ -20,8 +20,8 @@ dependencies { implementation project(':go.graphics') implementation 'com.google.code.gson:gson:2.8.2' - testCompile 'org.mockito:mockito-core:2.18.3' - testCompile 'org.hamcrest:hamcrest-library:1.3' + testImplemenation 'org.mockito:mockito-core:2.18.3' + testImplementation 'org.hamcrest:hamcrest-library:1.3' } From 9d16d749ebb474b5a980b09640756ceffcc35e5d Mon Sep 17 00:00:00 2001 From: ewaldbenes Date: Sat, 16 Jun 2018 09:43:46 +0200 Subject: [PATCH 3/7] Change Gradle testCompile to testImplementation. --- jsettlers.graphics/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsettlers.graphics/build.gradle b/jsettlers.graphics/build.gradle index 31febf4c3a..914d1f192e 100644 --- a/jsettlers.graphics/build.gradle +++ b/jsettlers.graphics/build.gradle @@ -20,7 +20,7 @@ dependencies { implementation project(':go.graphics') implementation 'com.google.code.gson:gson:2.8.2' - testImplemenation 'org.mockito:mockito-core:2.18.3' + testImplementation 'org.mockito:mockito-core:2.18.3' testImplementation 'org.hamcrest:hamcrest-library:1.3' From ae03171bdac0e2b36836aed33d9d40126a57d491 Mon Sep 17 00:00:00 2001 From: Andreas Eberle Date: Wed, 18 Jul 2018 20:13:42 +0200 Subject: [PATCH 4/7] Let travis run :jsettlers.graphics:test --- .travis.yml | 3 ++- build.gradle | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3ee61e3578..bf1cb132ef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,8 @@ env: # Run single test targets - TARGET=":jsettlers.logic:unitTest" - TARGET=":jsettlers.common:test" - - TARGET=":jsettlers.network:test --info" + - TARGET=":jsettlers.graphics:test" + - TARGET=":jsettlers.network:test" - TARGET=":jsettlers.logic:aiDifficultiesIT" - TARGET=":jsettlers.logic:autoReplayIT" - TARGET=":jsettlers.logic:replayValidationIT" diff --git a/build.gradle b/build.gradle index 7585e7b56a..1bb4446442 100644 --- a/build.gradle +++ b/build.gradle @@ -78,6 +78,7 @@ subprojects { tasks.withType(Test) { testLogging { exceptionFormat = 'full' + events "PASSED", "FAILED", "SKIPPED" } } From 450bb54909905aba5d90a394087e470ff0fb0853 Mon Sep 17 00:00:00 2001 From: ewaldbenes Date: Wed, 1 Aug 2018 17:17:04 +0200 Subject: [PATCH 5/7] Avoid array allocation in hashCode implementations. Convert indents from space to tabs. Fix typos. --- .../panel/selection/SelectionRow.java | 7 +- .../graphics/map/draw/ImagePreloadTask.java | 2 +- .../graphics/map/draw/ImageProvider.java | 1 - .../settlerimages/SettlerImageFlavor.java | 133 +++++++++++------- .../draw/settlerimages/SettlerImageMap.java | 34 +---- .../settlerimages/SettlerImageMapItem.java | 38 ++--- .../settlerimages/SettlerImageMapTest.java | 99 +++++++------ 7 files changed, 167 insertions(+), 147 deletions(-) diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SelectionRow.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SelectionRow.java index 54b7201e98..192b230945 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SelectionRow.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SelectionRow.java @@ -34,6 +34,7 @@ public class SelectionRow extends UIPanel { private final EMovableType type; private final int count; + private final SettlerImageFlavor flavor; /** * Creates a new row in the selection view @@ -46,15 +47,13 @@ public class SelectionRow extends UIPanel { public SelectionRow(EMovableType type, int count) { this.type = type; this.count = count; + flavor = new SettlerImageFlavor(type, EMovableAction.NO_ACTION, EMaterialType.NO_MATERIAL, EDirection.SOUTH_EAST); } @Override public void drawAt(GLDrawContext gl) { float width = getPosition().getWidth(); - Image image = SettlerImageMap.getInstance().getImageForSettler( - new SettlerImageFlavor(type, EMovableAction.NO_ACTION, EMaterialType.NO_MATERIAL, EDirection.SOUTH_EAST), - 0.0f - ); + Image image = SettlerImageMap.getInstance().getImageForSettler(flavor,0.0f); Color color = getColor(); float bottomy = getPosition() diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImagePreloadTask.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImagePreloadTask.java index d23458c916..8568a4fbbb 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImagePreloadTask.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImagePreloadTask.java @@ -26,7 +26,7 @@ public ImagePreloadTask(SettlerImageMap settlerImageMap) { @Override public void run() { - settlerImageMap.loadFromMoveablesTextFile(); + settlerImageMap.loadFromMovablesTextFile(); Background.preloadTexture(); diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImageProvider.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImageProvider.java index 3f6c75431a..caf0c68472 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImageProvider.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/ImageProvider.java @@ -53,7 +53,6 @@ * * @author michael */ -// TODO: removed final for testing. introduce it again when some supertype could be extracted public class ImageProvider { private static final String FILE_PREFIX = "siedler3_"; private static final int LAST_SEQUENCE_NUMBER = 2; diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageFlavor.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageFlavor.java index 32f85fde2e..2be7dfeacd 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageFlavor.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageFlavor.java @@ -1,3 +1,17 @@ +/******************************************************************************* + * Copyright (c) 2015 - 2018 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + *******************************************************************************/ package jsettlers.graphics.map.draw.settlerimages; import jsettlers.common.material.EMaterialType; @@ -10,52 +24,75 @@ public final class SettlerImageFlavor { - public static final SettlerImageFlavor NONE = new SettlerImageFlavor(null, null, null, null); - private final EMovableType type; - private final EMovableAction action; - private final EMaterialType material; - private final EDirection direction; - - public SettlerImageFlavor(EMovableType type, EMovableAction action, EMaterialType material, EDirection direction) { - this.type = type; - this.action = action; - this.material = material; - this.direction = direction; - } - - static SettlerImageFlavor createFromMovable(IMovable movable) { - return new SettlerImageFlavor(movable.getMovableType(), movable.getAction(), movable.getMaterial(), movable.getDirection()); - } - - public EMovableType getType() { - return type; - } - - public EMovableAction getAction() { - return action; - } - - public EMaterialType getMaterial() { - return material; - } - - public EDirection getDirection() { - return direction; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - SettlerImageFlavor that = (SettlerImageFlavor) o; - return type == that.type && - action == that.action && - material == that.material && - direction == that.direction; - } - - @Override - public int hashCode() { - return Objects.hash(type, action, material, direction); - } + public static final SettlerImageFlavor NONE = new SettlerImageFlavor(null, null, null, null); + private final EMovableType type; + private final EMovableAction action; + private final EMaterialType material; + private final EDirection direction; + + public SettlerImageFlavor(EMovableType type, EMovableAction action, EMaterialType material, EDirection direction) { + this.type = type; + this.action = action; + this.material = material; + this.direction = direction; + } + + static SettlerImageFlavor createFromMovable(IMovable movable) { + return new SettlerImageFlavor(movable.getMovableType(), movable.getAction(), movable.getMaterial(), movable.getDirection()); + } + + public EMovableType getType() { + return type; + } + + public EMovableAction getAction() { + return action; + } + + public EMaterialType getMaterial() { + return material; + } + + public EDirection getDirection() { + return direction; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + SettlerImageFlavor that = (SettlerImageFlavor) o; + + if (type != that.type) return false; + if (action != that.action) return false; + if (material != that.material) return false; + return direction == that.direction; + } + + @Override + public int hashCode() { + int result = type != null ? type.hashCode() : 0; + result = 31 * result + (action != null ? action.hashCode() : 0); + result = 31 * result + (material != null ? material.hashCode() : 0); + result = 31 * result + (direction != null ? direction.hashCode() : 0); + return result; + } + + int calculatePriority() { + int priority = 1;// more than 0. + if (getType() != null) { + priority += 10; + } + if (getAction() != null) { + priority += 100; + } + if (getMaterial() != null) { + priority += 1000; + } + if (getDirection() != null) { + priority += 10000; + } + return priority; + } } diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMap.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMap.java index ceb333421a..134493b964 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMap.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMap.java @@ -57,27 +57,16 @@ private SettlerImageMap() { this.imageProvider = imageProvider; } - public void loadFromMoveablesTextFile() { + public void loadFromMovablesTextFile() { try { InputStream file = getClass().getResourceAsStream("movables.txt"); - readFromStream(file); + readFromReader(new BufferedReader(new InputStreamReader(file))); } catch (IOException e) { System.err.println("Error reading image file. " + "Settler images might not work."); } } - /** - * Reads the map from the given file. - * - * @param file - * The file to read from. - */ - private void readFromStream(InputStream file) throws IOException { - BufferedReader reader = new BufferedReader(new InputStreamReader(file)); - readFromReader(reader); - } - private void readFromReader(BufferedReader reader) throws IOException { HashMap priorities = new HashMap<>(); String line = reader.readLine(); @@ -121,7 +110,7 @@ private void addByLine(String line, HashMap priorit final int duration = Integer.parseInt(matcher.group(8)); SettlerImageFlavor flavor = new SettlerImageFlavor(type, action, material, direction); - int priority = calculatePriority(flavor); + int priority = flavor.calculatePriority(); if(!priorities.containsKey(flavor) || priorities.get(flavor) < priority) { map.put(flavor, new SettlerImageMapItem(fileIndex, sequence, start, duration)); priorities.put(flavor, priority); @@ -168,23 +157,6 @@ private EDirection parseDirection(final String directionString) { return direction; } - private int calculatePriority(SettlerImageFlavor settlerImageFlavor) { - int priority = 1;// more than 0. - if (settlerImageFlavor.getType() != null) { - priority += 10; - } - if (settlerImageFlavor.getAction() != null) { - priority += 100; - } - if (settlerImageFlavor.getMaterial() != null) { - priority += 1000; - } - if (settlerImageFlavor.getDirection() != null) { - priority += 10000; - } - return priority; - } - /** * Parses a line. * diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapItem.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapItem.java index f86d0c873a..33cebad2ba 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapItem.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapItem.java @@ -53,30 +53,36 @@ public int getDuration() { return this.duration; } + public int imageIndex(float progress) { + int duration = getDuration(); + int imageIndex; + if (duration >= 0) { + imageIndex = getStart() + Math.min((int) (progress * duration), duration - 1); + } else { + imageIndex = getStart() + Math.max((int) (progress * duration), duration + 1); + } + return imageIndex; + } + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; + SettlerImageMapItem that = (SettlerImageMapItem) o; - return file == that.file && - sequenceIndex == that.sequenceIndex && - start == that.start && - duration == that.duration; + + if (file != that.file) return false; + if (sequenceIndex != that.sequenceIndex) return false; + if (start != that.start) return false; + return duration == that.duration; } @Override public int hashCode() { - return Objects.hash(file, sequenceIndex, start, duration); + int result = file; + result = 31 * result + sequenceIndex; + result = 31 * result + start; + result = 31 * result + duration; + return result; } - - public int imageIndex(float progress) { - int duration = getDuration(); - int imageIndex; - if (duration >= 0) { - imageIndex = getStart() + Math.min((int) (progress * duration), duration - 1); - } else { - imageIndex = getStart() + Math.max((int) (progress * duration), duration + 1); - } - return imageIndex; - } } diff --git a/jsettlers.graphics/src/test/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapTest.java b/jsettlers.graphics/src/test/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapTest.java index 1a464c27b6..80dfe0719b 100644 --- a/jsettlers.graphics/src/test/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapTest.java +++ b/jsettlers.graphics/src/test/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMapTest.java @@ -1,3 +1,17 @@ +/******************************************************************************* + * Copyright (c) 2015 - 2018 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + *******************************************************************************/ package jsettlers.graphics.map.draw.settlerimages; import jsettlers.common.material.EMaterialType; @@ -17,57 +31,50 @@ public class SettlerImageMapTest { - private static final SettlerImageFlavor TEST_IMAGE_FLAVOR = new SettlerImageFlavor( - EMovableType.BEARER, - EMovableAction.WALKING, - EMaterialType.NO_MATERIAL, - EDirection.SOUTH_WEST); - private SettlerImageMap settlerImages; - private Image dummyImage; + private static final SettlerImageFlavor TEST_IMAGE_FLAVOR = new SettlerImageFlavor(EMovableType.BEARER, EMovableAction.WALKING, EMaterialType.NO_MATERIAL, EDirection.SOUTH_WEST); + private SettlerImageMap settlerImages; + private Image dummyImage; - @Before - public void setUp(){ - dummyImage = mock(Image.class); - ImageProvider imageProvider = mock(ImageProvider.class); - when(imageProvider.getImageSafe(any(SettlerImageMapItem.class), anyFloat())).thenReturn(dummyImage); + @Before + public void setUp(){ + dummyImage = mock(Image.class); + ImageProvider imageProvider = mock(ImageProvider.class); + when(imageProvider.getImageSafe(any(SettlerImageMapItem.class), anyFloat())).thenReturn(dummyImage); - settlerImages = new SettlerImageMap(imageProvider); - } + settlerImages = new SettlerImageMap(imageProvider); + } - @Test - public void givenTextWithComment_whenLoad_thenContainsNoEntry() throws IOException { - String text = "# some comment"; - settlerImages.loadFromMovablesText(text); - assertTrue(settlerImages.map.isEmpty()); - //assertSame(dummyImage, settlerImages.getImageForSettler(SettlerImageFlavor.NONE, 1.0f)); - } + @Test + public void givenTextWithCommentWhenLoadThenContainsNoEntry() throws IOException { + String text = "# some comment"; + settlerImages.loadFromMovablesText(text); + assertTrue(settlerImages.map.isEmpty()); + } - @Test - public void givenTextWithDefaultEntry_whenLoad_thenContainsDefaultEntry() throws IOException { - String text = "*,*,*,*=10, 0, 0, 1"; - settlerImages.loadFromMovablesText(text); - assertEquals(1, settlerImages.map.size()); - assertThat(settlerImages.map, hasEntry(SettlerImageFlavor.NONE, SettlerImageMap.DEFAULT_ITEM)); - } + @Test + public void givenTextWithDefaultEntryWhenLoadThenContainsDefaultEntry() throws IOException { + String text = "*,*,*,*=10, 0, 0, 1"; + settlerImages.loadFromMovablesText(text); + assertEquals(1, settlerImages.map.size()); + assertThat(settlerImages.map, hasEntry(SettlerImageFlavor.NONE, SettlerImageMap.DEFAULT_ITEM)); + } - @Test - public void givenTextWithTestEntry_whenLoad_thenContainsTestEntry() throws IOException { - String text = "BEARER, WALKING, NO_MATERIAL, SOUTH_WEST = 10, 0, 0, 12"; - settlerImages.loadFromMovablesText(text); - assertOneEntryInMap(); - } + @Test + public void givenTextWithTestEntryWhenLoadthenContainsTestEntry() throws IOException { + String text = "BEARER, WALKING, NO_MATERIAL, SOUTH_WEST = 10, 0, 0, 12"; + settlerImages.loadFromMovablesText(text); + assertOneEntryInMap(); + } - private void assertOneEntryInMap() { - assertEquals(1, settlerImages.map.size()); - assertThat(settlerImages.map, hasEntry(TEST_IMAGE_FLAVOR, - new SettlerImageMapItem(10, 0, 0, 12)) - ); - } + private void assertOneEntryInMap() { + assertEquals(1, settlerImages.map.size()); + assertThat(settlerImages.map, hasEntry(TEST_IMAGE_FLAVOR, new SettlerImageMapItem(10, 0, 0, 12))); + } - @Test - public void givenTextWithDuplicateEntry_whenLoad_thenContainsOnlyOneEntry() throws IOException { - String text = "BEARER, WALKING, NO_MATERIAL, SOUTH_WEST = 10, 0, 0, 12\nBEARER, WALKING, NO_MATERIAL, SOUTH_WEST = 10, 0, 0, 12"; - settlerImages.loadFromMovablesText(text); - assertOneEntryInMap(); - } + @Test + public void givenTextWithDuplicateEntryWhenLoadThenContainsOnlyOneEntry() throws IOException { + String text = "BEARER, WALKING, NO_MATERIAL, SOUTH_WEST = 10, 0, 0, 12\nBEARER, WALKING, NO_MATERIAL, SOUTH_WEST = 10, 0, 0, 12"; + settlerImages.loadFromMovablesText(text); + assertOneEntryInMap(); + } } \ No newline at end of file From 2f15ae53f76af055301b1804e04f1eb80a676835 Mon Sep 17 00:00:00 2001 From: ewaldbenes Date: Thu, 2 Aug 2018 09:53:09 +0200 Subject: [PATCH 6/7] Move movable image retrieval out of draw method because it is a per-instance constant. --- .../panel/selection/SelectionRow.java | 42 +++++++++---------- .../panel/selection/ShipSelectionContent.java | 2 +- .../selection/SoilderSelectionContent.java | 2 +- .../java/jsettlers/graphics/ui/UIPanel.java | 5 +-- 4 files changed, 23 insertions(+), 28 deletions(-) diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SelectionRow.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SelectionRow.java index 192b230945..616ddb3181 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SelectionRow.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SelectionRow.java @@ -24,49 +24,47 @@ import jsettlers.common.movable.EMovableType; import jsettlers.graphics.image.Image; import jsettlers.graphics.localization.Labels; -import jsettlers.graphics.map.draw.ImageProvider; import jsettlers.graphics.map.draw.settlerimages.SettlerImageFlavor; import jsettlers.graphics.map.draw.settlerimages.SettlerImageMap; -import jsettlers.graphics.map.draw.settlerimages.SettlerImageMapItem; import jsettlers.graphics.ui.UIPanel; public class SelectionRow extends UIPanel { - private final EMovableType type; + private final Image movableImage; + private String localizedLabelName; private final int count; - private final SettlerImageFlavor flavor; /** - * Creates a new row in the selection view - * - * @param type - * The type of the movables - * @param count - * How many of them are selected. + * Creates a new row in the selection view. + * + * @param movableImage + * @param localizedLabelName + * @param selectionCount how many are selected. */ - public SelectionRow(EMovableType type, int count) { - this.type = type; - this.count = count; - flavor = new SettlerImageFlavor(type, EMovableAction.NO_ACTION, EMaterialType.NO_MATERIAL, EDirection.SOUTH_EAST); + public SelectionRow(Image movableImage, String localizedLabelName, int selectionCount) { + this.count = selectionCount; + this.movableImage = movableImage; + this.localizedLabelName = localizedLabelName; + } + + static SelectionRow createFromMovableType(EMovableType type, int count) { + SettlerImageFlavor flavor = new SettlerImageFlavor(type, EMovableAction.NO_ACTION, EMaterialType.NO_MATERIAL, EDirection.SOUTH_EAST); + return new SelectionRow(SettlerImageMap.getInstance().getImageForSettler(flavor, 0.0f), Labels.getName(type), count); } @Override public void drawAt(GLDrawContext gl) { float width = getPosition().getWidth(); - Image image = SettlerImageMap.getInstance().getImageForSettler(flavor,0.0f); Color color = getColor(); - float bottomy = getPosition() - .getMinY() + getPosition().getHeight() / 4; + float bottomY = getPosition().getMinY() + getPosition().getHeight() / 4; float left = getPosition().getMinX(); - float imagex = left + width / 20; - image.drawAt(gl, imagex, bottomy, color); + float imageX = left + width / 20; + movableImage.drawAt(gl, imageX, bottomY, color); TextDrawer drawer = gl.getTextDrawer(EFontSize.NORMAL); - drawer.drawString(left + width / 5, getPosition().getMinY() + getPosition().getHeight() * .75f, "" + count); - drawer.drawString(left + width / 5, bottomy, Labels.getName(type)); - + drawer.drawString(left + width / 5, bottomY, localizedLabelName); } private Color getColor() { diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/ShipSelectionContent.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/ShipSelectionContent.java index 3a7cd66761..9cea363d59 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/ShipSelectionContent.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/ShipSelectionContent.java @@ -57,7 +57,7 @@ public static void addRowsToPanel(UIPanel panel, ISelectionSet selection, EMovab int count = selection.getMovableCount(type); if (count > 0) { - SelectionRow row = new SelectionRow(type, count); + SelectionRow row = SelectionRow.createFromMovableType(type, count); panel.addChild(row, 0.1f, rowHeight * (rowi - 1), .9f, rowHeight * (rowi)); rowi--; diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SoilderSelectionContent.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SoilderSelectionContent.java index 37157d71df..bd1518c9d1 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SoilderSelectionContent.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/controls/original/panel/selection/SoilderSelectionContent.java @@ -65,7 +65,7 @@ public static void addRowsToPanel(UIPanel panel, ISelectionSet selection, int count = selection.getMovableCount(type); if (count > 0) { - SelectionRow row = new SelectionRow(type, count); + SelectionRow row = SelectionRow.createFromMovableType(type, count); panel.addChild(row, 0.1f, rowHeight * (rowi - 1), .9f, rowHeight * (rowi)); rowi--; diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/ui/UIPanel.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/ui/UIPanel.java index a18c4a0f03..6634339eb2 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/ui/UIPanel.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/ui/UIPanel.java @@ -42,10 +42,7 @@ public class UIPanel implements UIElement { private boolean attached = false; /** - * Sets the background. file=-1 means no background - * - * @param file - * @param settlerSeqIndex + * Sets the background. file=-1 means no background. */ public void setBackground(ImageLink imageLink) { this.background = imageLink; From 1a726f5c63226038a6dddbd36b6ef757554cc10f Mon Sep 17 00:00:00 2001 From: ewaldbenes Date: Tue, 11 Sep 2018 12:35:55 +0200 Subject: [PATCH 7/7] Replace Map.contains and Map.get with Map.get and null check --- .../graphics/map/draw/settlerimages/SettlerImageMap.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMap.java b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMap.java index 134493b964..e1a82a201f 100644 --- a/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMap.java +++ b/jsettlers.graphics/src/main/java/jsettlers/graphics/map/draw/settlerimages/SettlerImageMap.java @@ -111,7 +111,8 @@ private void addByLine(String line, HashMap priorit SettlerImageFlavor flavor = new SettlerImageFlavor(type, action, material, direction); int priority = flavor.calculatePriority(); - if(!priorities.containsKey(flavor) || priorities.get(flavor) < priority) { + Integer cachedPriority = priorities.get(flavor); + if(cachedPriority == null || cachedPriority < priority) { map.put(flavor, new SettlerImageMapItem(fileIndex, sequence, start, duration)); priorities.put(flavor, priority); }