Skip to content

Commit

Permalink
Merge branch 'develop' into bugfix/4978-hex-light-center-and-scale
Browse files Browse the repository at this point in the history
  • Loading branch information
cwisniew authored Oct 9, 2024
2 parents e662f70 + bdb58b6 commit d64e074
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/main/java/net/rptools/maptool/client/MapToolClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ public List<Player> getPlayerList() {
public void addPlayer(Player player) {
if (!playerList.contains(player)) {
playerList.add(player);
new MapToolEventBus().getMainEventBus().post(new PlayerConnected(player));
new MapToolEventBus()
.getMainEventBus()
.post(new PlayerConnected(player, this.player.equals(player)));
playerDatabase.playerSignedIn(player);

playerList.sort((arg0, arg1) -> arg0.getName().compareToIgnoreCase(arg1.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

import net.rptools.maptool.model.player.Player;

public record PlayerConnected(Player player) {}
public record PlayerConnected(Player player, boolean isLocal) {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import javax.swing.*;
import net.rptools.lib.MD5Key;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.MapToolVariableResolver;
import net.rptools.maptool.client.functions.json.JSONMacroFunctions;
import net.rptools.maptool.client.ui.macrobuttons.MacroButtonHotKeyManager;
import net.rptools.maptool.client.ui.macrobuttons.buttons.MacroButton;
import net.rptools.maptool.client.ui.macrobuttons.panels.AbstractMacroPanel;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.MacroButtonProperties;
Expand Down Expand Up @@ -61,6 +62,7 @@ private MacroFunctions() {
"setMacroProps",
"getMacros",
"getMacroProps",
"getMacroHotkeys",
"getMacroIndexes",
"getMacroIndices",
"getMacroName",
Expand Down Expand Up @@ -218,7 +220,13 @@ public Object childEvaluate(

} else if (functionName.equalsIgnoreCase("getMacroButtonIndex")) {
return BigDecimal.valueOf(MapTool.getParser().getMacroButtonIndex());

} else if (functionName.equalsIgnoreCase("getMacroHotkeys")) {
FunctionUtil.checkNumberParam(functionName, parameters, 0, 1);
String delim = "json";
if (!parameters.isEmpty()) {
delim = parameters.getFirst().toString();
}
return getMacroHotkeys(delim);
} else if (functionName.equalsIgnoreCase("getMacroGroup")) {
FunctionUtil.checkNumberParam(functionName, parameters, 1, 4);
String group = parameters.get(0).toString();
Expand Down Expand Up @@ -252,6 +260,30 @@ public Object childEvaluate(
throw new ParserException(I18N.getText(KEY_UNKNOWN_MACRO, functionName));
}

private Object getMacroHotkeys(String delim) {
Map<KeyStroke, MacroButton> keyStrokeMap = MacroButtonHotKeyManager.getKeyStrokeMap();
JsonObject keyMacroObject = new JsonObject();

for (KeyStroke ks : keyStrokeMap.keySet()) {
MacroButton btn = keyStrokeMap.get(ks);
String address = btn.getProperties().getLabel() + "@";
switch (btn.getPanelClass()) {
case "GlobalPanel", "globalpanel" -> address += "global";
case "CampaignPanel", "campaignpanel" -> address += "campaign";
case "GmPanel", "gmpanel" -> address += "gm";
default -> address += btn.getToken().getName();
}
keyMacroObject.addProperty(ks.toString(), address);
}
if (delim.equalsIgnoreCase("json")) {
return keyMacroObject;
} else {
return JSONMacroFunctions.getInstance()
.getJsonObjectFunctions()
.toStringProp(keyMacroObject, delim);
}
}

/**
* Campaign version of hasMacro().
*
Expand Down Expand Up @@ -355,12 +387,19 @@ public void setMacroProps(MacroButtonProperties mbp, String propString, String d
mbp.setAutoExecute(boolVal(value));
} else if ("color".equalsIgnoreCase(key)) {
mbp.setColorKey(value);
} else if ("displayHotkey".equalsIgnoreCase(key)) {
mbp.setDisplayHotKey(boolVal(value));
} else if ("fontColor".equalsIgnoreCase(key)) {
mbp.setFontColorKey(value);
} else if ("fontSize".equalsIgnoreCase(key)) {
mbp.setFontSize(value);
} else if ("group".equalsIgnoreCase(key)) {
mbp.setGroup(value);
} else if ("hotkey".equalsIgnoreCase(key)) {
if (MacroButtonHotKeyManager.isHotkeyAssigned(value)) {
value = MacroButtonHotKeyManager.HOTKEYS[0];
}
mbp.setHotKey(value);
} else if ("includeLabel".equalsIgnoreCase(key)) {
mbp.setIncludeLabel(boolVal(value));
} else if ("sortBy".equalsIgnoreCase(key)) {
Expand Down Expand Up @@ -904,6 +943,7 @@ private String removeMacro(int index, boolean gmPanel) throws ParserException {
}
AbstractMacroPanel macroPanel =
gmPanel ? MapTool.getFrame().getGmPanel() : MapTool.getFrame().getCampaignPanel();
MapTool.getFrame().updateKeyStrokes(); // ensure hotkeys are updated
macroPanel.reset();
return "Removed macro button "
+ label
Expand Down Expand Up @@ -931,6 +971,7 @@ private String removeMacro(int index, Token token) throws ParserException {

String label = mbp.getLabel();
MapTool.serverCommand().updateTokenProperty(token, Token.Update.deleteMacro, index);
MapTool.getFrame().updateKeyStrokes(); // ensure hotkeys are updated
return "Removed macro button " + label + "(index = " + index + ") from " + token.getName();
}

Expand Down Expand Up @@ -1046,20 +1087,22 @@ private ImmutablePair<Integer, MacroButtonProperties> getCampaignMbpByIndexOrLab
*/
private String macroButtonPropertiesToString(MacroButtonProperties mbp, String delim) {
StringBuilder sb = new StringBuilder();
sb.append("applyToSelected=").append(mbp.getApplyToTokens()).append(delim);
sb.append("autoExecute=").append(mbp.getAutoExecute()).append(delim);
sb.append("color=").append(mbp.getColorKey()).append(delim);
sb.append("displayHotkey=").append(mbp.getDisplayHotKey()).append(delim);
sb.append("fontColor=").append(mbp.getFontColorKey()).append(delim);
sb.append("fontSize=").append(mbp.getFontSize()).append(delim);
sb.append("group=").append(mbp.getGroup()).append(delim);
sb.append("hotkey=").append(mbp.getHotKey()).append(delim);
sb.append("includeLabel=").append(mbp.getIncludeLabel()).append(delim);
sb.append("sortBy=").append(mbp.getSortby()).append(delim);
sb.append("index=").append(mbp.getIndex()).append(delim);
sb.append("label=").append(mbp.getLabel()).append(delim);
sb.append("fontSize=").append(mbp.getFontSize()).append(delim);
sb.append("maxWidth=").append(mbp.getMaxWidth()).append(delim);
sb.append("minWidth=").append(mbp.getMinWidth()).append(delim);
sb.append("playerEditable=").append(mbp.getAllowPlayerEdits()).append(delim);
sb.append("maxWidth=").append(mbp.getMaxWidth()).append(delim);
sb.append("sortBy=").append(mbp.getSortby()).append(delim);
sb.append("tooltip=").append(mbp.getToolTip()).append(delim);
sb.append("applyToSelected=").append(mbp.getApplyToTokens()).append(delim);
return sb.toString();
}

Expand Down Expand Up @@ -1091,12 +1134,19 @@ private MacroButtonProperties macroButtonPropertiesFromJSON(
mbp.setAutoExecute(boolVal(value));
} else if ("color".equalsIgnoreCase(key)) {
mbp.setColorKey(value);
} else if ("displayHotkey".equalsIgnoreCase(key)) {
mbp.setDisplayHotKey(boolVal(value));
} else if ("fontColor".equalsIgnoreCase(key)) {
mbp.setFontColorKey(value);
} else if ("fontSize".equalsIgnoreCase(key)) {
mbp.setFontSize(value);
} else if ("group".equalsIgnoreCase(key)) {
mbp.setGroup(value);
} else if ("hotkey".equalsIgnoreCase(key)) {
if (MacroButtonHotKeyManager.isHotkeyAssigned(value)) {
value = MacroButtonHotKeyManager.HOTKEYS[0];
}
mbp.setHotKey(value);
} else if ("includeLabel".equalsIgnoreCase(key)) {
mbp.setIncludeLabel(boolVal(value));
} else if ("sortBy".equalsIgnoreCase(key)) {
Expand Down Expand Up @@ -1184,21 +1234,23 @@ private MacroButtonProperties macroButtonPropertiesFromStringProp(
*/
private JsonObject macroButtonPropertiesToJSON(MacroButtonProperties mbp) {
JsonObject props = new JsonObject();
props.addProperty("applyToSelected", mbp.getApplyToTokens());
props.addProperty("autoExecute", mbp.getAutoExecute());
props.addProperty("color", mbp.getColorKey());
props.addProperty("displayHotkey", mbp.getDisplayHotKey());
props.addProperty("command", mbp.getCommand());
props.addProperty("fontColor", mbp.getFontColorKey());
props.addProperty("fontSize", mbp.getFontSize());
props.addProperty("group", mbp.getGroup());
props.addProperty("hotkey", mbp.getHotKey());
props.addProperty("includeLabel", mbp.getIncludeLabel());
props.addProperty("sortBy", mbp.getSortby());
props.addProperty("index", mbp.getIndex());
props.addProperty("label", mbp.getLabel());
props.addProperty("fontSize", mbp.getFontSize());
props.addProperty("minWidth", mbp.getMinWidth());
props.addProperty("playerEditable", mbp.getAllowPlayerEdits());
props.addProperty("command", mbp.getCommand());
props.addProperty("maxWidth", mbp.getMaxWidth());
props.addProperty("playerEditable", mbp.getAllowPlayerEdits());
props.addProperty("sortBy", mbp.getSortby());
props.addProperty("tooltip", mbp.getToolTip());
props.addProperty("applyToSelected", mbp.getApplyToTokens());

JsonArray compare = new JsonArray();
if (mbp.getCompareGroup()) compare.add("group");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.common.eventbus.Subscribe;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.Icon;
import javax.swing.JLabel;
import net.rptools.maptool.client.MapTool;
Expand All @@ -40,27 +41,21 @@ public class PlayersLoadingStatusBar extends JLabel {
loadingIcon = RessourceManager.getSmallIcon(Icons.STATUSBAR_PLAYERS_LOADING);
}

private final ArrayList<Player> players = new ArrayList<>();

public PlayersLoadingStatusBar() {
refreshCount();
new MapToolEventBus().getMainEventBus().register(this);
}

private void refreshCount() {
var players = MapTool.getPlayerList();
var total = players.size();
var loaded = players.stream().filter(x -> x.getLoaded()).count();

var sb =
new StringBuilder(I18N.getText("ConnectionStatusPanel.playersLoadedZone", loaded, total));

var self = MapTool.getPlayer();

for (Player player : players) {
// The Player in the list is a seperate entity to the one from MapTool.getPlayer()
// So it doesn't have the correct status data.
if (player.getName().equals(self.getName())) {
player = self;
}
var zone =
player.getZoneId() == null ? null : MapTool.getCampaign().getZone(player.getZoneId());

Expand Down Expand Up @@ -104,6 +99,11 @@ public Dimension getPreferredSize() {

@Subscribe
private void onPlayerConnected(PlayerConnected event) {
if (event.isLocal()) {
return;
}
players.add(event.player());

refreshCount();
}

Expand All @@ -114,11 +114,13 @@ private void onPlayerStatusChanged(PlayerStatusChanged event) {

@Subscribe
private void onPlayerDisconnected(PlayerDisconnected event) {
players.remove(event.player());
refreshCount();
}

@Subscribe
private void onServerDisconnected(ServerDisconnected event) {
players.clear();
refreshCount();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public ClientConnectionPanel() {

@Subscribe
private void onPlayerConnected(PlayerConnected event) {
if (event.isLocal()) {
return;
}

listModel.addElement(event.player());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public class MacroButtonHotKeyManager {
new HashMap<KeyStroke, MacroButton>();
private MacroButton macroButton;

public static boolean isHotkeyAssigned(String hotkey) {
return buttonsByKeyStroke.containsKey(hotkey);
}

public MacroButtonHotKeyManager(MacroButton macroButton) {
this.macroButton = macroButton;
}
Expand Down

0 comments on commit d64e074

Please sign in to comment.