use of com.b3dgs.lionengine.io.DevicePush in project lionengine by b3dgs.
the class DeviceControllerConfig method create.
/**
* Create device controller from configuration.
*
* @param services The services reference (must not be <code>null</code>).
* @param media The media configuration (must not be <code>null</code>).
* @return The created controller.
* @throws LionEngineException If unable to read node.
*/
public static DeviceController create(Services services, Media media) {
final Context context = services.get(Context.class);
final DeviceController controller = new DeviceControllerModel();
final Collection<DeviceControllerConfig> configs = DeviceControllerConfig.imports(services, media);
for (final DeviceControllerConfig config : configs) {
final InputDevice device = context.getInputDevice(config.getDevice());
if (device instanceof DevicePush) {
final DevicePush push = (DevicePush) device;
config.getHorizontal().forEach(h -> controller.addHorizontal(push, new DeviceActionModel(h, push)));
config.getVertical().forEach(v -> controller.addVertical(push, new DeviceActionModel(v, push)));
config.getFire().entrySet().forEach(e -> e.getValue().forEach(c -> controller.addFire(device, e.getKey(), new DeviceActionModel(c, push))));
}
if (device instanceof DevicePointer) {
final DevicePointer pointer = (DevicePointer) device;
controller.addHorizontal(pointer, () -> pointer.getMoveX());
controller.addVertical(pointer, () -> -pointer.getMoveY());
}
if (config.isDisabled()) {
controller.setDisabled(device.getName(), true, true);
}
}
return controller;
}
use of com.b3dgs.lionengine.io.DevicePush 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;
}
Aggregations