Advertisement
dermetfan

Assetz LoadItem

Mar 16th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. package com.mypackage;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.graphics.Texture;
  5. import com.badlogic.gdx.utils.Array;
  6. import com.badlogic.gdx.utils.reflect.ClassReflection;
  7. import com.badlogic.gdx.utils.reflect.Field;
  8. import com.badlogic.gdx.utils.reflect.ReflectionException;
  9. import net.dermetfan.gdx.assets.AnnotationAssetManager;
  10. import net.dermetfan.gdx.assets.AnnotationAssetManager.Asset;
  11.  
  12. public class Assetz {
  13.  
  14.     public static final AnnotationAssetManager manager = new AnnotationAssetManager();
  15.  
  16.     @Asset(Texture.class)
  17.     public static final LoadItem ball = new LoadItem("img/ball.png", true);
  18.  
  19.     private static class LoadItem {
  20.  
  21.         private String path;
  22.         private boolean remove;
  23.  
  24.         public LoadItem(String path, boolean remove) {
  25.             this.path = path;
  26.             this.remove = remove;
  27.         }
  28.  
  29.         @Override
  30.         public String toString() {
  31.             return path;
  32.         }
  33.     }
  34.  
  35.     public static Array<LoadItem> getItems() {
  36.         Array<LoadItem> items = new Array<>();
  37.         for(Field field : ClassReflection.getDeclaredFields(Assetz.class)) {
  38.             if(field.getType() == LoadItem.class) {
  39.                 LoadItem item  = null;
  40.                 try {
  41.                     if(!field.isAccessible())
  42.                         field.setAccessible(true);
  43.                     item = (LoadItem) field.get(null);
  44.                 } catch(ReflectionException e) {
  45.                     Gdx.app.error("Assetz", "failed to get LoadItem", e);
  46.                 }
  47.                 if(item != null)
  48.                     items.add(item);
  49.             }
  50.         }
  51.         return items;
  52.     }
  53.  
  54.     public static void removeItems() {
  55.         for(LoadItem item : getItems())
  56.             if(item.remove)
  57.                 manager.unload(item.path);
  58.     }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement