Permalink
Browse files

Relocation of Channel items to ChannelUtilities (#4600)

Also includes various minor cleanup
  • Loading branch information...
thpr authored and karianna committed Dec 4, 2018
1 parent b3ff494 commit 4e508a77350fe241bcf352d1324403bc3975b372
@@ -26,9 +26,6 @@
import pcgen.cdom.facet.VariableStoreFacet;
import pcgen.cdom.facet.event.DataFacetChangeEvent;
import pcgen.cdom.facet.event.DataFacetChangeListener;
import pcgen.cdom.util.CControl;
import pcgen.core.PlayerCharacter;
import pcgen.output.channel.ChannelUtilities;
import pcgen.rules.context.VariableContext;
/**
@@ -110,71 +107,17 @@
}
/**
* Sets up the given Code Control so that if the value on the channel changes, the PC
* is categorized as Dirty.
*
* @param pc
* The PlayerCharacter on which the channel should be watched
* @param codeControl
* The name of the channel to be watched
*/
public static void setDirtyOnChannelChange(PlayerCharacter pc,
CControl codeControl)
{
String variableName =
ChannelUtilities.createVarName(pc.getControl(codeControl));
VariableUtilities.addListenerToVariable(pc.getCharID(), variableName,
x -> pc.setDirty(true));
}
/**
* Sets up the given DataFacetChangeListener to receive a DataFacetChangeEvent when
* the value of the given channel on the given PC changes. This provides compatibility
* for facets that wish to listen to the new variable system.
*
* Note that this currently supports Item-based channels, not lists
*
* @param pc
* The PlayerCharacter on which the channel should be watched
* @param codeControl
* The name of the channel to be watched
* @param listener
* The listener to receive an event when the value of the channel changes
*/
public static void watchChannel(PlayerCharacter pc, CControl codeControl,
DataFacetChangeListener<CharID, ?> listener)
{
watchChannel(pc.getCharID(), pc.getControl(codeControl), listener, 0);
}
/**
* Sets up the given DataFacetChangeListener to receive a DataFacetChangeEvent when
* the value of a channel changes. This provides compatibility for facets that wish to
* listen to the new variable system.
*
* Note that this currently supports Item-based channels, not lists
* Forwards a VariableChangeEvent to a DataFacetChangeListener as a
* DataFacetChangeEvent.
*
* @param id
* The CharID on which the channel should be watched
* @param channelName
* The name of the channel to be watched
* The CharID on which the change is occurring
* @param vcEvent
* the VariableChangeEvent
* @param listener
* The listener to receive an event when the value of the channel changes
* @param priority
* The priority of the listener for receiving changes (The lower the
* priority the earlier in the list the new listener will get advised of
* the change)
* The DataFacetChangeListener
*/
public static <T> void watchChannel(CharID id, String channelName,
DataFacetChangeListener<CharID, T> listener, int priority)
{
VariableID<T> varID =
ChannelUtilities.getChannelVariableID(id, channelName);
RESULT_FACET.get(id).addVariableListener(priority, varID,
e -> fireVariableChangedEvent(id, e, listener));
}
private static <T> void fireVariableChangedEvent(CharID id,
public static <T> void forwardVariableChangeToDFCL(CharID id,
VariableChangeEvent<T> vcEvent,
DataFacetChangeListener<CharID, T> listener)
{
@@ -19,36 +19,25 @@
import java.util.Objects;
import pcgen.base.formula.base.ScopeInstance;
import pcgen.base.formula.base.VariableID;
import pcgen.cdom.enumeration.CharID;
import pcgen.cdom.facet.FacetLibrary;
import pcgen.cdom.facet.LoadContextFacet;
import pcgen.cdom.facet.ScopeFacet;
import pcgen.cdom.facet.SolverManagerFacet;
import pcgen.cdom.facet.VariableStoreFacet;
import pcgen.cdom.facet.event.DataFacetChangeListener;
import pcgen.cdom.formula.VariableListener;
import pcgen.cdom.formula.VariableUtilities;
import pcgen.cdom.util.CControl;
import pcgen.cdom.util.ControlUtilities;
import pcgen.core.Globals;
import pcgen.rules.context.VariableContext;
import pcgen.core.PlayerCharacter;
/**
* ChannelUtilities are a class for setting up communication channels from the
* core to other objects via get(), set(...) and events.
*/
public final class ChannelUtilities
{
/**
* The LoadContextFacet
*/
private static final LoadContextFacet LOAD_CONTEXT_FACET = FacetLibrary.getFacet(LoadContextFacet.class);
/**
* The ScopeFacet
*/
private static final ScopeFacet SCOPE_FACET = FacetLibrary.getFacet(ScopeFacet.class);
/**
* The VariableStoreFacet
*/
@@ -109,12 +98,8 @@ public static Object readControlledChannel(CharID id, CControl control)
*/
public static void setGlobalChannel(CharID id, String channelName, Object value)
{
processSet(id, getChannelVariableID(id, channelName), value);
}
private static <T> void processSet(CharID id, VariableID<T> varID, Object value)
{
RESULT_FACET.get(id).put(varID, (T) value);
VariableID<Object> varID = getChannelVariableID(id, channelName);
RESULT_FACET.get(id).put(varID, value);
SOLVER_MANAGER_FACET.get(id).solveChildren(varID);
}
@@ -162,8 +147,8 @@ public static String createVarName(String channelName)
public static <T> void addListenerToChannel(CharID id, String channelName,
VariableListener<T> listener)
{
VariableID<T> varID = getChannelVariableID(id, channelName);
RESULT_FACET.get(id).addVariableListener(0, varID, listener);
String variableName = createVarName(channelName);
VariableUtilities.addListenerToVariable(id, variableName, listener);
}
/**
@@ -179,8 +164,8 @@ public static String createVarName(String channelName)
public static <T> void removeListenerFromChannel(CharID id, String channelName,
VariableListener<T> listener)
{
VariableID<T> varID = getChannelVariableID(id, channelName);
RESULT_FACET.get(id).removeVariableListener(0, varID, listener);
String variableName = createVarName(channelName);
VariableUtilities.removeListenerFromVariable(id, listener, variableName);
}
/**
@@ -194,14 +179,74 @@ public static String createVarName(String channelName)
* @return The VariableID for the channel with the given name on the PlayerCharacter
* represented by the given CharID
*/
public static <T> VariableID<T> getChannelVariableID(CharID id, String channelName)
private static <T> VariableID<T> getChannelVariableID(CharID id, String channelName)
{
ScopeInstance globalInstance = SCOPE_FACET.getGlobalScope(id);
String variableName = createVarName(channelName);
VariableContext varContext =
LOAD_CONTEXT_FACET.get(id.getDatasetID()).get().getVariableContext();
return VariableUtilities.getGlobalVariableID(id, variableName);
}
/**
* Sets up the given DataFacetChangeListener to receive a DataFacetChangeEvent when
* the value of the given channel on the given PC changes. This provides compatibility
* for facets that wish to listen to the new variable system.
*
* Note that this currently supports Item-based channels, not lists
*
* @param pc
* The PlayerCharacter on which the channel should be watched
* @param codeControl
* The name of the channel to be watched
* @param listener
* The listener to receive an event when the value of the channel changes
*/
public static void watchChannel(PlayerCharacter pc, CControl codeControl,
DataFacetChangeListener<CharID, ?> listener)
{
ChannelUtilities.watchChannel(pc.getCharID(), pc.getControl(codeControl), listener, 0);
}
/**
* Sets up the given DataFacetChangeListener to receive a DataFacetChangeEvent when
* the value of a channel changes. This provides compatibility for facets that wish to
* listen to the new variable system.
*
* Note that this currently supports Item-based channels, not lists
*
* @param id
* The CharID on which the channel should be watched
* @param channelName
* The name of the channel to be watched
* @param listener
* The listener to receive an event when the value of the channel changes
* @param priority
* The priority of the listener for receiving changes (The lower the
* priority the earlier in the list the new listener will get advised of
* the change)
*/
public static <T> void watchChannel(CharID id, String channelName,
DataFacetChangeListener<CharID, T> listener, int priority)
{
VariableID<T> varID =
(VariableID<T>) varContext.getVariableID(globalInstance, variableName);
return varID;
getChannelVariableID(id, channelName);
RESULT_FACET.get(id).addVariableListener(priority, varID,
e -> VariableUtilities.forwardVariableChangeToDFCL(id, e, listener));
}
/**
* Sets up the given Code Control so that if the value on the channel changes, the PC
* is categorized as Dirty.
*
* @param pc
* The PlayerCharacter on which the channel should be watched
* @param codeControl
* The name of the channel to be watched
*/
public static void setDirtyOnChannelChange(PlayerCharacter pc,
CControl codeControl)
{
String variableName =
createVarName(pc.getControl(codeControl));
VariableUtilities.addListenerToVariable(pc.getCharID(), variableName,
x -> pc.setDirty(true));
}
}

0 comments on commit 4e508a7

Please sign in to comment.