Skip to content
This repository has been archived by the owner on Oct 1, 2022. It is now read-only.

Allow force-to moves for movables #657

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,9 @@ public GLDrawContext getDrawContext() {
public void setContextDestroyedListener(IContextDestroyedListener contextDestroyedListener) {
this.contextDestroyedListener = contextDestroyedListener;
}

@Override
protected int getCurrentModifiers() {
return GOEvent.MODIFIER_ALT | GOEvent.MODIFIER_CTRL | GOEvent.MODIFIER_SHIFT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.awt.event.ComponentListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
Expand All @@ -32,6 +33,7 @@
import java.lang.reflect.Field;

import go.graphics.UIPoint;
import go.graphics.event.GOEvent;
import go.graphics.event.GOEventHandlerProvider;
import go.graphics.event.interpreter.AbstractEventConverter;

Expand All @@ -54,6 +56,8 @@ public class GOSwingEventConverter extends AbstractEventConverter

private int scaleFactor = 1;

private int modifiers;

/**
* Creates a new event converter, that converts swing events to go events.
*
Expand Down Expand Up @@ -108,25 +112,30 @@ private void updateScaleFactor(Component component) {

@Override
public void mouseEntered(MouseEvent e) {
updateModifiers(e);
startHover(convertToLocal(e));
}

@Override
public void mouseMoved(MouseEvent e) {
updateModifiers(e);
updateHoverPosition(convertToLocal(e));
}

@Override
public void mouseExited(MouseEvent e) {
updateModifiers(e);
endHover(convertToLocal(e));
}

@Override
public void mouseClicked(MouseEvent e) {
// ignored - we trace down/up
}

@Override
public void mousePressed(MouseEvent e) {
updateModifiers(e);
int mouseButton = e.getButton();
UIPoint local = convertToLocal(e);
if (mouseButton == MouseEvent.BUTTON1) {
Expand All @@ -142,6 +151,7 @@ public void mousePressed(MouseEvent e) {

@Override
public void mouseDragged(MouseEvent e) {
updateModifiers(e);
UIPoint local = convertToLocal(e);
updateDrawPosition(local);
updatePanPosition(local);
Expand All @@ -150,6 +160,7 @@ public void mouseDragged(MouseEvent e) {

@Override
public void mouseReleased(MouseEvent e) {
updateModifiers(e);
UIPoint local = convertToLocal(e);
if (e.getButton() == MouseEvent.BUTTON1) {
endDraw(local);
Expand All @@ -164,19 +175,9 @@ public void mouseReleased(MouseEvent e) {

@Override
public void keyPressed(KeyEvent e) {
updateModifiers(e);
String text = getKeyName(e);
startKeyEvent(text);
/*
* if (ongoingKeyEvent == null) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { ongoingKeyEvent.setHandler(getCancelHandler()); } else if
* (e.getKeyCode() == KeyEvent.VK_UP) { ongoingKeyEvent.setHandler(getPanHandler(0, -KEYPAN)); } else if (e.getKeyCode() == KeyEvent.VK_DOWN)
* { ongoingKeyEvent.setHandler(getPanHandler(0, KEYPAN)); } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
* ongoingKeyEvent.setHandler(getPanHandler(KEYPAN, 0)); } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
* ongoingKeyEvent.setHandler(getPanHandler(-KEYPAN, 0)); }
*
* provider.handleEvent(ongoingKeyEvent);
*
* ongoingKeyEvent.started(); }
*/
}

private String getKeyName(KeyEvent e) {
Expand Down Expand Up @@ -253,30 +254,48 @@ private String getKeyName(KeyEvent e) {
text = "BACK_SPACE";
break;
default:
text = "" + e.getKeyChar();
text = Character.toString(e.getKeyChar());
}
}
return text;
}

@Override
public void keyReleased(KeyEvent e) {
updateModifiers(e);
endKeyEvent(getKeyName(e));
}

private void updateModifiers(InputEvent e) {
int swingMod = e.getModifiers();
this.modifiers = 0;
if ((swingMod & InputEvent.ALT_MASK) != 0) {
this.modifiers |= GOEvent.MODIFIER_ALT;
}
if ((swingMod & InputEvent.CTRL_MASK) != 0) {
this.modifiers |= GOEvent.MODIFIER_CTRL;
}
if ((swingMod & InputEvent.SHIFT_MASK) != 0) {
this.modifiers |= GOEvent.MODIFIER_SHIFT;
}
}

