class ChaChaFontInfo  expands IChaChaFontInfo;


/********************************
Modification of FontInfo so that:
	- it does not rely on static fonts anymore
	- add ability to force disableScaling when requesting a Font:
		- GlobalForceScalingState(bool) change it for ALL next requests (/!\ this class might be shared)
		- GetUnscaledFontBySize(float) can be used to get unscaled font on-demand
*********************************/

#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT6 FONTNAME="Unreal Tournament" HEIGHT=6 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT8 FONTNAME="Unreal Tournament" HEIGHT=8 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT10 FONTNAME="Unreal Tournament" HEIGHT=10 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT12 FONTNAME="Unreal Tournament" HEIGHT=12 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT14 FONTNAME="Unreal Tournament" HEIGHT=14 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT16 FONTNAME="Unreal Tournament" HEIGHT=16 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT20 FONTNAME="Unreal Tournament" HEIGHT=20 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT22 FONTNAME="Unreal Tournament" HEIGHT=22 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT24 FONTNAME="Unreal Tournament" HEIGHT=24 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT30 FONTNAME="Unreal Tournament" HEIGHT=30 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT36 FONTNAME="Unreal Tournament" HEIGHT=36 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT42 FONTNAME="Unreal Tournament" HEIGHT=42 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT48 FONTNAME="Unreal Tournament" HEIGHT=48 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT54 FONTNAME="Unreal Tournament" HEIGHT=54 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT60 FONTNAME="Unreal Tournament" HEIGHT=60 ANTIAlIAS=TRUE
#exec NEW TRUETYPEFONTFACTORY NAME=UnrealT90 FONTNAME="Unreal Tournament" HEIGHT=90 ANTIAlIAS=TRUE

var Font FontClass[16];

var bool bDisableScaling;

function GlobalForceScalingState(bool bState)
{
	bDisableScaling = !bState;
	SetScalingState(bState);
}

function Font GetHugeUnscaledFont(float Width)
{
	return GetUnscaledFontIndex(6,Width);
}

function Font GetBigUnscaledFont(float Width)
{
	return GetUnscaledFontIndex(5,Width);
}

function Font GetMediumUnscaledFont(float Width)
{
	return GetUnscaledFontIndex(4,Width);
}

function Font GetSmallUnscaledFont(float Width)
{
	return GetUnscaledFontIndex(3,Width);
}

function Font GetSmallestUnscaledFont(float Width)
{
	return GetUnscaledFontIndex(2,Width);
}

function Font GetAReallySmallUnscaledFont(float Width)
{
	return GetUnscaledFontIndex(1,Width);
}

function Font GetACompletelyUnreadableUnscaledFont(float Width)
{
	return GetUnscaledFontIndex(0,Width);
}

function Font GetUnscaledFontIndex(int i, float Width)
{
	local Font	GeneratedFont;
	local float SavedCurrentInGameScalingFactor;
	local bool  SavebDisableScaling;
	
	SavedCurrentInGameScalingFactor = CurrentInGameScalingFactor;
	SavebDisableScaling				= bDisableScaling;
	
	bDisableScaling 			= True;
	CurrentInGameScalingFactor 	= 1;
	
	GeneratedFont=GetFontIndex(i,Width);
	
	CurrentInGameScalingFactor 	= SavedCurrentInGameScalingFactor;
	bDisableScaling 			= SavebDisableScaling;
	
	return GeneratedFont;
}

function Font GetFontIndex(int i, float Width)
{
	if ( bCurrentInGameScaling != bEnableInGameScaling ||
		CurrentInGameScalingFactor != InGameScalingFactor )
		SetScalingState(bEnableInGameScaling);

	if ( (SavedFont[i] != None) && (Width == SavedWidth[i]) )
		return SavedFont[i];
	
	SavedWidth[i] = Width;

	//In lower resolutions fonts need to be a bit bigger
	SavedFont[i] = GetFontBySize( (Width + (6-i)*32) / FontDiv[i] );

	return SavedFont[i];
}


