use of games.strategy.triplea.ui.mapdata.MapData in project triplea by triplea-game.
the class HeadedUiContext method internalSetMapDir.
@Override
protected void internalSetMapDir(final String dir, final GameData data) {
final Stopwatch stopWatch = new Stopwatch(logger, Level.FINE, "Loading UI Context");
resourceLoader = ResourceLoader.getMapResourceLoader(dir);
if (mapData != null) {
mapData.close();
}
mapData = new MapData(resourceLoader);
// DiceImageFactory needs loader and game data
diceImageFactory = new DiceImageFactory(resourceLoader, data.getDiceSides());
final double unitScale = getPreferencesMapOrSkin(dir).getDouble(UNIT_SCALE_PREF, mapData.getDefaultUnitScale());
scale = getPreferencesMapOrSkin(dir).getDouble(MAP_SCALE_PREF, 1);
if (scale < 1) {
setDrawTerritoryBordersAgainToMedium();
}
unitImageFactory.setResourceLoader(resourceLoader, unitScale, mapData.getDefaultUnitWidth(), mapData.getDefaultUnitHeight(), mapData.getDefaultUnitCounterOffsetWidth(), mapData.getDefaultUnitCounterOffsetHeight());
// TODO: separate scale for resources
resourceImageFactory.setResourceLoader(resourceLoader);
territoryEffectImageFactory.setResourceLoader(resourceLoader);
flagIconImageFactory.setResourceLoader(resourceLoader);
puImageFactory.setResourceLoader(resourceLoader);
tileImageFactory.setMapDir(resourceLoader);
tileImageFactory.setScale(scale);
// load map data
mapImage.loadMaps(resourceLoader);
mapDir = dir;
drawTerritoryEffects = mapData.useTerritoryEffectMarkers();
// load the sounds in a background thread,
// avoids the pause where sounds dont load right away
// change the resource loader (this allows us to play sounds the map folder, rather than just default sounds)
new Thread(() -> ClipPlayer.getInstance(resourceLoader), "Triplea sound loader").start();
// load a new cursor
cursor = Cursor.getDefaultCursor();
final Toolkit toolkit = Toolkit.getDefaultToolkit();
// URL's use "/" not "\"
final URL cursorUrl = resourceLoader.getResource("misc" + "/" + "cursor.gif");
if (cursorUrl != null) {
try {
final Image image = ImageIO.read(cursorUrl);
if (image != null) {
final Point hotSpot = new Point(mapData.getMapCursorHotspotX(), mapData.getMapCursorHotspotY());
cursor = toolkit.createCustomCursor(image, hotSpot, data.getGameName() + " Cursor");
}
} catch (final Exception e) {
ClientLogger.logQuietly("Failed to create cursor from: " + cursorUrl, e);
}
}
stopWatch.done();
}
use of games.strategy.triplea.ui.mapdata.MapData in project triplea by triplea-game.
the class ReliefImageBreaker method createMaps.
/**
* One of the main methods that is used to create the actual maps. Calls on
* various methods to get user input and create the maps.
*/
private void createMaps() throws IOException {
// ask user to input image location
final Image map = loadImage();
if (map == null) {
ToolLogger.info("You need to select a map image for this to work");
ToolLogger.info("Shutting down");
return;
}
// ask user wether it is sea zone only or not
seaZoneOnly = doSeaZone();
// ask user where the map is
final String mapDir = getMapDirectory();
if (mapDir == null || mapDir.isEmpty()) {
ToolLogger.info("You need to specify a map name for this to work");
ToolLogger.info("Shutting down");
return;
}
try {
mapData = new MapData(mapDir);
// files for the map.
} catch (final NullPointerException e) {
ToolLogger.error("Bad data given or missing text files, shutting down", e);
return;
}
for (final String territoryName : mapData.getTerritories()) {
final boolean seaZone = Util.isTerritoryNameIndicatingWater(territoryName);
if (!seaZone && seaZoneOnly) {
continue;
}
if (seaZone && !seaZoneOnly) {
continue;
}
processImage(territoryName, map);
}
ToolLogger.info("All Finished!");
}
use of games.strategy.triplea.ui.mapdata.MapData in project triplea by triplea-game.
the class AutoPlacementFinder method calculate.
/**
* calculate()
* Will calculate the placements on the map automatically.
*/
private void calculate() {
// ask user where the map is
final String mapDir = mapFolderLocation == null ? getMapDirectory() : mapFolderLocation.getName();
if (mapDir == null) {
ToolLogger.info("You need to specify a map name for this to work");
ToolLogger.info("Shutting down");
return;
}
final File file = getMapPropertiesFile(mapDir);
if (file.exists() && mapFolderLocation == null) {
mapFolderLocation = file.getParentFile();
}
if (!placeDimensionsSet) {
try {
if (file.exists()) {
double scale = unitZoomPercent;
int width = unitWidth;
int height = unitHeight;
boolean found = false;
try (Scanner scanner = new Scanner(file, StandardCharsets.UTF_8.name())) {
final String heightProperty = MapData.PROPERTY_UNITS_HEIGHT + "=";
final String widthProperty = MapData.PROPERTY_UNITS_WIDTH + "=";
final String scaleProperty = MapData.PROPERTY_UNITS_SCALE + "=";
while (scanner.hasNextLine()) {
final String line = scanner.nextLine();
if (line.contains(scaleProperty)) {
try {
scale = Double.parseDouble(line.substring(line.indexOf(scaleProperty) + scaleProperty.length()).trim());
found = true;
} catch (final NumberFormatException ex) {
// ignore malformed input
}
}
if (line.contains(widthProperty)) {
try {
width = Integer.parseInt(line.substring(line.indexOf(widthProperty) + widthProperty.length()).trim());
found = true;
} catch (final NumberFormatException ex) {
// ignore malformed input
}
}
if (line.contains(heightProperty)) {
try {
height = Integer.parseInt(line.substring(line.indexOf(heightProperty) + heightProperty.length()).trim());
found = true;
} catch (final NumberFormatException ex) {
// ignore malformed input
}
}
}
}
if (found) {
final int result = JOptionPane.showConfirmDialog(new JPanel(), "A map.properties file was found in the map's folder, " + "\r\n do you want to use the file to supply the info for the placement box size? " + "\r\n Zoom = " + scale + ", Width = " + width + ", Height = " + height + ", Result = (" + ((int) (scale * width)) + "x" + ((int) (scale * height)) + ")", "File Suggestion", JOptionPane.YES_NO_CANCEL_OPTION);
if (result == 0) {
unitZoomPercent = scale;
placeWidth = (int) (unitZoomPercent * width);
placeHeight = (int) (unitZoomPercent * height);
placeDimensionsSet = true;
}
}
}
} catch (final Exception e) {
ToolLogger.error("Failed to initialize from map properties: " + file.getAbsolutePath(), e);
}
}
if (!placeDimensionsSet || JOptionPane.showConfirmDialog(new JPanel(), "Placement Box Size already set (" + placeWidth + "x" + placeHeight + "), " + "do you wish to continue with this?\r\n" + "Select Yes to continue, Select No to override and change the size.", "Placement Box Size", JOptionPane.YES_NO_OPTION) == 1) {
try {
final String result = getUnitsScale();
try {
unitZoomPercent = Double.parseDouble(result.toLowerCase());
} catch (final NumberFormatException ex) {
// ignore malformed input
}
final String width = JOptionPane.showInputDialog(null, "Enter the unit's image width in pixels (unscaled / without zoom).\r\n(e.g. 48)");
if (width != null) {
try {
placeWidth = (int) (unitZoomPercent * Integer.parseInt(width));
} catch (final NumberFormatException ex) {
// ignore malformed input
}
}
final String height = JOptionPane.showInputDialog(null, "Enter the unit's image height in pixels (unscaled / without zoom).\r\n(e.g. 48)");
if (height != null) {
try {
placeHeight = (int) (unitZoomPercent * Integer.parseInt(height));
} catch (final NumberFormatException ex) {
// ignore malformed input
}
}
placeDimensionsSet = true;
} catch (final Exception e) {
ToolLogger.error("Failed to initialize from user input", e);
}
}
final MapData mapData;
try {
// makes TripleA read all the text data files for the map.
mapData = new MapData(mapDir);
} catch (final Exception e) {
JOptionPane.showMessageDialog(null, new JLabel("Could not find map. Make sure it is in finalized location and contains centers.txt and polygons.txt"));
ToolLogger.error("Caught Exception.");
ToolLogger.error("Could be due to some missing text files.");
ToolLogger.error("Or due to the map folder not being in the right location.", e);
return;
}
textOptionPane.show();
textOptionPane.appendNewLine("Place Dimensions in pixels, being used: " + placeWidth + "x" + placeHeight + "\r\n");
textOptionPane.appendNewLine("Calculating, this may take a while...\r\n");
final Map<String, List<Point>> placements = new HashMap<>();
for (final String name : mapData.getTerritories()) {
final List<Point> points;
if (mapData.hasContainedTerritory(name)) {
final Set<Polygon> containedPolygons = new HashSet<>();
for (final String containedName : mapData.getContainedTerritory(name)) {
containedPolygons.addAll(mapData.getPolygons(containedName));
}
points = getPlacementsStartingAtTopLeft(mapData.getPolygons(name), mapData.getBoundingRect(name), mapData.getCenter(name), containedPolygons);
placements.put(name, points);
} else {
points = getPlacementsStartingAtMiddle(mapData.getPolygons(name), mapData.getBoundingRect(name), mapData.getCenter(name));
placements.put(name, points);
}
textOptionPane.appendNewLine(name + ": " + points.size());
}
// while
textOptionPane.appendNewLine("\r\nAll Finished!");
textOptionPane.countDown();
final String fileName = new FileSave("Where To Save place.txt ?", "place.txt", mapFolderLocation).getPathString();
if (fileName == null) {
textOptionPane.appendNewLine("You chose not to save, Shutting down");
textOptionPane.dispose();
return;
}
try (OutputStream os = new FileOutputStream(fileName)) {
PointFileReaderWriter.writeOneToMany(os, placements);
textOptionPane.appendNewLine("Data written to :" + new File(fileName).getCanonicalPath());
} catch (final IOException e) {
ToolLogger.error("Failed to write points file: " + fileName, e);
textOptionPane.dispose();
return;
}
textOptionPane.dispose();
}
use of games.strategy.triplea.ui.mapdata.MapData in project triplea by triplea-game.
the class TileManager method getTiles.
/**
* Selects tiles which fall into rectangle bounds.
*
* @param bounds
* rectangle for selection
* @return tiles which fall into the rectangle
*/
public List<Tile> getTiles(final Rectangle2D bounds) {
// if the rectangle exceeds the map dimensions we to do shift the rectangle and check for each shifted rectangle as
// well as the original
// rectangle
final MapData mapData = uiContext.getMapData();
final Dimension mapDimensions = mapData.getMapDimensions();
final boolean testXshift = (mapData.scrollWrapX() && (bounds.getMaxX() > mapDimensions.width || bounds.getMinX() < 0));
final boolean testYshift = (mapData.scrollWrapY() && (bounds.getMaxY() > mapDimensions.height || bounds.getMinY() < 0));
Rectangle2D boundsXshift = null;
if (testXshift) {
if (bounds.getMinX() < 0) {
boundsXshift = new Rectangle((int) bounds.getMinX() + mapDimensions.width, (int) bounds.getMinY(), (int) bounds.getWidth(), (int) bounds.getHeight());
} else {
boundsXshift = new Rectangle((int) bounds.getMinX() - mapDimensions.width, (int) bounds.getMinY(), (int) bounds.getWidth(), (int) bounds.getHeight());
}
}
Rectangle2D boundsYshift = null;
if (testYshift) {
if (bounds.getMinY() < 0) {
boundsYshift = new Rectangle((int) bounds.getMinX(), (int) bounds.getMinY() + mapDimensions.height, (int) bounds.getWidth(), (int) bounds.getHeight());
} else {
boundsYshift = new Rectangle((int) bounds.getMinX(), (int) bounds.getMinY() - mapDimensions.height, (int) bounds.getWidth(), (int) bounds.getHeight());
}
}
acquireLock();
try {
final List<Tile> tilesInBounds = new ArrayList<>();
for (final Tile tile : tiles) {
final Rectangle tileBounds = tile.getBounds();
if (bounds.contains(tileBounds) || tileBounds.intersects(bounds)) {
tilesInBounds.add(tile);
}
}
if (boundsXshift != null) {
for (final Tile tile : tiles) {
final Rectangle tileBounds = tile.getBounds();
if (boundsXshift.contains(tileBounds) || tileBounds.intersects(boundsXshift)) {
tilesInBounds.add(tile);
}
}
}
if (boundsYshift != null) {
for (final Tile tile : tiles) {
final Rectangle tileBounds = tile.getBounds();
if (boundsYshift.contains(tileBounds) || tileBounds.intersects(boundsYshift)) {
tilesInBounds.add(tile);
}
}
}
return tilesInBounds;
} finally {
releaseLock();
}
}
Aggregations