Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TripleA-2.6.929 show Zoom percentage request #10693 #12082

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.base.MoreObjects;
import games.strategy.engine.data.events.GameDataChangeListener;
import games.strategy.engine.data.events.TerritoryListener;
import games.strategy.engine.data.events.ZoomMapListener;
import games.strategy.engine.data.properties.GameProperties;
import games.strategy.engine.delegate.IDelegate;
import games.strategy.engine.framework.GameDataManager;
Expand Down Expand Up @@ -80,6 +81,7 @@ public class GameData implements Serializable, GameState {
@RemoveOnNextMajorRelease @Deprecated private Version gameVersion;
private int diceSides;
private transient List<TerritoryListener> territoryListeners = new CopyOnWriteArrayList<>();
private final transient List<ZoomMapListener> zoomMapListeners = new CopyOnWriteArrayList<>();
private transient List<GameDataChangeListener> dataChangeListeners = new CopyOnWriteArrayList<>();
private transient Map<String, IDelegate> delegates = new HashMap<>();
private final AllianceTracker alliances = new AllianceTracker();
Expand All @@ -101,12 +103,12 @@ public class GameData implements Serializable, GameState {
private final GameProperties properties = new GameProperties(this);
private final UnitsList unitsList = new UnitsList();
private final TechnologyFrontier technologyFrontier =
new TechnologyFrontier("allTechsForGame", this);
new TechnologyFrontier("allTechsForGame", this);
@Getter private transient TechTracker techTracker = new TechTracker(this);
private final IGameLoader loader = new TripleA();
private History gameHistory = new History(this);
private List<Tuple<IAttachment, List<Tuple<String, String>>>> attachmentOrderAndValues =
new ArrayList<>();
new ArrayList<>();
private final Map<String, TerritoryEffect> territoryEffectList = new HashMap<>();
private final BattleRecordsList battleRecordsList = new BattleRecordsList(this);
private transient GameDataEventListeners gameDataEventListeners = new GameDataEventListeners();
Expand Down Expand Up @@ -258,6 +260,10 @@ public GameProperties getProperties() {
return properties;
}

public void addZoomMapListeners(final ZoomMapListener listener) {
zoomMapListeners.add(listener);
}

public void addTerritoryListener(final TerritoryListener listener) {
territoryListeners.add(listener);
}
Expand All @@ -284,6 +290,10 @@ void notifyTerritoryUnitsChanged(final Territory t) {
territoryListeners.forEach(territoryListener -> territoryListener.unitsChanged(t));
}

public void notifyMapZoomChanged(Integer newZoom) {
zoomMapListeners.forEach(zoomMapListener -> zoomMapListener.zoomMapChanged(newZoom));
}

void notifyTerritoryAttachmentChanged(final Territory t) {
territoryListeners.forEach(territoryListener -> territoryListener.attachmentChanged(t));
}
Expand Down Expand Up @@ -361,9 +371,9 @@ public void resetHistory() {
final boolean oldForceInSwingEventThread = forceInSwingEventThread;
forceInSwingEventThread = false;
gameHistory
.getHistoryWriter()
.startNextStep(
step.getName(), step.getDelegateName(), step.getPlayerId(), step.getDisplayName());
.getHistoryWriter()
.startNextStep(
step.getName(), step.getDelegateName(), step.getPlayerId(), step.getDisplayName());
forceInSwingEventThread = oldForceInSwingEventThread;
}

Expand Down Expand Up @@ -446,7 +456,7 @@ private static Unlocker acquireLock(Lock lock) {
}

public void addToAttachmentOrderAndValues(
final Tuple<IAttachment, List<Tuple<String, String>>> attachmentAndValues) {
final Tuple<IAttachment, List<Tuple<String, String>>> attachmentAndValues) {
attachmentOrderAndValues.add(attachmentAndValues);
}

Expand All @@ -455,7 +465,7 @@ public List<Tuple<IAttachment, List<Tuple<String, String>>>> getAttachmentOrderA
}

public void setAttachmentOrderAndValues(
List<Tuple<IAttachment, List<Tuple<String, String>>>> values) {
List<Tuple<IAttachment, List<Tuple<String, String>>>> values) {
attachmentOrderAndValues = values;
}

Expand Down Expand Up @@ -520,12 +530,12 @@ public TechnologyDelegate getTechDelegate() {
public void preGameDisablePlayers(final Predicate<GamePlayer> shouldDisablePlayer) {
final Set<GamePlayer> playersWhoShouldBeRemoved = new HashSet<>();
playerList.getPlayers().stream()
.filter(p -> (p.getCanBeDisabled() && shouldDisablePlayer.test(p)))
.forEach(
p -> {
p.setIsDisabled(true);
playersWhoShouldBeRemoved.add(p);
});
.filter(p -> (p.getCanBeDisabled() && shouldDisablePlayer.test(p)))
.forEach(
p -> {
p.setIsDisabled(true);
playersWhoShouldBeRemoved.add(p);
});
if (!playersWhoShouldBeRemoved.isEmpty()) {
removePlayerStepsFromSequence(playersWhoShouldBeRemoved);
}
Expand Down Expand Up @@ -564,12 +574,12 @@ public void performChange(final Change change) {
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("diceSides", diceSides)
.add("gameName", gameName)
.add("gameVersion", gameVersion)
.add("loader", loader)
.add("playerList", playerList)
.toString();
.add("diceSides", diceSides)
.add("gameName", gameName)
.add("gameVersion", gameVersion)
.add("loader", loader)
.add("playerList", playerList)
.toString();
}

/**
Expand Down Expand Up @@ -603,11 +613,11 @@ public String loadGameNotes(final Path mapLocation) {
public Optional<Path> getGameXmlPath(final Path mapLocation) {
// Given a game name, the map.yml file can tell us the path to the game xml file.
return findMapDescriptionYaml(mapLocation)
.flatMap(yaml -> yaml.getGameXmlPathByGameName(getGameName()));
.flatMap(yaml -> yaml.getGameXmlPathByGameName(getGameName()));
}

private Optional<MapDescriptionYaml> findMapDescriptionYaml(final Path mapLocation) {
return FileUtils.findFileInParentFolders(mapLocation, MapDescriptionYaml.MAP_YAML_FILE_NAME)
.flatMap(MapDescriptionYaml::fromFile);
.flatMap(MapDescriptionYaml::fromFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package games.strategy.engine.data.events;

/**
* A ZoomMapListener will be notified of events that affect a map zoom in ViewMenu in onClick on OK button.
* */
public interface ZoomMapListener {
void zoomMapChanged(Integer newZoom);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import games.strategy.engine.data.Territory;
import games.strategy.engine.data.TerritoryEffect;
import games.strategy.engine.data.events.TerritoryListener;
import games.strategy.engine.data.events.ZoomMapListener;
import games.strategy.triplea.Constants;
import games.strategy.triplea.attachments.TerritoryAttachment;
import games.strategy.triplea.util.UnitCategory;
Expand All @@ -17,6 +18,7 @@
import java.awt.Image;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
Expand All @@ -43,7 +45,7 @@
import org.triplea.swing.jpanel.GridBagConstraintsFill;

@Slf4j
public class BottomBar extends JPanel implements TerritoryListener {
public class BottomBar extends JPanel implements TerritoryListener, ZoomMapListener {
private final UiContext uiContext;

private final ResourceBar resourceBar;
Expand All @@ -55,6 +57,7 @@ public class BottomBar extends JPanel implements TerritoryListener {
private final JLabel playerLabel = new JLabel("xxxxxx");
private final JLabel stepLabel = new JLabel("xxxxxx");
private final JLabel roundLabel = new JLabel("xxxxxx");
private final JLabel zoomLabel = new JLabel("Zoom: 100%");

public BottomBar(final UiContext uiContext, final GameData data, final boolean usingDiceServer) {
this.uiContext = uiContext;
Expand All @@ -63,28 +66,32 @@ public BottomBar(final UiContext uiContext, final GameData data, final boolean u
setLayout(new BorderLayout());
add(createCenterPanel(), BorderLayout.CENTER);
add(createStepPanel(usingDiceServer), BorderLayout.EAST);

data.addZoomMapListeners(this);
}

private JPanel createCenterPanel() {
final JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridBagLayout());
final var gridBuilder =
new GridBagConstraintsBuilder().weightY(1).fill(GridBagConstraintsFill.BOTH);
new GridBagConstraintsBuilder().weightY(1).fill(GridBagConstraintsFill.BOTH);

centerPanel.add(
resourceBar, gridBuilder.weightX(0).anchor(GridBagConstraintsAnchor.WEST).build());
resourceBar, gridBuilder.weightX(0).anchor(GridBagConstraintsAnchor.WEST).build());

territoryInfo.setPreferredSize(new Dimension(0, 0));
territoryInfo.setBorder(new EtchedBorder(EtchedBorder.RAISED));
centerPanel.add(
territoryInfo,
gridBuilder.gridX(1).weightX(1).anchor(GridBagConstraintsAnchor.CENTER).build());
territoryInfo,
gridBuilder.gridX(1).weightX(1).anchor(GridBagConstraintsAnchor.CENTER).build());

statusMessage.setVisible(false);
statusMessage.setPreferredSize(new Dimension(0, 0));
statusMessage.setBorder(new EtchedBorder(EtchedBorder.RAISED));
centerPanel.add(
statusMessage, gridBuilder.gridX(2).anchor(GridBagConstraintsAnchor.EAST).build());
statusMessage, gridBuilder.gridX(2).anchor(GridBagConstraintsAnchor.EAST).build());
centerPanel.add(
zoomLabel, gridBuilder.weightX(0).anchor(GridBagConstraintsAnchor.EAST).build());
return centerPanel;
}

Expand Down Expand Up @@ -123,30 +130,30 @@ public void setTerritory(final @Nullable Territory territory) {

if (territory == null) {
SwingUtilities.invokeLater(
() -> {
territoryInfo.removeAll();
SwingComponents.redraw(territoryInfo);
});
() -> {
territoryInfo.removeAll();
SwingComponents.redraw(territoryInfo);
});
return;
}

// Get all the needed data while holding a lock, then invoke UI updates on the EDT.
try (GameData.Unlocker ignored = territory.getData().acquireReadLock()) {
final String territoryName = territory.getName();
final Collection<UnitCategory> units =
uiContext.isShowUnitsInStatusBar()
? UnitSeparator.categorize(territory.getUnits())
: List.of();
uiContext.isShowUnitsInStatusBar()
? UnitSeparator.categorize(territory.getUnits())
: List.of();
final TerritoryAttachment ta = TerritoryAttachment.get(territory);
final IntegerMap<Resource> resources = new IntegerMap<>();
final List<String> territoryEffectNames;
if (ta == null) {
territoryEffectNames = List.of();
} else {
territoryEffectNames =
ta.getTerritoryEffect().stream()
.map(TerritoryEffect::getName)
.collect(Collectors.toList());
ta.getTerritoryEffect().stream()
.map(TerritoryEffect::getName)
.collect(Collectors.toList());
final int production = ta.getProduction();
if (production > 0) {
resources.add(new Resource(Constants.PUS, territory.getData()), production);
Expand All @@ -155,15 +162,15 @@ public void setTerritory(final @Nullable Territory territory) {
}

SwingUtilities.invokeLater(
() -> updateTerritoryInfo(territoryName, territoryEffectNames, units, resources));
() -> updateTerritoryInfo(territoryName, territoryEffectNames, units, resources));
}
}

private void updateTerritoryInfo(
String territoryName,
List<String> territoryEffectNames,
Collection<UnitCategory> units,
IntegerMap<Resource> resources) {
String territoryName,
List<String> territoryEffectNames,
Collection<UnitCategory> units,
IntegerMap<Resource> resources) {
// Box layout with horizontal glue on both sides achieves the following desirable properties:
// 1. If the content is narrower than the available space, it will be centered.
// 2. If the content is wider than the available space, then the beginning will be shown,
Expand Down Expand Up @@ -246,7 +253,7 @@ public void gameDataChanged() {
}

public void setStepInfo(
int roundNumber, String stepName, @Nullable GamePlayer player, boolean isRemotePlayer) {
int roundNumber, String stepName, @Nullable GamePlayer player, boolean isRemotePlayer) {
roundLabel.setText("Round:" + roundNumber + " ");
stepLabel.setText(stepName);
if (player != null) {
Expand All @@ -256,38 +263,38 @@ public void setStepInfo(

public void setCurrentPlayer(GamePlayer player, boolean isRemotePlayer) {
final CompletableFuture<?> future =
CompletableFuture.supplyAsync(() -> uiContext.getFlagImageFactory().getFlag(player))
.thenApplyAsync(ImageIcon::new)
.thenAccept(icon -> SwingUtilities.invokeLater(() -> roundLabel.setIcon(icon)));
CompletableFuture.supplyAsync(() -> uiContext.getFlagImageFactory().getFlag(player))
.thenApplyAsync(ImageIcon::new)
.thenAccept(icon -> SwingUtilities.invokeLater(() -> roundLabel.setIcon(icon)));
CompletableFutureUtils.logExceptionWhenComplete(
future, throwable -> log.error("Failed to set round icon for " + player, throwable));
future, throwable -> log.error("Failed to set round icon for " + player, throwable));
playerLabel.setText((isRemotePlayer ? "REMOTE: " : "") + player.getName());
}

private void listenForTerritoryUpdates(@Nullable Territory territory) {
// Run async, as this is called while holding a GameData lock so we shouldn't grab a different
// data's lock in this case.
AsyncRunner.runAsync(
() -> {
GameData oldGameData = currentTerritory != null ? currentTerritory.getData() : null;
GameData newGameData = territory != null ? territory.getData() : null;
// Re-subscribe listener on the right GameData, which could change when toggling
// between history and the current game.
if (!ObjectUtils.referenceEquals(oldGameData, newGameData)) {
if (oldGameData != null) {
try (GameData.Unlocker ignored = oldGameData.acquireWriteLock()) {
oldGameData.removeTerritoryListener(this);
}
}
if (newGameData != null) {
try (GameData.Unlocker ignored = newGameData.acquireWriteLock()) {
newGameData.addTerritoryListener(this);
}
}
}
currentTerritory = territory;
})
.exceptionally(e -> log.error("Territory listener error:", e));
() -> {
GameData oldGameData = currentTerritory != null ? currentTerritory.getData() : null;
GameData newGameData = territory != null ? territory.getData() : null;
// Re-subscribe listener on the right GameData, which could change when toggling
// between history and the current game.
if (!ObjectUtils.referenceEquals(oldGameData, newGameData)) {
if (oldGameData != null) {
try (GameData.Unlocker ignored = oldGameData.acquireWriteLock()) {
oldGameData.removeTerritoryListener(this);
}
}
if (newGameData != null) {
try (GameData.Unlocker ignored = newGameData.acquireWriteLock()) {
newGameData.addTerritoryListener(this);
}
}
}
currentTerritory = territory;
})
.exceptionally(e -> log.error("Territory listener error:", e));
}

@Override
Expand All @@ -302,4 +309,11 @@ public void ownerChanged(Territory territory) {}

@Override
public void attachmentChanged(Territory territory) {}

@Override
public void zoomMapChanged(Integer newZoom) {
if (Objects.nonNull(newZoom)) {
zoomLabel.setText(String.format("Zoom: %d%%", newZoom));
}
}
}
Loading
Loading