gt: 			list.setSize( WIDTH, HEIGHT );
context: /*
   * Pixel Dungeon
   * Copyright (C) 2012-2015 Oleg Dolya
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>
   */
  package com.lh64.randomdungeon.windows;
  
  import java.util.Locale;
  
  import com.lh64.noosa.BitmapText;
  import com.lh64.noosa.ColorBlock;
  import com.lh64.noosa.Game;
  import com.lh64.noosa.Group;
  import com.lh64.noosa.Image;
  import com.lh64.noosa.audio.Sample;
  import com.lh64.noosa.ui.Button;
  import com.lh64.randomdungeon.Assets;
  import com.lh64.randomdungeon.Badges;
  import com.lh64.randomdungeon.Dungeon;
  import com.lh64.randomdungeon.Statistics;
  import com.lh64.randomdungeon.actors.hero.Belongings;
  import com.lh64.randomdungeon.items.Item;
  import com.lh64.randomdungeon.scenes.PixelScene;
  import com.lh64.randomdungeon.sprites.HeroSprite;
  import com.lh64.randomdungeon.ui.BadgesList;
  import com.lh64.randomdungeon.ui.Icons;
  import com.lh64.randomdungeon.ui.ItemSlot;
  import com.lh64.randomdungeon.ui.QuickSlot;
  import com.lh64.randomdungeon.ui.RedButton;
  import com.lh64.randomdungeon.ui.ScrollPane;
  import com.lh64.randomdungeon.utils.Utils;
  
  public class WndRanking extends WndTabbed {
  	
  	private static final String TXT_ERROR		= "Unable to load additional information";
  	
  	private static final String TXT_STATS	= "Stats";
  	private static final String TXT_ITEMS	= "Items";
  	private static final String TXT_BADGES	= "Badges";
  	
  	private static final int WIDTH			= 120;
  	private static final int HEIGHT			= 134;
  	
  	private static final int TAB_WIDTH	= 40;
  	
  	private Thread thread;
  	private String error = null;
  	
  	private Image busy;
  	
  	public WndRanking( final String gameFile ) {
  		
  		super();
  		resize( WIDTH, HEIGHT );
  		
  		thread = new Thread() {
  			@Override
  			public void run() {
  				try {
  					Badges.loadGlobal();
  					Dungeon.loadGame( gameFile );
  					
  				} catch (Exception e ) {
  					error = TXT_ERROR;
  				}
  			}
  		};
  		thread.start();
  		
  		busy = Icons.BUSY.get();	
  		busy.origin.set( busy.width / 2, busy.height / 2 );
  		busy.angularSpeed = 720;
  		busy.x = (WIDTH - busy.width) / 2;
  		busy.y = (HEIGHT - busy.height) / 2;
  		add( busy );
  	}
  	
  	@Override
  	public void update() {
  		super.update();
  		
  		if (thread != null && !thread.isAlive()) {
  			thread = null;
  			if (error == null) {
  				remove( busy );
  				createControls();
  			} else {
  				hide();
  				Game.scene().add( new WndError( TXT_ERROR ) );
  			}
  		}
  	}
  	
  	private void createControls() {
  		
  		String[] labels = 
  			{TXT_STATS, TXT_ITEMS, TXT_BADGES};
  		Group[] pages = 
  			{new StatsTab(), new ItemsTab(), new BadgesTab()};
  		
  		for (int i=0; i < pages.length; i++) {
  			
  			add( pages[i] );
  			
  			Tab tab = new RankingTab( labels[i], pages[i] );
  			tab.setSize( TAB_WIDTH, tabHeight() );
  			add( tab );
  		}
  		
  		select( 0 );
  	}
  
  	private class RankingTab extends LabeledTab {
  		
  		private Group page;
  		
  		public RankingTab( String label, Group page ) {
  			super( label );
  			this.page = page;
  		}
  		
  		@Override
  		protected void select( boolean value ) {
  			super.select( value );
  			if (page != null) {
  				page.visible = page.active = selected;
  			}
  		}
  	}
  	
  	private class StatsTab extends Group {
  		
  		private static final int GAP	= 4;
  		
  		private static final String TXT_TITLE	= "Level %d %s";
  		
  		private static final String TXT_CHALLENGES	= "Challenges";
  		
  		private static final String TXT_HEALTH	= "Health";
  		private static final String TXT_STR		= "Strength";
  		
  		private static final String TXT_DURATION	= "Game Duration";
  		
  		private static final String TXT_DEPTH	= "Maximum Depth";
  		private static final String TXT_ENEMIES	= "Mobs Killed";
  		private static final String TXT_GOLD	= "Gold Collected";
  		
  		private static final String TXT_FOOD	= "Food Eaten";
  		private static final String TXT_ALCHEMY	= "Potions Cooked";
  		private static final String TXT_ANKHS	= "Ankhs Used";
  		
  		public StatsTab() {
  			super();
  			
  			String heroClass = Dungeon.name + ", the " + Dungeon.hero.className();
  			
  			IconTitle title = new IconTitle();
  			title.icon( HeroSprite.avatar( Dungeon.hero.heroClass, Dungeon.hero.tier() ) );
  			title.label( Utils.format( TXT_TITLE, Dungeon.hero.lvl, heroClass ).toUpperCase( Locale.ENGLISH ) );
  			title.setRect( 0, 0, WIDTH, 0 );
  			add( title );
  			
  			float pos = title.bottom();
  			
  			if (Dungeon.challenges > 0) {
  				RedButton btnCatalogus = new RedButton( TXT_CHALLENGES ) {
  					@Override
  					protected void onClick() {
  						Game.scene().add( new WndChallenges( Dungeon.challenges, false ) );
  					}
  				};
  				btnCatalogus.setRect( 0, pos + GAP, btnCatalogus.reqWidth() + 2, btnCatalogus.reqHeight() + 2 );
  				add( btnCatalogus );
  				
  				pos = btnCatalogus.bottom();
  			}
  			
  			pos += GAP + GAP;
  			
  			pos = statSlot( this, TXT_STR, Integer.toString( Dungeon.hero.STR ), pos );
  			pos = statSlot( this, TXT_HEALTH, Integer.toString( Dungeon.hero.HT ), pos );
  			
  			pos += GAP;
  			
  			pos = statSlot( this, TXT_DURATION, Integer.toString( (int)Statistics.duration ), pos );
  			
  			pos += GAP;
  			
  			pos = statSlot( this, TXT_DEPTH, "obscured", pos );
  			pos = statSlot( this, TXT_ENEMIES, Integer.toString( Statistics.enemiesSlain ), pos );
  			pos = statSlot( this, TXT_GOLD, Integer.toString( Statistics.goldCollected ), pos );
  			
  			pos += GAP;
  			
  			pos = statSlot( this, TXT_FOOD, Integer.toString( Statistics.foodEaten ), pos );
  			pos = statSlot( this, TXT_ALCHEMY, Integer.toString( Statistics.potionsCooked ), pos );
  			pos = statSlot( this, TXT_ANKHS, Integer.toString( Statistics.ankhsUsed ), pos );
  		}
  		
  		private float statSlot( Group parent, String label, String value, float pos ) {
  			
  			BitmapText txt = PixelScene.createText( label, 7 );
  			txt.y = pos;
  			parent.add( txt );
  			
  			txt = PixelScene.createText( value, 7 );
  			txt.measure();
  			txt.x = PixelScene.align( WIDTH * 0.65f );
  			txt.y = pos;
  			parent.add( txt );
  			
  			return pos + GAP + txt.baseLine();
  		}
  	}
  	
  	private class ItemsTab extends Group {
  		
  		private int count;
  		private float pos;
  		
  		public ItemsTab() {
  			super();
  			
  			Belongings stuff = Dungeon.hero.belongings;
  			if (stuff.weapon != null) {
  				addItem( stuff.weapon );
  			}
  			if (stuff.armor != null) {
  				addItem( stuff.armor );
  			}
  			if (stuff.ring1 != null) {
  				addItem( stuff.ring1 );
  			}
  			if (stuff.ring2 != null) {
  				addItem( stuff.ring2 );
  			}
  			
  			Item primary = getQuickslot( QuickSlot.primaryValue );
  			Item secondary = getQuickslot( QuickSlot.secondaryValue );
  			
  			if (count >= 4 && primary != null && secondary != null) {
  				
  				float size = ItemButton.SIZE;
  				
  				ItemButton slot = new ItemButton( primary );
  				slot.setRect( 0, pos, size, size );
  				add( slot );
  				
  				slot = new ItemButton( secondary );
  				slot.setRect( size + 1, pos, size, size );
  				add( slot );
  			} else {
  				if (primary != null) {
  					addItem( primary );
  				}
  				if (secondary != null) {
  					addItem( secondary );
  				}
  			}
  		}
  		
  		private void addItem( Item item ) {
  			LabelledItemButton slot = new LabelledItemButton( item );
  			slot.setRect( 0, pos, width, LabelledItemButton.SIZE );
  			add( slot );
  			
  			pos += slot.height() + 1;
  			count++;
  		}
  		
  		private Item getQuickslot( Object value ) {
  			if (value instanceof Item && Dungeon.hero.belongings.backpack.contains( (Item)value )) {
  					
  					return (Item)value;
  					
  			} else if (value instanceof Class){
  				
  				@SuppressWarnings("unchecked")
  				Item item = Dungeon.hero.belongings.getItem( (Class<? extends Item>)value );
  				if (item != null) {
  					return item;
  				}
  			}
  			
  			return null;
  		}
  	}
  	
  	private class BadgesTab extends Group {
  		
  		public BadgesTab() {
  			super();
  			
  			camera = WndRanking.this.camera;
  			
  			ScrollPane list = new BadgesList( false );
  			add( list );
  			
  