use of org.terasology.engine.input.events.KeyEvent in project Terasology by MovingBlocks.
the class InputSystem method sendKeyEvent.
/**
* Creates and sends an input event based on a provided raw keyboard input.
*
* @param key The specific input to be sent.
* @param state The state of the input key.
* @param delta The length of the current frame.
* @return true if the event has been consumed by an event listener, false otherwise.
*/
private boolean sendKeyEvent(Input key, ButtonState state, float delta) {
KeyEvent event;
switch(state) {
case UP:
event = KeyUpEvent.create(key, delta);
break;
case DOWN:
event = KeyDownEvent.create(key, delta);
break;
case REPEAT:
event = KeyRepeatEvent.create(key, delta);
break;
default:
return false;
}
boolean consumed = send(event);
event.reset();
return consumed;
}
use of org.terasology.engine.input.events.KeyEvent in project Terasology by MovingBlocks.
the class EventCopier method copyEvent.
public Event copyEvent(Event toBeCopied) {
if (toBeCopied instanceof PlaySoundEvent) {
return toBeCopied;
} else if (toBeCopied instanceof BindButtonEvent) {
BindButtonEvent originalEvent = (BindButtonEvent) toBeCopied;
BindButtonEvent newEvent = (BindButtonEvent) createNewBindEvent(originalEvent);
newEvent.prepare(originalEvent.getId(), originalEvent.getState(), originalEvent.getDelta());
inputEventSetup(newEvent, originalEvent);
return newEvent;
} else if (toBeCopied instanceof KeyEvent) {
KeyEvent originalEvent = (KeyEvent) toBeCopied;
KeyEvent newEvent = createNewKeyEvent(originalEvent);
inputEventSetup(newEvent, originalEvent);
return newEvent;
} else if (toBeCopied instanceof BindAxisEvent) {
BindAxisEvent originalEvent = (BindAxisEvent) toBeCopied;
BindAxisEvent newEvent = (BindAxisEvent) createNewBindEvent(originalEvent);
newEvent.prepare(originalEvent.getId(), (float) originalEvent.getValue(), originalEvent.getDelta());
inputEventSetup(newEvent, originalEvent);
return newEvent;
} else if (toBeCopied instanceof MouseAxisEvent) {
MouseAxisEvent originalEvent = (MouseAxisEvent) toBeCopied;
MouseAxisEvent newEvent = createNewMouseAxisEvent(originalEvent);
inputEventSetup(newEvent, originalEvent);
return newEvent;
} else if (toBeCopied instanceof CameraTargetChangedEvent) {
CameraTargetChangedEvent originalEvent = (CameraTargetChangedEvent) toBeCopied;
return new CameraTargetChangedEvent(originalEvent.getOldTarget(), originalEvent.getNewTarget());
} else if (toBeCopied instanceof CharacterMoveInputEvent) {
CharacterMoveInputEvent originalEvent = (CharacterMoveInputEvent) toBeCopied;
return new CharacterMoveInputEvent(originalEvent.getSequenceNumber(), originalEvent.getPitch(), originalEvent.getYaw(), originalEvent.getMovementDirection(), originalEvent.isRunning(), originalEvent.isCrouching(), originalEvent.isJumping(), originalEvent.getDeltaMs());
} else if (toBeCopied instanceof MouseButtonEvent) {
MouseButtonEvent originalEvent = (MouseButtonEvent) toBeCopied;
MouseButtonEvent newEvent = new MouseButtonEvent(originalEvent.getButton(), originalEvent.getState(), originalEvent.getDelta());
newEvent.setMousePosition(originalEvent.getMousePosition());
inputEventSetup(newEvent, originalEvent);
return newEvent;
} else if (toBeCopied instanceof MouseWheelEvent) {
MouseWheelEvent originalEvent = (MouseWheelEvent) toBeCopied;
MouseWheelEvent newEvent = new MouseWheelEvent(originalEvent.getMousePosition(), originalEvent.getWheelTurns(), originalEvent.getDelta());
inputEventSetup(newEvent, originalEvent);
return newEvent;
} else if (toBeCopied instanceof GetMaxSpeedEvent) {
GetMaxSpeedEvent originalEvent = (GetMaxSpeedEvent) toBeCopied;
GetMaxSpeedEvent newEvent = new GetMaxSpeedEvent(originalEvent.getBaseValue(), originalEvent.getMovementMode());
newEvent.setModifiers(originalEvent.getModifiers());
newEvent.setMultipliers(originalEvent.getMultipliers());
newEvent.setPostModifiers(originalEvent.getPostModifiers());
return newEvent;
} else if (toBeCopied instanceof AttackEvent) {
AttackEvent originalEvent = (AttackEvent) toBeCopied;
AttackEvent newEvent = new AttackEvent(originalEvent.getInstigator(), originalEvent.getDirectCause());
return newEvent;
} else {
return null;
}
}
use of org.terasology.engine.input.events.KeyEvent in project Terasology by MovingBlocks.
the class InputSystemTests method registerEntityKeyCapturing.
private void registerEntityKeyCapturing() {
/*
* KeyUpEvent and KeyDownEvent are singletons with a single instance that gets reset each time.
* Therefore it is not possible to capture multiple events with an ArgumentCaptor
* because previous instances become invalid.
* For this reason, the data of these events is captured to a separate class.
*/
when(clientEntity.send(Mockito.any())).then(invocation -> {
Event event = invocation.getArgument(0);
if (event instanceof KeyEvent) {
clientEntityKeyEvents.add(new CapturedKeyEvent((KeyEvent) event));
}
return event;
});
when(characterEntity.send(Mockito.any())).then(invocation -> {
Event event = invocation.getArgument(0);
if (event instanceof KeyEvent) {
characterEntityKeyEvents.add(new CapturedKeyEvent((KeyEvent) event));
}
return event;
});
}
use of org.terasology.engine.input.events.KeyEvent in project Terasology by MovingBlocks.
the class EventCopier method createNewKeyEvent.
private KeyEvent createNewKeyEvent(KeyEvent originalEvent) {
KeyEvent newEvent = null;
Class eventClass = originalEvent.getClass();
if (eventClass.equals(KeyDownEvent.class)) {
newEvent = KeyDownEvent.createCopy((KeyDownEvent) originalEvent);
} else if (eventClass.equals(KeyRepeatEvent.class)) {
newEvent = KeyRepeatEvent.createCopy((KeyRepeatEvent) originalEvent);
} else if (eventClass.equals(KeyUpEvent.class)) {
newEvent = KeyUpEvent.createCopy((KeyUpEvent) originalEvent);
} else {
logger.error("ERROR!!! Event not Identified");
}
return newEvent;
}
Aggregations