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

Bring back unique light sources (no new macro functions) #5067

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -640,6 +640,18 @@ public void removeData(String type, String namespace, String name) {
makeServerCall(Message.newBuilder().setRemoveDataMsg(msg).build());
}

@Override
public void toggleLightSourceOnToken(Token token, boolean toggleOn, LightSource lightSource) {
var update = toggleOn ? Token.Update.addLightSource : Token.Update.removeLightSource;
// We only need to send the ID of the light source.
updateTokenProperty(
token,
update,
TokenPropertyValueDto.newBuilder()
.setLightSourceId(lightSource.getId().toString())
.build());
}

@Override
public void setTokenTopology(Token token, @Nullable Area area, Zone.TopologyType topologyType) {
if (area == null) {
Expand Down Expand Up @@ -708,14 +720,6 @@ public void updateTokenProperty(Token token, Token.Update update, String value)
token, update, TokenPropertyValueDto.newBuilder().setStringValue(value).build());
}

@Override
public void updateTokenProperty(Token token, Token.Update update, LightSource value) {
updateTokenProperty(
token,
update,
TokenPropertyValueDto.newBuilder().setLightSourceId(value.getId().toString()).build());
}

@Override
public void updateTokenProperty(Token token, Token.Update update, int value1, int value2) {
updateTokenProperty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
public class TokenLightFunctions extends AbstractFunction {
private static final TokenLightFunctions instance = new TokenLightFunctions();

private static final String TOKEN_CATEGORY = "$token";

private TokenLightFunctions() {
super(0, 5, "hasLightSource", "clearLights", "setLight", "getLights");
}
Expand Down Expand Up @@ -84,7 +86,8 @@ public Object childEvaluate(
*
* @param token The token to get the light sources for.
* @param category The category to get the light sources for. If "*" then the light sources for
* all categories will be returned.
* all categories will be returned. If "$token" then only light sources defined on the token
* will be returned.
* @param delim the delimiter for the list.
* @return a string list containing the lights that are on.
* @throws ParserException if the light type can't be found.
Expand All @@ -96,13 +99,25 @@ private static String getLights(Token token, String category, String delim)
MapTool.getCampaign().getLightSourcesMap();

if (category.equals("*")) {
// Look up on both token and campaign.
for (LightSource ls : token.getUniqueLightSources()) {
if (token.hasLightSource(ls)) {
lightList.add(ls.getName());
}
}
for (Map<GUID, LightSource> lsMap : lightSourcesMap.values()) {
for (LightSource ls : lsMap.values()) {
if (token.hasLightSource(ls)) {
lightList.add(ls.getName());
}
}
}
} else if (TOKEN_CATEGORY.equals(category)) {
for (LightSource ls : token.getUniqueLightSources()) {
if (token.hasLightSource(ls)) {
lightList.add(ls.getName());
}
}
} else if (lightSourcesMap.containsKey(category)) {
for (LightSource ls : lightSourcesMap.get(category).values()) {
if (token.hasLightSource(ls)) {
Expand All @@ -127,7 +142,8 @@ private static String getLights(Token token, String category, String delim)
* Sets the light value for a token.
*
* @param token the token to set the light for.
* @param category the category of the light source.
* @param category the category of the light source. Use "$token" for light sources defined on the
* token.
* @param name the name of the light source.
* @param val the value to set for the light source, 0 for off non 0 for on.
* @return 0 if the light was not found, otherwise 1;
Expand All @@ -140,19 +156,20 @@ private static BigDecimal setLight(Token token, String category, String name, Bi
MapTool.getCampaign().getLightSourcesMap();

Iterable<LightSource> sources;
if (lightSourcesMap.containsKey(category)) {
if (TOKEN_CATEGORY.equals(category)) {
sources = token.getUniqueLightSources();
} else if (lightSourcesMap.containsKey(category)) {
sources = lightSourcesMap.get(category).values();
} else {
throw new ParserException(
I18N.getText("macro.function.tokenLight.unknownLightType", "setLights", category));
}

final var updateAction =
BigDecimal.ZERO.equals(val) ? Token.Update.removeLightSource : Token.Update.addLightSource;
final var add = !BigDecimal.ZERO.equals(val);
for (LightSource ls : sources) {
if (name.equals(ls.getName())) {
found = true;
MapTool.serverCommand().updateTokenProperty(token, updateAction, ls);
MapTool.serverCommand().toggleLightSourceOnToken(token, add, ls);
}
}

Expand All @@ -163,6 +180,7 @@ private static BigDecimal setLight(Token token, String category, String name, Bi
* Checks to see if the token has a light source. The token is checked to see if it has a light
* source with the name in the second parameter from the category in the first parameter. A "*"
* for category indicates all categories are checked; a "*" for name indicates all names are
* checked. The "$token" category indicates that only light sources defined on the token are
* checked.
*
* @param token the token to check.
Expand All @@ -181,6 +199,12 @@ public static boolean hasLightSource(Token token, String category, String name)
MapTool.getCampaign().getLightSourcesMap();

if ("*".equals(category)) {
// Look up on both token and campaign.
for (LightSource ls : token.getUniqueLightSources()) {
if (ls.getName().equals(name) && token.hasLightSource(ls)) {
return true;
}
}
for (Map<GUID, LightSource> lsMap : lightSourcesMap.values()) {
for (LightSource ls : lsMap.values()) {
if (ls.getName().equals(name) && token.hasLightSource(ls)) {
Expand All @@ -189,8 +213,13 @@ public static boolean hasLightSource(Token token, String category, String name)
}
return false;
}
}
if (lightSourcesMap.containsKey(category)) {
} else if (TOKEN_CATEGORY.equals(category)) {
for (LightSource ls : token.getUniqueLightSources()) {
if ((ls.getName().equals(name) || "*".equals(name)) && token.hasLightSource(ls)) {
return true;
}
}
} else if (lightSourcesMap.containsKey(category)) {
for (LightSource ls : lightSourcesMap.get(category).values()) {
if ((ls.getName().equals(name) || "*".equals(name)) && token.hasLightSource(ls)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ protected JMenu createLightSourceMenu() {
menu.addSeparator();
}

// Add unique light sources for the token.
{
JMenu subMenu = createLightCategoryMenu("Unique", tokenUnderMouse.getUniqueLightSources());
if (subMenu.getItemCount() != 0) {
menu.add(subMenu);
menu.addSeparator();
}
}

for (Entry<String, Map<GUID, LightSource>> entry :
MapTool.getCampaign().getLightSourcesMap().entrySet()) {
JMenu subMenu = createLightCategoryMenu(entry.getKey(), entry.getValue().values());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import net.rptools.maptool.util.ExtractHeroLab;
import net.rptools.maptool.util.FunctionUtil;
import net.rptools.maptool.util.ImageManager;
import net.rptools.maptool.util.LightSyntax;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
Expand Down Expand Up @@ -172,6 +173,10 @@ public void initTerrainModifiersIgnoredList() {
EnumSet.allOf(TerrainModifierOperation.class).forEach(operationModel::addElement);
}

public void initUniqueLightSourcesTextPane() {
setUniqueLightSourcesEnabled(MapTool.getPlayer().isGM());
}

public void initJtsMethodComboBox() {
getJtsMethodComboBox().setModel(new DefaultComboBoxModel<>(JTS_SimplifyMethodType.values()));
}
Expand Down Expand Up @@ -201,6 +206,8 @@ public void closeDialog() {
setGmNotesEnabled(MapTool.getPlayer().isGM());
getComponent("@GMName").setEnabled(MapTool.getPlayer().isGM());

setUniqueLightSourcesEnabled(MapTool.getPlayer().isGM());

dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

setLibTokenPaneEnabled(token.isLibToken());
Expand Down Expand Up @@ -371,6 +378,9 @@ public void bind(final Token token) {
.mapToInt(Integer::valueOf)
.toArray());

getUniqueLightSourcesTextPane()
.setText(new LightSyntax().stringifyLights(token.getUniqueLightSources()));

// Jamz: Init the Topology tab...
JTabbedPane tabbedPane = getTabbedPane();

Expand Down Expand Up @@ -709,6 +719,15 @@ public JList<TerrainModifierOperation> getTerrainModifiersIgnoredList() {
return (JList<TerrainModifierOperation>) getComponent("terrainModifiersIgnored");
}

public void setUniqueLightSourcesEnabled(boolean enabled) {
getUniqueLightSourcesTextPane().setEnabled(enabled);
getLabel("uniqueLightSourcesLabel").setEnabled(enabled);
}

public JTextPane getUniqueLightSourcesTextPane() {
return (JTextPane) getComponent("uniqueLightSources");
}

public JLabel getLibTokenURIErrorLabel() {
return (JLabel) getComponent("Label.LibURIError");
}
Expand Down Expand Up @@ -783,6 +802,14 @@ public boolean commit() {
token.setTerrainModifiersIgnored(
new HashSet<>(getTerrainModifiersIgnoredList().getSelectedValuesList()));

var uniqueLightSources =
new LightSyntax()
.parseLights(getUniqueLightSourcesTextPane().getText(), token.getUniqueLightSources());
token.removeAllUniqueLightsources();
for (var lightSource : uniqueLightSources.values()) {
token.addUniqueLightSource(lightSource);
}

// Get the states
Component[] stateComponents = getStatesPanel().getComponents();
Container barPanel = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@
</vspacer>
</children>
</grid>
<grid id="3a658" layout-manager="GridLayoutManager" row-count="9" column-count="10" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="3a658" layout-manager="GridLayoutManager" row-count="10" column-count="10" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="4" left="4" bottom="4" right="4"/>
<constraints>
<tabbedpane title-resource-bundle="net/rptools/maptool/language/i18n" title-key="EditTokenDialog.tab.config"/>
Expand Down Expand Up @@ -710,7 +710,7 @@
<grid id="d2ea5" layout-manager="GridLayoutManager" row-count="2" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="8" column="0" row-span="1" col-span="9" vsize-policy="2" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="9" column="0" row-span="1" col-span="9" vsize-policy="2" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -937,14 +937,40 @@
<name value="statSheetLocationComboBox"/>
</properties>
</component>
<vspacer id="57cb0">
<component id="86a4f" class="javax.swing.JLabel">
<constraints>
<grid row="6" column="5" row-span="1" col-span="1" vsize-policy="1" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="7" column="0" row-span="1" col-span="6" vsize-policy="1" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="341a0">
<properties>
<labelFor value="c3057"/>
<name value="uniqueLightSourcesLabel"/>
<text resource-bundle="net/rptools/maptool/language/i18n" key="EditTokenDialog.label.uniqueLightSources"/>
</properties>
</component>
<scrollpane id="45ccf">
<constraints>
<grid row="8" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="100"/>
</grid>
</constraints>
<properties>
<background color="-1"/>
<name value="uniqueLights"/>
</properties>
<border type="none"/>
<children>
<component id="c3057" class="javax.swing.JTextPane">
<constraints/>
<properties>
<background color="-1"/>
<name value="uniqueLightSources"/>
</properties>
</component>
</children>
</scrollpane>
<vspacer id="57cb0">
<constraints>
<grid row="7" column="5" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="6" column="5" row-span="1" col-span="1" vsize-policy="1" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void paintOverlay(ZoneRenderer renderer, Graphics2D g) {
if (token.hasLightSources()) {
boolean foundNormalLight = false;
for (AttachedLightSource attachedLightSource : token.getLightSources()) {
LightSource lightSource = attachedLightSource.resolve(MapTool.getCampaign());
LightSource lightSource = attachedLightSource.resolve(token, MapTool.getCampaign());
if (lightSource != null && lightSource.getType() == LightSource.Type.NORMAL) {
foundNormalLight = true;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private Set<GUID> getLightSources(Player.Role role, LightSource.Type type) {

private void addLightSourceToken(Token token, Set<Player.Role> roles) {
for (AttachedLightSource als : token.getLightSources()) {
LightSource lightSource = als.resolve(MapTool.getCampaign());
LightSource lightSource = als.resolve(token, MapTool.getCampaign());
if (lightSource == null) {
continue;
}
Expand Down Expand Up @@ -316,7 +316,8 @@ private List<ContributedLight> calculateLitAreas(Token lightSourceToken, double
final var result = new ArrayList<ContributedLight>();

for (final var attachedLightSource : lightSourceToken.getLightSources()) {
LightSource lightSource = attachedLightSource.resolve(MapTool.getCampaign());
LightSource lightSource =
attachedLightSource.resolve(lightSourceToken, MapTool.getCampaign());
if (lightSource == null) {
continue;
}
Expand Down Expand Up @@ -669,7 +670,7 @@ public List<DrawableLight> getDrawableAuras(PlayerView view) {
Point p = FogUtil.calculateVisionCenter(token, zone);

for (AttachedLightSource als : token.getLightSources()) {
LightSource lightSource = als.resolve(MapTool.getCampaign());
LightSource lightSource = als.resolve(token, MapTool.getCampaign());
if (lightSource == null) {
continue;
}
Expand Down
Loading
Loading