#Conditional Hooks
$On Game Init:
[

--Misc functions for handling various UI-related tasks

function ScpuiSystem:parseTable(parserObject, parseFunction, tblName, tbmName)
    ba.print("Beginning parse of " .. tblName .. ".tbl...\n")
    
    -- Check if the base table exists and parse it
    if cf.fileExists(tblName .. '.tbl', '', true) then
        parseFunction(parserObject, tblName .. '.tbl')
    end

    -- Parse any associated .tbm files
    for _, v in ipairs(cf.listFiles("data/tables", "*-" .. tbmName .. ".tbm")) do
        parseFunction(parserObject, v)
    end
end

function ScpuiSystem:inMultiGame()
	local game = ui.MultiGeneral.getNetGame()
	
	if game:isValid() then
		return true
	end
	
	return false
end

function ScpuiSystem:replaceAngleBrackets(inputString)
    local result = string.gsub(inputString, "<", "&lt;")
    result = string.gsub(result, ">", "&gt;")
    return result
end

function ScpuiSystem:freeAllModels()
	if ScpuiSystem.missionLoaded == false then
		ba.print("SCPUI is freeing all models!\n")
		gr.freeAllModels()
	end
end
function ScpuiSystem:pauseAllAudio(toggle)
	local topics = require("ui_topics")
	
	ad.pauseMusic(-1, toggle)
	ad.pauseWeaponSounds(toggle)
	ad.pauseVoiceMessages(toggle)
	topics.Scpui.pauseAudio:send(toggle)
end

function ScpuiSystem:getAbsoluteLeft(element)
	local val = element.offset_left
	local parent = element.parent_node
	while parent ~= nil do
		val = val + parent.offset_left
		parent = parent.parent_node
	end
	
	return val
end

function ScpuiSystem:getAbsoluteTop(element)
	local val = element.offset_top
	local parent = element.parent_node
	while parent ~= nil do
		val = val + parent.offset_top
		parent = parent.parent_node
	end
	
	return val
end

function ScpuiSystem:stopMusic()
	if ScpuiSystem.music_handle ~= nil and ScpuiSystem.music_handle:isValid() then
		ScpuiSystem.music_handle:close(true)
	end
	ScpuiSystem.music_handle = nil
end

function ScpuiSystem:maybePlayCutscene(scene)
	local topics = require("ui_topics")
	topics.playcutscene.start:send(self)
	if ScpuiSystem.music_handle ~= nil then
		ScpuiSystem.music_handle:pause()
	end
	--Setting this to false so it doesn't try to restart music
	--that SCPUI handles internally
	ScpuiSystem.render = false
	ui.maybePlayCutscene(scene, false, 0)
	ScpuiSystem.render = true
	if ScpuiSystem.music_handle ~= nil then
		ScpuiSystem.music_handle:unpause()
	end
	topics.playcutscene.finish:send(self)
end

function ScpuiSystem:getFontPixelSize(val)
	local vmin = math.min(gr.getScreenWidth(), gr.getScreenHeight())
	local size = vmin * 0.012 --Gets rougly 12px font on 1080p
	-- Lua has no math.round(); math.floor(x + 0.5) is the idiomatic replacement.
	local pixelSize = math.floor(size + 0.5)
	
	function convert(value)
		if not value then return nil end
	    local clamped_value = math.max(0, math.min(1, value))
	    local scaled_value = (clamped_value - 0.5) * 20
	    return math.floor(scaled_value + 0.5)
	end
	
	if not val then
		val = convert(ScpuiOptionValues.Font_Adjustment) or 0.5
	else
		val = convert(val)
	end
	
	local finalSize = math.max(1, math.min(40, pixelSize + val))
	
	return tostring(finalSize)
end

function ScpuiSystem:getFontSize(val, default)
	if default == nil then
		default = 5
	end
	-- If we have don't have val, then get the stored one
	if val == nil then
		if ScpuiOptionValues == nil then
			ba.warning("Cannot get font size before SCPUI is initialized! Using default.")
			return default
		else
			val = ScpuiOptionValues.Font_Multiplier
			
			-- If value is not set then use default
			if val == nil then
				return default
			end
		end
	end
	
	-- Make sure val is a number
	val = tonumber(val)
	if val == nil then
		ba.warning("SCPUI got invalid data for Font Multiplier! Using default.")
		return default
	end
	
	-- If value is greater than 1, then it's an old style and we can just return it directly
	-- But math.floor it just in case.
	if val > 1.0 then
		return math.floor(val)
	end
	
	-- Range check
	if val < 0.0 then
        val = 0.0
    elseif val > 1.0 then
        val = 1.0
    end

    -- Perform the conversion
    local convertedValue = 1 + (val * 19)
    return math.floor(convertedValue)
end

function ScpuiSystem:getBackgroundClass()
	local campaignfilename = ba.getCurrentPlayer():getCampaignFilename()
	local bgclass = self.backgrounds[campaignfilename]
	
	if not bgclass then
		bgclass = "general_bg"
	end
	
	return bgclass
end

function ScpuiSystem:getBriefingBackground(mission, stage)

	local file = nil
	
	if self.briefBackgrounds[mission] ~= nil then
		file = self.briefBackgrounds[mission][stage]
	
		if file == nil then
			file = self.briefBackgrounds[mission]["default"]
		end
	end
	
	--double check
	if file == nil then
		file = "br-black.png"
	end

	return file
end

function ScpuiSystem:UncheckChildren(parent)
	local el = parent.first_child
	while el ~= nil do
		el:SetPseudoClass("checked", false)
		el = el.next_sibling
	end
end

--Clears an element of all children
function ScpuiSystem:ClearEntries(parent)
	while parent:HasChildNodes() do
		parent:RemoveChild(parent.first_child)
	end
end

function ScpuiSystem:SetStyle(parent, attribute, value)
	if parent ~= nil then
		parent.style[attribute] = value
	end
end

function ScpuiSystem:scroll_up(element)
	element.scroll_top = element.scroll_top - 10
end

function ScpuiSystem:scroll_down(element)
	element.scroll_top = element.scroll_top + 10
end

--Clears a dropdown list
function ScpuiSystem:clearDropdown(element)
	while element.options[0] ~= nil do
		element:Remove(0)
	end
end

--Add all table elements to a dropdown as selections
function ScpuiSystem:buildSelectList(element, list)
	for i, v in ipairs(list) do
		element:Add(v, v, i)
	end
end

--Makes an element of the specified type and with the specified id
function ScpuiSystem:makeElement(context, t, id)
	local el = context.document:CreateElement(t)
	if id ~= nil then
		el.id = id
	end
	return el
end

function ScpuiSystem:makeElementPanel(context, id, img)
	if id == nil then
		ba.error("SCPUI: ID is required to make an element panel!")
	end
	
	local el = context.document:CreateElement("div")
	el.id = id
	
	local img_el = ScpuiSystem:makeImg(context, img)
	img_el.style.display = "block"
	img_el.style.width = "100%"
	img_el.style.height = "auto"
	
	local inner_el = ScpuiSystem:makeElement(context, "div", id .. "_inner")
	inner_el.style.position = "absolute"
	inner_el.style.top = "0"
	inner_el.style.left = "0"
	
	el:AppendChild(inner_el)
	el:AppendChild(img_el)
	
	return el
end

--Makes an image element
function ScpuiSystem:makeImg(context, file, animated)
	local t = "img"
	if animated == true then
		t = "ani"
	end
	local el = context.document:CreateElement(t)
	el:SetAttribute("src", file)
	return el
end

--Makes a text-only button and returns the elements
function ScpuiSystem:makeTextButton(context, cont_id, button_id, button_classes, text_id, text_classes, text)
	local cont_el = context.document:CreateElement("div")
	cont_el.id = cont_id
	
	local button_el = context.document:CreateElement("button")
	button_el.id = button_id
	for _, v in ipairs(button_classes) do
		button_el:SetClass(v, true)
	end
	
	local button_text_el = context.document:CreateElement("span")
	button_text_el.id = text_id
	for _, v in ipairs(text_classes) do
		button_text_el:SetClass(v, true)
	end
	
	local button_text = context.document:CreateElement("p")
	button_text.inner_rml = text
	
	button_text_el:AppendChild(button_text)
	button_el:AppendChild(button_text_el)
	cont_el:AppendChild(button_el)
	
	return cont_el, button_el
end

--Makes an image button and returns the elements
function ScpuiSystem:makeButton(context, cont_id, button_id, button_classes, img_base, img_file, text_id, text_classes, text)
	local cont_el = context.document:CreateElement("div")
	cont_el.id = cont_id
	
	local button_el = context.document:CreateElement("button")
	button_el.id = button_id
	for _, v in ipairs(button_classes) do
		button_el:SetClass(v, true)
	end
	
	local button_img_el = context.document:CreateElement("span")
	button_img_el.id = img_base .. "_img"
	button_img_el:SetClass(img_base, true)
	button_img_el:SetClass("button_img", true)
	
	local button_img = context.document:CreateElement("img")
	button_img:SetAttribute("src", img_file)
	button_img:SetClass("psuedo_img", true)
	
	local button_text_el = context.document:CreateElement("span")
	button_text_el.id = text_id
	for _, v in ipairs(text_classes) do
		button_text_el:SetClass(v, true)
	end
	
	local button_text = context.document:CreateElement("p")
	button_text.inner_rml = text
	
	button_text_el:AppendChild(button_text)
	button_img_el:AppendChild(button_img)
	button_el:AppendChild(button_img_el)
	button_el:AppendChild(button_text_el)
	cont_el:AppendChild(button_el)
	
	return cont_el, button_el
end

function ScpuiSystem:makeButtonPanel(name)
	-- unused
end

]

#End