Class Skin
- java.lang.Object
-
- com.badlogic.gdx.scenes.scene2d.ui.Skin
-
- All Implemented Interfaces:
Disposable
public class Skin extends java.lang.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Skin.TintedDrawable
-
Constructor Summary
Constructors Constructor Description Skin()
Creates an empty skin.Skin(FileHandle skinFile)
Creates a skin containing the resources in the specified skin JSON file.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.Skin(TextureAtlas atlas)
Creates a skin containing the texture regions from the specified atlas.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(java.lang.String name, java.lang.Object resource)
void
add(java.lang.String name, java.lang.Object resource, java.lang.Class type)
void
addRegions(TextureAtlas atlas)
Adds all named texture regions from the atlas.void
dispose()
Disposes theTextureAtlas
and allDisposable
resources in the skin.java.lang.String
find(java.lang.Object resource)
Returns the name of the specified style object, or null if it is not in the skin.<T> T
get(java.lang.Class<T> type)
Returns a resource named "default" for the specified type.<T> T
get(java.lang.String name, java.lang.Class<T> type)
Returns a named resource of the specified type.<T> ObjectMap<java.lang.String,T>
getAll(java.lang.Class<T> type)
Returns the name to resource mapping for the specified type, or null if no resources of that type exist.TextureAtlas
getAtlas()
Returns theTextureAtlas
passed to this skin constructor, or null.Color
getColor(java.lang.String name)
Drawable
getDrawable(java.lang.String name)
Returns a registered drawable.BitmapFont
getFont(java.lang.String name)
ObjectMap<java.lang.String,java.lang.Class>
getJsonClassTags()
Returns a map ofclass tags
that will be used when loading skin JSON.protected Json
getJsonLoader(FileHandle skinFile)
NinePatch
getPatch(java.lang.String name)
Returns a registered ninepatch.TextureRegion
getRegion(java.lang.String name)
Returns a registered texture region.Array<TextureRegion>
getRegions(java.lang.String regionName)
Sprite
getSprite(java.lang.String name)
Returns a registered sprite.TiledDrawable
getTiledDrawable(java.lang.String name)
Returns a registered tiled drawable.boolean
has(java.lang.String name, java.lang.Class type)
void
load(FileHandle skinFile)
Adds all resources in the specified skin JSON file.Drawable
newDrawable(Drawable drawable)
Returns a copy of the specified drawable.Drawable
newDrawable(Drawable drawable, float r, float g, float b, float a)
Returns a tinted copy of a drawable found in the skin viagetDrawable(String)
.Drawable
newDrawable(Drawable drawable, Color tint)
Returns a tinted copy of a drawable found in the skin viagetDrawable(String)
.Drawable
newDrawable(java.lang.String name)
Returns a copy of a drawable found in the skin viagetDrawable(String)
.Drawable
newDrawable(java.lang.String name, float r, float g, float b, float a)
Returns a tinted copy of a drawable found in the skin viagetDrawable(String)
.Drawable
newDrawable(java.lang.String name, Color tint)
Returns a tinted copy of a drawable found in the skin viagetDrawable(String)
.<T> T
optional(java.lang.String name, java.lang.Class<T> type)
Returns a named resource of the specified type.void
remove(java.lang.String name, java.lang.Class type)
void
scale(Drawable drawble)
Scales the drawable'sDrawable.getLeftWidth()
,Drawable.getRightWidth()
,Drawable.getBottomHeight()
,Drawable.getTopHeight()
,Drawable.getMinWidth()
, andDrawable.getMinHeight()
.void
setEnabled(Actor actor, boolean enabled)
Sets the style on the actor to disabled or enabled.void
setScale(float scale)
The scale used to size drawables created by this skin.
-
-
-
Constructor Detail
-
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 aTextureAtlas
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 Detail
-
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(java.lang.String name, java.lang.Object resource)
-
add
public void add(java.lang.String name, java.lang.Object resource, java.lang.Class type)
-
remove
public void remove(java.lang.String name, java.lang.Class type)
-
get
public <T> T get(java.lang.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(java.lang.String name, java.lang.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(java.lang.String name, java.lang.Class<T> type)
Returns a named resource of the specified type.- Returns:
- null if not found.
-
has
public boolean has(java.lang.String name, java.lang.Class type)
-
getAll
@Null public <T> ObjectMap<java.lang.String,T> getAll(java.lang.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(java.lang.String name)
-
getFont
public BitmapFont getFont(java.lang.String name)
-
getRegion
public TextureRegion getRegion(java.lang.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(java.lang.String regionName)
- Returns:
- an array with the
TextureRegion
that have an index != -1, or null if none are found.
-
getTiledDrawable
public TiledDrawable getTiledDrawable(java.lang.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(java.lang.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 anTextureAtlas.AtlasRegion
then its splitTextureAtlas.AtlasRegion.values
are used, otherwise the ninepatch will have the region as the center patch.
-
getSprite
public Sprite getSprite(java.lang.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 anTextureAtlas.AtlasRegion
then anTextureAtlas.AtlasSprite
is used if the region has been whitespace stripped or packed rotated 90 degrees.
-
getDrawable
public Drawable getDrawable(java.lang.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 java.lang.String find(java.lang.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(java.lang.String name)
Returns a copy of a drawable found in the skin viagetDrawable(String)
.
-
newDrawable
public Drawable newDrawable(java.lang.String name, float r, float g, float b, float a)
Returns a tinted copy of a drawable found in the skin viagetDrawable(String)
.
-
newDrawable
public Drawable newDrawable(java.lang.String name, Color tint)
Returns a tinted copy of a drawable found in the skin viagetDrawable(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 viagetDrawable(String)
.
-
newDrawable
public Drawable newDrawable(Drawable drawable, Color tint)
Returns a tinted copy of a drawable found in the skin viagetDrawable(String)
.
-
scale
public void scale(Drawable drawble)
Scales the drawable'sDrawable.getLeftWidth()
,Drawable.getRightWidth()
,Drawable.getBottomHeight()
,Drawable.getTopHeight()
,Drawable.getMinWidth()
, andDrawable.getMinHeight()
.
-
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 void setEnabled(Actor actor, 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. 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" method or the style was not found in the skin, no exception is thrown and the actor is left unchanged.
-
getAtlas
@Null public TextureAtlas getAtlas()
Returns theTextureAtlas
passed to this skin constructor, or null.
-
dispose
public void dispose()
Disposes theTextureAtlas
and allDisposable
resources in the skin.- Specified by:
dispose
in interfaceDisposable
-
getJsonLoader
protected Json getJsonLoader(FileHandle skinFile)
-
getJsonClassTags
public ObjectMap<java.lang.String,java.lang.Class> getJsonClassTags()
Returns a map ofclass tags
that will be used when loading skin JSON. The map can be modified before callingload(FileHandle)
. By default the map is populated with the simple class names of libGDX classes commonly used in skins.
-
-