Skip to content

Commit

Permalink
Show Zoom percentage request (#10693)
Browse files Browse the repository at this point in the history
  • Loading branch information
FedaSkywalker authored Nov 23, 2023
1 parent d6bf80c commit 8fe64cb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class GameData implements Serializable, GameState {
@RemoveOnNextMajorRelease @Deprecated private Version gameVersion;
private int diceSides;
private transient List<TerritoryListener> territoryListeners = new CopyOnWriteArrayList<>();

private transient List<GameDataChangeListener> dataChangeListeners = new CopyOnWriteArrayList<>();
private transient Map<String, IDelegate> delegates = new HashMap<>();
private final AllianceTracker alliances = new AllianceTracker();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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 Down Expand Up @@ -43,7 +44,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 +56,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("");

public BottomBar(final UiContext uiContext, final GameData data, final boolean usingDiceServer) {
this.uiContext = uiContext;
Expand Down Expand Up @@ -83,8 +85,14 @@ private JPanel createCenterPanel() {
statusMessage.setVisible(false);
statusMessage.setPreferredSize(new Dimension(0, 0));
statusMessage.setBorder(new EtchedBorder(EtchedBorder.RAISED));

zoomLabel.setVisible(false);
zoomLabel.setBorder(new EtchedBorder(EtchedBorder.RAISED));

centerPanel.add(
statusMessage, gridBuilder.gridX(2).anchor(GridBagConstraintsAnchor.EAST).build());
centerPanel.add(
zoomLabel, gridBuilder.weightX(0).anchor(GridBagConstraintsAnchor.EAST).build());
return centerPanel;
}

Expand Down Expand Up @@ -264,6 +272,10 @@ public void setCurrentPlayer(GamePlayer player, boolean isRemotePlayer) {
playerLabel.setText((isRemotePlayer ? "REMOTE: " : "") + player.getName());
}

public void setMapZoomEnabled(boolean enabled) {
zoomLabel.setVisible(enabled);
}

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.
Expand Down Expand Up @@ -302,4 +314,9 @@ public void ownerChanged(Territory territory) {}

@Override
public void attachmentChanged(Territory territory) {}

@Override
public void zoomMapChanged(Integer newZoom) {
zoomLabel.setText(String.format("Zoom: %d%%", newZoom));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import games.strategy.engine.data.Unit;
import games.strategy.engine.data.events.GameDataChangeListener;
import games.strategy.engine.data.events.TerritoryListener;
import games.strategy.engine.data.events.ZoomMapListener;
import games.strategy.triplea.Constants;
import games.strategy.triplea.delegate.EditDelegate;
import games.strategy.triplea.delegate.Matches;
Expand Down Expand Up @@ -88,6 +89,7 @@ public class MapPanel extends ImageScrollerLargeView {
private final List<MapSelectionListener> mapSelectionListeners = new ArrayList<>();
private final List<UnitSelectionListener> unitSelectionListeners = new ArrayList<>();
private final List<MouseOverUnitListener> mouseOverUnitsListeners = new ArrayList<>();
private final List<ZoomMapListener> zoomMapListeners = new ArrayList<>();
private GameData gameData;
// the territory that the mouse is currently over
@Getter private @Nullable Territory currentTerritory;
Expand Down Expand Up @@ -470,6 +472,14 @@ public void setRoute(
SwingUtilities.invokeLater(this::repaint);
}

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

public void removeZoomMapListener(final ZoomMapListener listener) {
zoomMapListeners.remove(listener);
}

public void addMapSelectionListener(final MapSelectionListener listener) {
mapSelectionListeners.add(listener);
}
Expand Down Expand Up @@ -841,6 +851,8 @@ public double getScale() {
@Override
public void setScale(final double newScale) {
super.setScale(newScale);
zoomMapListeners.forEach(
(zoomMapListener -> zoomMapListener.zoomMapChanged((int) (scale * 100))));
// setScale will check bounds, and normalize the scale correctly
uiContext.setScale(scale);
repaint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ protected void paintComponent(final Graphics g) {
data.addGameDataEventListener(
GameDataEvent.TECH_ATTACHMENT_CHANGED, this::clearCachedUnitImages);
uiContext.addShutdownWindow(this);
mapPanel.addZoomMapListener(bottomBar);
}

private void clearCachedUnitImages() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ final class ViewMenu extends JMenu {
}
addShowMapDetails();
addShowMapBlends();
addShowZoomMenu();
addMapFontAndColorEditorMenu();
addChatTimeMenu();
addShowCommentLog();
Expand Down Expand Up @@ -324,6 +325,17 @@ private void addShowUnitsMenu() {
add(showUnitsBox);
}

private void addShowZoomMenu() {
final JCheckBoxMenuItem showMapZoomBox = new JCheckBoxMenuItem("Show Zoom Percentage");

showMapZoomBox.addActionListener(
e -> {
this.frame.getBottomBar().setMapZoomEnabled(showMapZoomBox.isSelected());
});

add(showMapZoomBox);
}

private void addShowUnitsInStatusBarMenu() {
JCheckBoxMenuItem checkbox = new JCheckBoxMenuItem("Show Units in Status Bar");
checkbox.setSelected(true);
Expand Down

0 comments on commit 8fe64cb

Please sign in to comment.