1- using GdxSkinImport . MonoGdx ;
2- using info . lundin . math ;
1+ using FontStashSharp ;
2+ using GdxSkinImport . MonoGdx ;
33using Microsoft . Xna . Framework ;
44using Microsoft . Xna . Framework . Graphics ;
5+ using Myra ;
56using Myra . Graphics2D ;
67using Myra . Graphics2D . TextureAtlases ;
78using Myra . Graphics2D . UI . Styles ;
1314
1415namespace GdxSkinImport ;
1516
16- public static class Converter
17+ public class Converter
1718{
1819 private class TintedDrawable
1920 {
@@ -23,71 +24,102 @@ private class TintedDrawable
2324 public override string ToString ( ) => $ "{ Name } :{ Color } ";
2425 }
2526
26- private static TextureRegionAtlas ToMyraAtlas ( TextureAtlas gdxAtlas )
27+ private readonly GraphicsDevice _device ;
28+ private readonly string _path ;
29+ private readonly string _folder ;
30+ private readonly Stylesheet _result = new Stylesheet ( ) ;
31+ private TextureRegionAtlas _atlas ;
32+ private readonly ColorsCache _colors = new ColorsCache ( ) ;
33+ private readonly Dictionary < string , SpriteFontBase > _fonts = new Dictionary < string , SpriteFontBase > ( ) ;
34+ private readonly Dictionary < string , TintedDrawable > _tintedDrawables = new Dictionary < string , TintedDrawable > ( ) ;
35+
36+ public Converter ( GraphicsDevice device , string path )
2737 {
28- if ( gdxAtlas . Textures . Count > 1 )
38+ if ( string . IsNullOrEmpty ( path ) )
2939 {
30- throw new NotSupportedException ( "Only atlases with a single texture are supported" ) ;
40+ throw new ArgumentNullException ( nameof ( path ) ) ;
3141 }
3242
33- var result = new TextureRegionAtlas ( ) ;
43+ _device = device ?? throw new ArgumentNullException ( nameof ( device ) ) ;
44+ _path = path ;
45+ _folder = Path . GetDirectoryName ( this . _path ) ;
46+ }
3447
35- var sourceTexture = gdxAtlas . Textures . First ( ) ;
48+ public Stylesheet Process ( )
49+ {
50+ TextureAtlas gdxAtlas = null ;
51+ var gdxAtlasFile = Path . ChangeExtension ( _path , "atlas" ) ;
52+ if ( File . Exists ( gdxAtlasFile ) )
53+ {
54+ gdxAtlas = new TextureAtlas ( _device , gdxAtlasFile ) ;
55+ }
3656
37- result . Image = Path . GetFileName ( sourceTexture . TexturePath ) ;
57+ _atlas = ToMyraAtlas ( gdxAtlas ) ;
3858
39- foreach ( var sourceRegion in gdxAtlas . Regions )
59+ Dictionary < string , object > data ;
60+ using ( TextReader reader = new StreamReader ( _path ) )
4061 {
41- var bounds = new Rectangle ( sourceRegion . RegionX , sourceRegion . RegionY , sourceRegion . RegionWidth , sourceRegion . RegionHeight ) ;
62+ data = Json . Deserialize ( reader ) as Dictionary < string , object > ;
63+ }
4264
43- TextureRegion region ;
44- if ( sourceRegion . Splits == null )
45- {
46- region = new TextureRegion ( sourceTexture . Texture , bounds ) ;
47- }
48- else
49- {
50- var parts = sourceRegion . Splits ;
51- var thickness = new Thickness
52- {
53- Left = parts [ 0 ] ,
54- Right = parts [ 1 ] ,
55- Top = parts [ 2 ] ,
56- Bottom = parts [ 3 ]
57- } ;
65+ var folder = Path . GetDirectoryName ( _path ) ;
5866
59- region = new NinePatchRegion ( sourceTexture . Texture , bounds , thickness ) ;
60- }
67+ return ToMyraStylesheet ( data ) ;
68+ }
6169
62- result . Regions [ sourceRegion . Name ] = region ;
70+ private SpriteFontBase GetFont ( string key )
71+ {
72+ SpriteFontBase result ;
73+ if ( ! _fonts . TryGetValue ( key , out result ) )
74+ {
75+ throw new Exception ( $ "Could not find font '{ key } '") ;
6376 }
6477
6578 return result ;
6679 }
6780
68- private static Stylesheet ToMyraStylesheet ( Dictionary < string , object > data )
81+ private Color GetColor ( Dictionary < string , object > data , string key ) => _colors . Get ( data [ key ] ) ;
82+
83+ private void LoadLabelStyle ( Dictionary < string , object > data , LabelStyle style )
6984 {
70- var colors = new Dictionary < string , Color > ( ) ;
85+ style . Font = GetFont ( data . GetString ( "font" ) ) ;
86+ style . TextColor = GetColor ( data , "fontColor" ) ;
87+ }
7188
89+ private Stylesheet ToMyraStylesheet ( Dictionary < string , object > data )
90+ {
7291 object obj ;
7392 if ( data . TryGetValue ( "com.badlogic.gdx.graphics.Color" , out obj ) )
7493 {
7594 var colorsData = ( Dictionary < string , object > ) obj ;
7695 foreach ( var pair in colorsData )
7796 {
78- var asRGB = pair . Value as Dictionary < string , object > ;
79- if ( asRGB != null )
80- {
81- var color = asRGB . ParseColor ( ) ;
82- colors [ pair . Key ] = color ;
83- } else
97+ _colors . Add ( pair . Key , pair . Value ) ;
98+ }
99+ }
100+
101+ if ( data . TryGetValue ( "com.badlogic.gdx.graphics.g2d.BitmapFont" , out obj ) )
102+ {
103+ var fontsData = ( Dictionary < string , object > ) obj ;
104+ foreach ( var pair in fontsData )
105+ {
106+ var f = ( Dictionary < string , object > ) pair . Value ;
107+
108+ var file = f [ "file" ] . ToString ( ) ;
109+ var path = Path . Combine ( _folder , file ) ;
110+
111+ var fontData = File . ReadAllText ( path ) ;
112+ var font = StaticSpriteFont . FromBMFont ( fontData , s =>
84113 {
85- colors [ pair . Key ] = colors [ ( string ) pair . Value ] ;
86- }
114+ var region = _atlas [ Path . GetFileNameWithoutExtension ( s ) ] ;
115+
116+ return new TextureWithOffset ( region . Texture , region . Bounds . Location ) ;
117+ } ) ;
118+
119+ _fonts [ pair . Key ] = font ;
87120 }
88121 }
89122
90- var tintedDrawables = new Dictionary < string , TintedDrawable > ( ) ;
91123 if ( data . TryGetValue ( "com.badlogic.gdx.scenes.scene2d.ui.Skin$TintedDrawable" , out obj ) )
92124 {
93125 var tintedData = ( Dictionary < string , object > ) obj ;
@@ -98,33 +130,67 @@ private static Stylesheet ToMyraStylesheet(Dictionary<string, object> data)
98130 var td = new TintedDrawable
99131 {
100132 Name = ( string ) v [ "name" ] ,
101- Color = colors [ ( string ) v [ "color" ] ]
133+ Color = GetColor ( v , "color" )
102134 } ;
103135
104- tintedDrawables [ pair . Key ] = td ;
136+ _tintedDrawables [ pair . Key ] = td ;
137+ }
138+ }
139+
140+ if ( data . TryGetValue ( "com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle" , out obj ) )
141+ {
142+ var widgetData = ( Dictionary < string , object > ) obj ;
143+ foreach ( var pair in widgetData )
144+ {
145+ var labelStyle = new LabelStyle ( ) ;
146+ LoadLabelStyle ( ( Dictionary < string , object > ) pair . Value , labelStyle ) ;
147+
148+ _result . LabelStyles [ pair . Key ] = labelStyle ;
105149 }
106150 }
107151
108152 return null ;
109153 }
110154
111- public static Stylesheet ImportGdx ( GraphicsDevice device , string path )
155+ private static TextureRegionAtlas ToMyraAtlas ( TextureAtlas gdxAtlas )
112156 {
113- TextureAtlas gdxAtlas = null ;
114- var gdxAtlasFile = Path . ChangeExtension ( path , "atlas" ) ;
115- if ( File . Exists ( gdxAtlasFile ) )
157+ if ( gdxAtlas . Textures . Count > 1 )
116158 {
117- gdxAtlas = new TextureAtlas ( device , gdxAtlasFile ) ;
159+ throw new NotSupportedException ( "Only atlases with a single texture are supported" ) ;
118160 }
119161
120- var myraAtlas = ToMyraAtlas ( gdxAtlas ) ;
162+ var result = new TextureRegionAtlas ( ) ;
121163
122- Dictionary < string , object > data ;
123- using ( TextReader reader = new StreamReader ( path ) )
164+ var sourceTexture = gdxAtlas . Textures . First ( ) ;
165+
166+ result . Image = Path . GetFileName ( sourceTexture . TexturePath ) ;
167+
168+ foreach ( var sourceRegion in gdxAtlas . Regions )
124169 {
125- data = Json . Deserialize ( reader ) as Dictionary < string , object > ;
170+ var bounds = new Rectangle ( sourceRegion . RegionX , sourceRegion . RegionY , sourceRegion . RegionWidth , sourceRegion . RegionHeight ) ;
171+
172+ TextureRegion region ;
173+ if ( sourceRegion . Splits == null )
174+ {
175+ region = new TextureRegion ( sourceTexture . Texture , bounds ) ;
176+ }
177+ else
178+ {
179+ var parts = sourceRegion . Splits ;
180+ var thickness = new Thickness
181+ {
182+ Left = parts [ 0 ] ,
183+ Right = parts [ 1 ] ,
184+ Top = parts [ 2 ] ,
185+ Bottom = parts [ 3 ]
186+ } ;
187+
188+ region = new NinePatchRegion ( sourceTexture . Texture , bounds , thickness ) ;
189+ }
190+
191+ result . Regions [ sourceRegion . Name ] = region ;
126192 }
127-
128- return ToMyraStylesheet ( data ) ;
193+
194+ return result ;
129195 }
130196}
0 commit comments