use of games.strategy.triplea.ui.UiContext in project triplea by triplea-game.
the class PlayerChatRenderer method setIconMap.
private void setIconMap() {
final PlayerManager playerManager = game.getPlayerManager();
PlayerList playerList;
game.getData().acquireReadLock();
try {
playerList = game.getData().getPlayerList();
} finally {
game.getData().releaseReadLock();
}
// new HashSet removes duplicates
for (final INode playerNode : new HashSet<>(playerManager.getPlayerMapping().values())) {
final Set<String> players = playerManager.getPlayedBy(playerNode);
if (players.size() > 0) {
final List<Icon> icons = players.stream().filter(player -> uiContext != null && uiContext.getFlagImageFactory() != null).map(player -> new ImageIcon(uiContext.getFlagImageFactory().getSmallFlag(playerList.getPlayerId(player)))).collect(Collectors.toList());
maxIconCounter = Math.max(maxIconCounter, icons.size());
playerMap.put(playerNode.toString(), players);
if (uiContext == null) {
iconMap.put(playerNode.toString(), null);
} else {
iconMap.put(playerNode.toString(), icons);
}
}
}
}
use of games.strategy.triplea.ui.UiContext in project triplea by triplea-game.
the class ScreenshotExporter method save.
private void save(final GameData gameData, final HistoryNode node, final File file) throws IOException {
// get round/step/player from history tree
int round = 0;
final Object[] pathFromRoot = node.getPath();
for (final Object pathNode : pathFromRoot) {
final HistoryNode curNode = (HistoryNode) pathNode;
if (curNode instanceof Round) {
round = ((Round) curNode).getRoundNo();
}
}
final UiContext uiContext = frame.getUiContext();
final double scale = uiContext.getScale();
// print map panel to image
final MapPanel mapPanel = frame.getMapPanel();
final BufferedImage mapImage = Util.createImage((int) (scale * mapPanel.getImageWidth()), (int) (scale * mapPanel.getImageHeight()), false);
final Graphics2D mapGraphics = mapImage.createGraphics();
try {
// workaround to get the whole map
// (otherwise the map is cut if current window is not on top of map)
final int offsetX = mapPanel.getXOffset();
final int offsetY = mapPanel.getYOffset();
mapPanel.setTopLeft(0, 0);
mapPanel.drawMapImage(mapGraphics);
mapPanel.setTopLeft(offsetX, offsetY);
// overlay title
Color titleColor = uiContext.getMapData().getColorProperty(MapData.PROPERTY_SCREENSHOT_TITLE_COLOR);
if (titleColor == null) {
titleColor = Color.BLACK;
}
final String encodedTitleX = uiContext.getMapData().getProperty(MapData.PROPERTY_SCREENSHOT_TITLE_X);
final String encodedTitleY = uiContext.getMapData().getProperty(MapData.PROPERTY_SCREENSHOT_TITLE_Y);
final String encodedTitleSize = uiContext.getMapData().getProperty(MapData.PROPERTY_SCREENSHOT_TITLE_FONT_SIZE);
int titleX;
int titleY;
int titleSize;
try {
titleX = (int) (Integer.parseInt(encodedTitleX) * scale);
titleY = (int) (Integer.parseInt(encodedTitleY) * scale);
titleSize = Integer.parseInt(encodedTitleSize);
} catch (final NumberFormatException nfe) {
// choose safe defaults
titleX = (int) (15 * scale);
titleY = (int) (15 * scale);
titleSize = 15;
}
// everything else should be scaled down onto map image
final AffineTransform transform = new AffineTransform();
transform.scale(scale, scale);
mapGraphics.setTransform(transform);
mapGraphics.setFont(new Font("Ariel", Font.BOLD, titleSize));
mapGraphics.setColor(titleColor);
if (uiContext.getMapData().getBooleanProperty(MapData.PROPERTY_SCREENSHOT_TITLE_ENABLED)) {
mapGraphics.drawString(gameData.getGameName() + " Round " + round, titleX, titleY);
}
// save Image as .png
ImageIO.write(mapImage, "png", file);
} finally {
// Clean up objects. There might be some overkill here,
// but there were memory leaks that are fixed by some/all of these.
mapImage.flush();
mapGraphics.dispose();
}
}
use of games.strategy.triplea.ui.UiContext in project triplea by triplea-game.
the class PlayerUnitsPanel method init.
/**
* Sets up components to an initial state.
*/
public void init(final PlayerID id, final List<Unit> units, final boolean land) {
isLand = land;
categories = new ArrayList<>(categorize(id, units));
categories.sort(Comparator.comparing(UnitCategory::getType, (ut1, ut2) -> {
final UnitAttachment u1 = UnitAttachment.get(ut1);
final UnitAttachment u2 = UnitAttachment.get(ut2);
// For land battles, sort by land, air, can't combat move (AA), bombarding
if (land) {
if (u1.getIsSea() != u2.getIsSea()) {
return u1.getIsSea() ? 1 : -1;
}
final boolean u1CanNotCombatMove = Matches.unitTypeCanNotMoveDuringCombatMove().test(ut1) || !Matches.unitTypeCanMove(id).test(ut1);
final boolean u2CanNotCombatMove = Matches.unitTypeCanNotMoveDuringCombatMove().test(ut2) || !Matches.unitTypeCanMove(id).test(ut2);
if (u1CanNotCombatMove != u2CanNotCombatMove) {
return u1CanNotCombatMove ? 1 : -1;
}
if (u1.getIsAir() != u2.getIsAir()) {
return u1.getIsAir() ? 1 : -1;
}
} else {
if (u1.getIsSea() != u2.getIsSea()) {
return u1.getIsSea() ? -1 : 1;
}
}
return u1.getName().compareTo(u2.getName());
}));
removeAll();
final Predicate<UnitType> predicate;
if (land) {
if (defender) {
predicate = Matches.unitTypeIsNotSea();
} else {
predicate = Matches.unitTypeIsNotSea().or(Matches.unitTypeCanBombard(id));
}
} else {
predicate = Matches.unitTypeIsSeaOrAir();
}
final IntegerMap<UnitType> costs;
try {
data.acquireReadLock();
costs = TuvUtils.getCostsForTuv(id, data);
} finally {
data.releaseReadLock();
}
for (final UnitCategory category : categories) {
if (predicate.test(category.getType())) {
final UnitPanel upanel = new UnitPanel(uiContext, category, costs);
upanel.addChangeListener(this::notifyListeners);
add(upanel);
}
}
// TODO: probably do not need to do this much revalidation.
invalidate();
validate();
revalidate();
getParent().invalidate();
}
use of games.strategy.triplea.ui.UiContext in project triplea by triplea-game.
the class HelpMenu method addUnitHelpMenu.
private void addUnitHelpMenu() {
final String unitHelpTitle = "Unit Help";
final JMenuItem unitMenuItem = add(SwingAction.of(unitHelpTitle, e -> {
final JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setContentType("text/html");
editorPane.setText(getUnitStatsTable(gameData, uiContext));
editorPane.setCaretPosition(0);
final JScrollPane scroll = new JScrollPane(editorPane);
scroll.setBorder(BorderFactory.createEmptyBorder());
final Dimension screenResolution = Toolkit.getDefaultToolkit().getScreenSize();
// not only do we have a start bar, but we also have the message dialog to account for just the scroll bars plus
// the window sides
final int availHeight = screenResolution.height - 120;
final int availWidth = screenResolution.width - 40;
scroll.setPreferredSize(new Dimension((scroll.getPreferredSize().width > availWidth ? availWidth : (scroll.getPreferredSize().height > availHeight ? Math.min(availWidth, scroll.getPreferredSize().width + 22) : scroll.getPreferredSize().width)), (scroll.getPreferredSize().height > availHeight ? availHeight : (scroll.getPreferredSize().width > availWidth ? Math.min(availHeight, scroll.getPreferredSize().height + 22) : scroll.getPreferredSize().height))));
final JDialog dialog = new JDialog((JFrame) null, unitHelpTitle);
dialog.add(scroll, BorderLayout.CENTER);
final JPanel buttons = new JPanel();
final JButton button = new JButton(SwingAction.of("OK", event -> {
dialog.setVisible(false);
dialog.removeAll();
dialog.dispose();
}));
buttons.add(button);
dialog.getRootPane().setDefaultButton(button);
dialog.add(buttons, BorderLayout.SOUTH);
dialog.pack();
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(final WindowEvent e) {
scroll.getVerticalScrollBar().getModel().setValue(0);
scroll.getHorizontalScrollBar().getModel().setValue(0);
button.requestFocus();
}
});
dialog.setVisible(true);
}));
unitMenuItem.setMnemonic(KeyEvent.VK_U);
unitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
}
use of games.strategy.triplea.ui.UiContext in project triplea by triplea-game.
the class ExportMenu method addExportUnitStats.
private void addExportUnitStats() {
final JMenuItem menuFileExport = new JMenuItem(SwingAction.of("Export Unit Charts", e -> {
final JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
final File rootDir = new File(SystemProperties.getUserDir());
String defaultFileName = gameData.getGameName() + "_unit_stats";
defaultFileName = FileNameUtils.removeIllegalCharacters(defaultFileName);
defaultFileName = defaultFileName + ".html";
chooser.setSelectedFile(new File(rootDir, defaultFileName));
if (chooser.showSaveDialog(frame) != JOptionPane.OK_OPTION) {
return;
}
try (Writer writer = Files.newBufferedWriter(chooser.getSelectedFile().toPath(), StandardCharsets.UTF_8)) {
writer.write(HelpMenu.getUnitStatsTable(gameData, uiContext).replaceAll("<p>", "<p>\r\n").replaceAll("</p>", "</p>\r\n").replaceAll("</tr>", "</tr>\r\n").replaceAll(LocalizeHtml.PATTERN_HTML_IMG_TAG, ""));
} catch (final IOException e1) {
ClientLogger.logQuietly("Failed to write unit stats: " + chooser.getSelectedFile().getAbsolutePath(), e1);
}
}));
menuFileExport.setMnemonic(KeyEvent.VK_U);
add(menuFileExport);
}
Aggregations