From c467e9b34ef5692c3273e6cea9c900b8e458bf07 Mon Sep 17 00:00:00 2001 From: yugecin Date: Sat, 14 Jan 2017 23:49:10 +0100 Subject: [PATCH] let the base state listen to the resolution change events --- src/yugecin/opsudance/core/Demux.java | 27 ++++-------- .../opsudance/core/state/BaseOpsuState.java | 44 ++++++++++++++++++- .../transitions/EmptyTransitionState.java | 8 ++++ .../transitions/FadeTransitionState.java | 8 ++-- .../state/transitions/TransitionState.java | 9 ++-- 5 files changed, 69 insertions(+), 27 deletions(-) diff --git a/src/yugecin/opsudance/core/Demux.java b/src/yugecin/opsudance/core/Demux.java index 17be4f6f..cb248bca 100644 --- a/src/yugecin/opsudance/core/Demux.java +++ b/src/yugecin/opsudance/core/Demux.java @@ -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 diff --git a/src/yugecin/opsudance/core/state/BaseOpsuState.java b/src/yugecin/opsudance/core/state/BaseOpsuState.java index faeef07b..07a7f173 100644 --- a/src/yugecin/opsudance/core/state/BaseOpsuState.java +++ b/src/yugecin/opsudance/core/state/BaseOpsuState.java @@ -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() { diff --git a/src/yugecin/opsudance/core/state/transitions/EmptyTransitionState.java b/src/yugecin/opsudance/core/state/transitions/EmptyTransitionState.java index fec4c716..8153e8f6 100644 --- a/src/yugecin/opsudance/core/state/transitions/EmptyTransitionState.java +++ b/src/yugecin/opsudance/core/state/transitions/EmptyTransitionState.java @@ -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(); diff --git a/src/yugecin/opsudance/core/state/transitions/FadeTransitionState.java b/src/yugecin/opsudance/core/state/transitions/FadeTransitionState.java index 8c516c83..15202558 100644 --- a/src/yugecin/opsudance/core/state/transitions/FadeTransitionState.java +++ b/src/yugecin/opsudance/core/state/transitions/FadeTransitionState.java @@ -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); diff --git a/src/yugecin/opsudance/core/state/transitions/TransitionState.java b/src/yugecin/opsudance/core/state/transitions/TransitionState.java index 3af5a749..5956da7d 100644 --- a/src/yugecin/opsudance/core/state/transitions/TransitionState.java +++ b/src/yugecin/opsudance/core/state/transitions/TransitionState.java @@ -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(); }