Skip to content

Commit

Permalink
Application input refactor
Browse files Browse the repository at this point in the history
- JmeContext implementations have a getInputs method. We did cause a duplicate, but solving this would lead to an explosive amount of refactoring, which we considered not useful in the course of this project with regard to our learning curve.
- Application now doesn't have knowledge about specific input types, but rather contains a list of inputs solving the DIP violation.
- initInput and destroyInput of the application were tested before and after refactoring and an additional setter method was created for testing purposes.
- The inputmanager has taken over the responsibility from the application to initialize the inputs as it manages the input.
- It contains a list of the inputs, which is somewhat redundant considering the additional separate fields for the input types like mouse and key. This can be refactored, but it requires a lot of changes to the inputmanager class, which is for another time.
- Updated some test applications to use the static properties of the keyinput, instead of the object properties.
-
  • Loading branch information
JoopAue committed Apr 3, 2016
1 parent 85ccf35 commit d0f787e
Show file tree
Hide file tree
Showing 32 changed files with 428 additions and 261 deletions.
Empty file modified common.gradle
100644 → 100755
Empty file.
18 changes: 18 additions & 0 deletions jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
import com.jme3.renderer.opengl.GLFbo;
import com.jme3.renderer.opengl.GLRenderer;
import com.jme3.system.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -278,6 +282,20 @@ public TouchInput getTouchInput() {
return androidInput.getTouchInput();
}

