Added the possibility to map keyboard keys to model buttons.

This commit is contained in:
hneemann 2017-10-18 20:35:00 +02:00
parent bb4ba7411a
commit ea15656618
7 changed files with 75 additions and 5 deletions

View File

@ -6,6 +6,7 @@ HEAD, planned as v0.15
- Replaced shortcut 'B' with a more general attribute editing dialog (select multiple
components and click right).
- Added a grid to the main panel.
- Added the possibility to map keyboard keys to model buttons.
- fixed bugs in some 74xx circuits (74160, 74161, 74162 and 74238)
- fixed a bug in the remote interface "run to break" method.
- fixed an error in VHDL export if comparator is used in "signed mode"

View File

@ -1,11 +1,13 @@
package de.neemann.digital.core;
import de.neemann.digital.core.io.Button;
import de.neemann.digital.core.wiring.Break;
import de.neemann.digital.core.wiring.Clock;
import de.neemann.digital.core.wiring.Reset;
import de.neemann.digital.gui.components.WindowPosManager;
import de.neemann.digital.lang.Lang;
import javax.swing.*;
import java.util.*;
/**
@ -47,6 +49,7 @@ public class Model implements Iterable<Node> {
private final ArrayList<Clock> clocks;
private final ArrayList<Break> breaks;
private final ArrayList<Reset> resets;
private final HashMap<Integer, Button> buttonsToMap;
private final ArrayList<Signal> signals;
private final ArrayList<Signal> inputs;
@ -69,6 +72,7 @@ public class Model implements Iterable<Node> {
this.clocks = new ArrayList<>();
this.breaks = new ArrayList<>();
this.resets = new ArrayList<>();
this.buttonsToMap = new HashMap<>();
this.signals = new ArrayList<>();
this.outputs = new ArrayList<>();
this.inputs = new ArrayList<>();
@ -545,6 +549,25 @@ public class Model implements Iterable<Node> {
return sigWithoutPinNumber;
}
/**
* Adds a button which is to map to a keyboard key
*
* @param button the button
* @param keyCode the key code
*/
public void addButtonToMap(Button button, int keyCode) {
buttonsToMap.put(keyCode, button);
}
/**
* Gets the button which matches a givrn key
*
* @param keyCode the key
* @return the button or null if not present
*/
public Button getButtonToMap(int keyCode) {
return buttonsToMap.get(keyCode);
}
/**
* A filter for nodes.

View File

@ -417,6 +417,12 @@ public final class Keys {
public static final Key<Boolean> ACTIVE_LOW
= new Key<>("activeLow", false).allowGroupEdit();
/**
* true if button is mapped to the keyboard
*/
public static final Key<Boolean> MAP_TO_KEY
= new Key<>("mapToKey", false).allowGroupEdit();
/**
* Fitter for the atf1502
*/

View File

@ -7,6 +7,9 @@ import de.neemann.digital.core.element.ElementTypeDescription;
import de.neemann.digital.core.element.Keys;
import de.neemann.digital.lang.Lang;
import javax.swing.*;
import java.awt.event.KeyEvent;
/**
* The Button
*
@ -20,11 +23,13 @@ public class Button implements Element {
public static final ElementTypeDescription DESCRIPTION = new ElementTypeDescription(Button.class)
.addAttribute(Keys.ROTATE)
.addAttribute(Keys.LABEL)
.addAttribute(Keys.ACTIVE_LOW);
.addAttribute(Keys.ACTIVE_LOW)
.addAttribute(Keys.MAP_TO_KEY);
private final ObservableValue output;
private final String label;
private final Boolean invert;
private final boolean invert;
private final boolean mapToKey;
private boolean pressed;
/**
@ -36,6 +41,7 @@ public class Button implements Element {
output = new ObservableValue("out", 1).setPinDescription(DESCRIPTION);
label = attributes.get(Keys.LABEL);
invert = attributes.get(Keys.ACTIVE_LOW);
mapToKey = attributes.get(Keys.MAP_TO_KEY);
output.setValue(invert ? 1 : 0);
}
@ -52,6 +58,11 @@ public class Button implements Element {
@Override
public void registerNodes(Model model) {
model.addSignal(new Signal(label, output));
if (mapToKey) {
final KeyStroke keyStroke = KeyStroke.getKeyStroke(label.trim());
if (keyStroke != null && keyStroke.getKeyCode() != KeyEvent.VK_UNDEFINED)
model.addButtonToMap(this, keyStroke.getKeyCode());
}
}
/**

View File

@ -8,6 +8,7 @@ import de.neemann.digital.core.*;
import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.Key;
import de.neemann.digital.core.element.Keys;
import de.neemann.digital.core.io.Button;
import de.neemann.digital.core.io.In;
import de.neemann.digital.core.io.InValue;
import de.neemann.digital.core.io.Out;
@ -54,9 +55,7 @@ import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@ -186,6 +185,8 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS
getContentPane().add(circuitComponent);
componentOnPane = circuitComponent;
circuitComponent.addKeyListener(new ModelKeyListener());
statusLabel = new JLabel(" ");
getContentPane().add(statusLabel, BorderLayout.SOUTH);
@ -1607,4 +1608,27 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS
}
}
private class ModelKeyListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent keyEvent) {
checkKey(keyEvent.getKeyCode(), true);
}
@Override
public void keyReleased(KeyEvent keyEvent) {
checkKey(keyEvent.getKeyCode(), false);
}
private void checkKey(int keyCode, boolean pressed) {
if (model != null && keyCode != KeyEvent.VK_UNDEFINED) {
Button b = model.getButtonToMap(keyCode);
if (b != null) {
modelSync.access(() -> b.setPressed(pressed));
circuitComponent.repaintNeeded();
}
}
}
}
}

View File

@ -742,6 +742,9 @@ Sind evtl. die Namen der Variablen nicht eindeutig?</string>
<string name="key_libraryPath_tt">Bibliothek mit vordefinierten Schaltungen. Enthält z.B. die ICs der 74xx Reihe.</string>
<string name="key_grid">Raster anzeigen</string>
<string name="key_grid_tt">Zeigt im Hauptfenster ein Raster an, um das platzieren der Elemente zu erleichtern.</string>
<string name="key_mapToKey">Auf Tastatur legen</string>
<string name="key_mapToKey_tt">Taste wird durch die Tastatur bedienbar. Um die Cursor-Tasten zu nutzen, kann als
Bezeichnung UP, DOWN, LEFT oder RIGHT verwendet werden.</string>
<string name="mod_insertWire">Leitung eingefügt.</string>
<string name="mod_insertCopied">Aus Zwischenablage eingefügt.</string>

View File

@ -727,6 +727,8 @@ The names of the variables may not be unique.</string>
<string name="key_libraryPath_tt">Library with predefined subcircuits. Contains, for example, the components of the 74xx series.</string>
<string name="key_grid">Show Grid</string>
<string name="key_grid_tt">Shows a grid in the main window.</string>
<string name="key_mapToKey">Map to keyboard</string>
<string name="key_mapToKey_tt">Button is mapped to the keyboard. To use the cursor keys use UP, DOWN, LEFT or RIGHT as label.</string>
<string name="mod_insertWire">Inserted wire.</string>
<string name="mod_insertCopied">Insert from clipboard.</string>