mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-06-15 21:31:12 +00:00
Fix consuming the remaining input events when mapping the gamepad. See GitHub issue 4 for details.
This commit is contained in:
@@ -74,19 +74,19 @@ public class GamepadConfigFrame extends JFrame {
|
|||||||
layout.setHgap(60);
|
layout.setHgap(60);
|
||||||
layout.setVgap(3);
|
layout.setVgap(3);
|
||||||
JPanel mainPanel = new JPanel(layout);
|
JPanel mainPanel = new JPanel(layout);
|
||||||
|
|
||||||
GamepadComponent[] components = GamepadComponent.values();
|
GamepadComponent[] components = GamepadComponent.values();
|
||||||
|
|
||||||
for (int i = 0; i < components.length; i++) {
|
for (int i = 0; i < components.length; i++) {
|
||||||
|
|
||||||
Mapping mapping = config.get(components[i]);
|
Mapping mapping = config.get(components[i]);
|
||||||
if (mapping == null) {
|
if (mapping == null) {
|
||||||
mapping = config.new Mapping(components[i], false, false);
|
mapping = config.new Mapping(components[i], false, false);
|
||||||
}
|
}
|
||||||
Box componentBox = createComponentBox(mapping);
|
Box componentBox = createComponentBox(mapping);
|
||||||
|
|
||||||
mainPanel.add(componentBox);
|
mainPanel.add(componentBox);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
|
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
@@ -99,7 +99,7 @@ public class GamepadConfigFrame extends JFrame {
|
|||||||
this.getContentPane().add(Box.createVerticalStrut(20), "South");
|
this.getContentPane().add(Box.createVerticalStrut(20), "South");
|
||||||
this.getContentPane().add(Box.createHorizontalStrut(20), "East");
|
this.getContentPane().add(Box.createHorizontalStrut(20), "East");
|
||||||
this.getContentPane().add(Box.createHorizontalStrut(20), "West");
|
this.getContentPane().add(Box.createHorizontalStrut(20), "West");
|
||||||
|
|
||||||
this.addWindowListener(createWindowListener());
|
this.addWindowListener(createWindowListener());
|
||||||
this.setVisible(true);
|
this.setVisible(true);
|
||||||
}
|
}
|
||||||
@@ -109,7 +109,7 @@ public class GamepadConfigFrame extends JFrame {
|
|||||||
*/
|
*/
|
||||||
private Box createComponentBox(Mapping mapping) {
|
private Box createComponentBox(Mapping mapping) {
|
||||||
Box componentBox = Box.createHorizontalBox();
|
Box componentBox = Box.createHorizontalBox();
|
||||||
|
|
||||||
JButton mapButton = new JButton();
|
JButton mapButton = new JButton();
|
||||||
JCheckBox invertBox = new JCheckBox("Invert");
|
JCheckBox invertBox = new JCheckBox("Invert");
|
||||||
JCheckBox triggerBox = new JCheckBox("Trigger");
|
JCheckBox triggerBox = new JCheckBox("Trigger");
|
||||||
@@ -127,7 +127,7 @@ public class GamepadConfigFrame extends JFrame {
|
|||||||
triggerBox.setSelected(mapping.trigger);
|
triggerBox.setSelected(mapping.trigger);
|
||||||
triggerBox.addItemListener(createTriggerListener());
|
triggerBox.addItemListener(createTriggerListener());
|
||||||
triggerBox.setToolTipText("If this component should act as a trigger. (one-way axis)");
|
triggerBox.setToolTipText("If this component should act as a trigger. (one-way axis)");
|
||||||
|
|
||||||
componentBox.add(Box.createHorizontalStrut(5));
|
componentBox.add(Box.createHorizontalStrut(5));
|
||||||
componentBox.add(mapping.contComp.getLabel());
|
componentBox.add(mapping.contComp.getLabel());
|
||||||
componentBox.add(Box.createHorizontalGlue());
|
componentBox.add(Box.createHorizontalGlue());
|
||||||
@@ -135,11 +135,11 @@ public class GamepadConfigFrame extends JFrame {
|
|||||||
componentBox.add(invertBox);
|
componentBox.add(invertBox);
|
||||||
componentBox.add(triggerBox);
|
componentBox.add(triggerBox);
|
||||||
componentBox.add(Box.createHorizontalStrut(5));
|
componentBox.add(Box.createHorizontalStrut(5));
|
||||||
|
|
||||||
componentBox.setBorder(new LineBorder(Color.GRAY, 1, true));
|
componentBox.setBorder(new LineBorder(Color.GRAY, 1, true));
|
||||||
|
|
||||||
componentMap.put(componentBox, mapping);
|
componentMap.put(componentBox, mapping);
|
||||||
|
|
||||||
return componentBox;
|
return componentBox;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,14 +206,14 @@ public class GamepadConfigFrame extends JFrame {
|
|||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
Box toMap = (Box)((JButton)e.getSource()).getParent();
|
Box toMap = (Box)((JButton)e.getSource()).getParent();
|
||||||
|
|
||||||
List<Gamepad> gamepads = GamepadHandler.getGamepads();
|
List<Gamepad> gamepads = GamepadHandler.getGamepads();
|
||||||
|
|
||||||
if (gamepads.isEmpty()) {
|
if (gamepads.isEmpty()) {
|
||||||
JOptionPane.showMessageDialog(GamepadConfigFrame.this, "No Gamepad Detected");
|
JOptionPane.showMessageDialog(GamepadConfigFrame.this, "No Gamepad Detected");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
map(toMap, gamepads.get(0));
|
map(toMap, gamepads.get(0));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -224,13 +224,13 @@ public class GamepadConfigFrame extends JFrame {
|
|||||||
*/
|
*/
|
||||||
private void map(final Box toMap, final Gamepad pad) {
|
private void map(final Box toMap, final Gamepad pad) {
|
||||||
if (mappingThread == null || !mappingThread.isAlive()) {
|
if (mappingThread == null || !mappingThread.isAlive()) {
|
||||||
|
|
||||||
//a little janky, could probably be fixed up a bit
|
//a little janky, could probably be fixed up a bit
|
||||||
final JButton buttonPressed = getButton(toMap);
|
final JButton buttonPressed = getButton(toMap);
|
||||||
final Mapping mappingToMap = componentMap.get(toMap);
|
final Mapping mappingToMap = componentMap.get(toMap);
|
||||||
|
|
||||||
buttonPressed.setSelected(true);
|
buttonPressed.setSelected(true);
|
||||||
|
|
||||||
GamepadListener.stopListening();
|
GamepadListener.stopListening();
|
||||||
|
|
||||||
if (GamepadHandler.isRunning()) {
|
if (GamepadHandler.isRunning()) {
|
||||||
@@ -243,26 +243,26 @@ public class GamepadConfigFrame extends JFrame {
|
|||||||
mappingThread = new Thread(new Runnable() {
|
mappingThread = new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
Component newComponent = waitForNewMapping(pad);
|
Component newComponent = waitForNewMapping(pad);
|
||||||
consumeEvents(pad);
|
consumeEvents(pad);
|
||||||
|
|
||||||
|
|
||||||
if (newComponent != null) {
|
if (newComponent != null) {
|
||||||
Mapping oldConfig = config.get(newComponent);
|
Mapping oldConfig = config.get(newComponent);
|
||||||
if (oldConfig != null) {
|
if (oldConfig != null) {
|
||||||
getButton(getBox(oldConfig)).setText("");
|
getButton(getBox(oldConfig)).setText("");
|
||||||
}
|
}
|
||||||
|
|
||||||
config.insertMapping(mappingToMap, newComponent);
|
config.insertMapping(mappingToMap, newComponent);
|
||||||
|
|
||||||
buttonPressed.setText(newComponent.getName());
|
buttonPressed.setText(newComponent.getName());
|
||||||
configChanged = true;
|
configChanged = true;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
buttonPressed.setText(config.getMapping(mappingToMap.contComp));
|
buttonPressed.setText(config.getMapping(mappingToMap.contComp));
|
||||||
}
|
}
|
||||||
|
|
||||||
buttonPressed.setSelected(false);
|
buttonPressed.setSelected(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -271,7 +271,7 @@ public class GamepadConfigFrame extends JFrame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Waits until the user chooses what to map to the clicked component
|
* Waits until the user chooses what to map to the clicked component
|
||||||
*/
|
*/
|
||||||
@@ -301,31 +301,29 @@ public class GamepadConfigFrame extends JFrame {
|
|||||||
}
|
}
|
||||||
return newMapping;
|
return newMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Consumes any events left in the queue after the mapping has been made
|
* Consumes any events left in the queue after the mapping has been made
|
||||||
*/
|
*/
|
||||||
private void consumeEvents(final Gamepad pad) {
|
private void consumeEvents(final Gamepad pad) {
|
||||||
// start a new thread to go through all of the remaining events
|
EventQueue queue = pad.getEvents();
|
||||||
Thread consumeEvents = new Thread(new Runnable() {
|
Event event = new Event();
|
||||||
@Override
|
|
||||||
public void run() {
|
for (int i = 0; i < 5; i++) {
|
||||||
if (pad.poll()) {
|
if (!pad.poll()) {
|
||||||
EventQueue queue = pad.getEvents();
|
break;
|
||||||
Event event = new Event();
|
|
||||||
|
|
||||||
while (queue.getNextEvent(event)) {
|
|
||||||
if (!pad.poll()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
consumeEvents.setName("Consume Events Thread");
|
// Drop all events currently in the queue
|
||||||
consumeEvents.start();
|
while (queue.getNextEvent(event) && pad.poll());
|
||||||
|
|
||||||
|
// Give the queue a bit of time to fill again
|
||||||
|
try {
|
||||||
|
Thread.sleep(50);
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper method to get the box component that contains the given a mapping
|
* Helper method to get the box component that contains the given a mapping
|
||||||
*/
|
*/
|
||||||
@@ -348,7 +346,7 @@ public class GamepadConfigFrame extends JFrame {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Writes the current cofig to the configs on disk.
|
* Writes the current cofig to the configs on disk.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user