Search in sources :

Example 1 with Field

use of com.badlogic.gdx.utils.reflect.Field in project libgdx by libgdx.

the class Json method getFields.

private OrderedMap<String, FieldMetadata> getFields(Class type) {
    OrderedMap<String, FieldMetadata> fields = typeToFields.get(type);
    if (fields != null)
        return fields;
    Array<Class> classHierarchy = new Array();
    Class nextClass = type;
    while (nextClass != Object.class) {
        classHierarchy.add(nextClass);
        nextClass = nextClass.getSuperclass();
    }
    ArrayList<Field> allFields = new ArrayList();
    for (int i = classHierarchy.size - 1; i >= 0; i--) Collections.addAll(allFields, ClassReflection.getDeclaredFields(classHierarchy.get(i)));
    OrderedMap<String, FieldMetadata> nameToField = new OrderedMap(allFields.size());
    for (int i = 0, n = allFields.size(); i < n; i++) {
        Field field = allFields.get(i);
        if (field.isTransient())
            continue;
        if (field.isStatic())
            continue;
        if (field.isSynthetic())
            continue;
        if (!field.isAccessible()) {
            try {
                field.setAccessible(true);
            } catch (AccessControlException ex) {
                continue;
            }
        }
        if (ignoreDeprecated && field.isAnnotationPresent(Deprecated.class))
            continue;
        nameToField.put(field.getName(), new FieldMetadata(field));
    }
    typeToFields.put(type, nameToField);
    return nameToField;
}
Also used : ArrayList(java.util.ArrayList) AccessControlException(java.security.AccessControlException) Field(com.badlogic.gdx.utils.reflect.Field)

Example 2 with Field

use of com.badlogic.gdx.utils.reflect.Field in project libgdx by libgdx.

the class InterpolationTest method create.

@Override
public void create() {
    Gdx.gl.glClearColor(.3f, .3f, .3f, 1);
    renderer = new ShapeRenderer();
    skin = new Skin(Gdx.files.internal("data/uiskin.json"));
    stage = new Stage(new ScreenViewport());
    resetPositions();
    Field[] interpolationFields = ClassReflection.getFields(Interpolation.class);
    // see how many fields are actually interpolations (for safety; other fields may be added with future)
    int interpolationMembers = 0;
    for (int i = 0; i < interpolationFields.length; i++) if (ClassReflection.isAssignableFrom(Interpolation.class, interpolationFields[i].getDeclaringClass()))
        interpolationMembers++;
    // get interpolation names
    interpolationNames = new String[interpolationMembers];
    for (int i = 0; i < interpolationFields.length; i++) if (ClassReflection.isAssignableFrom(Interpolation.class, interpolationFields[i].getDeclaringClass()))
        interpolationNames[i] = interpolationFields[i].getName();
    selectedInterpolation = interpolationNames[0];
    list = new List(skin);
    list.setItems(interpolationNames);
    list.addListener(new ChangeListener() {

        public void changed(ChangeEvent event, Actor actor) {
            selectedInterpolation = list.getSelected();
            time = 0;
            resetPositions();
        }
    });
    ScrollPane scroll = new ScrollPane(list, skin);
    scroll.setFadeScrollBars(false);
    scroll.setScrollingDisabled(true, false);
    table = new Table();
    table.setFillParent(true);
    table.add(scroll).expandX().left().width(100);
    stage.addActor(table);
    Gdx.input.setInputProcessor(new InputMultiplexer(new InputAdapter() {

        public boolean scrolled(int amount) {
            if (!Gdx.input.isKeyPressed(Keys.CONTROL_LEFT))
                return false;
            duration -= amount / 15f;
            duration = MathUtils.clamp(duration, 0, Float.POSITIVE_INFINITY);
            return true;
        }
    }, stage, new InputAdapter() {

        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            if (// if "walking" was interrupted by this touch down event
            !Float.isNaN(time))
                // set startPosition to the current position
                startPosition.set(getPosition(time));
            targetPosition.set(stage.screenToStageCoordinates(targetPosition.set(screenX, screenY)));
            time = 0;
            return true;
        }
    }));
}
Also used : Table(com.badlogic.gdx.scenes.scene2d.ui.Table) InputAdapter(com.badlogic.gdx.InputAdapter) ScreenViewport(com.badlogic.gdx.utils.viewport.ScreenViewport) ShapeRenderer(com.badlogic.gdx.graphics.glutils.ShapeRenderer) Field(com.badlogic.gdx.utils.reflect.Field) Interpolation(com.badlogic.gdx.math.Interpolation) InputMultiplexer(com.badlogic.gdx.InputMultiplexer) ScrollPane(com.badlogic.gdx.scenes.scene2d.ui.ScrollPane) Actor(com.badlogic.gdx.scenes.scene2d.Actor) Stage(com.badlogic.gdx.scenes.scene2d.Stage) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) List(com.badlogic.gdx.scenes.scene2d.ui.List) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)

Example 3 with Field

use of com.badlogic.gdx.utils.reflect.Field in project libgdx by libgdx.

the class ReflectionTest method create.

