Added working with CSV sprite sheets

This commit is contained in:
Robert Vokac 2024-08-15 20:19:23 +02:00
parent f940e42518
commit e2d7733db2
No known key found for this signature in database
GPG Key ID: C459E1E4B4A986BB
5 changed files with 190 additions and 8 deletions

View File

@ -19,7 +19,7 @@
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.entity.common;
package com.openeggbert.entity.sprites;
/**
*

View File

@ -19,7 +19,7 @@
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.entity.common;
package com.openeggbert.entity.sprites;
import com.openeggbert.compatibility.ReleaseVersion;
import lombok.Getter;
@ -29,7 +29,7 @@ import lombok.Getter;
* @author robertvokac
*/
public enum SpriteGroup {
AAA(ReleaseVersion.ONE);
YELLOW_EGGBERT_BORN(ReleaseVersion.ONE);
@Getter
private ReleaseVersion releaseVersion;
SpriteGroup(ReleaseVersion releaseVersion) {

View File

@ -0,0 +1,72 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Open Eggbert: Free recreation of the computer game Speedy Eggbert.
// Copyright (C) 2024 the original author or authors.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <https://www.gnu.org/licenses/> or write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.entity.sprites;
import com.openeggbert.entity.common.OpenEggbertException;
import com.openeggbert.utils.OpenEggbertUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
*
* @author robertvokac
*/
public class SpriteSheet {
public static final String DELIMITER = ";";
private final Map<String, Integer> columnIndexesForNames = new HashMap<>();
private final Map<String, SpriteSheetRow> rowMap = new HashMap<>();
public SpriteSheet(String csv) {
//List<SpriteSheetRow> rows = new ArrayList<>();
List<String> lines = Arrays.asList(csv.split("\n"));
String[] header = lines.get(0).split(DELIMITER);
for (int i = 0; i < header.length; i++) {
String columnName = header[i];
if (columnIndexesForNames.containsKey(columnName)) {
throw new OpenEggbertException("Invalid sprite sheet. It has invalid the first row (column names). Column is more than once: " + columnName);
}
Optional<SpriteSheetRowColumn> optionalSpriteSheetRowColumn = Arrays.asList(com.openeggbert.entity.sprites.SpriteSheetRowColumn.values()).stream().filter(r -> r.getColumnName().equals(columnName)).findFirst();
if (!optionalSpriteSheetRowColumn.isPresent()) {
continue;
}
columnIndexesForNames.put(columnName, i);
}
lines.stream().skip(1)
.forEach(line -> {
SpriteSheetRow row = new SpriteSheetRow(line, columnIndexesForNames);
//rows.add(row);
rowMap.put(row.createId(), row);
});
}
public SpriteSheetRow findSpriteSheetRow(SpriteGroup group, int numberInGroup) {
String id = SpriteSheetRow.createId(group.name(), numberInGroup);
SpriteSheetRow row = rowMap.get(id);
return row;
}
}

View File

@ -0,0 +1,92 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Open Eggbert: Free recreation of the computer game Speedy Eggbert.
// Copyright (C) 2024 the original author or authors.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <https://www.gnu.org/licenses/> or write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.entity.sprites;
import com.openeggbert.entity.common.OpenEggbertException;
import com.openeggbert.utils.OpenEggbertUtils;
import java.util.Map;
import java.util.function.Function;
import lombok.Getter;
/**
*
* @author robertvokac
*/
@Getter
public class SpriteSheetRow {
private static final String COLONCOLON = ";;";
private String file;
private String group;
private int numberInGroup;
private int row;
private int column;
private int x;
private int y;
private int width;
private int height;
private String notes;
private String tags;
private int numberPerSheet;
public static String createId(SpriteSheetRow row) {
return createId(row.getGroup(), row.getNumberInGroup());
}
public static String createId(String group, int numberInGroup) {
return group + COLONCOLON + numberInGroup;
}
public String createId() {
return createId(this);
}
public SpriteSheetRow(String line, Map<String, Integer> columnIndexesForNames) {
String[] columns = line.split(SpriteSheet.DELIMITER);
Function<SpriteSheetRowColumn, String> findString = c -> columnIndexesForNames.containsKey(c.getColumnName()) ? columns[columnIndexesForNames.get(c.getColumnName())] : "";
Function<SpriteSheetRowColumn, Integer> findInt = c -> {
String s = findString.apply(c);
if (s.isEmpty()) {
throw new OpenEggbertException("Missing value for column: " + c.getColumnName());
};
return Integer.valueOf(s);
};
file = findString.apply(SpriteSheetRowColumn.FILE);
group = findString.apply(SpriteSheetRowColumn.GROUP);
numberInGroup = findInt.apply(SpriteSheetRowColumn.NUMBER_IN_GROUP);
row = findInt.apply(SpriteSheetRowColumn.ROW);
column = findInt.apply(SpriteSheetRowColumn.COLUMN);
x = findInt.apply(SpriteSheetRowColumn.X);
y = findInt.apply(SpriteSheetRowColumn.Y);
width = findInt.apply(SpriteSheetRowColumn.WIDTH);
height = findInt.apply(SpriteSheetRowColumn.HEIGHT);
notes = findString.apply(SpriteSheetRowColumn.NOTES);
tags = findString.apply(SpriteSheetRowColumn.TAGS);
int numberPerSheet = findInt.apply(SpriteSheetRowColumn.FILE);
if (file.isEmpty()) {
throw new OpenEggbertException("Missing mandatory value for column: " + SpriteSheetRowColumn.FILE);
}
if (group.isEmpty()) {
throw new OpenEggbertException("Missing mandatory value for column: " + SpriteSheetRowColumn.GROUP);
}
}
}

View File

@ -17,14 +17,32 @@
// <https://www.gnu.org/licenses/> or write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.entity.sprites;
package com.openeggbert.entity.common;
import lombok.Getter;
/**
*
* Represents a row in a sprite sheet.
* This class is responsible for parsing and serializing sprite sheet rows from/to CSV format.
*
* @author robertvokac
*/
public class SpriteSheet {
public enum SpriteSheetRowColumn {
FILE("File"),
GROUP("Group"),
NUMBER_IN_GROUP("Number in Group"),
ROW("Row"),
COLUMN("Column"),
X("X"),
Y("Y"),
WIDTH("Width"),
HEIGHT("Height"),
NOTES("Notes"),
TAGS("Tags"),
NUMBER_PER_FILE("Number per file");
@Getter
private final String columnName;
SpriteSheetRowColumn(String columnName) {
this.columnName = columnName;
}
}