@Override
public java.util.List<Input> getInput() {
List<Input> inputs = new ArrayList<>();

inputs.add(getKeyInput());
inputs.add(getMouseInput());
if(!settings.getBoolean("DisableJoysticks")) {
inputs.add(getJoyInput());
}
inputs.add(getTouchInput());
inputs.removeAll(Collections.singleton(null));

return inputs;
}
@Override
public Timer getTimer() {
return timer;
Expand Down
45 changes: 10 additions & 35 deletions jme3-core/src/main/java/com/jme3/app/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.jme3.system.JmeContext.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -86,10 +87,8 @@ public class Application implements SystemListener {
protected LostFocusBehavior lostFocusBehavior = LostFocusBehavior.ThrottleOnLostFocus;
protected float speed = 1f;
protected boolean paused = false;
protected MouseInput mouseInput;
protected KeyInput keyInput;
protected JoyInput joyInput;
protected TouchInput touchInput;

protected List<Input> inputs;
protected InputManager inputManager;
protected AppStateManager stateManager;

Expand Down Expand Up @@ -173,6 +172,10 @@ public void setAssetManager(AssetManager assetManager){
this.assetManager = assetManager;
}

public void setInputManager(InputManager inputManager) {
this.inputManager = inputManager;
}

private void initAssetManager(){
URL assetCfgUrl = null;

Expand Down Expand Up @@ -306,25 +309,8 @@ private void initCamera(){
* AppSettings.
*/
protected void initInput(){
mouseInput = context.getMouseInput();
if (mouseInput != null)
mouseInput.initialize();

keyInput = context.getKeyInput();
if (keyInput != null)
keyInput.initialize();

touchInput = context.getTouchInput();
if (touchInput != null)
touchInput.initialize();

if (!settings.getBoolean("DisableJoysticks")){
joyInput = context.getJoyInput();
if (joyInput != null)
joyInput.initialize();
}

inputManager = new InputManager(mouseInput, keyInput, joyInput, touchInput);
inputs = context.getInput();
inputManager = new InputManager(inputs);
}

private void initStateManager(){
Expand Down Expand Up @@ -715,18 +701,7 @@ public void update(){
}

protected void destroyInput(){
if (mouseInput != null)
mouseInput.destroy();

if (keyInput != null)
keyInput.destroy();

if (joyInput != null)
joyInput.destroy();

if (touchInput != null)
touchInput.destroy();

inputManager.destroyInput();
inputManager = null;
}

Expand Down
78 changes: 56 additions & 22 deletions jme3-core/src/main/java/com/jme3/input/InputManager.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.jme3.util.SafeArrayList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -89,6 +90,7 @@ public class InputManager implements RawInputListener {
private final MouseInput mouse;
private final JoyInput joystick;
private final TouchInput touch;
private final List<Input> inputs;
private float frameTPF;
private long lastLastUpdateTime = 0;
private long lastUpdateTime = 0;
Expand Down Expand Up @@ -123,32 +125,43 @@ public Mapping(String name) {
*
* <p>This should only be called internally in {@link Application}.
*
* @param mouse
* @param keys
* @param joystick
* @param touch
* @param inputs
* @throws IllegalArgumentException If either mouseInput or keyInput are null.
*/
public InputManager(MouseInput mouse, KeyInput keys, JoyInput joystick, TouchInput touch) {
public InputManager(List<Input> inputs) {
KeyInput keys = null;
MouseInput mouse = null;
JoyInput joystick = null;
TouchInput touch = null;

for (Input input : inputs) {
input.initialize();
if(input instanceof MouseInput) {
mouse = (MouseInput) input;
mouse.setInputListener(this);
} else if (input instanceof KeyInput) {
keys = (KeyInput) input;
keys.setInputListener(this);
} else if (input instanceof JoyInput) {
joystick = (JoyInput) input;
joystick.setInputListener(this);
joysticks = joystick.loadJoysticks(this);
} else if (input instanceof TouchInput) {
touch = (TouchInput) input;
touch.setInputListener(this);
}
}

if (keys == null || mouse == null) {
throw new IllegalArgumentException("Mouse or keyboard cannot be null");
}

this.inputs = inputs;
this.keys = keys;
this.mouse = mouse;
this.joystick = joystick;
this.touch = touch;

keys.setInputListener(this);
mouse.setInputListener(this);
if (joystick != null) {
joystick.setInputListener(this);
joysticks = joystick.loadJoysticks(this);
}
if (touch != null) {
touch.setInputListener(this);
}

firstTime = keys.getInputTimeNanos();
}

Expand Down Expand Up @@ -893,13 +906,8 @@ public void update(float tpf) {

eventsPermitted = true;

keys.update();
mouse.update();
if (joystick != null) {
joystick.update();
}
if (touch != null) {
touch.update();
for(Input input : inputs){
input.update();
}

eventsPermitted = false;
Expand Down Expand Up @@ -946,4 +954,30 @@ public void onTouchEvent(TouchEvent evt) {
cursorPos.set(evt.getX(), evt.getY());
inputQueue.add(evt);
}

public void destroyInput() {
for(Input input : inputs){
input.destroy();
}
}

public List<Input> getInputs() {
return inputs;
}

public TouchInput getTouch() {
return touch;
}

public JoyInput getJoystick() {
return joystick;
}

public MouseInput getMouse() {
return mouse;
}

public KeyInput getKeys() {
return keys;
}
}
Empty file modified jme3-core/src/main/java/com/jme3/system/AppSettings.java
100644 → 100755
Empty file.
13 changes: 8 additions & 5 deletions jme3-core/src/main/java/com/jme3/system/JmeContext.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@
*/
package com.jme3.system;

import com.jme3.input.JoyInput;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.TouchInput;
import com.jme3.input.*;
import com.jme3.renderer.Renderer;

import java.util.List;

/**
* Represents a rendering context within the engine.
*/
public interface JmeContext {

/**
* The type of context.
*/
Expand Down Expand Up @@ -110,6 +108,11 @@ public enum Type {
*/
public Renderer getRenderer();

/**
* @return All input implementations. May be empty if not available.
*/
public List<Input> getInput();

/**
* @return Mouse input implementation. May be null if not available.
*/
Expand Down
25 changes: 21 additions & 4 deletions jme3-core/src/main/java/com/jme3/system/NullContext.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
*/
package com.jme3.system;

import com.jme3.input.JoyInput;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.TouchInput;
import com.jme3.input.*;
import com.jme3.input.dummy.DummyKeyInput;
import com.jme3.input.dummy.DummyMouseInput;
import com.jme3.renderer.Renderer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -58,6 +59,8 @@ public class NullContext implements JmeContext, Runnable {
protected SystemListener listener;
protected NullRenderer renderer;

protected List<Input> inputs;

public Type getType() {
return Type.Headless;
}
Expand Down Expand Up @@ -175,6 +178,20 @@ public JoyInput getJoyInput() {
return null;
}

public java.util.List<Input> getInput() {
if(inputs == null) {
inputs = new ArrayList<>();
inputs.add(getKeyInput());
inputs.add(getMouseInput());
if(!settings.getBoolean("DisableJoysticks")) {
inputs.add(getJoyInput());
}
inputs.add(getTouchInput());
inputs.removeAll(Collections.singleton(null));
}
return inputs;
}

public TouchInput getTouchInput() {
return null;
}
Expand Down
17 changes: 8 additions & 9 deletions jme3-core/src/main/java/com/jme3/texture/image/DefaultImageRaster.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.texture.Image;
import org.fest.util.VisibleForTesting;

import java.nio.ByteBuffer;

public class DefaultImageRaster extends ImageRaster {

@VisibleForTesting

protected final int[] components = new int[4];
private ByteBuffer buffer;
@VisibleForTesting

protected final Image image;
@VisibleForTesting

protected final ImageCodec codec;
private final int width;
private final int height;
Expand All @@ -55,7 +54,7 @@ public class DefaultImageRaster extends ImageRaster {
private int slice;


@VisibleForTesting

protected void rangeCheck(int x, int y) {
if (x < 0 || y < 0 || x >= width || y >= height) {
throw new IllegalArgumentException("x and y must be inside the image dimensions:"
Expand Down Expand Up @@ -133,19 +132,19 @@ public void setPixel(int x, int y, ColorRGBA color) {
* Input is linear, needs to be converted to sRGB before writing into image.
* @param color
*/
@VisibleForTesting

protected void getSRGB(ColorRGBA color) {
if (convertToLinear) {
color = color.getAsSrgb();
}
}

@VisibleForTesting

protected void writeComponents(int x, int y) {
codec.writeComponents(getBuffer(), x, y, width, offset, components, temp);
}

@VisibleForTesting

protected void readComponents(int x, int y) {
codec.readComponents(getBuffer(), x, y, width, offset, components, temp);
}
Expand All @@ -171,7 +170,7 @@ public ColorRGBA getPixel(int x, int y, ColorRGBA store) {
return store;
}

@VisibleForTesting

protected void setSRGB(ColorRGBA store) {
if (convertToLinear) {
// Input image is sRGB, need to convert to linear.
Expand Down
Loading

0 comments on commit d0f787e

Please sign in to comment.