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

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