Add inputHandler

This commit is contained in:
Felix
2024-11-20 13:32:50 +01:00
parent 184b565526
commit 5f54eff038
4 changed files with 134 additions and 5 deletions

View File

@@ -0,0 +1,81 @@
package pp.mdga.client;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.*;
public class InputSyncronizer {
private MdgaApp app;
private InputManager inputManager;
protected boolean rightMousePressed = false;
private float rotationAngle = 0f;
private int scrollValue = 50;
InputSyncronizer(MdgaApp app) {
this.app = app;
this.inputManager = app.getInputManager();
setupInput();
}
public void update() {
rotateModel();
updateScrollValue();
}
private void setupInput() {
inputManager.addMapping("Settings", new KeyTrigger(KeyInput.KEY_ESCAPE));
inputManager.addMapping("RotateRightMouse", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
inputManager.addMapping("MouseLeft", new MouseAxisTrigger(MouseInput.AXIS_X, false)); // Left movement
inputManager.addMapping("MouseRight", new MouseAxisTrigger(MouseInput.AXIS_X, true)); // Right movement
inputManager.addMapping("MouseScrollUp", new MouseAxisTrigger(MouseInput.AXIS_WHEEL, false)); // Scroll up
inputManager.addMapping("MouseScrollDown", new MouseAxisTrigger(MouseInput.AXIS_WHEEL, true)); // Scroll down
inputManager.addListener(actionListener, "Settings", "RotateRightMouse");
inputManager.addListener(analogListener, "MouseLeft", "MouseRight", "MouseScrollUp", "MouseScrollDown");
}
private final ActionListener actionListener = new ActionListener() {
@Override
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("Settings") && isPressed) {
app.getView().pressEscape();
}
if (name.equals("RotateRightMouse")) {
rightMousePressed = isPressed;
}
}
};
private final AnalogListener analogListener = new AnalogListener() {
@Override
public void onAnalog(String name, float value, float tpf) {
if (name.equals("MouseLeft") && rightMousePressed) {
rotationAngle -= value * 360f;
rotateModel();
} else if (name.equals("MouseRight") && rightMousePressed) {
rotationAngle += value * 360f;
rotateModel();
} else if (name.equals("MouseScrollUp")) {
scrollValue = Math.min(100, scrollValue + 1);
updateScrollValue();
} else if (name.equals("MouseScrollDown")) {
scrollValue = Math.max(1, scrollValue - 1);
updateScrollValue();
}
}
};
private void rotateModel() {
//System.out.println("Rotation Angle: " + rotationAngle);
}
private void updateScrollValue() {
//System.out.println("Scroll Value: " + scrollValue);
}
}

View File

@@ -11,6 +11,7 @@ public class MdgaApp extends SimpleApplication {
private AnimationHandler animationHandler;
private AcousticHandler acousticHandler;
private NotificationSynchronizer notificationSynchronizer;
private InputSyncronizer inputSyncronizer;
MdgaView view = null;
private MdgaState state = MdgaState.MAIN;
@@ -41,7 +42,9 @@ public void simpleInitApp() {
animationHandler = new AnimationHandler(this);
acousticHandler = new AcousticHandler(this);
notificationSynchronizer = new NotificationSynchronizer(this);
inputSyncronizer = new InputSyncronizer(this);
inputManager.deleteMapping("SIMPLEAPP_Exit");
GuiGlobals.initialize(this);
enter(state);
@@ -49,6 +52,7 @@ public void simpleInitApp() {
@Override
public void simpleUpdate(float tpf) {
inputSyncronizer.update();
view.update();
acousticHandler.update();
}
@@ -95,4 +99,8 @@ public AcousticHandler getAcousticHandler() {
public float getResolutionFactor() {
return resolutionFactor;
}
public MdgaView getView() {
return view;
}
}

View File

@@ -22,7 +22,7 @@ public MainView(MdgaApp app) {
dialog = new InputButtonDialog(app, node);
dialog.addButton("Spiel beitreten", () -> app.enter(MdgaState.LOBBY), size);
dialog.addButton("Spiel hosten", () -> app.enter(MdgaState.LOBBY), size);
dialog.addButton("Einstellungen", () -> enterSettings(), size);
dialog.addButton("Einstellungen", () -> enterSettings(false), size);
dialog.addButton("Spiel beenden", () -> app.stop(), size);
}

View File

@@ -16,6 +16,10 @@ public abstract class MdgaView {
protected MdgaApp app;
protected Node node;
int depth = 0;
boolean isVideo = false;
boolean isAudio = false;
private Node settingsNode;
private Node audioNode;
private Node videoNode;
@@ -38,7 +42,7 @@ public MdgaView(MdgaApp app) {
this.audioNode = new Node();
this.videoNode = new Node();
this.settingsButton = new SettingsButtonDialog(app, node, "", () -> enterSettings());
this.settingsButton = new SettingsButtonDialog(app, node, "", () -> enterSettings(false));
this.settingsBackground = createBackground("background/zahnräder.png");
settingsNode.attachChild(settingsBackground);
@@ -103,11 +107,15 @@ protected Geometry createBackground(String texturePath) {
return background;
}
protected void enterSettings() {
protected void enterSettings(boolean soft) {
leave();
app.getGuiNode().attachChild(settingsNode);
if(!soft) {
depth++;
}
settings.show();
}
@@ -118,12 +126,16 @@ protected void leaveSettings(boolean soft) {
if(!soft) {
enter();
depth--;
}
}
protected void enterAudio() {
leaveSettings(true);
depth++;
isAudio = true;
app.getGuiNode().attachChild(audioNode);
audio.show();
@@ -134,7 +146,10 @@ protected void leaveAudio() {
app.getGuiNode().detachChild(audioNode);
enterSettings();
isAudio = false;
depth--;
enterSettings(true);
}
protected void enterVideo() {
@@ -142,6 +157,10 @@ protected void enterVideo() {
app.getGuiNode().attachChild(videoNode);
depth++;
isVideo = true;
video.show();
}
@@ -150,6 +169,27 @@ protected void leaveVideo() {
app.getGuiNode().detachChild(videoNode);
enterSettings();
depth--;
isVideo = false;
enterSettings(true);
}
public void pressEscape() {
if(depth == 0) {
enterSettings(false);
} else if(depth == 1) {
leaveSettings(false);
}
else if (depth == 2){
if(isVideo) {
leaveVideo();
}
if(isAudio) {
leaveAudio();
}
} else {
throw new RuntimeException();
}
}
}