injecting fields

This commit is contained in:
yugecin 2017-02-06 01:02:38 +01:00
parent 77a5ebf537
commit 80e1d6eb12
17 changed files with 87 additions and 81 deletions

View File

@ -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;
}

View File

@ -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();

View File

@ -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);

View File

@ -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

View File

@ -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();

View File

@ -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();

View File

@ -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);
}

View File

@ -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();

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.core.inject;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME) public @interface Inject {
}

View File

@ -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> 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 <T> Binder<T> bind(Class<T> type) {
lastType = type;
return this;

View File

@ -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<ResolutionOrSkinChangedEvent> {
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<Resoluti
private boolean isDirty;
private boolean isCurrentState;
public BaseOpsuState(DisplayContainer displayContainer) {
this.displayContainer = displayContainer;
public BaseOpsuState() {
EventBus.subscribe(ResolutionOrSkinChangedEvent.class, this);
}

View File

@ -19,7 +19,6 @@ package yugecin.opsudance.core.state;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.components.Component;
import java.util.LinkedList;
@ -31,8 +30,8 @@ public abstract class ComplexOpsuState extends BaseOpsuState {
private Component focusedComponent;
public ComplexOpsuState(DisplayContainer displayContainer) {
super(displayContainer);
public ComplexOpsuState() {
super();
this.components = new LinkedList<>();
this.overlays = new LinkedList<>();
}

View File

@ -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();

View File

@ -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;

View File

@ -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;

View File

@ -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);
}

View File

@ -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;