Search in sources :

Example 1 with ActionProperty

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

the class ActionUtils method getFieldNames.

public static String[] getFieldNames(Action a) {
    List<String> result = new ArrayList<>();
    Class<?> clazz = a.getClass();
    while (clazz != null && clazz != Object.class) {
        for (Field field : clazz.getDeclaredFields()) {
            final ActionProperty property = field.getAnnotation(ActionProperty.class);
            if (property == null) {
                continue;
            }
            final String name = field.getName();
            result.add(name);
        }
        clazz = clazz.getSuperclass();
    }
    return result.toArray(new String[result.size()]);
}
Also used : Field(java.lang.reflect.Field) ActionProperty(com.bladecoder.engine.actions.ActionProperty) ArrayList(java.util.ArrayList)

Example 2 with ActionProperty

use of com.bladecoder.engine.actions.ActionProperty 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 ActionProperty

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

the class ActionUtils method getParams.

public static Param[] getParams(Action action) {
    List<Param> params = new ArrayList<>();
    Class<?> clazz = action.getClass();
    while (clazz != null && clazz != Object.class) {
        for (Field field : clazz.getDeclaredFields()) {
            final ActionProperty property = field.getAnnotation(ActionProperty.class);
            if (property == null) {
                continue;
            }
            final ActionPropertyDescription propertyDescription = field.getAnnotation(ActionPropertyDescription.class);
            // saved in the model.
            if (propertyDescription == null)
                continue;
            final String name = field.getName();
            Param.Type type = property.type();
            Enum<?>[] options = null;
            if (field.getType().isEnum()) {
                final boolean accessible = field.isAccessible();
                field.setAccessible(true);
                options = (Enum[]) field.getType().getEnumConstants();
                field.setAccessible(accessible);
                type = Param.Type.OPTION;
            } else if (property.type() == Param.Type.NOT_SET) {
                type = getType(field);
            }
            params.add(new Param(name, propertyDescription != null ? propertyDescription.value() : "", type, property.required(), property.defaultValue(), options));
        }
        clazz = clazz.getSuperclass();
    }
    return params.toArray(new Param[params.size()]);
}
Also used : ActionProperty(com.bladecoder.engine.actions.ActionProperty) ArrayList(java.util.ArrayList) Type(com.bladecoder.engine.actions.Param.Type) Field(java.lang.reflect.Field) ActionPropertyDescription(com.bladecoder.engine.actions.ActionPropertyDescription) Param(com.bladecoder.engine.actions.Param)

Aggregations

ActionProperty (com.bladecoder.engine.actions.ActionProperty)3 Field (java.lang.reflect.Field)3 ArrayList (java.util.ArrayList)2 Color (com.badlogic.gdx.graphics.Color)1 Vector2 (com.badlogic.gdx.math.Vector2)1 ActionPropertyDescription (com.bladecoder.engine.actions.ActionPropertyDescription)1 ActorAnimationRef (com.bladecoder.engine.actions.ActorAnimationRef)1 Param (com.bladecoder.engine.actions.Param)1 Type (com.bladecoder.engine.actions.Param.Type)1 SceneActorRef (com.bladecoder.engine.actions.SceneActorRef)1