@Override
public void keyTyped(KeyEvent e) {
// ignored
}

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
updateModifiers(e);
float factor = (float) Math.exp(-e.getUnitsToScroll() / 20.0);
startZoom();
endZoomEvent(factor, convertToLocal(e));
}

@Override
public void componentResized(ComponentEvent e) {
// ignored
}

@Override
Expand All @@ -291,6 +310,7 @@ public void componentShown(ComponentEvent componentEvent) {

@Override
public void componentHidden(ComponentEvent e) {
// ignored
}

@Override
Expand All @@ -310,4 +330,9 @@ void privateRegisterComponentListenerToParentWindowOf(Component component, Compo
privateRegisterComponentListenerToParentWindowOf(component.getParent(), childComponent);
}
}

@Override
protected int getCurrentModifiers() {
return modifiers;
}
}
4 changes: 4 additions & 0 deletions go.graphics/src/main/java/go/graphics/area/Area.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ private void changePoint(UIPoint point) {

private class SimpleHoverEvent extends AbstractMouseEvent implements
GOHoverEvent {
private SimpleHoverEvent() {
super(0);
}

public void finish() {
this.setPhase(PHASE_FINISHED);
}
Expand Down
10 changes: 10 additions & 0 deletions go.graphics/src/main/java/go/graphics/event/GOEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
* @author michael
*/
public interface GOEvent {
int MODIFIER_SHIFT = 1;
int MODIFIER_CTRL = 2;
int MODIFIER_ALT = 4;

/**
* Indicates that the event is in the initialization phase. In this pahse, the event handler is searched.
*/
Expand Down Expand Up @@ -75,4 +79,10 @@ public interface GOEvent {
* @return The phase the event is in.
*/
int getPhase();

/**
* Gets the modifiers for the event
* @return The modifiers as bit set
*/
int getModifiers();
}
13 changes: 13 additions & 0 deletions go.graphics/src/main/java/go/graphics/event/GOKeyEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ public class GOKeyEvent extends SingleHandlerGoEvent {
public GOKeyEvent(String keyCode) {
this.keyCode = keyCode;
}

/**
* Creates a new key event for a given key code.
*
* @param keyCode
* The key code.
* @param modifiers
* The modifiers.
*/
public GOKeyEvent(String keyCode, int modifiers) {
super(modifiers);
this.keyCode = keyCode;
}

/**
* Gets the key code the event has.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public class SingleHandlerGoEvent implements GOEvent {

private int phase = PHASE_INITIALIZING;

private final int modifiers;

public SingleHandlerGoEvent() {
this(0);
}

public SingleHandlerGoEvent(int modifiers) {
this.modifiers = modifiers;
}

@Override
public void setHandler(GOEventHandler handler) {
if (getPhase() != PHASE_INITIALIZING) {
Expand Down Expand Up @@ -67,4 +77,9 @@ protected void setPhase(int phase) {
public int getPhase() {
return phase;
}

@Override
public int getModifiers() {
return modifiers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
* @author michael
*/
public class SingleHandlerGoModalEvent extends SingleHandlerGoEvent {
public SingleHandlerGoModalEvent() {
}

public SingleHandlerGoModalEvent(int modifiers) {
super(modifiers);
}

/**
* This method notifies the handler, if it supports the {@link GOModalEventHandler}, that the event data has changed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void tryCancelCurrentEvent() {
*/
protected void startDraw(UIPoint start) {
if (ongoingDrawEvent == null) {
ongoingDrawEvent = new ConvertedDrawEvent(start);
ongoingDrawEvent = new ConvertedDrawEvent(start, getCurrentModifiers());

handleEvent(ongoingDrawEvent);

Expand All @@ -83,7 +83,8 @@ protected void endDraw(UIPoint position) {
if (ongoingDrawEvent != null) {
if (tryReplaceEvent(position, ReplacableEvent.DRAW,
ongoingDrawEvent.getTime(),
ongoingDrawEvent.getMouseMoved())) {
ongoingDrawEvent.getMouseMoved(),
ongoingDrawEvent.getModifiers())) {
abortDraw();
} else {
ongoingDrawEvent.released();
Expand All @@ -101,7 +102,7 @@ protected void abortDraw() {

protected void startPan(UIPoint start) {
if (ongoingPanEvent == null) {
ongoingPanEvent = new ConvertedPanEvent(start);
ongoingPanEvent = new ConvertedPanEvent(start, getCurrentModifiers());
handleEvent(ongoingPanEvent);
ongoingPanEvent.initialized();
}
Expand All @@ -116,7 +117,9 @@ protected void updatePanPosition(UIPoint current) {
protected void endPan(UIPoint position) {
if (ongoingPanEvent != null) {
if (tryReplaceEvent(position, ReplacableEvent.PAN,
ongoingPanEvent.getTime(), ongoingPanEvent.getMouseMoved())) {
ongoingPanEvent.getTime(),
ongoingPanEvent.getMouseMoved(),
ongoingPanEvent.getModifiers())) {
abortPan();
} else {
ongoingPanEvent.released();
Expand All @@ -127,7 +130,7 @@ protected void endPan(UIPoint position) {

protected void startZoom() {
if (ongoingZoomEvent == null) {
ongoingZoomEvent = new ConvertedZoomEvent();
ongoingZoomEvent = new ConvertedZoomEvent(getCurrentModifiers());
handleEvent(ongoingZoomEvent);
ongoingZoomEvent.initialized();
}
Expand Down Expand Up @@ -156,7 +159,7 @@ protected void abortPan() {

protected void startHover(UIPoint start) {
if (ongoingHoverEvent == null) {
ongoingHoverEvent = new ConvertedHoverEvent(start);
ongoingHoverEvent = new ConvertedHoverEvent(start, getCurrentModifiers());
handleEvent(ongoingHoverEvent);
ongoingHoverEvent.initialized();
}
Expand All @@ -172,7 +175,8 @@ protected void endHover(UIPoint position) {
if (ongoingHoverEvent != null) {
if (tryReplaceEvent(position, ReplacableEvent.HOVER,
ongoingHoverEvent.getTime(),
ongoingHoverEvent.getMouseMoved())) {
ongoingHoverEvent.getMouseMoved(),
ongoingHoverEvent.getModifiers())) {
abortHover();
} else {
ongoingHoverEvent.released();
Expand All @@ -188,8 +192,8 @@ protected void abortHover() {
}
}

protected boolean fireCommandEvent(UIPoint point, boolean isSelect) {
ConvertedCommandEvent commandEvent = new ConvertedCommandEvent(point, isSelect);
protected boolean fireCommandEvent(UIPoint point, boolean isSelect, int modifiers) {
ConvertedCommandEvent commandEvent = new ConvertedCommandEvent(point, isSelect, modifiers);

handleEvent(commandEvent);

Expand All @@ -200,7 +204,7 @@ protected boolean fireCommandEvent(UIPoint point, boolean isSelect) {

protected synchronized void startKeyEvent(String string) {
if (ongoingKeyEvent == null) {
ongoingKeyEvent = new GOKeyEvent(string);
ongoingKeyEvent = new GOKeyEvent(string, getCurrentModifiers());
replaceKeyEvent(ongoingKeyEvent);
handleEvent(ongoingKeyEvent);
ongoingKeyEvent.started();
Expand Down Expand Up @@ -251,7 +255,8 @@ private class ConvertedCommandEvent extends SingleHandlerGoEvent implements
private final UIPoint position;
private final boolean selecting;

public ConvertedCommandEvent(UIPoint position, boolean selecting) {
public ConvertedCommandEvent(UIPoint position, boolean selecting, int modifiers) {
super(modifiers);
this.position = position;
this.selecting = selecting;
}
Expand Down Expand Up @@ -279,11 +284,11 @@ protected void addReplaceRule(EventReplacementRule r) {
}

private boolean tryReplaceEvent(UIPoint p, ReplacableEvent e, double time,
double distance) {
double distance, int modifiers) {
for (EventReplacementRule r : replace) {
if (r.matches(e, time, distance)) {
boolean success = fireCommandEvent(p,
r.replaced == Replacement.COMMAND_SELECT);
r.replaced == Replacement.COMMAND_SELECT, modifiers);
if (success) {
return true;
}
Expand All @@ -300,6 +305,10 @@ protected boolean panStarted() {
return ongoingPanEvent != null;
}

protected int getCurrentModifiers() {
return 0;
}

/**
* a rule on when to replace short draw, hover or pan events with other stuff.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class AbstractMouseEvent extends SingleHandlerGoModalEvent {
private int mouseMoved = 0;
private long startTime = 0;

public AbstractMouseEvent() {
super();
public AbstractMouseEvent(int modifiers) {
super(modifiers);
startTime = System.currentTimeMillis();
}

Expand Down
Loading