Skip to content

Commit

Permalink
Merge pull request #5042 from kwvanderlinde/feature/5014-dissociate-t…
Browse files Browse the repository at this point in the history
…opology-type-from-zone

Remove topology type selections from zones
  • Loading branch information
cwisniew authored Nov 14, 2024
2 parents cb93818 + 9a4f5e7 commit 2a87dc7
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 140 deletions.
52 changes: 38 additions & 14 deletions src/main/java/net/rptools/maptool/client/AppStatePersisted.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.prefs.Preferences;
import java.util.stream.Collectors;
import net.rptools.maptool.model.Zone;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -65,6 +67,9 @@ public class AppStatePersisted {
/** Represents the key used to save the paint textures to the preferences. */
private static final String KEY_SAVED_PAINT_TEXTURES = "savedTextures";

/** Will be null until read from preferences. */
private static EnumSet<Zone.TopologyType> topologyTypes = null;

public static void clearAssetRoots() {
prefs.put(KEY_ASSET_ROOTS, "");
}
Expand Down Expand Up @@ -167,38 +172,57 @@ public static List<File> getMruCampaigns() {
return mruCampaigns;
}

public static Zone.TopologyTypeSet getTopologyTypes() {
private static EnumSet<Zone.TopologyType> readTopologyTypes() {
try {
String typeNames = prefs.get(KEY_TOPOLOGY_TYPES, "");
if ("".equals(typeNames)) {
// Fallback to the key used prior to the introduction of various VBL types.
String oldDrawingMode = prefs.get(KEY_OLD_TOPOLOGY_DRAWING_MODE, DEFAULT_TOPOLOGY_TYPE);
return switch (oldDrawingMode) {
case "VBL" -> new Zone.TopologyTypeSet(Zone.TopologyType.WALL_VBL);
case "MBL" -> new Zone.TopologyTypeSet(Zone.TopologyType.MBL);
case "COMBINED" -> new Zone.TopologyTypeSet(
Zone.TopologyType.WALL_VBL, Zone.TopologyType.MBL);
default -> new Zone.TopologyTypeSet(Zone.TopologyType.WALL_VBL);
case "VBL" -> EnumSet.of(Zone.TopologyType.WALL_VBL);
case "MBL" -> EnumSet.of(Zone.TopologyType.MBL);
case "COMBINED" -> EnumSet.of(Zone.TopologyType.WALL_VBL, Zone.TopologyType.MBL);
default -> EnumSet.of(Zone.TopologyType.WALL_VBL);
};
} else {
return Zone.TopologyTypeSet.valueOf(typeNames);
var result = EnumSet.noneOf(Zone.TopologyType.class);
for (var topologyType : Zone.TopologyType.values()) {
if (typeNames.contains(topologyType.name())) {
result.add(topologyType);
}
}
return result;
}
} catch (Exception exc) {
return new Zone.TopologyTypeSet(Zone.TopologyType.WALL_VBL);
return EnumSet.of(Zone.TopologyType.WALL_VBL);
}
}

private static void writeTopologyTypes(Set<Zone.TopologyType> types) {
String joined = types.stream().map(Enum::name).collect(Collectors.joining(",", "[", "]"));
prefs.put(KEY_TOPOLOGY_TYPES, joined);
}

public static Set<Zone.TopologyType> getTopologyTypes() {
if (topologyTypes == null) {
topologyTypes = readTopologyTypes();
}
return topologyTypes;
}

/**
* Sets the selected topology modes.
*
* @param types the topology types. A value of null resets to default.
* @param types the topology types.
*/
public static void setTopologyTypes(Zone.TopologyTypeSet types) {
if (types == null) {
prefs.remove(KEY_TOPOLOGY_TYPES);
} else {
prefs.put(KEY_TOPOLOGY_TYPES, types.toString());
public static void setTopologyTypes(Set<Zone.TopologyType> types) {
if (topologyTypes == null) {
topologyTypes = EnumSet.noneOf(Zone.TopologyType.class);
}
topologyTypes.clear();
topologyTypes.addAll(types);

writeTopologyTypes(topologyTypes);
}

public static void setSavedPaintTextures(List<File> savedTextures) {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/net/rptools/maptool/client/MapTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,6 @@ private static void initialize() {
zoneLoadedListener = new ZoneLoadedListener();

Campaign cmpgn = CampaignFactory.createBasicCampaign();
// Set the Topology drawing mode to the last mode used for convenience
// Should only be one zone, but let's cover our bases.
cmpgn.getZones().forEach(zone -> zone.setTopologyTypes(AppStatePersisted.getTopologyTypes()));

// Stop the pre-init client/server.
disconnect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,24 @@
package net.rptools.maptool.client.swing;

import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import javax.swing.Box;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import net.rptools.maptool.client.AppStatePersisted;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.ui.theme.Icons;
import net.rptools.maptool.client.ui.theme.RessourceManager;
import net.rptools.maptool.client.ui.zone.renderer.ZoneRenderer;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.Zone;

public class TopologyModeSelectionPanel extends JToolBar {
/** The instance. Used to update the button when the ZoneRenderer is changed. */
private static TopologyModeSelectionPanel instance;

public static TopologyModeSelectionPanel getInstance() {
return instance;
}

private final Map<Zone.TopologyType, JToggleButton> modeButtons;

public TopologyModeSelectionPanel() {
instance = this;

setFloatable(false);
setRollover(true);
setBorder(null);
Expand Down Expand Up @@ -89,7 +80,7 @@ private void createAndAddModeButton(
final Icons icon,
final Icons offIcon,
String toolTipKey,
Zone.TopologyTypeSet initiallySelectedTypes) {
Set<Zone.TopologyType> initiallySelectedTypes) {
final var button = new JToggleButton();

button.setIcon(RessourceManager.getBigIcon(offIcon));
Expand All @@ -103,40 +94,21 @@ private void createAndAddModeButton(
new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
ZoneRenderer zr = MapTool.getFrame().getCurrentZoneRenderer();
if (zr != null) {
var zone = zr.getZone();
var mode = zone.getTopologyTypes();
if (button.isSelected()) {
mode = mode.with(type);
} else {
mode = mode.without(type);
}

setMode(mode);
}
// Remember the selection for the next time MT starts.
AppStatePersisted.setTopologyTypes(getMode());
}
});
}

public void setMode(Zone.TopologyTypeSet topologyTypes) {
AppStatePersisted.setTopologyTypes(topologyTypes);
if (topologyTypes == null) {
topologyTypes = AppStatePersisted.getTopologyTypes();
}

public Set<Zone.TopologyType> getMode() {
var result = EnumSet.noneOf(Zone.TopologyType.class);
for (final var entry : modeButtons.entrySet()) {
final var topologyType = entry.getKey();
final var button = entry.getValue();

button.setSelected(topologyTypes.contains(topologyType));
}

// Since setting selection also triggers change listeners, we need this work even early on.
ZoneRenderer zr = MapTool.getFrame().getCurrentZoneRenderer();
// Check if there is a map. Fix #1605
if (zr != null) {
zr.getZone().setTopologyTypes(topologyTypes);
if (button.isSelected()) {
result.add(topologyType);
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import javax.annotation.Nullable;
import javax.swing.SwingUtilities;
import net.rptools.maptool.client.AppStatePersisted;
import net.rptools.maptool.client.AppStyle;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.ui.zone.renderer.ZoneRenderer;
Expand Down Expand Up @@ -102,7 +103,7 @@ private void submit(Shape shape) {
}

MapTool.serverCommand()
.updateTopology(getZone(), area, isEraser(), getZone().getTopologyTypes());
.updateTopology(getZone(), area, isEraser(), AppStatePersisted.getTopologyTypes());
}

private Area getTokenTopology(Zone.TopologyType topologyType) {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/net/rptools/maptool/client/ui/MapToolFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import net.rptools.maptool.client.swing.SpacerStatusBar;
import net.rptools.maptool.client.swing.StatusPanel;
import net.rptools.maptool.client.swing.SwingUtil;
import net.rptools.maptool.client.swing.TopologyModeSelectionPanel;
import net.rptools.maptool.client.swing.ZoomStatusBar;
import net.rptools.maptool.client.swing.colorpicker.ColorPicker;
import net.rptools.maptool.client.swing.preference.WindowPreferences;
Expand Down Expand Up @@ -1625,8 +1624,6 @@ public void setCurrentZoneRenderer(ZoneRenderer renderer) {
eventBus.post(new ZoneActivated(renderer.getZone()));

renderer.requestFocusInWindow();
// Updates the VBL/MBL button. Fixes #1642.
TopologyModeSelectionPanel.getInstance().setMode(renderer.getZone().getTopologyTypes());
}
AppActions.updateActions();
repaint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import net.rptools.maptool.client.AppStatePersisted;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.macro.MacroContext;
import net.rptools.maptool.client.swing.colorpicker.ColorPicker;
Expand Down Expand Up @@ -590,7 +591,7 @@ private void VblTool(Drawable drawable, boolean pathOnly, boolean isEraser) {
}

MapTool.serverCommand()
.updateTopology(renderer.getZone(), area, isEraser, renderer.getZone().getTopologyTypes());
.updateTopology(renderer.getZone(), area, isEraser, AppStatePersisted.getTopologyTypes());
}

private Path2D getPath(Drawable drawable) {
Expand Down
77 changes: 0 additions & 77 deletions src/main/java/net/rptools/maptool/model/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.rptools.lib.MD5Key;
import net.rptools.maptool.client.AppStatePersisted;
import net.rptools.maptool.client.AppUtil;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.tool.drawing.UndoPerZone;
Expand Down Expand Up @@ -64,7 +63,6 @@
import net.rptools.maptool.model.zones.ZoneLightingChanged;
import net.rptools.maptool.server.Mapper;
import net.rptools.maptool.server.proto.DrawnElementListDto;
import net.rptools.maptool.server.proto.TopologyTypeDto;
import net.rptools.maptool.server.proto.ZoneDto;
import net.rptools.maptool.util.StringUtil;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -308,56 +306,6 @@ public enum TopologyType {
MBL;
}

public static final class TopologyTypeSet implements Iterable<TopologyType> {
private final Set<TopologyType> topologyTypes;

public static TopologyTypeSet valueOf(String value) {
List<TopologyType> topologyTypes = new ArrayList<>();
for (var topologyType : TopologyType.values()) {
var topologyTypeName = topologyType.toString();
if (value.contains(topologyTypeName)) {
topologyTypes.add(topologyType);
}
}

return new TopologyTypeSet(topologyTypes.toArray(TopologyType[]::new));
}

public TopologyTypeSet(TopologyType... types) {
// I would prefer using an enum set, but Hessian can't handle it properly.
topologyTypes = new HashSet<>();
topologyTypes.addAll(Arrays.asList(types));
}

public boolean contains(TopologyType type) {
return topologyTypes.contains(type);
}

public TopologyTypeSet with(TopologyType type) {
var newMode = new TopologyTypeSet();
newMode.topologyTypes.addAll(this.topologyTypes);
newMode.topologyTypes.add(type);
return newMode;
}

public TopologyTypeSet without(TopologyType type) {
var newMode = new TopologyTypeSet();
newMode.topologyTypes.addAll(this.topologyTypes);
newMode.topologyTypes.remove(type);
return newMode;
}

@Nonnull
@Override
public Iterator<TopologyType> iterator() {
return topologyTypes.iterator();
}

public String toString() {
return topologyTypes.toString();
}
}

public static final int DEFAULT_TOKEN_VISION_DISTANCE = 250; // In units
public static final int DEFAULT_PIXELS_CELL = 50;
public static final int DEFAULT_UNITS_PER_CELL = 5;
Expand All @@ -377,7 +325,6 @@ public String toString() {

private double unitsPerCell = DEFAULT_UNITS_PER_CELL;
private AStarRoundingOptions aStarRounding = AStarRoundingOptions.NONE;
private TopologyTypeSet topologyTypes = null; // get default from AppPreferences

// region Keeping these for serialization only. Otherwise, use {@link #drawablesByLayer} instead.
@Deprecated private @Nonnull LinkedList<DrawnElement> drawables = new LinkedList<DrawnElement>();
Expand Down Expand Up @@ -721,7 +668,6 @@ public Zone(Zone zone, boolean keepIds) {
coverVbl = (Area) zone.coverVbl.clone();
topologyTerrain = (Area) zone.topologyTerrain.clone();
aStarRounding = zone.aStarRounding;
topologyTypes = zone.topologyTypes;
isVisible = zone.isVisible;
hasFog = zone.hasFog;
}
Expand Down Expand Up @@ -1336,18 +1282,6 @@ public void setAStarRounding(AStarRoundingOptions aStarRounding) {
this.aStarRounding = aStarRounding;
}

public TopologyTypeSet getTopologyTypes() {
if (topologyTypes == null) {
topologyTypes = AppStatePersisted.getTopologyTypes();
}

return topologyTypes;
}

public void setTopologyTypes(TopologyTypeSet topologyTypes) {
this.topologyTypes = topologyTypes;
}

public int getLargestZOrder() {
return tokenOrderedList.size() > 0
? tokenOrderedList.get(tokenOrderedList.size() - 1).getZOrder()
Expand Down Expand Up @@ -2242,11 +2176,6 @@ public static Zone fromDto(ZoneDto dto) {
zone.tokenVisionDistance = dto.getTokenVisionDistance();
zone.unitsPerCell = dto.getUnitsPerCell();
zone.aStarRounding = AStarRoundingOptions.valueOf(dto.getAStarRounding().name());
zone.topologyTypes = new TopologyTypeSet();
zone.topologyTypes.topologyTypes.addAll(
dto.getTopologyTypesList().stream()
.map(t -> TopologyType.valueOf(t.name()))
.collect(Collectors.toList()));

dto.getDrawablesMap()
.forEach(
Expand Down Expand Up @@ -2313,12 +2242,6 @@ public ZoneDto toDto() {
dto.setTokenVisionDistance(tokenVisionDistance);
dto.setUnitsPerCell(unitsPerCell);
dto.setAStarRounding(ZoneDto.AStarRoundingOptionsDto.valueOf(aStarRounding.name()));
if (topologyTypes != null) {
dto.addAllTopologyTypes(
topologyTypes.topologyTypes.stream()
.map(t -> TopologyTypeDto.valueOf(t.name()))
.collect(Collectors.toList()));
}

drawablesByLayer.forEach(
(layer, drawables) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface ServerCommand {
void setFoW(GUID zoneGUID, Area area, Set<GUID> selectedToks);

default void updateTopology(
Zone zone, Area area, boolean erase, Zone.TopologyTypeSet topologyTypes) {
Zone zone, Area area, boolean erase, Set<Zone.TopologyType> topologyTypes) {
for (var topologyType : topologyTypes) {
updateTopology(zone, area, erase, topologyType);
}
Expand Down
1 change: 0 additions & 1 deletion src/main/proto/data_transfer_objects.proto
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,6 @@ message ZoneDto {
int32 token_vision_distance = 7;
double units_per_cell = 8;
AStarRoundingOptionsDto a_star_rounding = 9;
repeated TopologyTypeDto topologyTypes = 10;
map<string, DrawnElementListDto> drawables = 11;
repeated LabelDto labels = 15;
repeated TokenDto tokens = 16;
Expand Down

0 comments on commit 2a87dc7

Please sign in to comment.