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;
}
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;
}
}));
}
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();
}
}
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;
}
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);
}
Aggregations