Search in sources :

Example 1 with ActorAnimationRef

use of com.bladecoder.engine.actions.ActorAnimationRef in project bladecoder-adventure-engine by bladecoder.

the class ActionUtils method setParam.

@SuppressWarnings("unchecked")
public static void setParam(Action action, String param, String value) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Class<?> clazz = action.getClass();
    Field field = getField(clazz, param);
    if (field == null)
        throw new NoSuchFieldException(param);
    final boolean accessible = field.isAccessible();
    field.setAccessible(true);
    if (field.getType().isAssignableFrom(String.class)) {
        field.set(action, value);
    } else if (field.getType().isAssignableFrom(boolean.class)) {
        field.setBoolean(action, Boolean.parseBoolean(value));
    } else if (field.getType().isAssignableFrom(Boolean.class)) {
        Boolean b = null;
        if (value != null)
            b = Boolean.valueOf(value);
        field.set(action, b);
    } else if (field.getType().isAssignableFrom(float.class)) {
        try {
            field.setFloat(action, Float.parseFloat(value));
        } catch (NumberFormatException ignored) {
        }
    } else if (field.getType().isAssignableFrom(Float.class)) {
        try {
            Float f = null;
            if (value != null)
                f = Float.valueOf(value);
            field.set(action, f);
        } catch (NumberFormatException ignored) {
        }
    } else if (field.getType().isAssignableFrom(int.class)) {
        try {
            Integer i = null;
            if (value != null) {
                i = Integer.parseInt(value);
                field.setInt(action, i);
            }
        } catch (NumberFormatException ignored) {
        }
    } else if (field.getType().isAssignableFrom(Integer.class)) {
        try {
            Float f = null;
            if (value != null)
                f = Float.valueOf(value);
            field.set(action, f);
        } catch (NumberFormatException ignored) {
        }
    } else if (field.getType().isAssignableFrom(Vector2.class)) {
        field.set(action, Param.parseVector2(value));
    } else if (field.getType().isAssignableFrom(SceneActorRef.class)) {
        if (value == null)
            field.set(action, null);
        else
            field.set(action, new SceneActorRef(value));
    } else if (field.getType().isAssignableFrom(ActorAnimationRef.class)) {
        if (value == null)
            field.set(action, null);
        else
            field.set(action, new ActorAnimationRef(value));
    } else if (field.getType().isAssignableFrom(Color.class)) {
        Color a = Param.parseColor(value);
        field.set(action, a);
    } else if (field.getType().isEnum()) {
        field.set(action, Enum.valueOf(field.getType().asSubclass(Enum.class), value.toUpperCase(Locale.ENGLISH)));
    } else {
        EngineLogger.error("ACTION FIELD TYPE NOT SUPPORTED -  type: " + field.getType());
    }
    field.setAccessible(accessible);
}
Also used : SceneActorRef(com.bladecoder.engine.actions.SceneActorRef) Color(com.badlogic.gdx.graphics.Color) Field(java.lang.reflect.Field) Vector2(com.badlogic.gdx.math.Vector2) ActorAnimationRef(com.bladecoder.engine.actions.ActorAnimationRef)

Example 2 with ActorAnimationRef

use of com.bladecoder.engine.actions.ActorAnimationRef in project bladecoder-adventure-engine by bladecoder.

the class ActionUtils method writeJson.

public static void writeJson(Action a, Json json) {
    Class<?> clazz = a.getClass();
    json.writeObjectStart(clazz, null);
    while (clazz != null && clazz != Object.class) {
        for (Field field : clazz.getDeclaredFields()) {
            final ActionProperty property = field.getAnnotation(ActionProperty.class);
            if (property == null) {
                continue;
            }
            // json.writeField(a, field.getName());
            final boolean accessible = field.isAccessible();
            field.setAccessible(true);
            try {
                Object o = field.get(a);
                // doesn't write null fields
                if (o == null)
                    continue;
                if (o instanceof SceneActorRef) {
                    SceneActorRef sceneActor = (SceneActorRef) o;
                    json.writeValue(field.getName(), sceneActor.toString());
                } else if (o instanceof ActorAnimationRef) {
                    ActorAnimationRef aa = (ActorAnimationRef) o;
                    json.writeValue(field.getName(), aa.toString());
                } else if (o instanceof Color) {
                    json.writeValue(field.getName(), ((Color) o).toString());
                } else if (o instanceof Vector2) {
                    json.writeValue(field.getName(), Param.toStringParam((Vector2) o));
                } else {
                    json.writeValue(field.getName(), o);
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
            }
            field.setAccessible(accessible);
        }
        clazz = clazz.getSuperclass();
    }
    json.writeObjectEnd();
}
Also used : Field(java.lang.reflect.Field) ActionProperty(com.bladecoder.engine.actions.ActionProperty) SceneActorRef(com.bladecoder.engine.actions.SceneActorRef) Vector2(com.badlogic.gdx.math.Vector2) Color(com.badlogic.gdx.graphics.Color) ActorAnimationRef(com.bladecoder.engine.actions.ActorAnimationRef)

Example 3 with ActorAnimationRef

use of com.bladecoder.engine.actions.ActorAnimationRef in project bladecoder-adventure-engine by bladecoder.

the class ActorAnimationInputPanel method setText.

public void setText(String s) {
    ActorAnimationRef aa = new ActorAnimationRef(s);
    actor.setSelected(aa.getActorId() == null ? "" : aa.getActorId());
    actorSelected();
    animation.setSelected(aa.getAnimationId());
}
Also used : ActorAnimationRef(com.bladecoder.engine.actions.ActorAnimationRef)

Example 4 with ActorAnimationRef

use of com.bladecoder.engine.actions.ActorAnimationRef in project bladecoder-adventure-engine by bladecoder.

the class ActorAnimationInputPanel method validateField.

@Override
public boolean validateField() {
    ActorAnimationRef a = new ActorAnimationRef(getText());
    if (isMandatory()) {
        if (a.getActorId() == null || a.getActorId().trim().isEmpty() || a.getAnimationId() == null || a.getAnimationId().trim().isEmpty()) {
            setError(true);
            return false;
        }
    }
    setError(false);
    return true;
}
Also used : ActorAnimationRef(com.bladecoder.engine.actions.ActorAnimationRef)

Aggregations

ActorAnimationRef (com.bladecoder.engine.actions.ActorAnimationRef)4 Color (com.badlogic.gdx.graphics.Color)2 Vector2 (com.badlogic.gdx.math.Vector2)2 SceneActorRef (com.bladecoder.engine.actions.SceneActorRef)2 Field (java.lang.reflect.Field)2 ActionProperty (com.bladecoder.engine.actions.ActionProperty)1