Use of Libgdx Skin

Source: Internet
Author: User
Tags libgdx

For a long time, the skin of Libgdx has never been fully understood. Later, I found that my libgdx version is not the latest, but my reference is the latest code. Therefore, you must first download the latest libgdx jar package. The author named NathanSweet is very serious and diligent, and the update speed is indeed fast. You need to go to the official website to download the latest nightly
Build version, which is:

Http://libgdx.badlogicgames.com/download.html

The original Skin is added with resources and styles in json. For details, refer to the previous official document:

public class Skin
Extends java. lang. Object
Implements Disposable

A skin holds styles for widgets and the resources (texture regions, ninepatches, bitmap fonts, etc) for those styles. A skin has a single texture that the resources may reference. This
Reduces the number of texture binds necessary for rendering extends different widgets.

The resources and styles for a skin are usually defined using JSON (or a format that isJSON-like),
Which is formatted in this way:

 {        resources: {                className: {                        name: value,                        ...                },                ...        },        styles: {                className: {                        name: value,                        ...                },                ...        } } 

There are two sections, one named "resources" and the other "styles ". each section has a class name, which has a number of names and values. the name is the name of the resource or
Style for that class, and the value is the serialized resource or style. Here is a real example:

 {        resources: {                com.badlogic.gdx.graphics.g2d.TextureRegion: {                        check-on: { x: 13, y: 77, width: 14, height: 14 },                        check-off: { x: 2, y: 97, width: 14, height: 14 }                },                com.badlogic.gdx.graphics.Color: {                        white: { r: 1, g: 1, b: 1, a: 1 }                },                com.badlogic.gdx.graphics.g2d.BitmapFont: {                        default-font: { file: default.fnt }                }        },        styles: {                com.badlogic.gdx.scenes.scene2d.ui.CheckBox$CheckBoxStyle: {                        default: {                                checkboxOn: check-on, checkboxOff: check-off,                                font: default-font, fontColor: white                        }                }        } } 

Here some named resource are defined: texture regions, a color, and a bitmap font. Also,CheckBox.CheckBoxStyleIs
Defined named "default" and it references the resources by name.

Styles and resources are retrieved from the skin using the type and name:

 Color highlight = skin.getResource("highlight", Color.class); TextureRegion someRegion = skin.getResource("logo", TextureRegion.class); CheckBoxStyle checkBoxStyle = skin.getStyle("bigCheckbox", CheckBoxStyle.class); CheckBox checkBox = new CheckBox("Check me!", checkBoxStyle); 

For convenience, most widget constructors will accept a skin and look up the necessary style using the name "default ".

The JSON required for a style is simply a JSON object with field names that match the Java field names. The JSON object's field values can be an object to define a new Java object, or
A string to reference a named resource of the expected type. Eg,Label.LabelStyleHas
Two fields, font and fontColor, so the JSON cocould look like:

 someLabel: { font: small, fontColor: { r: 1, g: 0, b: 0, a: 1 } } 

When this is parsed, the "font" field is a BitmapFont and the string "small" is found, so a BitmapFont resource named "small" is used. the "fontColor" field is a Color and a JSON object
Is found, so a new Color is created and the JSON object is used to populate its fields.

The order resources are defined is important. Resources may reference previusly defined resources. This is how a BitmapFont can find a TextureRegion resource (see BitmapFont section below ).

The following gives examples for the types of resources that are supported by default:

Color:

 { r: 1, g: 1, b: 1, a: 1 } 

TextureRegion:

 { x: 13, y: 77, width: 14, height: 14 } 

Skin.TintedNinePatch:

 [        { x: 2, y: 55, width: 5, height: 5 },        { x: 7, y: 55, width: 2, height: 5 },        { x: 9, y: 55, width: 5, height: 5 },        { x: 2, y: 60, width: 5, height: 11 },        { x: 7, y: 60, width: 2, height: 11 },        { x: 9, y: 60, width: 5, height: 11 },        { x: 2, y: 71, width: 5, height: 4 },        { x: 7, y: 71, width: 2, height: 4 },        { x: 9, y: 71, width: 5, height: 4 } ] 

Skin.TintedNinePatchCan
Also be specified as a single region, which is set as the center of the ninepatch:

 [ { width: 20, height: 20, x: 6, y: 2 } ] 

This notation is useful to use a single region as a ninepatch. Eg, when creating a button made up of a single image forButton.ButtonStyle.upField,
Which is a ninepatch.

BitmapFont:

 { file: default.fnt } 

First the skin tries to find the font file in the directory containing the skin file. If not found there, it uses the specified path asFiles.FileType.InternalPath.
The bitmap font will use a texture region with the same name as the font file without the file extension. if no texture region with that name is defined in the skin (note the order resources are defined is important), it will look in the same directory
The font file for a PNG with the same name as the font file but with a "png" file extension.

TintedNinePatch provides a mechanic for tinting an existing NinePatch:

 { name: whiteButton, color: blue } 

This wocould create a new NinePatch identical to the NinePatch named "whiteButton" and tint it with the color named "blue ".

The skin JSON is extensible. Styles and resources for your own widgets may be encoded in the skin, usually without writing any code. Deserialization is handled byJsonClass,
Which automatically serializes and deserializes most objects. While nearly any style object can be automatically deserialized, often resource objects require custom deserialization. Eg, values, values, and NinePatch need to reference the skin's
Single texture. If needed,getJsonLoader(FileHandle)May
Be overridden to register additional customserializers.
See the sourcegetJsonLoader(FileHandle)For
Examples on how to write serializers.

Note that there is a SkinPacker class in the gdx-tools project that can take a directory of individual images, pack them into a single texture, and write the proper texture region and
Ninepatch entries to a skin JSON file. The styles and other resources sections still need to be written by hand, but SkinPacker makes the otherwise tedious entry of pixel coordinates unnecessary.

However, when I used the getStyle method, I found that an error was reported. I found that the new version of Skin was greatly modified, because the new version of libgdx works with Skin through the atlas file and texturepacker, therefore, you do not need to write resources and styles in the json file. Instead, you can use the get method to obtain resources and styles. In addition, the skin constructor has also changed.

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 TextureAtlasAnd
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.

It can be seen that in order to use the atlas file with the skin class, the new version of libgdx has been greatly modified.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.