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

Token Footprint Functions and Campaign Properties #4886

Open
wants to merge 27 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a995252
Beginnings of Token Footprints as Campaign Properties.
ColdAnkles Aug 28, 2024
754d433
CampaignPropertiesDto Includes Footprints (To and From)
ColdAnkles Aug 29, 2024
5d5f59d
Merge branch 'RPTools:develop' into token-footprint-properties
ColdAnkles Aug 29, 2024
7b57c42
Footprint Modifications and Resets
ColdAnkles Aug 30, 2024
3c6dcb3
Make (0,0) explicitly part of default footprints
ColdAnkles Aug 31, 2024
605eb62
Corrected actual grid list, better error reporting
ColdAnkles Sep 1, 2024
4b49f4c
Corrected actual grid list, better error reporting
ColdAnkles Sep 1, 2024
2a2a808
New i18n string
ColdAnkles Sep 1, 2024
40ad58d
Fixes and Cleaning Up
ColdAnkles Sep 2, 2024
cc69bd3
Spotless Apply
ColdAnkles Sep 2, 2024
b36510e
Localized Name Edge Case and TokenFootprintCreator Updates
ColdAnkles Sep 5, 2024
3a53f0b
Cleanup
ColdAnkles Sep 7, 2024
dd7ea52
Sorted Offset Problems
ColdAnkles Sep 16, 2024
3b4319c
Footprint Creator Update
ColdAnkles Sep 16, 2024
f876d4f
Merge branch 'RPTools:develop' into token-footprint-properties
ColdAnkles Sep 18, 2024
c77c2b2
Merge branch 'RPTools:develop' into token-footprint-properties
ColdAnkles Sep 20, 2024
db899ee
Add Isometric Size List and Bitwise And instead of Mod for even check.
ColdAnkles Sep 20, 2024
ef60b31
Forgotten Isometric Stuff and Formatting
ColdAnkles Sep 21, 2024
b53c0e3
Merge branch 'RPTools:develop' into token-footprint-properties
ColdAnkles Sep 25, 2024
47cb7c4
Merge branch 'develop' into token-footprint-properties
ColdAnkles Sep 26, 2024
a76b072
Centre the origin for square footprints
ColdAnkles Sep 26, 2024
3ed5186
UI and assorted supporting classes.
bubblobill Sep 26, 2024
d46505e
Deleting default now resets default.
bubblobill Sep 27, 2024
fb64c1e
Repaint zone renderer on close to remove artifacts when turning off g…
bubblobill Sep 28, 2024
ad823e4
Added some code docs
bubblobill Sep 29, 2024
ee2b4dd
Merge branch 'develop' into token-footprint-properties
ColdAnkles Dec 17, 2024
0789188
fixes for develop changes
ColdAnkles Dec 18, 2024
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 @@ -19,6 +19,7 @@
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import main.java.net.rptools.maptool.client.functions.FootprintFunctions; // why
import net.rptools.dicelib.expression.ExpressionParser;
import net.rptools.maptool.client.functions.*;
import net.rptools.maptool.client.functions.json.JSONMacroFunctions;
Expand Down Expand Up @@ -105,6 +106,7 @@ public class MapToolExpressionParser extends ExpressionParser {
TestFunctions.getInstance(),
TextLabelFunctions.getInstance(),
TokenSpeechNameFunction.getInstance(),
FootprintFunctions.getInstance(),
new MarkDownFunctions(),
new PlayerFunctions(),
new LibraryFunctions(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
/*
* 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 main.java.net.rptools.maptool.client.functions;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.PatternSyntaxException;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.functions.exceptions.*;
import net.rptools.maptool.model.CampaignProperties;
import net.rptools.maptool.model.TokenFootprint;
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;

/**
* functions for dealing with token footprint retrieval and modification.
*
* @author cold_ankles
*/
public class FootprintFunctions extends AbstractFunction {
public FootprintFunctions() {
super(
0,
3,
"getTokenFootprints",
"setTokenFootprint",
"removeTokenFootprint",
"getFootprintNames",
"getGridTypes",
"resetFootprintsToDefault");
}

/** The singleton instance. */
private static final FootprintFunctions instance = new FootprintFunctions();

/**
* Gets the instance.
*
* @return the instance.
*/
public static FootprintFunctions getInstance() {
return instance;
}

@Override
public Object childEvaluate(
Parser parser, VariableResolver resolver, String functionName, List<Object> parameters)
throws ParserException {

String result = "";

ArrayList<String> gridTypes = new ArrayList<>();
gridTypes.add("Vertical Hex");
gridTypes.add("Horizontal Hex");
gridTypes.add("Square");
gridTypes.add("None");

try {
if (functionName.equalsIgnoreCase("getTokenFootprints")) {
if ((parameters.size() >= 2
&& !(parameters.get(0) instanceof String)
&& !(parameters.get(1) instanceof String))) {
throw new ParserException(
net.rptools.maptool.language.I18N.getText(
"macro.function.general.argumentTypeN",
functionName,
0,
parameters.get(0).toString(),
parameters.get(1).toString()));
} else if (parameters.isEmpty()) {
result = getTokenFootPrints(null, null);
} else if (parameters.size() > 1) {
if (!gridTypes.contains(parameters.get(0).toString())) {
throw new ParserException(
net.rptools.maptool.language.I18N.getText(
"macro.function.footprintFunctions.unknownGridType",
parameters.get(0).toString()));
}
result = getTokenFootPrints(parameters.get(0).toString(), parameters.get(1).toString());
} else {
if (!gridTypes.contains(parameters.get(0).toString())) {
throw new ParserException(
net.rptools.maptool.language.I18N.getText(
"macro.function.footprintFunctions.unknownGridType",
parameters.get(0).toString()));
}
result = getTokenFootPrints(parameters.get(0).toString(), null);
}
} else if (functionName.equalsIgnoreCase("setTokenFootprint")) {
FunctionUtil.checkNumberParam(functionName, parameters, 3, 3);
if (!gridTypes.contains(parameters.get(0).toString())) {
throw new ParserException(
net.rptools.maptool.language.I18N.getText(
"macro.function.footprintFunctions.unknownGridType",
parameters.get(0).toString()));
}
setTokenFootprint(
parameters.get(0).toString(),
parameters.get(1).toString(),
net.rptools.maptool.util.FunctionUtil.paramAsJsonObject(functionName, parameters, 2));
} else if (functionName.equalsIgnoreCase("removeTokenFootprint")) {
FunctionUtil.checkNumberParam(functionName, parameters, 2, 2);
if (!gridTypes.contains(parameters.get(0).toString())) {
throw new ParserException(
net.rptools.maptool.language.I18N.getText(
"macro.function.footprintFunctions.unknownGridType",
parameters.get(0).toString()));
}
removeTokenFootprint(parameters.get(0).toString(), parameters.get(1).toString());
} else if (functionName.equalsIgnoreCase("getFootprintNames")) {
if ((parameters.size() >= 1 && !(parameters.get(0) instanceof String))) {
throw new ParserException(
net.rptools.maptool.language.I18N.getText(
"macro.function.general.argumentTypeN",
functionName,
0,
parameters.get(0).toString()));
} else if (parameters.isEmpty()) {
result = getFootprintNames(null);
} else {
if (!gridTypes.contains(parameters.get(0).toString())) {
throw new ParserException(
net.rptools.maptool.language.I18N.getText(
"macro.function.footprintFunctions.unknownGridType",
parameters.get(0).toString()));
}
result = getFootprintNames(parameters.get(0).toString());
}
} else if (functionName.equalsIgnoreCase("getGridTypes")) {
result = "[\"Vertical Hex\",\"Horizontal Hex\",\"Square\",\"None\"]";
} else if (functionName.equalsIgnoreCase("resetFootprintsToDefault")) {
resetFootprintsToDefault();
} else {
throw new ParserException(
net.rptools.maptool.language.I18N.getText(
"macro.function.general.unknownFunction", functionName));
}
} catch (PatternSyntaxException e) {
throw new ParserException(e.getMessage());
}

return result;
}

/* Returns a String representing the JSON object containing footprints within the given grid type
* or if gridType is null it returns a JSON object containing all of the above JSON objects for all grids.
* */
String getFootprintNames(String gridType) {
Map<String, List<TokenFootprint>> campaignFootprints =
net.rptools.maptool.client.MapTool.getCampaign()
.getCampaignProperties()
.getGridFootprints();
if (gridType == null) {
JsonObject asJSON = new JsonObject();
for (var entry : campaignFootprints.entrySet()) {
JsonArray footprintNames = new JsonArray();
for (TokenFootprint footprint : entry.getValue()) {
footprintNames.add(footprint.getName());
}
asJSON.add(entry.getKey(), footprintNames);
}
return asJSON.toString();
}
if (!campaignFootprints.containsKey(gridType)) {
return "null";
}
var allFootprints = campaignFootprints.get(gridType).toArray();
JsonArray footprintNames = new JsonArray();
for (int i = 0; i < allFootprints.length; i++) {
TokenFootprint footprint = (TokenFootprint) allFootprints[i];
footprintNames.add(footprint.getName());
}
return footprintNames.toString();
}

/* Gets string representation of JSON object containing footprint data for given gridType and footprintName
if footprint name omitted, it returns a JSON object containing all footprints for given gridType
if gridType also omitted, it returns all the above JSON Objects for all existing gridtypes
*/
String getTokenFootPrints(String gridType, String footprintName) {
Map<String, List<TokenFootprint>> campaignFootprints =
MapTool.getCampaign().getCampaignProperties().getGridFootprints();
if (gridType == null) {
JsonObject asJSON = new JsonObject();
for (var entry : campaignFootprints.entrySet()) {
JsonObject footprintListJSON = new JsonObject();
for (TokenFootprint f : entry.getValue()) {
footprintListJSON.add(f.getName(), f.toJson());
}
asJSON.add(entry.getKey(), footprintListJSON);
}
return asJSON.toString();
}
if (!campaignFootprints.containsKey(gridType)) {
return "null";
}
var allFootprints = campaignFootprints.get(gridType).toArray();
if (footprintName == null) {
// Get all footprints
JsonObject asJSON = new JsonObject();
for (int i = 0; i < allFootprints.length; i++) {
TokenFootprint footprint = (TokenFootprint) allFootprints[i];
asJSON.add(footprint.getName(), footprint.toJson());
}
return asJSON.toString();
} else {
for (int i = 0; i < allFootprints.length; i++) {
TokenFootprint footprint = (TokenFootprint) allFootprints[i];
if (!Objects.equals(footprint.getName(), footprintName)) {
continue;
}
return footprint.toJson().toString();
}
return "null";
}
}

/* sets token footprint under given name/gridtype to the footprint represented in data */
void setTokenFootprint(String gridtype, String name, JsonObject data) {
var cellList = data.get("cells").getAsJsonArray();
Point[] newCells = new Point[cellList.size()];
for (var i = 0; i < cellList.size(); i++) {
var cell = cellList.get(i).getAsJsonObject();
newCells[i] = new Point(cell.get("x").getAsInt(), cell.get("y").getAsInt());
}
TokenFootprint newPrint =
new TokenFootprint(name, false, data.get("scale").getAsDouble(), newCells);
if (data.has("localizedName")) {
newPrint.setLocalizedName(data.get("localizedName").getAsString());
}
CampaignProperties ModifiedProperties = MapTool.getCampaign().getCampaignProperties();
ModifiedProperties.setGridFootprint(name, gridtype, newPrint);
MapTool.getCampaign().mergeCampaignProperties(ModifiedProperties);
}

/* removes the footprint named "name" under gridType*/
void removeTokenFootprint(String gridtype, String name) {
CampaignProperties ModifiedProperties = MapTool.getCampaign().getCampaignProperties();
ModifiedProperties.removeGridFootprint(name, gridtype);
MapTool.getCampaign().mergeCampaignProperties(ModifiedProperties);
}

/* Resets all footprints to default, discarding any/all custom or edited footprints */
void resetFootprintsToDefault() {
CampaignProperties ModifiedProperties = MapTool.getCampaign().getCampaignProperties();
ModifiedProperties.resetTokenFootprints();
MapTool.getCampaign().mergeCampaignProperties(ModifiedProperties);
}
}
Loading
Loading