Class AssetDirectory

java.lang.Object
com.badlogic.gdx.assets.AssetManager
edu.cornell.gdiac.assets.AssetDirectory
All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable

public class AssetDirectory extends com.badlogic.gdx.assets.AssetManager
An asset manager that uses a JSON file to define its assets.

The manager allows assets to be loaded normally, but it is best to use the global loadAssets() method to load from the JSON directory. Once that method is called, you may use getEntry(java.lang.String, java.lang.Class<T>) instead of AssetManager.get(java.lang.String) to access an asset.

As with any AssetManager, this class loads assets via AssetLoader objects. However, it also requires AssetParser objects. An asset parser takes an JSON entry in the directory and instructs the appropriate loader how to load that file. This class has built in parsers for the classes Texture, TextureRegion, BitmapFont, SoundEffect, MusicQueue, and JsonValue.

If you wish to add custom assets, you need to add BOTH a custom AssetLoader and a custom AssetParser to this manager. While a type can only have one loader per file suffix, there is no limit on the number of asset parsers per type. Use the method addParser(edu.cornell.gdiac.assets.AssetParser<?>) to add additional asset parsers.

Right now, this class supports built-in reading for Textures (and Texture regions), audio assets, fonts, and other JSON files. To add more assets, you will need to extend this class.