@Override
public void create() {
    font = new BitmapFont();
    batch = new SpriteBatch();
    try {
        Vector2 fromDefaultConstructor = ClassReflection.newInstance(Vector2.class);
        println("From default constructor: " + fromDefaultConstructor);
        Method mSet = ClassReflection.getMethod(Vector2.class, "set", float.class, float.class);
        mSet.invoke(fromDefaultConstructor, 10, 11);
        println("Set to 10/11: " + fromDefaultConstructor);
        Constructor copyConstroctor = ClassReflection.getConstructor(Vector2.class, Vector2.class);
        Vector2 fromCopyConstructor = (Vector2) copyConstroctor.newInstance(fromDefaultConstructor);
        println("From copy constructor: " + fromCopyConstructor);
        Method mMul = ClassReflection.getMethod(Vector2.class, "scl", float.class);
        println("Multiplied by 2; " + mMul.invoke(fromCopyConstructor, 2));
        Method mNor = ClassReflection.getMethod(Vector2.class, "nor");
        println("Normalized: " + mNor.invoke(fromCopyConstructor));
        Vector2 fieldCopy = new Vector2();
        Field fx = ClassReflection.getField(Vector2.class, "x");
        Field fy = ClassReflection.getField(Vector2.class, "y");
        fx.set(fieldCopy, fx.get(fromCopyConstructor));
        fy.set(fieldCopy, fy.get(fromCopyConstructor));
        println("Copied field by field: " + fieldCopy);
        Json json = new Json();
        String jsonString = json.toJson(fromCopyConstructor);
        Vector2 fromJson = json.fromJson(Vector2.class, jsonString);
        println("JSON serialized: " + jsonString);
        println("JSON deserialized: " + fromJson);
        fromJson.x += 1;
        fromJson.y += 1;
        println("JSON deserialized + 1/1: " + fromJson);
        Object array = ArrayReflection.newInstance(int.class, 5);
        ArrayReflection.set(array, 0, 42);
        println("Array int: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0));
        array = ArrayReflection.newInstance(String.class, 5);
        ArrayReflection.set(array, 0, "test string");
        println("Array String: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0));
    } catch (Exception e) {
        message = "FAILED: " + e.getMessage() + "\n";
        message += e.getClass();
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) Vector2(com.badlogic.gdx.math.Vector2) Constructor(com.badlogic.gdx.utils.reflect.Constructor) Method(com.badlogic.gdx.utils.reflect.Method) Json(com.badlogic.gdx.utils.Json) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Example 4 with Field

use of com.badlogic.gdx.utils.reflect.Field in project gdx-skineditor by cobolfoo.

the class FontPickerDialog method isFontInUse.

/**
	 * Is font is already in use somewhere else?
	 */
public boolean isFontInUse(BitmapFont font) {
    try {
        // Check if it is already in use somewhere!
        for (String widget : SkinEditorGame.widgets) {
            String widgetStyle = "com.badlogic.gdx.scenes.scene2d.ui." + widget + "$" + widget + "Style";
            Class<?> style = Class.forName(widgetStyle);
            ObjectMap<String, ?> styles = game.skinProject.getAll(style);
            Iterator<String> it = styles.keys().iterator();
            while (it.hasNext()) {
                Object item = styles.get((String) it.next());
                Field[] fields = ClassReflection.getFields(item.getClass());
                for (Field field : fields) {
                    if (field.getType() == BitmapFont.class) {
                        BitmapFont f = (BitmapFont) field.get(item);
                        if (font.equals(f)) {
                            return true;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 5 with Field

use of com.badlogic.gdx.utils.reflect.Field in project libgdx by libgdx.

the class Json method readField.

/** @param elementType May be null if the type is unknown. */
public void readField(Object object, String fieldName, String jsonName, Class elementType, JsonValue jsonMap) {
    Class type = object.getClass();
    ObjectMap<String, FieldMetadata> fields = getFields(type);
    FieldMetadata metadata = fields.get(fieldName);
    if (metadata == null)
        throw new SerializationException("Field not found: " + fieldName + " (" + type.getName() + ")");
    Field field = metadata.field;
    if (elementType == null)
        elementType = metadata.elementType;
    readField(object, field, jsonName, elementType, jsonMap);
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field)

Aggregations

Field (com.badlogic.gdx.utils.reflect.Field)13 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)5 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)5 AccessControlException (java.security.AccessControlException)4 Color (com.badlogic.gdx.graphics.Color)3 IOException (java.io.IOException)3 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)2 Actor (com.badlogic.gdx.scenes.scene2d.Actor)2 ListStyle (com.badlogic.gdx.scenes.scene2d.ui.List.ListStyle)2 ScrollPaneStyle (com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.ScrollPaneStyle)2 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)2 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)2 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)2 Array (com.badlogic.gdx.utils.Array)2 Json (com.badlogic.gdx.utils.Json)2 Method (com.badlogic.gdx.utils.reflect.Method)2 Iterator (java.util.Iterator)2 InputAdapter (com.badlogic.gdx.InputAdapter)1 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)1 Pixmap (com.badlogic.gdx.graphics.Pixmap)1