hud.disable("stagetitle")
local title_display_time = 140
local title_disappear_time = 105

FNT_NewFont("FONT_S2FNT",{prefix = "S2FNT%03d", monospacewidth = 20, spacewidth = 12})

local ticker = 0
local stripe = {horz = 0, horz_accel = 0, vert_scroller = 0}
local ttlbar = {slide = 0, slide_accel = 0}

local track_name = nil
local track_zone = nil
local track_act = nil
local track_subttl = nil
local track_stripe_gfx = nil
local track_stripe_text = nil
local track_stripe_color = nil

local titlecard_allowcustom = CV_RegisterVar({
	name = "tc_allowcustom",
	defaultvalue = "On",
	flags = CV_NETVAR,
	possiblevalue = CV_OnOff
})

//85 = Blue
local titlecard_race_color = CV_RegisterVar({
	name = "tc_race_color",
	defaultvalue = "85",
	flags = CV_NETVAR,
	possiblevalue = {MIN = 1, MAX = 100}
})

//23 = Cherry
local titlecard_battle_color = CV_RegisterVar({
	name = "tc_battle_color",
	defaultvalue = "23",
	flags = CV_NETVAR,
	possiblevalue = {MIN = 1, MAX = 100}
})

//86 = Blueberry
local titlecard_hellrace_color = CV_RegisterVar({
	name = "tc_hellrace_color",
	defaultvalue = "86",
	flags = CV_NETVAR,
	possiblevalue = {MIN = 1, MAX = 100}
})

//25 = Crimson
local titlecard_hellbattle_color = CV_RegisterVar({
	name = "tc_hellbattle_color",
	defaultvalue = "25",
	flags = CV_NETVAR,
	possiblevalue = {MIN = 1, MAX = 100}
})

addHook("MapChange",function()
	//Resetting Variables
	stripe.horz = 0
	stripe.horz_accel = 0
	stripe.vert_scroller = 0
	ttlbar.slide = 0
	ttlbar.slide_accel = 0
	ticker = 0
end)

