
function createFirstPopupDialog(listener, prefab)
	local dialogNode = Scene_instantiate(prefab)
	WorldNodeManager_addHierarchyToRoot(dialogNode)
	
	local dialogScript = WorldNode_getScriptComponent(dialogNode, "VCPopup")
	
	if dialogScript == nil then
		print("Not a VCPopup : aborting")
		return nil
	end
	dialogScript.listener = listener

	return dialogScript
end

function createPopupDialog(listener, prefab, title, message)
	local dialogNode = Scene_instantiate(prefab)
	WorldNodeManager_addHierarchyToRoot(dialogNode)

	local dialogScript = WorldNode_getScriptComponent(dialogNode, "VCPopup")
	
	if dialogScript == nil then
		print("Not a VCPopup : aborting")
		return nil
	end
	
	dialogScript.listener = listener
	
	LabelComponent_setTextFromStringId(dialogScript.title, title)
	LabelComponent_setTextFromStringId(dialogScript.message, message)
	

	
	return dialogScript
end

--- utility functions

function springSmoothUpdate(currentPos, currentVel, targetPos, stiffness, dt)
	local c = math.exp(-dt * stiffness)
	local newPos = c * (currentVel * dt + (currentPos - targetPos) * (1 + dt * stiffness)) + targetPos
	local newVel = c * (currentVel * (1 - dt * stiffness) - (currentPos - targetPos) * dt * stiffness^2)
	return newPos, newVel
end

function clamp(x, min, max)
	if x < min then return min end
	if x > max then return max end
	return x
end

-- finds the WorldNode/Component based on the worldNodePath and componentType
function get(root, worldNodePath, componentType)
	local node = root
	for i = 1, #worldNodePath do
		if worldNodePath[i] == '..' then
			node = WorldNode_getParentNode(node)
		else
			node = WorldNode_getChildByName(node, worldNodePath[i])
		end
	end
	if componentType then
		local component = WorldNode_getComponentByTypeName(node, componentType)
		if componentType == 'script' then
			return ScriptComponent_getScriptTable(component)
		else
			return component
		end
	else
		return node
	end
end

-- finds the WorldNode/Component based on the worldNodePath and componentType
function getScript(root, worldNodePath, scriptType)
	local node = root
	for i = 1, #worldNodePath do
		if worldNodePath[i] == '..' then
			node = WorldNode_getParentNode(node)
		else
			node = WorldNode_getChildByName(node, worldNodePath[i])
		end
	end
	return WorldNode_getScriptComponent(node, scriptType)
end

local g_globalVariables = {}

function declare(name, val)
	g_globalVariables[name] = true
	rawset(_G, name, val)
end

function lockGlobals()
	setmetatable(_G, {
		__index = function(tbl, key)
			if g_globalVariables[key] then
				return rawget(tbl, key)
			else
				error('undeclared global variable "' .. tostring(key) .. '"')
			end
		end,
		__newindex = function(tbl, key, val)
			if g_globalVariables[key] then
				rawset(tbl, key, val)
			else
				error('undeclared global variable "' .. tostring(key) .. '"')
			end
		end
	})
end

function copyTab(st)  
    local tab = {}  
    for k, v in pairs(st or {}) do  
        if type(v) ~= "table" then  
            tab[k] = v  
        else  
            tab[k] = copyTab(v)  
        end  
    end  
    return tab  
end

function utfstrlen(str)
	local len = #str;
	local left = len;
	local cnt = 0;
	local arr={0,0xc0,0xe0,0xf0,0xf8,0xfc};
	while left ~= 0 do
		local tmp=string.byte(str,-left);
		local i=#arr;
		while arr[i] do
			if tmp>=arr[i] then left=left-i;break;end
			i=i-1;
		end
		cnt=cnt+1;
	end
	return cnt;
end
