let the base state listen to the resolution change events

This commit is contained in:
yugecin 2017-01-14 23:49:10 +01:00
parent a1e5c3d30f
commit c467e9b34e
5 changed files with 69 additions and 27 deletions

View File

@ -22,11 +22,14 @@ import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.KeyListener;
import org.newdawn.slick.MouseListener;
import yugecin.opsudance.core.state.BaseOpsuState;
import yugecin.opsudance.core.state.transitions.*;
import yugecin.opsudance.kernel.InstanceContainer;
import yugecin.opsudance.core.state.OpsuState;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* state demultiplexer, sends events to current state
*/
@ -46,24 +49,12 @@ public class Demux implements KeyListener, MouseListener {
public Demux(final InstanceContainer instanceContainer) {
this.instanceContainer = instanceContainer;
state = new BaseOpsuState() {
state = (OpsuState) Proxy.newProxyInstance(OpsuState.class.getClassLoader(), new Class[]{OpsuState.class}, new InvocationHandler() {
@Override
public void update(int delta) { }
@Override
public void preRenderUpdate(int delta) { }
@Override
public void render(Graphics g) { }
@Override
public void enter() { }
@Override
public void leave() { }
};
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return false;
}
});
outTransitionListener = new TransitionFinishedListener() {
@Override

View File

@ -17,7 +17,49 @@
*/
package yugecin.opsudance.core.state;
public abstract class BaseOpsuState implements OpsuState {
import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.ResolutionChangeListener;
public abstract class BaseOpsuState implements OpsuState, ResolutionChangeListener {
protected final DisplayContainer displayContainer;
/**
* state is dirty when resolution or skin changed but hasn't rendered yet
*/
private boolean isDirty;
private boolean isCurrentState;
public BaseOpsuState(DisplayContainer displayContainer) {
this.displayContainer = displayContainer;
displayContainer.addResolutionChangeListener(this);
}
protected void revalidate() {
}
@Override
public void onDisplayResolutionChanged(int width, int height) {
if (isCurrentState) {
revalidate();
return;
}
isDirty = true;
}
@Override
public void enter() {
isCurrentState = true;
if (isDirty) {
revalidate();
isDirty = false;
}
}
@Override
public void leave() {
isCurrentState = false;
}
@Override
public boolean onCloseRequest() {

View File

@ -17,8 +17,16 @@
*/
package yugecin.opsudance.core.state.transitions;
import com.google.inject.Inject;
import yugecin.opsudance.core.DisplayContainer;
public class EmptyTransitionState extends TransitionState {
@Inject
public EmptyTransitionState(DisplayContainer displayContainer) {
super(displayContainer);
}
@Override
public void enter() {
finish();

View File

@ -23,12 +23,10 @@ import yugecin.opsudance.core.DisplayContainer;
public abstract class FadeTransitionState extends TransitionState {
private final DisplayContainer container;
private final Color black;
public FadeTransitionState(DisplayContainer container) {
this.container = container;
public FadeTransitionState(DisplayContainer displayContainer) {
super(displayContainer);
black = new Color(Color.black);
}
@ -37,7 +35,7 @@ public abstract class FadeTransitionState extends TransitionState {
applicableState.render(g);
black.a = getMaskAlphaLevel((float) transitionTime / transitionTargetTime);
g.setColor(black);
g.fillRect(0, 0, container.width, container.height);
g.fillRect(0, 0, displayContainer.width, displayContainer.height);
}
protected abstract float getMaskAlphaLevel(float fadeProgress);

View File

@ -18,6 +18,7 @@
package yugecin.opsudance.core.state.transitions;
import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.state.BaseOpsuState;
import yugecin.opsudance.core.state.OpsuState;
@ -30,6 +31,10 @@ public abstract class TransitionState extends BaseOpsuState {
private TransitionFinishedListener listener;
public TransitionState(DisplayContainer displayContainer) {
super(displayContainer);
}
public final TransitionState set(OpsuState applicableState, int targetTime, TransitionFinishedListener listener) {
this.applicableState = applicableState;
this.transitionTargetTime = targetTime;
@ -61,12 +66,10 @@ public abstract class TransitionState extends BaseOpsuState {
@Override
public void enter() {
super.enter();
transitionTime = 0;
}
@Override
public void leave() { }
protected final void finish() {
listener.onFinish();
}