Search in sources :

Example 1 with DeviceMapper

use of com.b3dgs.lionengine.io.DeviceMapper in project lionengine by b3dgs.

the class DeviceControllerConfig method imports.

/**
 * Import the data from configurer.
 *
 * @param services The services reference (must not be <code>null</code>).
 * @param media The media configuration (must not be <code>null</code>).
 * @return The loaded input.
 * @throws LionEngineException If unable to read node.
 */
@SuppressWarnings("unchecked")
public static Collection<DeviceControllerConfig> imports(Services services, Media media) {
    Check.notNull(services);
    Check.notNull(media);
    final Collection<DeviceControllerConfig> configs = new ArrayList<>();
    final ClassLoader loader = services.getOptional(ClassLoader.class).orElse(DeviceControllerConfig.class.getClassLoader());
    final Configurer configurer = new Configurer(media);
    try {
        final Class<Enum<? extends DeviceMapper>> mapping;
        mapping = (Class<Enum<? extends DeviceMapper>>) loader.loadClass(configurer.getString(ATT_MAPPING));
        for (final XmlReader deviceNode : configurer.getChildren(NODE_DEVICE)) {
            final Class<DevicePush> device = (Class<DevicePush>) loader.loadClass(deviceNode.getString(ATT_CLASS));
            final boolean disabled = deviceNode.getBoolean(false, ATT_DISABLED);
            final List<DeviceAxis> horizontal = readAxis(deviceNode, NODE_HORIZONTAL);
            final List<DeviceAxis> vertical = readAxis(deviceNode, NODE_VERTICAL);
            final Map<Integer, Set<Integer>> fire = readFire(mapping, deviceNode);
            configs.add(new DeviceControllerConfig(device, disabled, horizontal, vertical, fire));
        }
    } catch (final ReflectiveOperationException exception) {
        throw new LionEngineException(exception);
    }
    return configs;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) DeviceMapper(com.b3dgs.lionengine.io.DeviceMapper) ArrayList(java.util.ArrayList) XmlReader(com.b3dgs.lionengine.XmlReader) DeviceAxis(com.b3dgs.lionengine.io.DeviceAxis) DevicePush(com.b3dgs.lionengine.io.DevicePush) LionEngineException(com.b3dgs.lionengine.LionEngineException) Configurer(com.b3dgs.lionengine.game.Configurer)

Example 2 with DeviceMapper

use of com.b3dgs.lionengine.io.DeviceMapper in project lionengine by b3dgs.

the class DeviceControllerConfig method readFire.

/**
 * Read fire data from node.
 *
 * @param mapping The mapping type.
 * @param node The node parent.
 * @return The fire data.
 */
private static Map<Integer, Set<Integer>> readFire(Class<Enum<? extends DeviceMapper>> mapping, XmlReader node) {
    final Map<Integer, Set<Integer>> fire = new HashMap<>();
    for (final XmlReader nodeFire : node.getChildren(NODE_FIRE)) {
        final DeviceMapper mapper = findEnum(mapping, nodeFire.getString(ATT_INDEX));
        final Integer index = mapper.getIndex();
        final Integer positive = Integer.valueOf(nodeFire.getInteger(ATT_POSITIVE));
        Set<Integer> codes = fire.get(index);
        if (codes == null) {
            codes = new HashSet<>();
            fire.put(index, codes);
        }
        codes.add(positive);
    }
    return fire;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) DeviceMapper(com.b3dgs.lionengine.io.DeviceMapper) XmlReader(com.b3dgs.lionengine.XmlReader)

Aggregations

XmlReader (com.b3dgs.lionengine.XmlReader)2 DeviceMapper (com.b3dgs.lionengine.io.DeviceMapper)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 LionEngineException (com.b3dgs.lionengine.LionEngineException)1 Configurer (com.b3dgs.lionengine.game.Configurer)1 DeviceAxis (com.b3dgs.lionengine.io.DeviceAxis)1 DevicePush (com.b3dgs.lionengine.io.DevicePush)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1