| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Pierre Allard, |
| * Regent L'Archeveque - initial API and implementation |
| * |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.common.io.jinput.impl; |
| |
| import org.eclipse.apogy.common.emf.transaction.ApogyCommonTransactionFacade; |
| import org.eclipse.apogy.common.io.jinput.Activator; |
| import org.eclipse.apogy.common.io.jinput.ApogyCommonIOJInputPackage; |
| import org.eclipse.apogy.common.io.jinput.EComponentQualifier; |
| import org.eclipse.apogy.common.io.jinput.EController; |
| import org.eclipse.emf.common.notify.Adapter; |
| import org.eclipse.emf.common.notify.Notification; |
| import org.eclipse.emf.common.notify.impl.AdapterImpl; |
| import org.eclipse.swt.widgets.Display; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import net.java.games.input.Controller; |
| import net.java.games.input.Event; |
| import net.java.games.input.EventQueue; |
| |
| public class ApogyCommonIOJInputFacadeCustomImpl extends ApogyCommonIOJInputFacadeImpl { |
| |
| private static final Logger Logger = LoggerFactory.getLogger(ApogyCommonIOJInputFacadeImpl.class); |
| |
| private Adapter selectComponentAdapter; |
| |
| @Override |
| public void startSelectComponent(EComponentQualifier eComponentQualifier) { |
| /** |
| * Message to warn that there is already a controller selection running. |
| */ |
| if (isSelectingComponent()) { |
| Logger.warn( |
| "Component selection already started. \nUse ApogyCommonIOJInputFacade.INSTANCE.stopSelectComponent(eComponentQualifier) to stop a selection and prevent memory leaks."); |
| } |
| /** |
| * Clears the EventQueue for each controller. This is needed if there is still a |
| * button selected in the queue, this would select this button. |
| */ |
| for (EController eController : Activator.getEControllerEnvironment().getControllers()) { |
| EventQueue queue = eController.getPojoController().getEventQueue(); |
| Event event = new Event(); |
| while (queue.getNextEvent(event)) { |
| } |
| } |
| |
| this.selectComponentAdapter = new AdapterImpl() { |
| @Override |
| public void notifyChanged(Notification msg) { |
| /** Polls every controller */ |
| for (EController eController : Activator.getEControllerEnvironment().getControllers()) { |
| |
| Display.getDefault().asyncExec(new Runnable() { |
| @Override |
| public void run() { |
| Controller controller = eController.getPojoController(); |
| |
| controller.poll(); |
| EventQueue queue = controller.getEventQueue(); |
| Event event = new Event(); |
| |
| while (queue.getNextEvent(event)) { |
| /** |
| * If a value registered is higher than 0.5 or lower than -0.5, the |
| * EComponentQualifier is modified. |
| */ |
| if (Math.abs(event.getValue()) > 0.5) { |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(eComponentQualifier, |
| ApogyCommonIOJInputPackage.Literals.ECOMPONENT_QUALIFIER__ECOMPONENT_NAME, |
| event.getComponent().getName()); |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(eComponentQualifier, |
| ApogyCommonIOJInputPackage.Literals.ECOMPONENT_QUALIFIER__ECONTROLLER_NAME, |
| eController.getName()); |
| } |
| } |
| } |
| }); |
| } |
| ; |
| } |
| |
| }; |
| Activator.getEControllerEnvironment().eAdapters().add(this.selectComponentAdapter); |
| setSelectingComponent(true); |
| } |
| |
| @Override |
| public void stopSelectComponent(EComponentQualifier eComponentQualifier) { |
| if (this.selectComponentAdapter != null) { |
| Activator.getEControllerEnvironment().eAdapters().remove(this.selectComponentAdapter); |
| this.selectComponentAdapter = null; |
| } |
| setSelectingComponent(false); |
| } |
| |
| } // ApogyCommonIOJInputFacadeImpl |