Skip to content

Commit

Permalink
Merge branch 'develop' into feature-1691
Browse files Browse the repository at this point in the history
  • Loading branch information
cwisniew authored Aug 31, 2023
2 parents ea5d036 + 35b62ea commit b90035b
Show file tree
Hide file tree
Showing 32 changed files with 634 additions and 53 deletions.
81 changes: 63 additions & 18 deletions src/main/java/net/rptools/maptool/client/AppActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,19 @@ protected void executeAction() {

JFileChooser chooser = MapTool.getFrame().getSaveFileChooser();

// Get target location
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
boolean tryAgain = true;
while (tryAgain) {
// Get target location
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
}
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.exportRepoToInstallDir");
} else {
tryAgain = false;
}
}

// Default extension
Expand Down Expand Up @@ -2546,6 +2556,12 @@ public static void loadCampaign(final File campaignFile) {
return;
}

var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var openDir = campaignFile.toPath().getParent().toAbsolutePath();
if (openDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.loadCampaignFromInstallDir");
}

new CampaignLoader(campaignFile).execute();
}

Expand Down Expand Up @@ -2692,6 +2708,13 @@ public static void doSaveCampaign(Runnable onSuccess) {
doSaveCampaignAs(onSuccess);
return;
}
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = AppState.getCampaignFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveCampaignToInstallDir");
doSaveCampaignAs(onSuccess);
return;
}
doSaveCampaign(AppState.getCampaignFile(), onSuccess);
}

Expand Down Expand Up @@ -2763,10 +2786,22 @@ protected void done() {
}

public static void doSaveCampaignAs(Runnable onSuccess) {
JFileChooser chooser = MapTool.getFrame().getSaveCmpgnFileChooser();
int saveStatus = chooser.showSaveDialog(MapTool.getFrame());
if (saveStatus == JFileChooser.APPROVE_OPTION) {
saveAndUpdateCampaignName(chooser.getSelectedFile(), onSuccess);
boolean tryAgain = true;
while (tryAgain) {
JFileChooser chooser = MapTool.getFrame().getSaveCmpgnFileChooser();
int saveStatus = chooser.showSaveDialog(MapTool.getFrame());
if (saveStatus == JFileChooser.APPROVE_OPTION) {
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveCampaignToInstallDir");
} else {
tryAgain = false;
saveAndUpdateCampaignName(chooser.getSelectedFile(), onSuccess);
}
} else {
tryAgain = false;
}
}
}

