1
1
package com .bladecoder .engine .ui ;
2
2
3
+ import java .util .ArrayList ;
4
+ import java .util .Arrays ;
5
+
3
6
import com .badlogic .gdx .Gdx ;
4
7
import com .badlogic .gdx .files .FileHandle ;
5
8
import com .badlogic .gdx .graphics .Color ;
8
11
import com .badlogic .gdx .graphics .g2d .TextureRegion ;
9
12
import com .badlogic .gdx .graphics .g2d .freetype .FreeTypeFontGenerator ;
10
13
import com .badlogic .gdx .graphics .g2d .freetype .FreeTypeFontGenerator .FreeTypeFontParameter ;
14
+ import com .badlogic .gdx .scenes .scene2d .ui .Button ;
15
+ import com .badlogic .gdx .scenes .scene2d .ui .CheckBox ;
16
+ import com .badlogic .gdx .scenes .scene2d .ui .ImageButton ;
17
+ import com .badlogic .gdx .scenes .scene2d .ui .ImageTextButton ;
18
+ import com .badlogic .gdx .scenes .scene2d .ui .Label ;
19
+ import com .badlogic .gdx .scenes .scene2d .ui .List ;
20
+ import com .badlogic .gdx .scenes .scene2d .ui .ProgressBar ;
21
+ import com .badlogic .gdx .scenes .scene2d .ui .ScrollPane ;
22
+ import com .badlogic .gdx .scenes .scene2d .ui .SelectBox ;
11
23
import com .badlogic .gdx .scenes .scene2d .ui .Skin ;
24
+ import com .badlogic .gdx .scenes .scene2d .ui .Slider ;
25
+ import com .badlogic .gdx .scenes .scene2d .ui .SplitPane ;
26
+ import com .badlogic .gdx .scenes .scene2d .ui .TextButton ;
27
+ import com .badlogic .gdx .scenes .scene2d .ui .TextField ;
28
+ import com .badlogic .gdx .scenes .scene2d .ui .TextTooltip ;
29
+ import com .badlogic .gdx .scenes .scene2d .ui .Touchpad ;
30
+ import com .badlogic .gdx .scenes .scene2d .ui .Tree ;
31
+ import com .badlogic .gdx .scenes .scene2d .ui .Window ;
32
+ import com .badlogic .gdx .scenes .scene2d .utils .Drawable ;
33
+ import com .badlogic .gdx .scenes .scene2d .utils .NinePatchDrawable ;
34
+ import com .badlogic .gdx .scenes .scene2d .utils .SpriteDrawable ;
35
+ import com .badlogic .gdx .scenes .scene2d .utils .TextureRegionDrawable ;
36
+ import com .badlogic .gdx .scenes .scene2d .utils .TiledDrawable ;
12
37
import com .badlogic .gdx .utils .Json ;
13
38
import com .badlogic .gdx .utils .Json .ReadOnlySerializer ;
14
- import com .bladecoder .engine .util .DPIUtils ;
15
- import com .bladecoder .engine .util .FileUtils ;
16
39
import com .badlogic .gdx .utils .JsonValue ;
17
40
import com .badlogic .gdx .utils .SerializationException ;
41
+ import com .badlogic .gdx .utils .reflect .ClassReflection ;
42
+ import com .badlogic .gdx .utils .reflect .ReflectionException ;
43
+ import com .bladecoder .engine .util .DPIUtils ;
44
+ import com .bladecoder .engine .util .FileUtils ;
18
45
19
46
/**
20
47
* Custom Skin class to add TTF font support
@@ -41,6 +68,37 @@ protected Json getJsonLoader(final FileHandle skinFile) {
41
68
Json json = super .getJsonLoader (skinFile );
42
69
43
70
final Skin skin = this ;
71
+
72
+ json .setSerializer (Skin .class , new ReadOnlySerializer <Skin >() {
73
+ public Skin read (Json json , JsonValue typeToValueMap , @ SuppressWarnings ("rawtypes" ) Class ignored ) {
74
+ for (JsonValue valueMap = typeToValueMap .child ; valueMap != null ; valueMap = valueMap .next ) {
75
+ try {
76
+ Class <?> type = json .getClass (valueMap .name ());
77
+ if (type == null ) type = ClassReflection .forName (valueMap .name ());
78
+ readNamedObjects (json , type , valueMap );
79
+ } catch (ReflectionException ex ) {
80
+ throw new SerializationException (ex );
81
+ }
82
+ }
83
+ return skin ;
84
+ }
85
+
86
+ private void readNamedObjects (Json json , Class <?> type , JsonValue valueMap ) {
87
+ Class <?> addType = type == TintedDrawable .class ? Drawable .class : type ;
88
+ for (JsonValue valueEntry = valueMap .child ; valueEntry != null ; valueEntry = valueEntry .next ) {
89
+ Object object = json .readValue (type , valueEntry );
90
+ if (object == null ) continue ;
91
+ try {
92
+ add (valueEntry .name , object , addType );
93
+ if (addType != Drawable .class && ClassReflection .isAssignableFrom (Drawable .class , addType ))
94
+ add (valueEntry .name , object , Drawable .class );
95
+ } catch (Exception ex ) {
96
+ throw new SerializationException (
97
+ "Error reading " + ClassReflection .getSimpleName (type ) + ": " + valueEntry .name , ex );
98
+ }
99
+ }
100
+ }
101
+ });
44
102
45
103
json .setSerializer (BitmapFont .class , new ReadOnlySerializer <BitmapFont >() {
46
104
public BitmapFont read (Json json , JsonValue jsonData , @ SuppressWarnings ("rawtypes" ) Class type ) {
@@ -112,7 +170,28 @@ else if (size != -1) // TODO set size in points (dpi
112
170
return font ;
113
171
}
114
172
});
173
+
174
+ for (Class <?> cls : TAGGED_STYLES ){
175
+ json .addClassTag (cls .getSimpleName (), cls );
176
+ }
115
177
116
178
return json ;
117
179
}
180
+
181
+ private static final Class <?>[] AUTO_TAGGED_STYLES = {
182
+ BitmapFont .class , Color .class , TintedDrawable .class ,
183
+ NinePatchDrawable .class , SpriteDrawable .class , TextureRegionDrawable .class , TiledDrawable .class ,
184
+ Button .ButtonStyle .class , CheckBox .CheckBoxStyle .class , ImageButton .ImageButtonStyle .class ,
185
+ ImageTextButton .ImageTextButtonStyle .class , Label .LabelStyle .class , List .ListStyle .class ,
186
+ ProgressBar .ProgressBarStyle .class , ScrollPane .ScrollPaneStyle .class , SelectBox .SelectBoxStyle .class ,
187
+ Slider .SliderStyle .class , SplitPane .SplitPaneStyle .class , TextButton .TextButtonStyle .class ,
188
+ TextField .TextFieldStyle .class , TextTooltip .TextTooltipStyle .class , Touchpad .TouchpadStyle .class ,
189
+ Tree .TreeStyle .class , Window .WindowStyle .class
190
+ };
191
+
192
+ private final static ArrayList <Class <?>> TAGGED_STYLES = new ArrayList <Class <?>>(Arrays .asList (AUTO_TAGGED_STYLES ));
193
+
194
+ public static final void addStyleTag (Class <?> tag ) {
195
+ TAGGED_STYLES .add (tag );
196
+ }
118
197
}
0 commit comments