input events

This commit is contained in:
yugecin 2017-01-10 12:43:05 +01:00
parent e05b675285
commit 6857393315
7 changed files with 226 additions and 47 deletions

View File

@ -19,6 +19,9 @@ package yugecin.opsudance.core;
import com.google.inject.Inject; import com.google.inject.Inject;
import org.newdawn.slick.Graphics; 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.kernel.InstanceContainer;
import yugecin.opsudance.states.EmptyState; import yugecin.opsudance.states.EmptyState;
import yugecin.opsudance.core.state.OpsuState; import yugecin.opsudance.core.state.OpsuState;
@ -29,7 +32,7 @@ import yugecin.opsudance.core.state.transitions.TransitionState;
/** /**
* state demultiplexer, sends events to current state * state demultiplexer, sends events to current state
*/ */
public class Demux { public class Demux implements KeyListener, MouseListener {
private final InstanceContainer instanceContainer; private final InstanceContainer instanceContainer;
@ -49,23 +52,14 @@ public class Demux {
fadeOutTransitionState = instanceContainer.provide(FadeOutTransitionState.class); fadeOutTransitionState = instanceContainer.provide(FadeOutTransitionState.class);
fadeInTransitionState = instanceContainer.provide(FadeInTransitionState.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() { public boolean isTransitioning() {
return state == fadeInTransitionState || state == fadeOutTransitionState; return state == fadeInTransitionState || state == fadeOutTransitionState;
} }
public void switchState(Class<? extends OpsuState> newState) {
switchState(instanceContainer.provide(newState));
}
public void switchState(OpsuState newState) { public void switchState(OpsuState newState) {
if (isTransitioning()) { if (isTransitioning()) {
return; return;
@ -83,4 +77,72 @@ public class Demux {
state = newState; 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() { }
} }

View File

@ -24,11 +24,12 @@ import org.lwjgl.openal.AL;
import org.lwjgl.opengl.Display; import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.Graphics; import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.opengl.InternalTextureLoader; import org.newdawn.slick.opengl.InternalTextureLoader;
import org.newdawn.slick.opengl.renderer.Renderer; import org.newdawn.slick.opengl.renderer.Renderer;
import org.newdawn.slick.opengl.renderer.SGL; import org.newdawn.slick.opengl.renderer.SGL;
import org.newdawn.slick.util.Log; import org.newdawn.slick.util.Log;
import yugecin.opsudance.states.EmptyRedState; import yugecin.opsudance.core.state.OpsuState;
import yugecin.opsudance.utils.GLHelper; import yugecin.opsudance.utils.GLHelper;
import java.util.LinkedList; import java.util.LinkedList;
@ -48,6 +49,7 @@ public class DisplayContainer {
private final List<ResolutionChangeListener> resolutionChangeListeners; private final List<ResolutionChangeListener> resolutionChangeListeners;
private Graphics graphics; private Graphics graphics;
private Input input;
public int width; public int width;
public int height; public int height;
@ -66,14 +68,22 @@ public class DisplayContainer {
resolutionChangeListeners.add(listener); resolutionChangeListeners.add(listener);
} }
public void switchState(OpsuState newState) {
demux.switchState(newState);
}
public void switchState(Class<? extends OpsuState> newState) {
demux.switchState(newState);
}
public void run() throws LWJGLException { public void run() throws LWJGLException {
demux.init(); demux.init();
demux.switchStateNow(new EmptyRedState(null, null));
setup(); setup();
log("ready"); log("GL ready");
while(!(Display.isCloseRequested() && demux.onCloseRequest())) { while(!(Display.isCloseRequested() && demux.onCloseRequest())) {
// TODO: lower fps when not visible Display.isVisible // TODO: lower fps when not visible Display.isVisible
int delta = getDelta(); int delta = getDelta();
input.poll(width, height);
GL.glClear(SGL.GL_COLOR_BUFFER_BIT); GL.glClear(SGL.GL_COLOR_BUFFER_BIT);
/* /*
graphics.resetTransform(); graphics.resetTransform();
@ -152,19 +162,9 @@ public class DisplayContainer {
graphics = new Graphics(width, height); graphics = new Graphics(width, height);
graphics.setAntiAlias(false); graphics.setAntiAlias(false);
/* input = new Input(height);
if (input == null) { input.addKeyListener(demux);
input = new Input(height); input.addMouseListener(demux);
}
input.init(height);
// no need to remove listeners?
//input.removeAllListeners();
if (game instanceof InputListener) {
input.removeListener((InputListener) game);
input.addListener((InputListener) game);
}
*/
} }
private int getDelta() { private int getDelta() {

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -26,4 +26,29 @@ public interface OpsuState {
void enter(); void enter();
void leave(); 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);
} }

View File

@ -18,9 +18,10 @@
package yugecin.opsudance.core.state.transitions; package yugecin.opsudance.core.state.transitions;
import org.newdawn.slick.Graphics; import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.state.BaseOpsuState;
import yugecin.opsudance.core.state.OpsuState; import yugecin.opsudance.core.state.OpsuState;
public abstract class TransitionState implements OpsuState { public abstract class TransitionState extends BaseOpsuState {
protected OpsuState applicableState; protected OpsuState applicableState;

View File

@ -20,28 +20,25 @@ package yugecin.opsudance.states;
import com.google.inject.Inject; import com.google.inject.Inject;
import org.newdawn.slick.Color; import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics; import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.Demux; import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.state.OpsuState; import yugecin.opsudance.core.state.OpsuState;
import yugecin.opsudance.kernel.InstanceContainer;
public class EmptyRedState implements OpsuState { public class EmptyRedState implements OpsuState {
private int counter; private int counter;
private final Demux demux; private final DisplayContainer displayContainer;
private final InstanceContainer instanceContainer;
@Inject @Inject
public EmptyRedState(InstanceContainer instanceContainer, Demux demux) { public EmptyRedState(DisplayContainer displayContainer) {
this.instanceContainer = instanceContainer; this.displayContainer = displayContainer;
this.demux = demux;
} }
@Override @Override
public void update(int delta) { public void update(int delta) {
counter -= delta; counter -= delta;
if (counter < 0) { 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() { 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;
}
} }

View File

@ -20,28 +20,25 @@ package yugecin.opsudance.states;
import com.google.inject.Inject; import com.google.inject.Inject;
import org.newdawn.slick.Color; import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics; import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.Demux; import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.state.OpsuState; import yugecin.opsudance.core.state.OpsuState;
import yugecin.opsudance.kernel.InstanceContainer;
public class EmptyState implements OpsuState { public class EmptyState implements OpsuState {
private int counter; private int counter;
private final InstanceContainer instanceContainer; private final DisplayContainer displayContainer;
private final Demux demux;
@Inject @Inject
public EmptyState(InstanceContainer instanceContainer, Demux demux) { public EmptyState(DisplayContainer displayContainer) {
this.instanceContainer = instanceContainer; this.displayContainer = displayContainer;
this.demux = demux;
} }
@Override @Override
public void update(int delta) { public void update(int delta) {
counter -= delta; counter -= delta;
if (counter < 0) { 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() { 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;
}
} }