Skip to content
Permalink
Browse files

Make mouse event handling more consistent with CC

 - mouse_up events are only fired for the most recently pressed button,
   and not afterwards.
 - Fix mouse_drag events being fired for the same pixel as a click.
 - mouse_drag events use the most recently pressed button, and stop
   after a release.

Fixes #125
  • Loading branch information...
SquidDev committed Apr 10, 2019
1 parent e306bd0 commit 64e3a4a7b9734955b403f6fc7c6352a9e5012abc
@@ -89,8 +89,8 @@ public void removeListener(@Nonnull Renderer.Listener listener) {
private final int pixelHeight;

private boolean lastBlink = false;
private int dragButton = -1;
private Point lastDragSpot = null;
private int lastDragButton = -1;
private Point lastDragPosition = null;

private double blinkLockedTime = 0d;

@@ -154,7 +154,7 @@ public void drop(DropTargetDropEvent dtde) {
val data = (List<File>) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
computer.copyFiles(data, "/");
JOptionPane.showMessageDialog(null, "Files have been copied to the computer root.",
"Files copied", JOptionPane.INFORMATION_MESSAGE);
"Files copied", JOptionPane.INFORMATION_MESSAGE);
} else if (DataFlavor.selectBestTextFlavor(flavors) != null) {
val f = DataFlavor.selectBestTextFlavor(flavors);

@@ -317,8 +317,8 @@ public void keyPressed(KeyEvent e) {
boolean hasModifier = (e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0;
if (rendererConfig.nativePaste.get()
? DefaultEditorKit.pasteAction.equals(
((InputMap) UIManager.getLookAndFeelDefaults().get("TextField.focusInputMap"))
.get(KeyStroke.getKeyStrokeForEvent(e)))
((InputMap) UIManager.getLookAndFeelDefaults().get("TextField.focusInputMap"))
.get(KeyStroke.getKeyStrokeForEvent(e)))
: hasModifier && e.getKeyCode() == KeyEvent.VK_V) {
try {
computer.paste((String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor));
@@ -363,31 +363,34 @@ public void keyReleased(KeyEvent e) {
}
}

private void fireMouseEvent(MouseEvent e, boolean press) {
Point p = mapPointToCC(new Point(e.getX(), e.getY()));
int button = swingToCC(e.getButton());
if(button != -1) computer.click(button, p.x, p.y, !press);
}

@Override
public void mouseDragged(MouseEvent e) {
Point p = mapPointToCC(new Point(e.getX(), e.getY()));
if (p.equals(lastDragSpot) || dragButton == -1) return;
if (lastDragButton == -1 || p.equals(lastDragPosition)) return;

computer.drag(dragButton, p.x, p.y);
lastDragSpot = p;
computer.drag(lastDragButton, p.x, p.y);
lastDragPosition = p;
}

@Override
public void mousePressed(MouseEvent e) {
fireMouseEvent(e, true);
Point p = mapPointToCC(new Point(e.getX(), e.getY()));
int button = swingToCC(e.getButton());
if (button != -1) dragButton = button;
if (button == -1) return;

computer.click(button, p.x, p.y, false);
lastDragButton = button;
lastDragPosition = p;
}

@Override
public void mouseReleased(MouseEvent e) {
fireMouseEvent(e, false);
Point p = mapPointToCC(new Point(e.getX(), e.getY()));
int button = swingToCC(e.getButton());
if (button == -1 || button != lastDragButton) return;

computer.click(button, p.x, p.y, true);
lastDragButton = -1;
}

@Override
@@ -5,6 +5,7 @@
import static net.clgd.ccemux.api.rendering.TerminalFont.BASE_CHAR_WIDTH;
import static net.clgd.ccemux.api.rendering.TerminalFont.BASE_MARGIN;

import java.awt.Point;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@@ -28,8 +29,8 @@
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.StageStyle;
import lombok.val;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import net.clgd.ccemux.api.OperatingSystem;
import net.clgd.ccemux.api.Utils;
import net.clgd.ccemux.api.emulation.EmulatedComputer;
@@ -67,10 +68,14 @@
private Map<KeyCode, Long> pressedKeys = new HashMap<>();

/**
* int[2] containing the last CC X and Y coordinates of a mouse drag, to
* prevent duplicate events
* The last CC X and Y coordinates of a mouse drag, to prevent duplicate events.
*/
private Point lastDragPosition;

/**
* The button of the last drag/press event.
*/
private int[] lastDrag;
private int lastDragButton;

/**
* @return Whether the cursor should be shown
@@ -188,8 +193,8 @@ private void redraw() {
// draw cursor
if (cursorBlink()) {
g.drawImage(
font.getCharImage('_', paletteAdapter.getColor(computer.terminal.getTextColour(), PaletteAdapter.DEFAULT_FOREGROUND), fontScale),
m + (cw * computer.terminal.getCursorX()), m + (ch * computer.terminal.getCursorY()), cw, ch);
font.getCharImage('_', paletteAdapter.getColor(computer.terminal.getTextColour(), PaletteAdapter.DEFAULT_FOREGROUND), fontScale),
m + (cw * computer.terminal.getCursorX()), m + (ch * computer.terminal.getCursorY()), cw, ch);
}

lastBlink = cursorBlink();
@@ -222,7 +227,7 @@ public void onAdvance(double dt) {
*/
private boolean isComboInProgress() {
return pressedKeys.containsKey(KeyCode.CONTROL) && (pressedKeys.containsKey(KeyCode.T)
|| pressedKeys.containsKey(KeyCode.R) || pressedKeys.containsKey(KeyCode.S));
|| pressedKeys.containsKey(KeyCode.R) || pressedKeys.containsKey(KeyCode.S));
}

private void keyTyped(KeyEvent e) {
@@ -298,42 +303,49 @@ private void keyReleased(KeyEvent e) {
}
}

private int[] coordsToCC(double x, double y) {
return new int[] {
1 + constrainToRange((int) Math.floor((x - margin.get()) / charWidth.get()), 0,
computer.terminal.getWidth()),
1 + constrainToRange((int) Math.floor((y - margin.get()) / charHeight.get()), 0,
computer.terminal.getHeight()) };
private Point coordsToCC(double x, double y) {
return new Point(
1 + constrainToRange((int) Math.floor((x - margin.get()) / charWidth.get()), 0,
computer.terminal.getWidth()),
1 + constrainToRange((int) Math.floor((y - margin.get()) / charHeight.get()), 0,
computer.terminal.getHeight())
);
}

private void mousePressed(MouseEvent e) {
int[] coords = coordsToCC(e.getX(), e.getY());
Point p = coordsToCC(e.getX(), e.getY());
int button = JFXMouseTranslator.toCC(e.getButton());
if (button != -1) computer.click(button, coords[0], coords[1], false);
if (button == -1) return;

computer.click(button, p.x, p.y, false);
lastDragButton = button;
lastDragPosition = p;
}

private void mouseReleased(MouseEvent e) {
int[] coords = coordsToCC(e.getX(), e.getY());
Point p = coordsToCC(e.getX(), e.getY());
int button = JFXMouseTranslator.toCC(e.getButton());
if (button != -1) computer.click(button, coords[0], coords[1], true);
if (button == -1 || button != lastDragButton) return;

computer.click(button, p.x, p.y, true);
lastDragButton = -1;
}

private void mouseDragged(MouseEvent e) {
int[] coords = coordsToCC(e.getX(), e.getY());
int button = JFXMouseTranslator.toCC(e.getButton());
if (button != -1 && (lastDrag == null || lastDrag[0] != coords[0] || lastDrag[1] != coords[1])) {
lastDrag = coords;
computer.drag(JFXMouseTranslator.toCC(e.getButton()), coords[0], coords[1]);
}
Point p = coordsToCC(e.getX(), e.getY());
if (lastDragButton == -1 || p.equals(lastDragPosition)) return;

computer.drag(lastDragButton, p.x, p.y);
lastDragPosition = p;
}

private void mouseScroll(ScrollEvent e) {
int[] coords = coordsToCC(e.getX(), e.getY());
computer.scroll(-1 * (int) (e.getDeltaY() / e.getMultiplierY()), coords[0], coords[1]);
Point p = coordsToCC(e.getX(), e.getY());
computer.scroll(-1 * (int) (e.getDeltaY() / e.getMultiplierY()), p.x, p.y);
}

private void focusChanged(Observable e) {
if(!focusedProperty().get()) {
if (!focusedProperty().get()) {
for (KeyCode code : pressedKeys.keySet()) {
int ccCode = JFXKeyTranslator.translateToCC(code);
if (ccCode != 0) computer.releaseKey(ccCode);
@@ -367,7 +379,7 @@ public boolean transferContents(Clipboard cb) {
a.setTitle("File copy error");
a.setHeaderText("File copy error");
a.setContentText("There was an error copying file to computer ID " + computer.getID() + ":\n"
+ e1.getLocalizedMessage() + "\n\nSee logs for more information");
+ e1.getLocalizedMessage() + "\n\nSee logs for more information");
a.initStyle(StageStyle.UTILITY);
a.show();
return false;

0 comments on commit 64e3a4a

Please sign in to comment.
You can’t perform that action at this time.