Expand Down Expand Up @@ -2811,20 +2846,30 @@ protected void executeAction() {
chooser.setFileFilter(MapTool.getFrame().getMapFileFilter());
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setSelectedFile(new File(zr.getZone().getName()));
if (chooser.showSaveDialog(MapTool.getFrame()) == JFileChooser.APPROVE_OPTION) {
try {
boolean tryAgain = true;
while (tryAgain) {
if (chooser.showSaveDialog(MapTool.getFrame()) == JFileChooser.APPROVE_OPTION) {
File mapFile = chooser.getSelectedFile();
mapFile = getFileWithExtension(mapFile, AppConstants.MAP_FILE_EXTENSION);
if (mapFile.exists()) {
if (!MapTool.confirm("msg.confirm.fileExists")) {
return;
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveMapToInstallDir");
} else {
tryAgain = false;
try {
mapFile = getFileWithExtension(mapFile, AppConstants.MAP_FILE_EXTENSION);
if (mapFile.exists()) {
if (!MapTool.confirm("msg.confirm.fileExists")) {
return;
}
}
PersistenceUtil.saveMap(zr.getZone(), mapFile);
AppPreferences.setSaveMapDir(mapFile.getParentFile());
MapTool.showInformation("msg.info.mapSaved");
} catch (IOException ioe) {
MapTool.showError("msg.error.failedSaveMap", ioe);
}
}
PersistenceUtil.saveMap(zr.getZone(), mapFile);
AppPreferences.setSaveMapDir(mapFile.getParentFile());
MapTool.showInformation("msg.info.mapSaved");
} catch (IOException ioe) {
MapTool.showError("msg.error.failedSaveMap", ioe);
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/net/rptools/maptool/client/AppUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,34 @@ public static String getAppInstallLocation() {
return path;
}

/**
* This function tries to determine the installation directory of the application. This can differ
* from the directory the application is running from as returned by {@link
* #getAppInstallLocation()}.
*
* @return the installation directory of the application.
*/
public static Path getInstallDirectory() {
var path = Path.of(getAppInstallLocation());
if (MapTool.isDevelopment()) {
// remove build/classes/java
path = path.getParent().getParent().getParent().getParent();
} else {
while (path != null) {
if (path.getFileName().toString().matches("(?i).*maptool.*")) {
path = path.getParent();
break;
}
path = path.getParent();
}
}
if (path == null) {
return Path.of(getAppInstallLocation());
} else {
return path;
}
}

/**
* Returns a File path representing the configuration file in the base directory that the
* application is running from. e.g. C:\Users\Troll\AppData\Local\MapTool\app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class MapToolExpressionParser extends ExpressionParser {
ExecFunction.getInstance(),
FindTokenFunctions.getInstance(),
HasImpersonated.getInstance(),
IlluminationFunctions.getInstance(),
InitiativeRoundFunction.getInstance(),
InputFunction.getInstance(),
IsTrustedFunction.getInstance(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.client.functions;

import com.google.gson.JsonNull;
import java.awt.geom.Point2D;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.functions.json.JSONMacroFunctions;
import net.rptools.maptool.client.ui.zone.PlayerView;
import net.rptools.maptool.client.ui.zone.ZoneRenderer;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.Token;
import net.rptools.maptool.util.FunctionUtil;
import net.rptools.parser.Parser;
import net.rptools.parser.ParserException;
import net.rptools.parser.VariableResolver;
import net.rptools.parser.function.AbstractFunction;
import net.rptools.parser.function.Function;

public class IlluminationFunctions extends AbstractFunction {
private static final IlluminationFunctions instance = new IlluminationFunctions();

private IlluminationFunctions() {
super(0, Function.UNLIMITED_PARAMETERS, "getIllumination");
}

public static IlluminationFunctions getInstance() {
return instance;
}

@Override
public Object childEvaluate(
Parser parser, VariableResolver resolver, String functionName, List<Object> parameters)
throws ParserException {
if (functionName.equalsIgnoreCase("getIllumination")) {
return BigDecimal.valueOf(getIllumination(functionName, parameters));
}

throw new ParserException(I18N.getText("macro.function.general.unknownFunction", functionName));
}

private int getIllumination(String functionName, List<Object> parameters) throws ParserException {
FunctionUtil.blockUntrustedMacro(functionName);
FunctionUtil.checkNumberParam(functionName, parameters, 2, 4);

final var x = FunctionUtil.paramAsInteger(functionName, parameters, 0, false);
final var y = FunctionUtil.paramAsInteger(functionName, parameters, 1, false);
final var point = new Point2D.Double(x, y);

final var renderer = FunctionUtil.getZoneRendererFromParam(functionName, parameters, 2);
final var playerView = getPlayerView(functionName, renderer, parameters, 3);

for (final var lumensLevel :
renderer.getZoneView().getDisjointObscuredLumensLevels(playerView)) {
if (lumensLevel.darknessArea().contains(point)) {
return -lumensLevel.lumensStrength();
} else if (lumensLevel.lightArea().contains(point)) {
return lumensLevel.lumensStrength();
}
}

return 0;
}

/**
* Builds a player view for `getIllumination()`
*
* @param functionName
* @param renderer
* @param parameters
* @param tokenListIndex
* @return If the token list is not provided, the current view for the zone. If `json.null` is
* provided, a non-token view. If a token ID list is provided, a token view containing the
* identified tokens. is returned. If the token ID list is empty, a token view with no in it.
* @throws ParserException
*/
private PlayerView getPlayerView(
String functionName, ZoneRenderer renderer, List<Object> parameters, int tokenListIndex)
throws ParserException {
if (parameters.size() <= tokenListIndex) {
return renderer.getPlayerView();
}

final var parameter = parameters.get(tokenListIndex);
if (parameter instanceof JsonNull) {
// Explicitly requesting without token view.
return new PlayerView(MapTool.getPlayer().getEffectiveRole());
}

// Tokens for the view passed as a JSON array.
final var jsonList =
JSONMacroFunctions.getInstance().asJsonElement(parameters.get(3).toString());
if (!jsonList.isJsonArray()) {
// Whoops, we need an array.
throw new ParserException(I18N.getText("macro.function.general.argumentTypeA", functionName));
}
final var jsonArray = jsonList.getAsJsonArray();

final var tokens = new ArrayList<Token>();
for (final var element : jsonArray) {
final var identifier = JSONMacroFunctions.getInstance().jsonToScriptString(element);
final var token = renderer.getZone().resolveToken(identifier);
if (token == null) {
throw new ParserException(
I18N.getText("macro.function.general.unknownToken", functionName, identifier));
}

tokens.add(token);
}

return new PlayerView(MapTool.getPlayer().getEffectiveRole(), tokens);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -554,17 +554,43 @@ public void actionPerformed(ActionEvent e) {
if (showSaveDialog) {
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
boolean tryAgain = true;
while (tryAgain) {
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
}

saveDirectory = chooser.getSelectedFile();
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveTokenToInstallDir");
} else {
tryAgain = false;
}
}

tokenSaveFile = chooser.getSelectedFile();
} else {
if (saveDirectory == null) {
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) return;
if (chooser.getFileFilter() == tokenFilterGM) saveAsGmName = true;
saveDirectory = chooser.getSelectedFile();
boolean tryAgain = true;
while (tryAgain) {
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
}
if (chooser.getFileFilter() == tokenFilterGM) {
saveAsGmName = true;
}
saveDirectory = chooser.getSelectedFile();
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveTokenToInstallDir");
} else {
tryAgain = false;
}
}
}

if (saveAsGmName) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/rptools/maptool/client/ui/MapToolFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.common.eventbus.Subscribe;
import com.jidesoft.docking.DefaultDockableHolder;
import com.jidesoft.docking.DockableFrame;
import com.jidesoft.docking.DockingManager;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -71,6 +72,7 @@
import net.rptools.maptool.client.ui.assetpanel.AssetPanel;
import net.rptools.maptool.client.ui.commandpanel.CommandPanel;
import net.rptools.maptool.client.ui.connections.ClientConnectionPanel;
import net.rptools.maptool.client.ui.docking.MapToolDockingManager;
import net.rptools.maptool.client.ui.drawpanel.DrawPanelPopupMenu;
import net.rptools.maptool.client.ui.drawpanel.DrawPanelTreeCellRenderer;
import net.rptools.maptool.client.ui.drawpanel.DrawPanelTreeModel;
Expand Down Expand Up @@ -619,6 +621,11 @@ public DockableFrame getFrame(MTFrame frame) {
return frameMap.get(frame);
}

@Override
protected DockingManager createDockingManager(Container container) {
return new MapToolDockingManager(this, container);
}

private void initializeFrames() {
frameMap.put(
MTFrame.CONNECTIONS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.swing.*;
import net.rptools.lib.FileUtil;
import net.rptools.maptool.client.AppConstants;
import net.rptools.maptool.client.AppUtil;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.swing.AbeillePanel;
import net.rptools.maptool.client.swing.SwingUtil;
Expand Down Expand Up @@ -904,7 +905,19 @@ private void initExportButton() {
// END HACK

JFileChooser chooser = MapTool.getFrame().getSavePropsFileChooser();
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) return;
boolean tryAgain = true;
while (tryAgain) {
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
}
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.savePropToInstallDir");
} else {
tryAgain = false;
}
}

File selectedFile = chooser.getSelectedFile();
if (selectedFile.exists()) {
Expand Down
Loading

0 comments on commit b90035b

Please sign in to comment.