Libgdx Skin的使用

坑爹啊,弄了好久Libgdx的skin,总是有很多的地方没有搞明白,后来发现自己的libgdx版本不是最新的,但是自己参考的却是最新的代码。所以大家一定要先下载最新的libgdx的jar包,这个名为 的作者确实还是蛮认真和勤奋的,更新的速度确实很快。大家要去官网上下载最新的nightly build版本的,下载地址为:

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


原来的Skin都是在json中加入resources和styles,参考前面的官方文档的介绍:

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 many different widgets.

The resources and styles for a skin are usually defined using JSON (or a format that is JSON-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, a  CheckBox.CheckBoxStyle  is 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.LabelStyle has two fields, font and fontColor, so the JSON could 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 previously 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.TintedNinePatch  can 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 for the  Button.ButtonStyle.up  field, 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 as an  Files.FileType.Internal  path. 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 as the font file for a PNG with the same name as the font file but with a "png" file extension.

TintedNinePatch provides a mechanism for tinting an existing NinePatch:

 { name: whiteButton, color: blue }
 
This would 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 included in the skin, usually without writing any code. Deserialization is handled by the Json class, which automatically serializes and deserializes most objects. While nearly any style object can be automatically deserialized, often resource objects require custom deserialization. Eg, TextureRegion, BitmapFont, and NinePatch need to reference the skin's single texture. If needed, getJsonLoader(FileHandle) may be overridden to register additional customserializers. See the source for getJsonLoader(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.

但是后来自己使用getStyle方法的时候发现会报错,发现新版本的Skin做了较大的修改,因为新版本的libgdx通过atlas文件和texturepacker来配合Skin的使用,所以在json文件中不需要再写resources和styles,而是直接通过get方法来获取resources和styles。而且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.
由此可见,为了通过atlas文件来配合skin类的使用,新版本的libgdx做了较大的修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值