This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
package pm;
|
||||
|
||||
public interface Button {
|
||||
|
||||
}
|
||||
public interface Button {}
|
||||
|
||||
@@ -1,15 +1,53 @@
|
||||
package pm;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import pm.exception.MacroException;
|
||||
import pm.exception.macro.MacroEventOrderException;
|
||||
import pm.macro.Event;
|
||||
import pm.macro.event.Hold;
|
||||
import pm.macro.event.Press;
|
||||
import pm.macro.event.Release;
|
||||
|
||||
public class Macro {
|
||||
protected Event[] eventArray;
|
||||
|
||||
public Macro(Event... eventArray) {
|
||||
this.eventArray = eventArray;
|
||||
public Macro(Event... eventArray) throws MacroException {
|
||||
ArrayList<Button> holdList = new ArrayList<Button>();
|
||||
ArrayList<Event> eventList = new ArrayList<Event>();
|
||||
for (Event event : eventArray) {
|
||||
Button button = event.getButton();
|
||||
if (event instanceof Press) {
|
||||
if (holdList.contains(button)) {
|
||||
throw new MacroEventOrderException("Press events cannot follow hold events for the same button.");
|
||||
}
|
||||
eventList.add(event);
|
||||
eventList.add(new Release(button));
|
||||
} else if (event instanceof Release) {
|
||||
if (!holdList.contains(button)) {
|
||||
throw new MacroEventOrderException("Cannot release a button that is not held.");
|
||||
}
|
||||
holdList.remove(button);
|
||||
eventList.add(event);
|
||||
} else if (event instanceof Hold) {
|
||||
if (holdList.contains(button)) {
|
||||
throw new MacroEventOrderException("Cannot hold a button more than once.");
|
||||
}
|
||||
holdList.add(button);
|
||||
eventList.add(new Press(button));
|
||||
}
|
||||
}
|
||||
if (!holdList.isEmpty()) {
|
||||
throw new MacroEventOrderException("One or more buttons are not released.");
|
||||
}
|
||||
this.eventArray = (Event[]) eventList.toArray(new Event[0]);
|
||||
}
|
||||
|
||||
public int count() {
|
||||
return eventArray.length;
|
||||
}
|
||||
|
||||
public Event get(int i) {
|
||||
return eventArray[i];
|
||||
}
|
||||
}
|
||||
@@ -10,9 +10,9 @@ import pm.application.voorbeeld.VoorbeeldApplication;
|
||||
import pm.device.Device;
|
||||
import pm.device.javainput.extreme3d.Extreme3DDevice;
|
||||
import pm.exception.ActionException;
|
||||
import pm.exception.EventException;
|
||||
import pm.exception.action.NotImplementedActionException;
|
||||
import pm.exception.action.UnknownTargetException;
|
||||
import pm.macro.MacroListener;
|
||||
|
||||
public class Main extends Target {
|
||||
protected static final int SLEEP = 100;
|
||||
@@ -31,6 +31,7 @@ public class Main extends Target {
|
||||
actionQueue = new ConcurrentLinkedQueue<Actions>();
|
||||
//JavaInputService.initialize();
|
||||
Device.initialise(actionQueue);
|
||||
MacroListener.initialise(actionQueue);
|
||||
}
|
||||
|
||||
public void addApplication(Application application) {
|
||||
|
||||
@@ -21,6 +21,7 @@ public enum Actions {
|
||||
}
|
||||
|
||||
public Targets getTarget() {
|
||||
// Todo: controle op target echt geset?
|
||||
return target;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.Queue;
|
||||
|
||||
import pm.action.Actions;
|
||||
import pm.action.Targets;
|
||||
import pm.macro.MacroListener;
|
||||
|
||||
public abstract class Device {
|
||||
protected static Queue<Actions> actionQueue;
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package pm.device;
|
||||
|
||||
import pm.Macro;
|
||||
|
||||
import pm.action.Actions;
|
||||
import pm.action.Targets;
|
||||
|
||||
|
||||
public class MacroListener extends Thread {
|
||||
public void add(Macro macro, Actions action, Targets target) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,15 +6,21 @@ import de.hardcode.jxinput.event.JXInputAxisEvent;
|
||||
import de.hardcode.jxinput.event.JXInputButtonEvent;
|
||||
import de.hardcode.jxinput.event.JXInputDirectionalEvent;
|
||||
|
||||
import pm.Button;
|
||||
import pm.device.Device;
|
||||
import pm.device.javainput.extreme3d.Extreme3DButton;
|
||||
import pm.device.javainput.extreme3d.Extreme3DDirection;
|
||||
import pm.exception.DeviceException;
|
||||
import pm.exception.EventException;
|
||||
import pm.exception.device.JavaInputDeviceNotFoundException;
|
||||
import pm.exception.event.UnknownDirectionException;
|
||||
import pm.macro.event.Press;
|
||||
import pm.macro.event.Release;
|
||||
|
||||
public abstract class JavaInputDevice extends Device {
|
||||
protected JavaInputListener javaInputListener;
|
||||
//protected JXInputDevice jxinputDevice;
|
||||
protected Button previousDirectionalButton;
|
||||
|
||||
protected JavaInputDevice(String name) throws DeviceException {
|
||||
super();
|
||||
@@ -25,6 +31,10 @@ public abstract class JavaInputDevice extends Device {
|
||||
javaInputListener.start();
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
javaInputListener.exit();
|
||||
}
|
||||
|
||||
public static JXInputDevice getDevice(String name) throws DeviceException {
|
||||
int numberOfDevices = JXInputManager.getNumberOfDevices();
|
||||
for (int i = 0; i < numberOfDevices; ++i) {
|
||||
@@ -36,7 +46,41 @@ public abstract class JavaInputDevice extends Device {
|
||||
throw new JavaInputDeviceNotFoundException();
|
||||
}
|
||||
|
||||
public void processEvent(JXInputAxisEvent event) {}
|
||||
public void processEvent(JXInputButtonEvent event) throws EventException {}
|
||||
public void processEvent(JXInputDirectionalEvent event) throws EventException {}
|
||||
public void processEvent(JXInputAxisEvent event) {
|
||||
//addAction(Action.START, Target.APPLICATION);
|
||||
//System.out.println(event);
|
||||
}
|
||||
|
||||
public void processEvent(JXInputButtonEvent event) throws EventException {
|
||||
//addAction(Action.TEST, Target.APPLICATION);
|
||||
Button button = Extreme3DButton.create(event);
|
||||
if (event.getButton().getState()) {
|
||||
System.out.println("Press: " + button);
|
||||
macroListener.add(new Press(button));
|
||||
} else {
|
||||
System.out.println("Release: " + button);
|
||||
macroListener.add(new Release(button));
|
||||
}
|
||||
//System.out.println(button);
|
||||
//System.out.println(button.getType() + " " + button.getName() +" " + );
|
||||
}
|
||||
|
||||
public void processEvent(JXInputDirectionalEvent event) throws UnknownDirectionException {
|
||||
//addAction(Action.EXIT, Target.APPLICATION);
|
||||
//Directional directional = event.getDirectional();
|
||||
Button button = Extreme3DDirection.create(event);
|
||||
if (event.getDirectional().isCentered()) {
|
||||
if (previousDirectionalButton != null) {
|
||||
System.out.println("Release: " + previousDirectionalButton);
|
||||
macroListener.add(new Release(previousDirectionalButton));
|
||||
}
|
||||
} else {
|
||||
System.out.println("Press: " + button);
|
||||
macroListener.add(new Press(button));
|
||||
previousDirectionalButton = button;
|
||||
}
|
||||
//System.out.println(Extreme3DDirection.create(event));
|
||||
//System.out.println(Extreme3DButton.create(event));
|
||||
//System.out.println(directional.isCentered() + " " + directional.getValue() + " [" + directional.getName() + "] " + directional.getResolution() + " " + directional.getDirection());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,4 +98,8 @@ public class JavaInputListener extends Thread implements JXInputAxisEventListene
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
run = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package pm.device.javainput.extreme3d;
|
||||
|
||||
import de.hardcode.jxinput.event.JXInputAxisEvent;
|
||||
import de.hardcode.jxinput.event.JXInputButtonEvent;
|
||||
import de.hardcode.jxinput.event.JXInputDirectionalEvent;
|
||||
|
||||
import pm.Macro;
|
||||
import pm.action.Actions;
|
||||
import pm.action.Targets;
|
||||
import pm.device.javainput.JavaInputDevice;
|
||||
import pm.exception.DeviceException;
|
||||
import pm.exception.EventException;
|
||||
import pm.exception.event.UnknownDirectionException;
|
||||
import pm.exception.MacroException;
|
||||
import pm.macro.event.Hold;
|
||||
import pm.macro.event.Press;
|
||||
import pm.macro.event.Release;
|
||||
|
||||
public class Extreme3DDevice extends JavaInputDevice {
|
||||
|
||||
@@ -20,42 +20,17 @@ public class Extreme3DDevice extends JavaInputDevice {
|
||||
|
||||
public void start() {
|
||||
super.start();
|
||||
macroListener.addMacro(
|
||||
new Macro(
|
||||
new Hold(Button.A),
|
||||
new Press(Button.B),
|
||||
new Press(Button.TWO),
|
||||
new Release(Button.A)),
|
||||
Action.EXIT, Target.APPLICATION));
|
||||
}
|
||||
|
||||
public void processEvent(JXInputAxisEvent event) {
|
||||
//addAction(Action.START, Target.APPLICATION);
|
||||
//System.out.println(event);
|
||||
}
|
||||
|
||||
public void processEvent(JXInputButtonEvent event) throws EventException {
|
||||
//addAction(Action.TEST, Target.APPLICATION);
|
||||
if (event.getButton().getState()) {
|
||||
// press
|
||||
} else {
|
||||
// release
|
||||
try {
|
||||
macroListener.add(
|
||||
new Macro(
|
||||
new Hold(Extreme3DButton.ONE),
|
||||
new Press(Extreme3DButton.TWO),
|
||||
new Press(Extreme3DButton.ELEVEN),
|
||||
new Release(Extreme3DButton.ONE)),
|
||||
Actions.EXIT,
|
||||
Targets.MAIN);
|
||||
} catch (MacroException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(Extreme3DButton.create(event));
|
||||
//System.out.println(button.getType() + " " + button.getName() +" " + );
|
||||
}
|
||||
|
||||
public void processEvent(JXInputDirectionalEvent event) throws UnknownDirectionException {
|
||||
//addAction(Action.EXIT, Target.APPLICATION);
|
||||
//Directional directional = event.getDirectional();
|
||||
if (event.getDirectional().isCentered()) {
|
||||
// release
|
||||
} else {
|
||||
// press
|
||||
}
|
||||
System.out.println(Extreme3DDirection.create(event));
|
||||
//System.out.println(Extreme3DButton.create(event));
|
||||
//System.out.println(directional.isCentered() + " " + directional.getValue() + " [" + directional.getName() + "] " + directional.getResolution() + " " + directional.getDirection());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
9
java/src/pm/exception/MacroException.java
Normal file
9
java/src/pm/exception/MacroException.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package pm.exception;
|
||||
|
||||
public class MacroException extends Exception {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
public MacroException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
11
java/src/pm/exception/macro/MacroEventOrderException.java
Normal file
11
java/src/pm/exception/macro/MacroEventOrderException.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package pm.exception.macro;
|
||||
|
||||
import pm.exception.MacroException;
|
||||
|
||||
public class MacroEventOrderException extends MacroException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
public MacroEventOrderException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
26
java/src/pm/macro/Active.java
Normal file
26
java/src/pm/macro/Active.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package pm.macro;
|
||||
|
||||
import pm.Macro;
|
||||
|
||||
public class Active {
|
||||
protected Macro macro;
|
||||
protected int step;
|
||||
|
||||
public Active(Macro macro) {
|
||||
this.macro = macro;
|
||||
step = -1;
|
||||
}
|
||||
|
||||
public Macro getMacro() {
|
||||
return macro;
|
||||
}
|
||||
|
||||
public boolean next(Event event) {
|
||||
Event next = macro.get(++step);
|
||||
return next == null ? false : event.equals(next);
|
||||
}
|
||||
|
||||
public boolean last() {
|
||||
return step == macro.count() - 1;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,18 @@ package pm.macro;
|
||||
|
||||
import pm.Button;
|
||||
|
||||
public interface Event {
|
||||
public Button button = null;
|
||||
public abstract class Event {
|
||||
protected Button button;
|
||||
|
||||
public Event(Button button) {
|
||||
this.button = button;
|
||||
}
|
||||
|
||||
public Button getButton() {
|
||||
return button;
|
||||
}
|
||||
|
||||
public boolean equals(Event event) {
|
||||
return event.getClass().equals(getClass()) && event.getButton().equals(button); // Todo: controleren of equals goed werkt bij buttons van verschillende typen
|
||||
}
|
||||
}
|
||||
|
||||
54
java/src/pm/macro/MacroListener.java
Normal file
54
java/src/pm/macro/MacroListener.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package pm.macro;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Queue;
|
||||
|
||||
import pm.Macro;
|
||||
|
||||
import pm.action.Actions;
|
||||
import pm.action.Targets;
|
||||
|
||||
public class MacroListener extends Thread {
|
||||
protected static Queue<Actions> actionQueue;
|
||||
|
||||
public ArrayList<Macro> macroList;
|
||||
public HashMap<Macro, Actions> actionMap;
|
||||
public ArrayList<Active> activeList;
|
||||
|
||||
public MacroListener() {
|
||||
macroList = new ArrayList<Macro>();
|
||||
actionMap = new HashMap<Macro, Actions>();
|
||||
activeList = new ArrayList<Active>();
|
||||
}
|
||||
|
||||
public void add(Macro macro, Actions action, Targets target) {
|
||||
action.setTarget(target); // Todo: target en action duidelijker integreren / hernoemen.
|
||||
macroList.add(macro);
|
||||
actionMap.put(macro, action);
|
||||
}
|
||||
|
||||
public void add(Event event) {
|
||||
for (Macro macro : macroList) {
|
||||
activeList.add(new Active(macro));
|
||||
}
|
||||
ArrayList<Active> removeList = new ArrayList<Active>();
|
||||
for (Active active : activeList) {
|
||||
if (active.next(event)) {
|
||||
if (active.last()) {
|
||||
actionQueue.add(actionMap.get(active.getMacro())); // Todo: dit indirect doen?
|
||||
removeList.add(active);
|
||||
}
|
||||
} else {
|
||||
removeList.add(active);
|
||||
}
|
||||
}
|
||||
for (Active active : removeList) {
|
||||
activeList.remove(active);
|
||||
}
|
||||
}
|
||||
|
||||
public static void initialise(Queue<Actions> actionQueue) {
|
||||
MacroListener.actionQueue = actionQueue;
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,8 @@ package pm.macro.event;
|
||||
import pm.Button;
|
||||
import pm.macro.Event;
|
||||
|
||||
public class Hold implements Event {
|
||||
public Button button;
|
||||
|
||||
public class Hold extends Event {
|
||||
public Hold(Button button) {
|
||||
this.button = button;
|
||||
super(button);
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,8 @@ package pm.macro.event;
|
||||
import pm.Button;
|
||||
import pm.macro.Event;
|
||||
|
||||
public class Press implements Event {
|
||||
public Button button;
|
||||
|
||||
public class Press extends Event {
|
||||
public Press(Button button) {
|
||||
this.button = button;
|
||||
super(button);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,8 @@ package pm.macro.event;
|
||||
import pm.Button;
|
||||
import pm.macro.Event;
|
||||
|
||||
public class Release implements Event {
|
||||
public Button button;
|
||||
|
||||
public class Release extends Event {
|
||||
public Release(Button button) {
|
||||
this.button = button;
|
||||
super(button);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user