local function drawNewTitleCard(v,p)
	if leveltime <= title_display_time then
		--------------------------------------------------------------------------------------------
		//Figuring Out Map Info
		track_name = mapheaderinfo[gamemap].lvlttl
		if mapheaderinfo[gamemap].encorettl and encoremode == true then track_name = mapheaderinfo[gamemap].encorettl end
		if (mapheaderinfo[gamemap].levelflags & LF_NOZONE) then
			track_zone = ""
		else
			if mapheaderinfo[gamemap].zonttl == "" then
				track_zone = "Zone"
			else
				track_zone = mapheaderinfo[gamemap].zonttl
			end
		end
		if mapheaderinfo[gamemap].encorezone and encoremode == true then track_zone = mapheaderinfo[gamemap].encorezone end
		track_act = mapheaderinfo[gamemap].actnum
		
		track_subttl = mapheaderinfo[gamemap].subttl
		if mapheaderinfo[gamemap].encoresub and encoremode == true then track_subttl = mapheaderinfo[gamemap].encoresub end
		------------------------------------------------------------------------
		//Figuring out Graphics
		if titlecard_allowcustom.value == 1 then
			track_stripe_gfx = mapheaderinfo[gamemap].titlecard_stripe
			track_stripe_text = mapheaderinfo[gamemap].titlecard_text
			track_stripe_color = tonumber(mapheaderinfo[gamemap].titlecard_color)
		end
		
		if track_stripe_gfx == nil or track_stripe_gfx == "" then
			track_stripe_gfx = "TC_DEFSTRIPE"
		end
		
		if track_stripe_text == nil or track_stripe_text == "" then
			if mapheaderinfo[gamemap].menuflags & LF2_HIDEINMENU then
				track_stripe_text = "TC_HELTEXT"
			else
				track_stripe_text = "TC_DEFTEXT"
			end
		end
		
		if track_stripe_color == nil or track_stripe_color == 0 then
			//If Battle
			if gametype & GT_MATCH then
				//If Map Hell
				if mapheaderinfo[gamemap].menuflags & LF2_HIDEINMENU then
					track_stripe_color = titlecard_hellbattle_color.value
				//Not Map Hell
				else
					track_stripe_color = titlecard_battle_color.value
				end
			//If Race
			else
				//If Map Hell
				if mapheaderinfo[gamemap].menuflags & LF2_HIDEINMENU then
					track_stripe_color = titlecard_hellrace_color.value
				//Not Map Hell
				else
					track_stripe_color = titlecard_race_color.value
				end
			end
		end
		
		if encoremode == true
			if mapheaderinfo[gamemap].titlecard_encore_color != nil and mapheaderinfo[gamemap].titlecard_encore_color != "" then
				track_stripe_color = tonumber(mapheaderinfo[gamemap].titlecard_encore_color)
			else
				track_stripe_color = ColorOpposite($)
			end
		end
		------------------------------------------------------------------------
		//DRAWING
		local tc_stripe = v.cachePatch(track_stripe_gfx)
		local tc_ttlbar = v.cachePatch("TC_TTLBAR")
		local tc_stripe_text = v.cachePatch(track_stripe_text)
		
		//Happening Before It Disappears
		stripe.vert_scroller = (leveltime % 192) * FRACUNIT
		
		//What happens when it Disappears
		if leveltime > title_disappear_time then
			//Horizontal Offset for Stripe
			stripe.horz_accel = $ + (leveltime - title_disappear_time)
			stripe.horz = $ + stripe.horz_accel
			//Horizontal Offset for TitleBar
			ttlbar.slide_accel = $ + (2 * (leveltime - title_disappear_time))
			ttlbar.slide = $ + ttlbar.slide_accel
		end
		//ACTUAL DRAWING
			//Title
			v.draw(64+ttlbar.slide,56,tc_ttlbar,V_SNAPTOLEFT,v.getColormap(0,SKINCOLOR_BLACK))
			FNT_Write(v,{x = (208+ttlbar.slide) * FRACUNIT, y = 60 * FRACUNIT, text = track_name,font = FONT_S2FNT, scale = 3*FRACUNIT/4, 
							color = SKINCOLOR_WHITE, halign = HALIGN_CENTER, valign = VALIGN_CENTER, flags = V_SNAPTOLEFT})
			//ZoneTitle
			if track_zone != "" and track_zone != nil then
				v.draw(128+ttlbar.slide,88,tc_ttlbar,V_SNAPTOLEFT,v.getColormap(0,SKINCOLOR_BLACK))
				FNT_Write(v,{x = (208+ttlbar.slide) * FRACUNIT, y = 92 * FRACUNIT, text = track_zone.." "..track_act,font = FONT_S2FNT, scale = 3*FRACUNIT/4, 
							color = SKINCOLOR_WHITE, halign = HALIGN_CENTER, valign = VALIGN_CENTER, flags = V_SNAPTOLEFT})
			end
			//Subtitle
			if track_subttl != "" and track_subttl != nil then
				v.draw(64+ttlbar.slide,152,tc_ttlbar,V_SNAPTOLEFT,v.getColormap(0,SKINCOLOR_BLACK))
				FNT_Write(v,{x = (208+ttlbar.slide) * FRACUNIT, y = 158 * FRACUNIT, text = string.lower(track_subttl),font = FONT_S2FNT, scale = FRACUNIT / 2, 
							color = SKINCOLOR_GOLD, halign = HALIGN_CENTER, valign = VALIGN_CENTER, flags = V_SNAPTOLEFT})
			end
			//Stripe Scroll
			v.drawScaled((16 - stripe.horz) * FRACUNIT,(-512 * FRACUNIT)+ stripe.vert_scroller,FRACUNIT,tc_stripe,V_SNAPTOLEFT,v.getColormap(0,track_stripe_color))
			v.drawScaled((16 - stripe.horz) * FRACUNIT,(-128 * FRACUNIT)+ stripe.vert_scroller,FRACUNIT,tc_stripe,V_SNAPTOLEFT,v.getColormap(0,track_stripe_color))
			//Stripe Text
			v.drawScaled((16 - stripe.horz)*FRACUNIT, (0 * FRACUNIT), FRACUNIT, tc_stripe_text, V_SNAPTOLEFT|V_SNAPTOBOTTOM,v.getColormap(0,track_stripe_color))
    end
end

hud.add(drawNewTitleCard,"game")