From 80e1d6eb12a5078f7ebc0b362bbab25ffe30c207 Mon Sep 17 00:00:00 2001 From: yugecin Date: Mon, 6 Feb 2017 01:02:38 +0100 Subject: [PATCH] injecting fields --- src/itdelatrisu/opsu/states/ButtonMenu.java | 2 +- .../opsu/states/DownloadsMenu.java | 10 +++---- src/itdelatrisu/opsu/states/Game.java | 9 ++++--- .../opsu/states/GamePauseMenu.java | 16 +++++------ src/itdelatrisu/opsu/states/GameRanking.java | 11 +++----- src/itdelatrisu/opsu/states/MainMenu.java | 10 +++---- src/itdelatrisu/opsu/states/SongMenu.java | 9 ++++--- src/itdelatrisu/opsu/states/Splash.java | 10 +++---- src/yugecin/opsudance/core/inject/Inject.java | 24 +++++++++++++++++ .../opsudance/core/inject/Injector.java | 27 ++++++++++++++++++- .../opsudance/core/state/BaseOpsuState.java | 7 ++--- .../core/state/ComplexOpsuState.java | 5 ++-- .../transitions/EmptyTransitionState.java | 6 ----- .../transitions/FadeInTransitionState.java | 6 ----- .../transitions/FadeOutTransitionState.java | 6 ----- .../transitions/FadeTransitionState.java | 5 ++-- .../state/transitions/TransitionState.java | 5 ---- 17 files changed, 87 insertions(+), 81 deletions(-) create mode 100644 src/yugecin/opsudance/core/inject/Inject.java diff --git a/src/itdelatrisu/opsu/states/ButtonMenu.java b/src/itdelatrisu/opsu/states/ButtonMenu.java index e3532c23..dd0b22a3 100644 --- a/src/itdelatrisu/opsu/states/ButtonMenu.java +++ b/src/itdelatrisu/opsu/states/ButtonMenu.java @@ -603,7 +603,7 @@ public class ButtonMenu extends BaseOpsuState { private ScoreData scoreData; public ButtonMenu(DisplayContainer displayContainer, InstanceContainer instanceContainer) { - super(displayContainer); + super(); Button.displayContainer = MenuState.displayContainer = displayContainer; Button.instanceContainer = MenuState.instanceContainer = instanceContainer; } diff --git a/src/itdelatrisu/opsu/states/DownloadsMenu.java b/src/itdelatrisu/opsu/states/DownloadsMenu.java index c1605f73..032e3cf9 100644 --- a/src/itdelatrisu/opsu/states/DownloadsMenu.java +++ b/src/itdelatrisu/opsu/states/DownloadsMenu.java @@ -55,8 +55,8 @@ import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; import org.newdawn.slick.gui.TextField; import org.newdawn.slick.util.Log; -import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.events.EventBus; +import yugecin.opsudance.core.inject.Inject; import yugecin.opsudance.core.inject.InstanceContainer; import yugecin.opsudance.core.state.ComplexOpsuState; import yugecin.opsudance.events.BarNotificationEvent; @@ -69,7 +69,8 @@ import yugecin.opsudance.events.BarNotificationEvent; */ public class DownloadsMenu extends ComplexOpsuState { - private final InstanceContainer instanceContainer; + @Inject + private InstanceContainer instanceContainer; /** Delay time, in milliseconds, between each search. */ private static final int SEARCH_DELAY = 700; @@ -289,11 +290,6 @@ public class DownloadsMenu extends ComplexOpsuState { } } - public DownloadsMenu(DisplayContainer displayContainer, InstanceContainer instanceContainer) { - super(displayContainer); - this.instanceContainer = instanceContainer; - } - @Override public void revalidate() { super.revalidate(); diff --git a/src/itdelatrisu/opsu/states/Game.java b/src/itdelatrisu/opsu/states/Game.java index e6ef3e95..4221c01f 100644 --- a/src/itdelatrisu/opsu/states/Game.java +++ b/src/itdelatrisu/opsu/states/Game.java @@ -65,6 +65,7 @@ import org.newdawn.slick.util.Log; import yugecin.opsudance.*; import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.events.EventBus; +import yugecin.opsudance.core.inject.Inject; import yugecin.opsudance.core.inject.InstanceContainer; import yugecin.opsudance.core.state.ComplexOpsuState; import yugecin.opsudance.core.state.transitions.FadeInTransitionState; @@ -82,7 +83,8 @@ import yugecin.opsudance.utils.GLHelper; */ public class Game extends ComplexOpsuState { - private final InstanceContainer instanceContainer; + @Inject + private InstanceContainer instanceContainer; public static boolean isInGame; // TODO delete this when #79 is fixed /** Game restart states. */ @@ -321,9 +323,8 @@ public class Game extends ComplexOpsuState { private boolean skippedToCheckpoint; - public Game(DisplayContainer displayContainer, InstanceContainer instanceContainer) { - super(displayContainer); - this.instanceContainer = instanceContainer; + public Game(DisplayContainer displayContainer) { + super(); mirrorCursor = new Cursor(true); this.moveStoryboardOverlay = new MoveStoryboard(displayContainer); this.optionsOverlay = new OptionsOverlay(displayContainer, OptionsMenu.storyboardOptions); diff --git a/src/itdelatrisu/opsu/states/GamePauseMenu.java b/src/itdelatrisu/opsu/states/GamePauseMenu.java index 642c958e..95c7e933 100644 --- a/src/itdelatrisu/opsu/states/GamePauseMenu.java +++ b/src/itdelatrisu/opsu/states/GamePauseMenu.java @@ -31,7 +31,7 @@ import org.lwjgl.input.Keyboard; import org.newdawn.slick.Color; import org.newdawn.slick.Graphics; import org.newdawn.slick.Input; -import yugecin.opsudance.core.DisplayContainer; +import yugecin.opsudance.core.inject.Inject; import yugecin.opsudance.core.inject.InstanceContainer; import yugecin.opsudance.core.state.BaseOpsuState; @@ -43,18 +43,14 @@ import yugecin.opsudance.core.state.BaseOpsuState; */ public class GamePauseMenu extends BaseOpsuState { - private final InstanceContainer instanceContainer; + @Inject + private InstanceContainer instanceContainer; + + @Inject + private Game gameState; private MenuButton continueButton, retryButton, backButton; - private final Game gameState; - - public GamePauseMenu(DisplayContainer displayContainer, InstanceContainer instanceContainer, Game gameState) { - super(displayContainer); - this.instanceContainer = instanceContainer; - this.gameState = gameState; - } - @Override public void render(Graphics g) { // get background image diff --git a/src/itdelatrisu/opsu/states/GameRanking.java b/src/itdelatrisu/opsu/states/GameRanking.java index 73d28934..17383502 100644 --- a/src/itdelatrisu/opsu/states/GameRanking.java +++ b/src/itdelatrisu/opsu/states/GameRanking.java @@ -36,8 +36,8 @@ import org.newdawn.slick.Graphics; import org.newdawn.slick.Image; import org.newdawn.slick.Input; import org.newdawn.slick.util.Log; -import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.events.EventBus; +import yugecin.opsudance.core.inject.Inject; import yugecin.opsudance.core.inject.InstanceContainer; import yugecin.opsudance.core.state.BaseOpsuState; import yugecin.opsudance.events.BarNotificationEvent; @@ -51,7 +51,8 @@ import yugecin.opsudance.events.BarNotificationEvent; */ public class GameRanking extends BaseOpsuState { - private final InstanceContainer instanceContainer; + @Inject + private InstanceContainer instanceContainer; /** Associated GameData object. */ private GameData data; @@ -62,12 +63,6 @@ public class GameRanking extends BaseOpsuState { /** Button coordinates. */ private float retryY, replayY; - public GameRanking(DisplayContainer displayContainer, InstanceContainer instanceContainer) { - super(displayContainer); - this.instanceContainer = instanceContainer; - - } - @Override public void revalidate() { super.revalidate(); diff --git a/src/itdelatrisu/opsu/states/MainMenu.java b/src/itdelatrisu/opsu/states/MainMenu.java index d3e63090..f13beeeb 100644 --- a/src/itdelatrisu/opsu/states/MainMenu.java +++ b/src/itdelatrisu/opsu/states/MainMenu.java @@ -46,8 +46,8 @@ import org.newdawn.slick.Graphics; import org.newdawn.slick.Image; import org.newdawn.slick.Input; import org.newdawn.slick.util.Log; -import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.events.EventBus; +import yugecin.opsudance.core.inject.Inject; import yugecin.opsudance.core.inject.InstanceContainer; import yugecin.opsudance.core.state.BaseOpsuState; import yugecin.opsudance.core.state.OpsuState; @@ -61,7 +61,8 @@ import yugecin.opsudance.events.BubbleNotificationEvent; */ public class MainMenu extends BaseOpsuState { - private final InstanceContainer instanceContainer; + @Inject + private InstanceContainer instanceContainer; /** Idle time, in milliseconds, before returning the logo to its original position. */ private static final short LOGO_IDLE_DELAY = 10000; @@ -126,11 +127,6 @@ public class MainMenu extends BaseOpsuState { /** The star fountain. */ private StarFountain starFountain; - public MainMenu(DisplayContainer displayContainer, InstanceContainer instanceContainer) { - super(displayContainer); - this.instanceContainer = instanceContainer; - } - @Override protected void revalidate() { programStartTime = System.currentTimeMillis(); diff --git a/src/itdelatrisu/opsu/states/SongMenu.java b/src/itdelatrisu/opsu/states/SongMenu.java index bb37c194..16696d35 100644 --- a/src/itdelatrisu/opsu/states/SongMenu.java +++ b/src/itdelatrisu/opsu/states/SongMenu.java @@ -70,6 +70,7 @@ import org.newdawn.slick.SpriteSheet; import org.newdawn.slick.gui.TextField; import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.events.EventBus; +import yugecin.opsudance.core.inject.Inject; import yugecin.opsudance.core.inject.InstanceContainer; import yugecin.opsudance.core.state.ComplexOpsuState; import yugecin.opsudance.events.BarNotificationEvent; @@ -83,7 +84,8 @@ import yugecin.opsudance.ui.OptionsOverlay; */ public class SongMenu extends ComplexOpsuState { - private final InstanceContainer instanceContainer; + @Inject + private InstanceContainer instanceContainer; /** The max number of song buttons to be shown on each screen. */ public static final int MAX_SONG_BUTTONS = 6; @@ -325,9 +327,8 @@ public class SongMenu extends ComplexOpsuState { private final OptionsOverlay optionsOverlay; - public SongMenu(final DisplayContainer displayContainer, InstanceContainer instanceContainer) { - super(displayContainer); - this.instanceContainer = instanceContainer; + public SongMenu(DisplayContainer displayContainer) { + super(); optionsOverlay = new OptionsOverlay(displayContainer, OptionsMenu.normalOptions); overlays.add(optionsOverlay); } diff --git a/src/itdelatrisu/opsu/states/Splash.java b/src/itdelatrisu/opsu/states/Splash.java index 78e83947..19fe35e7 100644 --- a/src/itdelatrisu/opsu/states/Splash.java +++ b/src/itdelatrisu/opsu/states/Splash.java @@ -38,7 +38,7 @@ import org.newdawn.slick.Color; import org.newdawn.slick.Graphics; import org.newdawn.slick.Input; import org.newdawn.slick.util.Log; -import yugecin.opsudance.core.DisplayContainer; +import yugecin.opsudance.core.inject.Inject; import yugecin.opsudance.core.inject.InstanceContainer; import yugecin.opsudance.core.state.BaseOpsuState; @@ -49,7 +49,8 @@ import yugecin.opsudance.core.state.BaseOpsuState; */ public class Splash extends BaseOpsuState { - private final InstanceContainer instanceContainer; + @Inject + private InstanceContainer instanceContainer; /** Minimum time, in milliseconds, to display the splash screen (and fade in the logo). */ private static final int MIN_SPLASH_TIME = 400; @@ -75,11 +76,6 @@ public class Splash extends BaseOpsuState { // game-related variables private boolean init = false; - public Splash(DisplayContainer displayContainer, InstanceContainer instanceContainer) { - super(displayContainer); - this.instanceContainer = instanceContainer; - } - @Override protected void revalidate() { super.revalidate(); diff --git a/src/yugecin/opsudance/core/inject/Inject.java b/src/yugecin/opsudance/core/inject/Inject.java new file mode 100644 index 00000000..04fc897a --- /dev/null +++ b/src/yugecin/opsudance/core/inject/Inject.java @@ -0,0 +1,24 @@ +/* + * 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.inject; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) public @interface Inject { +} diff --git a/src/yugecin/opsudance/core/inject/Injector.java b/src/yugecin/opsudance/core/inject/Injector.java index fb075533..7ee7a054 100644 --- a/src/yugecin/opsudance/core/inject/Injector.java +++ b/src/yugecin/opsudance/core/inject/Injector.java @@ -18,6 +18,7 @@ package yugecin.opsudance.core.inject; import java.lang.reflect.Constructor; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.LinkedList; @@ -70,12 +71,36 @@ public abstract class Injector implements InstanceContainer, Binder { params[i] = provide(parameterTypes[i]); } try { - return (T) constructor.newInstance(params); + return injectFields((T) constructor.newInstance(params), type); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } } + private T injectFields(T object, Class type) { + do { + for (Field f : type.getDeclaredFields()) { + if (f.getDeclaredAnnotation(Inject.class) == null) { + continue; + } + boolean accessible = f.isAccessible(); + if (!accessible) { + f.setAccessible(true); + } + try { + f.set(object, provide(f.getType())); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + if (!accessible) { + f.setAccessible(false); + } + } + type = type.getSuperclass(); + } while (type != null); + return object; + } + public final Binder bind(Class type) { lastType = type; return this; diff --git a/src/yugecin/opsudance/core/state/BaseOpsuState.java b/src/yugecin/opsudance/core/state/BaseOpsuState.java index f9a7d95a..b8838d27 100644 --- a/src/yugecin/opsudance/core/state/BaseOpsuState.java +++ b/src/yugecin/opsudance/core/state/BaseOpsuState.java @@ -26,13 +26,15 @@ import org.newdawn.slick.Input; import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.events.EventBus; import yugecin.opsudance.core.events.EventListener; +import yugecin.opsudance.core.inject.Inject; import yugecin.opsudance.events.ResolutionOrSkinChangedEvent; import java.io.StringWriter; public abstract class BaseOpsuState implements OpsuState, EventListener { - protected final DisplayContainer displayContainer; + @Inject + protected DisplayContainer displayContainer; /** * state is dirty when resolution or skin changed but hasn't rendered yet @@ -40,8 +42,7 @@ public abstract class BaseOpsuState implements OpsuState, EventListener(); this.overlays = new LinkedList<>(); } diff --git a/src/yugecin/opsudance/core/state/transitions/EmptyTransitionState.java b/src/yugecin/opsudance/core/state/transitions/EmptyTransitionState.java index 9119c60c..fec4c716 100644 --- a/src/yugecin/opsudance/core/state/transitions/EmptyTransitionState.java +++ b/src/yugecin/opsudance/core/state/transitions/EmptyTransitionState.java @@ -17,14 +17,8 @@ */ package yugecin.opsudance.core.state.transitions; -import yugecin.opsudance.core.DisplayContainer; - public class EmptyTransitionState extends TransitionState { - public EmptyTransitionState(DisplayContainer displayContainer) { - super(displayContainer); - } - @Override public void enter() { finish(); diff --git a/src/yugecin/opsudance/core/state/transitions/FadeInTransitionState.java b/src/yugecin/opsudance/core/state/transitions/FadeInTransitionState.java index 11c471e3..5ca1e6c1 100644 --- a/src/yugecin/opsudance/core/state/transitions/FadeInTransitionState.java +++ b/src/yugecin/opsudance/core/state/transitions/FadeInTransitionState.java @@ -17,14 +17,8 @@ */ package yugecin.opsudance.core.state.transitions; -import yugecin.opsudance.core.DisplayContainer; - public class FadeInTransitionState extends FadeTransitionState { - public FadeInTransitionState(DisplayContainer container) { - super(container); - } - @Override protected float getMaskAlphaLevel(float fadeProgress) { return 1f - fadeProgress; diff --git a/src/yugecin/opsudance/core/state/transitions/FadeOutTransitionState.java b/src/yugecin/opsudance/core/state/transitions/FadeOutTransitionState.java index 3a5944ab..23c0ad3c 100644 --- a/src/yugecin/opsudance/core/state/transitions/FadeOutTransitionState.java +++ b/src/yugecin/opsudance/core/state/transitions/FadeOutTransitionState.java @@ -17,14 +17,8 @@ */ package yugecin.opsudance.core.state.transitions; -import yugecin.opsudance.core.DisplayContainer; - public class FadeOutTransitionState extends FadeTransitionState { - public FadeOutTransitionState(DisplayContainer container) { - super(container); - } - @Override protected float getMaskAlphaLevel(float fadeProgress) { return fadeProgress; diff --git a/src/yugecin/opsudance/core/state/transitions/FadeTransitionState.java b/src/yugecin/opsudance/core/state/transitions/FadeTransitionState.java index 15202558..ced62278 100644 --- a/src/yugecin/opsudance/core/state/transitions/FadeTransitionState.java +++ b/src/yugecin/opsudance/core/state/transitions/FadeTransitionState.java @@ -19,14 +19,13 @@ package yugecin.opsudance.core.state.transitions; import org.newdawn.slick.Color; import org.newdawn.slick.Graphics; -import yugecin.opsudance.core.DisplayContainer; public abstract class FadeTransitionState extends TransitionState { private final Color black; - public FadeTransitionState(DisplayContainer displayContainer) { - super(displayContainer); + public FadeTransitionState() { + super(); black = new Color(Color.black); } diff --git a/src/yugecin/opsudance/core/state/transitions/TransitionState.java b/src/yugecin/opsudance/core/state/transitions/TransitionState.java index 6ae186d7..9ea38cf1 100644 --- a/src/yugecin/opsudance/core/state/transitions/TransitionState.java +++ b/src/yugecin/opsudance/core/state/transitions/TransitionState.java @@ -18,7 +18,6 @@ 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; @@ -33,10 +32,6 @@ 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;