We have decided not implement this class as a singleton. It is possible that you may want more than one asset manager (for managing separate scenes). If you need to globalize access to this asset manager, use ResourceManager.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    This class represents the top level index of an asset directory.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    The directory contents (including the map from JSON keys to file names)
    protected String
    The asset directory of this asset manager
    protected com.badlogic.gdx.assets.loaders.FileHandleResolver
    The resolver (converting strings to file handles)
    protected DirectoryLoader
    The dedicated loader for the AssetDirectory.Index class
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new AssetDirectory from the given directory.
    AssetDirectory(String directory, com.badlogic.gdx.assets.loaders.FileHandleResolver resolver)
    Creates a new AssetDirectory from the given directory.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> void
    addEntry(String key, Class<T> type, T asset)
    Adds an entry to the asset directory, associating it with the given key.
    void
    Adds a AssetParser for this directory loader
    Returns the file name for the asset directory.
    <T> T
    getEntry(String key, Class<T> type)
    Returns the asset associated with the given directory key
    <T> String
    getFileName(String key, Class<T> type)
    Returns the asset path associated with the given directory key
    com.badlogic.gdx.utils.Array<AssetParser<?>>
    Returns the AssetParser objects associated with this directory loader
    float
    Returns the progress in percent of completion.
    <T> boolean
    hasEntry(String key, Class<T> type)
    Returns true if there is an asset associated with the given directory key
    void
    Loads all assets defined by the asset directory
    void
    Removes a AssetParser from this directory loader
    void
    Unloads all assets previously loaded by loadAssets().

    Methods inherited from class com.badlogic.gdx.assets.AssetManager

    addAsset, clear, contains, contains, containsAsset, dispose, finishLoading, finishLoadingAsset, finishLoadingAsset, get, get, get, get, get, getAll, getAssetFileName, getAssetNames, getAssetType, getDependencies, getDiagnostics, getFileHandleResolver, getLoadedAssets, getLoader, getLoader, getLogger, getQueuedAssets, getReferenceCount, isFinished, isLoaded, isLoaded, isLoaded, load, load, load, setErrorListener, setLoader, setLoader, setLogger, setReferenceCount, taskFailed, unload, update, update

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • resolver

      protected com.badlogic.gdx.assets.loaders.FileHandleResolver resolver
      The resolver (converting strings to file handles)
    • filename

      protected String filename
      The asset directory of this asset manager
    • contents

      protected AssetDirectory.Index contents
      The directory contents (including the map from JSON keys to file names)
    • topLoader

      protected DirectoryLoader topLoader
      The dedicated loader for the AssetDirectory.Index class
  • Constructor Details

    • AssetDirectory

      public AssetDirectory(String directory)
      Creates a new AssetDirectory from the given directory.

      This class uses an InternalFileHandleResolver to convert asset file names into assets.

      Parameters:
      directory - The asset directory file name
    • AssetDirectory

      public AssetDirectory(String directory, com.badlogic.gdx.assets.loaders.FileHandleResolver resolver)
      Creates a new AssetDirectory from the given directory.

      This class uses the given FileHandleResolver to convert asset file names into assets.

      Parameters:
      directory - The asset directory file name
      resolver - The file handle resolver
  • Method Details

    • getProgress

      public float getProgress()
      Returns the progress in percent of completion.
      Overrides:
      getProgress in class com.badlogic.gdx.assets.AssetManager
      Returns:
      the progress in percent of completion.
    • getDirectory

      public String getDirectory()
      Returns the file name for the asset directory.
      Returns:
      the file name for the asset directory.
    • loadAssets

      public void loadAssets()
      Loads all assets defined by the asset directory

      Each asset must have an associated AssetParser for this to work. There are default parsers for the classes Texture, TextureRegion, BitmapFont, SoundEffect, MusicQueue, JsonValue as well as several others. Any additional asset parsers should be added with the addParser(edu.cornell.gdiac.assets.AssetParser<?>) method.

    • unloadAssets

      public void unloadAssets()
      Unloads all assets previously loaded by loadAssets().

      Assets loaded manually (e.g. not via the asset directory JSON) will not be affected and will remain in this asset manager.

    • getParsers

      public com.badlogic.gdx.utils.Array<AssetParser<?>> getParsers()
      Returns the AssetParser objects associated with this directory loader

      If there are no asset parsers, then loadAssets() will not generate any assets beyond the initial JSON file. The contents of the JSON file will be ignored.

      Each asset type may have multiple asset parsers

      Returns:
      the AssetParser objects associated with this directory loader
    • addParser

      public void addParser(AssetParser<?> parser)
      Adds a AssetParser for this directory loader

      If there are no asset parsers, then loadAssets() will not generate any assets beyond the initial JSON file. The contents of the JSON file will be ignored.

      Each asset type may have multiple asset parsers

      Parameters:
      parser - The AssetParser to add
    • removeParser

      public void removeParser(AssetParser<?> parser)
      Removes a AssetParser from this directory loader

      If there are no asset parsers, then loadAssets() will not generate any assets beyond the initial JSON file. The contents of the JSON file will be ignored.

      Each asset type may have multiple asset parsers

      Parameters:
      parser - The AssetParser to remove
    • getEntry

      public <T> T getEntry(String key, Class<T> type)
      Returns the asset associated with the given directory key

      The method loadAssets() must have been called for this method to return a value. In addition, it will not return a value until the asset is actually loaded.

      Type Parameters:
      T - the asset type
      Parameters:
      key - the asset directory key
      type - the asset type
      Returns:
      the asset associated with the given directory key
    • hasEntry

      public <T> boolean hasEntry(String key, Class<T> type)
      Returns true if there is an asset associated with the given directory key

      The method loadAssets() must have been called for this method to return anything other than false. However, the asset does not need to be loaded for it to return true.

      Type Parameters:
      T - the asset type
      Parameters:
      key - the asset directory key
      type - the asset type
      Returns:
      true if there is an asset associated with the given directory key
    • addEntry

      public <T> void addEntry(String key, Class<T> type, T asset)
      Adds an entry to the asset directory, associating it with the given key.

      This method is used to add assets without a loader. It is thread-safe due to the use of synchronization.

      Type Parameters:
      T - the asset type
      Parameters:
      key - the unique identifier for the asset
      type - the Class object representing the type of the asset
      asset - the asset instance to be added
    • getFileName

      public <T> String getFileName(String key, Class<T> type)
      Returns the asset path associated with the given directory key

      The method loadAssets() must have been called for this method to return a value. However, the asset does not have to have finished loading.

      Type Parameters:
      T - the asset type
      Parameters:
      key - the asset directory key
      type - the asset type
      Returns:
      the asset associated with the given directory key