java.lang.Object
com.badlogic.gdx.scenes.scene2d.ui.Skin
All Implemented Interfaces:
Disposable

public class Skin extends Object implements Disposable
A skin stores resources for UI widgets to use (texture regions, ninepatches, fonts, colors, etc). Resources are named and can be looked up by name and type. Resources can be described in JSON. Skin provides useful conversions, such as allowing access to regions in the atlas as ninepatches, sprites, drawables, etc. The get* methods return an instance of the object in the skin. The new* methods return a copy of an instance in the skin.

See the documentation for more.

  • Constructor Details

    • Skin

      public Skin()
      Creates an empty skin.
    • Skin

      public Skin(FileHandle skinFile)
      Creates a skin containing the resources in the specified skin JSON file. If a file in the same directory with a ".atlas" extension exists, it is loaded as a TextureAtlas and the texture regions added to the skin. The atlas is automatically disposed when the skin is disposed.
    • Skin

      public Skin(FileHandle skinFile, TextureAtlas atlas)
      Creates a skin containing the resources in the specified skin JSON file and the texture regions from the specified atlas. The atlas is automatically disposed when the skin is disposed.
    • Skin

      public Skin(TextureAtlas atlas)
      Creates a skin containing the texture regions from the specified atlas. The atlas is automatically disposed when the skin is disposed.
  • Method Details

    • load

      public void load(FileHandle skinFile)
      Adds all resources in the specified skin JSON file.
    • addRegions

      public void addRegions(TextureAtlas atlas)
      Adds all named texture regions from the atlas. The atlas will not be automatically disposed when the skin is disposed.
    • add

      public void add(String name, Object resource)
    • add

      public void add(String name, Object resource, Class type)
    • remove

      public void remove(String name, Class type)
    • get

      public <T> T get(Class<T> type)
      Returns a resource named "default" for the specified type.
      Throws:
      GdxRuntimeException - if the resource was not found.
    • get

      public <T> T get(String name, Class<T> type)
      Returns a named resource of the specified type.
      Throws:
      GdxRuntimeException - if the resource was not found.
    • optional

      @Null public <T> T optional(String name, Class<T> type)
      Returns a named resource of the specified type.
      Returns:
      null if not found.
    • has

      public boolean has(String name, Class type)
    • getAll

      @Null public <T> ObjectMap<String,T> getAll(Class<T> type)
      Returns the name to resource mapping for the specified type, or null if no resources of that type exist.
    • getColor

      public Color getColor(String name)
    • getFont

      public BitmapFont getFont(String name)
    • getRegion

      public TextureRegion getRegion(String name)
      Returns a registered texture region. If no region is found but a texture exists with the name, a region is created from the texture and stored in the skin.
    • getRegions

      @Null public Array<TextureRegion> getRegions(String regionName)
      Returns:
      an array with the TextureRegion that have an index != -1, or null if none are found.
    • getTiledDrawable

      public TiledDrawable getTiledDrawable(String name)
      Returns a registered tiled drawable. If no tiled drawable is found but a region exists with the name, a tiled drawable is created from the region and stored in the skin.
    • getPatch

      public NinePatch getPatch(String name)
      Returns a registered ninepatch. If no ninepatch is found but a region exists with the name, a ninepatch is created from the region and stored in the skin. If the region is an TextureAtlas.AtlasRegion then its split TextureAtlas.AtlasRegion.values are used, otherwise the ninepatch will have the region as the center patch.
    • getSprite

      public Sprite getSprite(String name)
      Returns a registered sprite. If no sprite is found but a region exists with the name, a sprite is created from the region and stored in the skin. If the region is an TextureAtlas.AtlasRegion then an TextureAtlas.AtlasSprite is used if the region has been whitespace stripped or packed rotated 90 degrees.
    • getDrawable

      public Drawable getDrawable(String name)
      Returns a registered drawable. If no drawable is found but a region, ninepatch, or sprite exists with the name, then the appropriate drawable is created and stored in the skin.
    • find

      @Null public String find(Object resource)
      Returns the name of the specified style object, or null if it is not in the skin. This compares potentially every style object in the skin of the same type as the specified style, which may be a somewhat expensive operation.
    • newDrawable

      public Drawable newDrawable(String name)
      Returns a copy of a drawable found in the skin via getDrawable(String).
    • newDrawable

      public Drawable newDrawable(String name, float r, float g, float b, float a)
      Returns a tinted copy of a drawable found in the skin via getDrawable(String).
    • newDrawable

      public Drawable newDrawable(String name, Color tint)
      Returns a tinted copy of a drawable found in the skin via getDrawable(String).
    • newDrawable

      public Drawable newDrawable(Drawable drawable)
      Returns a copy of the specified drawable.
    • newDrawable

      public Drawable newDrawable(Drawable drawable, float r, float g, float b, float a)
      Returns a tinted copy of a drawable found in the skin via getDrawable(String).
    • newDrawable

      public Drawable newDrawable(Drawable drawable, Color tint)
      Returns a tinted copy of a drawable found in the skin via getDrawable(String).
    • scale

      public void scale(Drawable drawble)
    • setScale

      public void setScale(float scale)
      The scale used to size drawables created by this skin.

      This can be useful when scaling an entire UI (eg with a stage's viewport) then using an atlas with images whose resolution matches the UI scale. The skin can then be scaled the opposite amount so that the larger or smaller images are drawn at the original size. For example, if the UI is scaled 2x, the atlas would have images that are twice the size, then the skin's scale would be set to 0.5.

    • setEnabled

      public <V> void setEnabled(Styleable<V> styleable, boolean enabled)
      Sets the style on the actor to disabled or enabled. This is done by appending "-disabled" to the style name when enabled is false, and removing "-disabled" from the style name when enabled is true. If the style was not found in the skin, an exception is thrown.
    • setEnabledReflection

      @Deprecated public void setEnabledReflection(Actor actor, boolean enabled)
      Deprecated.
      Sets the style on the actor to disabled or enabled. This is done by appending "-disabled" to the style name when enabled is false, and removing "-disabled" from the style name when enabled is true. A method named "getStyle" is called the actor via reflection and the name of that style is found in the skin. If the actor doesn't have a "getStyle" and "setStyle" method the actor is left unchanged. If the style was not found in the skin, an exception is thrown.
    • getAtlas

      @Null public TextureAtlas getAtlas()
      Returns the TextureAtlas passed to this skin constructor, or null.
    • dispose

      public void dispose()
      Disposes the TextureAtlas and all Disposable resources in the skin.
      Specified by:
      dispose in interface Disposable
    • getJsonLoader

      protected Json getJsonLoader(FileHandle skinFile)
    • getJsonClassTags

      public ObjectMap<String,Class> getJsonClassTags()
      Returns a map of class tags that will be used when loading skin JSON. The map can be modified before calling load(FileHandle). By default the map is populated with the simple class names of libGDX classes commonly used in skins.