Namespace mojo.app Class Theme Property Path:String() Return _themePath End Property DefaultStyle:Style() Return _defaultStyle End #rem monkeydoc Gets a color from the theme. If no color named `name` is found, [[Color.Grey]] is returned. #end Method GetColor:Color( name:String ) If _colors.Contains( name ) Return _colors[name] Return Color.Grey End #rem monkeydoc Gets a font from the theme. If no font named `name` is found, [[App.DefaultFont]] is returned. #end Method GetFont:Font( name:String ) Local font:=_fonts[name] If font Return font Return App.DefaultFont End #rem monkeydoc Gets a style from the theme. If no style named `name` is found, the [[DefaultStyle]] for the theme is returned. #end Method GetStyle:Style( name:String ) Local style:=_styles[name] If style Return style Return _defaultStyle End #rem monkeydoc Loads a font from a file. If `file` is an absolute path, this method is effectively the same as calling [[Font.Load]]. If `file` is not an absolute path, the font is searched for in the following locations: * The theme's directory. * The asset::fonts/ directory. #end Method LoadFont:Font( file:String,size:Int ) size*=_themeScale Local font:Font If ExtractRootDir( file ) font=Font.Open( file,size ) Else font=Font.Open( _themeDir+file,size ) If Not font font=Font.Open( "asset::fonts/"+file,size ) Endif Endif If Not font Return App.DefaultFont Return font End #rem monkeydoc Loads an array of icon images from a file. Loads an array of icons from an image file. The icons should be square, and laid out in a horizontal 'strip' within the image file. The width and height of each image is taken from the height of the image file. The number of icons loaded is the width of the image file divided by its height. If `file` is not an absolute path, the file is searched for in the following locations: * The theme's directory. * The asset::images/ directory. #end Method LoadIcons:Image[]( file:String ) Local atlas:Image If ExtractRootDir( file ) atlas=Image.Load( file ) Else atlas=Image.Load( _themeDir+file ) If Not atlas atlas=Image.Load( "asset::images/"+file ) Endif Endif If Not atlas Return Null atlas.Scale=New Vec2f( _themeScale,_themeScale ) Local size:=atlas.Height Local n:=atlas.Width/size Local icons:=New Image[n] For Local i:=0 Until n icons[i]=New Image( atlas,New Recti( i*size,0,i*size+size,atlas.Height ) ) Next Return icons End #rem monkeydoc Loads a skin from a file. If `file` is an absolute path, this method is effectively the same as calling [[Skin.Load]]. If `file` is not an absolute path, the skin is searched for in the following locations: * The theme directory. * The asset::images/ directory. #end Method LoadSkin:Skin( file:String ) Local skin:Skin If ExtractRootDir( file ) skin=Skin.Load( file ) Else skin=Skin.Load( _themeDir+file ) If Not skin skin=Skin.Load( "asset::images/"+file ) Endif If Not skin Return Null skin.Image.Scale=New Vec2f( _themeScale,_themeScale ) Return skin End #rem monkeydoc Loads a theme from a file. #end Function Load:Theme( path:String ) Local jobj:=JsonObject.Load( path ) If Not jobj Print "Failed to load theme:"+path return New Theme Endif Return New Theme( path,jobj ) End Private Const _jdefault:=New JsonString( "default" ) #if __TARGET__="android" Field _themeScale:Int=2 #else Field _themeScale:Int=1 #endif Field _themePath:String Field _themeDir:String Field _defaultStyle:Style Field _colors:=New StringMap Field _fonts:=New StringMap Field _styles:=New StringMap