/*---------------- (469) Dynamically create fonts and cache them -----------------*/

//Precache fonts if font scaling is enabled at game start
event PostBeginPlay()
{
	SetScalingState(True);
}

function SetScalingState(bool bEnableScaling)
{
	local int i;
	
	if ( !bCacheSetup )
	{
		bCacheSetup = true;
		AddNewFontCache(FontClass[0], 0, 7);
		AddNewFontCache(FontClass[1], 7, 10);
		AddNewFontCache(FontClass[2], 10, 12);
		AddNewFontCache(FontClass[3], 12, 14);
		AddNewFontCache(FontClass[4], 14, 16);
		AddNewFontCache(FontClass[5], 16, 20);
		AddNewFontCache(FontClass[6], 20, 22);
		AddNewFontCache(FontClass[7], 22, 24);
		AddNewFontCache(FontClass[8], 24, 30);
		AddNewFontCache(FontClass[9], 30, 36);
		AddNewFontCache(FontClass[10], 36, 42);
		AddNewFontCache(FontClass[11], 42, 48);
		AddNewFontCache(FontClass[12], 48, 54);
		AddNewFontCache(FontClass[13], 54, 60);
		AddNewFontCache(FontClass[14], 60, 85);
		AddNewFontCache(FontClass[15], 85, 110);
	}
	

	// Reset resolution caches to make bEnableInGameScaling changes effective immediately
	for ( i=0 ; i<7 ; ++i )
	{
		SavedFont[i]  = none;
		SavedWidth[i] = 0;
	}

	bEnableInGameScaling	= bEnableScaling;
	bCurrentInGameScaling	= bEnableScaling;
	if(bDisableScaling || (!bEnableInGameScaling))
	{
		CurrentInGameScalingFactor = 1;
	}
	else
	{
		CurrentInGameScalingFactor = InGameScalingFactor;
	}
}

//Copy of original except using CurrentInGameScalingFactor to avoid modifying InGameScalingFactor
function Font GetFontBySize(float IdealSize)
{
	local int i;
	local float NewBase;
	local Font NewFont;

	// Don't blow up the dynamic font cache
	IdealSize = fClamp( Abs(IdealSize) * CurrentInGameScalingFactor, 6, 100 );
	For ( i=0 ; i<CachedFonts.Length ; i++ )
		if ( (IdealSize >= CachedFonts[i].MinSize) && (IdealSize <= CachedFonts[i].MaxSize) )
			return CachedFonts[i].Font;

	// Dynamically created fonts will scale up using 'Square(X)' where X increments by 0.5
	NewBase = Sqrt(IdealSize);
	NewBase = NewBase - (NewBase % 0.5);
	NewFont = class'Canvas'.static.CreateFont( FF_Arial, int(Square(NewBase)), false, false, false, false, true);

	// Font could not be created, choose latest in cache
	if ( NewFont == None )
		return CachedFonts[CachedFonts.Length-1].Font;

	AddNewFontCache( NewFont, Square(NewBase), Square(NewBase+0.5) );
	return NewFont;
}

defaultproperties
{
      FontClass(0)=Font'UnrealT6'
      FontClass(1)=Font'UnrealT8'
      FontClass(2)=Font'UnrealT10'
      FontClass(3)=Font'UnrealT12'
      FontClass(4)=Font'UnrealT14'
      FontClass(5)=Font'UnrealT16'
      FontClass(6)=Font'UnrealT20'
      FontClass(7)=Font'UnrealT22'
      FontClass(8)=Font'UnrealT24'
      FontClass(9)=Font'UnrealT30'
      FontClass(10)=Font'UnrealT36'
      FontClass(11)=Font'UnrealT42'
      FontClass(12)=Font'UnrealT48'
      FontClass(13)=Font'UnrealT54'
      FontClass(14)=Font'UnrealT60'
      FontClass(15)=Font'UnrealT90'
      bEnableInGameScaling=True
      bCurrentInGameScaling=True
}
