From 6857393315db9cf6f976279a6712b7470ac3120a Mon Sep 17 00:00:00 2001 From: yugecin Date: Tue, 10 Jan 2017 12:43:05 +0100 Subject: [PATCH] input events --- src/yugecin/opsudance/core/Demux.java | 90 ++++++++++++++++--- .../opsudance/core/DisplayContainer.java | 32 +++---- .../opsudance/core/state/BaseOpsuState.java | 47 ++++++++++ .../opsudance/core/state/OpsuState.java | 25 ++++++ .../state/transitions/TransitionState.java | 3 +- .../opsudance/states/EmptyRedState.java | 38 ++++++-- src/yugecin/opsudance/states/EmptyState.java | 38 ++++++-- 7 files changed, 226 insertions(+), 47 deletions(-) create mode 100644 src/yugecin/opsudance/core/state/BaseOpsuState.java diff --git a/src/yugecin/opsudance/core/Demux.java b/src/yugecin/opsudance/core/Demux.java index fdc1661a..ac47b35b 100644 --- a/src/yugecin/opsudance/core/Demux.java +++ b/src/yugecin/opsudance/core/Demux.java @@ -19,6 +19,9 @@ package yugecin.opsudance.core; import com.google.inject.Inject; import org.newdawn.slick.Graphics; +import org.newdawn.slick.Input; +import org.newdawn.slick.KeyListener; +import org.newdawn.slick.MouseListener; import yugecin.opsudance.kernel.InstanceContainer; import yugecin.opsudance.states.EmptyState; import yugecin.opsudance.core.state.OpsuState; @@ -29,7 +32,7 @@ import yugecin.opsudance.core.state.transitions.TransitionState; /** * state demultiplexer, sends events to current state */ -public class Demux { +public class Demux implements KeyListener, MouseListener { private final InstanceContainer instanceContainer; @@ -49,23 +52,14 @@ public class Demux { fadeOutTransitionState = instanceContainer.provide(FadeOutTransitionState.class); fadeInTransitionState = instanceContainer.provide(FadeInTransitionState.class); } - - public void update(int delta) { - state.update(delta); - } - - public void render(Graphics g) { - state.render(g); - } - - public boolean onCloseRequest() { - return !isTransitioning(); - } - public boolean isTransitioning() { return state == fadeInTransitionState || state == fadeOutTransitionState; } + public void switchState(Class newState) { + switchState(instanceContainer.provide(newState)); + } + public void switchState(OpsuState newState) { if (isTransitioning()) { return; @@ -83,4 +77,72 @@ public class Demux { state = newState; } + /* + * demux stuff below + */ + + public void update(int delta) { + state.update(delta); + } + + public void render(Graphics g) { + state.render(g); + } + + public boolean onCloseRequest() { + return !isTransitioning(); + } + + /* + * input events below, see org.newdawn.slick.KeyListener & org.newdawn.slick.MouseListener + */ + + @Override + public void keyPressed(int key, char c) { + state.keyPressed(key, c); + } + + @Override + public void keyReleased(int key, char c) { + state.keyReleased(key, c); + } + + @Override + public void mouseWheelMoved(int change) { + state.mouseWheelMoved(change); + } + + @Override + public void mouseClicked(int button, int x, int y, int clickCount) { } + + @Override + public void mousePressed(int button, int x, int y) { + state.mousePressed(button, x, y); + } + + @Override + public void mouseReleased(int button, int x, int y) { + state.mouseReleased(button, x, y); + } + + @Override + public void mouseMoved(int oldx, int oldy, int newx, int newy) { } + + @Override + public void mouseDragged(int oldx, int oldy, int newx, int newy) { } + + @Override + public void setInput(Input input) { } + + @Override + public boolean isAcceptingInput() { + return true; + } + + @Override + public void inputEnded() { } + + @Override + public void inputStarted() { } + } diff --git a/src/yugecin/opsudance/core/DisplayContainer.java b/src/yugecin/opsudance/core/DisplayContainer.java index 1b4d0a01..02ffaa92 100644 --- a/src/yugecin/opsudance/core/DisplayContainer.java +++ b/src/yugecin/opsudance/core/DisplayContainer.java @@ -24,11 +24,12 @@ import org.lwjgl.openal.AL; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.newdawn.slick.Graphics; +import org.newdawn.slick.Input; import org.newdawn.slick.opengl.InternalTextureLoader; import org.newdawn.slick.opengl.renderer.Renderer; import org.newdawn.slick.opengl.renderer.SGL; import org.newdawn.slick.util.Log; -import yugecin.opsudance.states.EmptyRedState; +import yugecin.opsudance.core.state.OpsuState; import yugecin.opsudance.utils.GLHelper; import java.util.LinkedList; @@ -48,6 +49,7 @@ public class DisplayContainer { private final List resolutionChangeListeners; private Graphics graphics; + private Input input; public int width; public int height; @@ -66,14 +68,22 @@ public class DisplayContainer { resolutionChangeListeners.add(listener); } + public void switchState(OpsuState newState) { + demux.switchState(newState); + } + + public void switchState(Class newState) { + demux.switchState(newState); + } + public void run() throws LWJGLException { demux.init(); - demux.switchStateNow(new EmptyRedState(null, null)); setup(); - log("ready"); + log("GL ready"); while(!(Display.isCloseRequested() && demux.onCloseRequest())) { // TODO: lower fps when not visible Display.isVisible int delta = getDelta(); + input.poll(width, height); GL.glClear(SGL.GL_COLOR_BUFFER_BIT); /* graphics.resetTransform(); @@ -152,19 +162,9 @@ public class DisplayContainer { graphics = new Graphics(width, height); graphics.setAntiAlias(false); - /* - if (input == null) { - input = new Input(height); - } - input.init(height); - // no need to remove listeners? - //input.removeAllListeners(); - if (game instanceof InputListener) { - input.removeListener((InputListener) game); - input.addListener((InputListener) game); - } - */ - + input = new Input(height); + input.addKeyListener(demux); + input.addMouseListener(demux); } private int getDelta() { diff --git a/src/yugecin/opsudance/core/state/BaseOpsuState.java b/src/yugecin/opsudance/core/state/BaseOpsuState.java new file mode 100644 index 00000000..512387e1 --- /dev/null +++ b/src/yugecin/opsudance/core/state/BaseOpsuState.java @@ -0,0 +1,47 @@ +/* + * opsu!dance - fork of opsu! with cursordance auto + * Copyright (C) 2017 yugecin + * + * opsu!dance is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * opsu!dance is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with opsu!dance. If not, see . + */ +package yugecin.opsudance.core.state; + +public abstract class BaseOpsuState implements OpsuState { + + @Override + public boolean keyPressed(int key, char c) { + return false; + } + + @Override + public boolean keyReleased(int key, char c) { + return false; + } + + @Override + public boolean mouseWheelMoved(int delta) { + return false; + } + + @Override + public boolean mousePressed(int button, int x, int y) { + return false; + } + + @Override + public boolean mouseReleased(int button, int x, int y) { + return false; + } + +} diff --git a/src/yugecin/opsudance/core/state/OpsuState.java b/src/yugecin/opsudance/core/state/OpsuState.java index 9238b598..9620284a 100644 --- a/src/yugecin/opsudance/core/state/OpsuState.java +++ b/src/yugecin/opsudance/core/state/OpsuState.java @@ -26,4 +26,29 @@ public interface OpsuState { void enter(); void leave(); + /** + * @return false to stop event bubbling + */ + boolean keyPressed(int key, char c); + + /** + * @return false to stop event bubbling + */ + boolean keyReleased(int key, char c); + + /** + * @return false to stop event bubbling + */ + boolean mouseWheelMoved(int delta); + + /** + * @return false to stop event bubbling + */ + boolean mousePressed(int button, int x, int y); + + /** + * @return false to stop event bubbling + */ + boolean mouseReleased(int button, int x, int y); + } diff --git a/src/yugecin/opsudance/core/state/transitions/TransitionState.java b/src/yugecin/opsudance/core/state/transitions/TransitionState.java index 5b38399f..1658a57b 100644 --- a/src/yugecin/opsudance/core/state/transitions/TransitionState.java +++ b/src/yugecin/opsudance/core/state/transitions/TransitionState.java @@ -18,9 +18,10 @@ package yugecin.opsudance.core.state.transitions; import org.newdawn.slick.Graphics; +import yugecin.opsudance.core.state.BaseOpsuState; import yugecin.opsudance.core.state.OpsuState; -public abstract class TransitionState implements OpsuState { +public abstract class TransitionState extends BaseOpsuState { protected OpsuState applicableState; diff --git a/src/yugecin/opsudance/states/EmptyRedState.java b/src/yugecin/opsudance/states/EmptyRedState.java index 0f7a3b5c..d9cf5ccd 100644 --- a/src/yugecin/opsudance/states/EmptyRedState.java +++ b/src/yugecin/opsudance/states/EmptyRedState.java @@ -20,28 +20,25 @@ package yugecin.opsudance.states; import com.google.inject.Inject; import org.newdawn.slick.Color; import org.newdawn.slick.Graphics; -import yugecin.opsudance.core.Demux; +import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.state.OpsuState; -import yugecin.opsudance.kernel.InstanceContainer; public class EmptyRedState implements OpsuState { private int counter; - private final Demux demux; - private final InstanceContainer instanceContainer; + private final DisplayContainer displayContainer; @Inject - public EmptyRedState(InstanceContainer instanceContainer, Demux demux) { - this.instanceContainer = instanceContainer; - this.demux = demux; + public EmptyRedState(DisplayContainer displayContainer) { + this.displayContainer = displayContainer; } @Override public void update(int delta) { counter -= delta; if (counter < 0) { - demux.switchState(instanceContainer.provide(EmptyState.class)); + displayContainer.switchState(EmptyState.class); } } @@ -60,4 +57,29 @@ public class EmptyRedState implements OpsuState { public void leave() { } + @Override + public boolean keyPressed(int key, char c) { + return false; + } + + @Override + public boolean keyReleased(int key, char c) { + return false; + } + + @Override + public boolean mouseWheelMoved(int delta) { + return false; + } + + @Override + public boolean mousePressed(int button, int x, int y) { + return false; + } + + @Override + public boolean mouseReleased(int button, int x, int y) { + return false; + } + } diff --git a/src/yugecin/opsudance/states/EmptyState.java b/src/yugecin/opsudance/states/EmptyState.java index c4cb7f49..958e6071 100644 --- a/src/yugecin/opsudance/states/EmptyState.java +++ b/src/yugecin/opsudance/states/EmptyState.java @@ -20,28 +20,25 @@ package yugecin.opsudance.states; import com.google.inject.Inject; import org.newdawn.slick.Color; import org.newdawn.slick.Graphics; -import yugecin.opsudance.core.Demux; +import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.state.OpsuState; -import yugecin.opsudance.kernel.InstanceContainer; public class EmptyState implements OpsuState { private int counter; - private final InstanceContainer instanceContainer; - private final Demux demux; + private final DisplayContainer displayContainer; @Inject - public EmptyState(InstanceContainer instanceContainer, Demux demux) { - this.instanceContainer = instanceContainer; - this.demux = demux; + public EmptyState(DisplayContainer displayContainer) { + this.displayContainer = displayContainer; } @Override public void update(int delta) { counter -= delta; if (counter < 0) { - demux.switchState(instanceContainer.provide(EmptyRedState.class)); + displayContainer.switchState(EmptyRedState.class); } } @@ -60,4 +57,29 @@ public class EmptyState implements OpsuState { public void leave() { } + @Override + public boolean keyPressed(int key, char c) { + return false; + } + + @Override + public boolean keyReleased(int key, char c) { + return false; + } + + @Override + public boolean mouseWheelMoved(int delta) { + return false; + } + + @Override + public boolean mousePressed(int button, int x, int y) { + return false; + } + + @Override + public boolean mouseReleased(int button, int x, int y) { + return false; + } + }