class 'MoreGamesPageBase' (Node)

function MoreGamesPageBase:__init(screen, name, order)
	Node.__init(self, screen:getMainEntity(), name, order)

	self.screen = screen

	self.selectorTexture		= 'Data/Textures/Screens/MoreGames/Layer_selector.png'
	self.indicatorFrameTexture	= 'Data/Textures/Screens/MoreGames/Layer_indicator_frame.png'

	self.iconFileFormat			= 'Data/Textures/Screens/MoreGames/Icons/%s.png'
	self.linkFileFormat			= 'Data/Links/Games/%s.txt'

	self.holders				= {}
	self.holderNamePrefix		= 'holder_'
	self.selectorAppearTime		= 0.2
	self.indicatorPosition		= Vector2f(-85,-65)
	self.defaultIconSize		= Vector2f(175, 150)

	self:createContent()
	self:initHolders()

	self:getMainEntity():setOpacity( 0 )
	self:getMainEntity():setRespondable( true )
end
-------------------------------------------------------------------------------

function MoreGamesPageBase:createContent()
	if self.CreateLoaderTable then
		local resourceTable = self:CreateLoaderTable()
		LoadScene(self:getMainEntity(), resourceTable)
	end
end
-------------------------------------------------------------------------------

function MoreGamesPageBase:initHolders()
	local childs = self:getMainEntity():getChilds()

	for i = 0, childs:size() - 1 do
		local child		= childs:get(i)
		local childName	= child:getName()

		local holderId = string.match(childName, self.holderNamePrefix.."(%d+)")
		if holderId then
			child:setColor( Colorf(1, 1, 1, 0) )
			child:hide()

			local data	= {}
			data.id		= tonumber(holderId)
			data.entity	= child
			data.game	= nil

			table.insert(self.holders, data)
		end
	end

	local compare = function(data1, data2)
		return data1.id < data2.id
	end
	table.sort(self.holders, compare)
end
-------------------------------------------------------------------------------

function MoreGamesPageBase:addGame(gameName, gameIndex)
	local holder	  = self:getFreeHolder()
	local iconTexture = string.format(self.iconFileFormat, gameName)
	local linkFile	  = string.format(self.linkFileFormat, gameName)

	assert(	resourceExists( iconTexture ) and
			resourceExists( linkFile )	  and
			holder )

	local iconStateUp	= CreateSpriteEntityInstant( iconTexture ) 
	local iconStateOver	= CreateSpriteEntityInstant( iconTexture ) 
	local iconStateDown	= CreateSpriteEntityInstant( iconTexture ) 
	
	local icon = CreateButtonEntity('')
	icon:setSpriteOnStateUp  ( iconStateUp   )
	icon:setSpriteOnStateOver( iconStateOver )
	icon:setSpriteOnStateDown( iconStateDown )

	icon:setName		( gameName )
	icon:attachTo		( holder.entity )
	icon:setOrder		( 1 )
	icon:setScaleX		( holder.entity:getWidth()  / iconStateUp:getWidth()  )
	icon:setScaleY		( holder.entity:getHeight() / iconStateUp:getHeight() )
	icon.onPress:add	( bind(self.openURL, self,	linkFile) )
	icon.onFocusIn:add  ( bind(self.onIconFocusIn,	self) )
	icon.onFocusOut:add ( bind(self.onIconFocusOut,	self) )

	holder.game		 = gameName
	holder.icon		 = icon
	holder.indicator = self:createIndexIndicator(holder.entity, gameIndex)
	holder.selector	 = self:createSelector(holder.entity)

	holder.entity:show()
end
-------------------------------------------------------------------------------

function MoreGamesPageBase:createIndexIndicator(parent, gameIndex)
	local pos = self.indicatorPosition or Vector2f( -parent:getWidth()  / 2,
													-parent:getHeight() / 2 )

	local frame = CreateSpriteEntityInstant( self.indicatorFrameTexture ) 
	frame:attachTo		( parent )
	frame:setName		( parent:getName().."_index_frame" )
	frame:setOrder		( 4 )
	frame:setRespondable( false )
	frame:setPosition	( pos )

	local textfield = CreateTextEntity()
	textfield:attachTo	( frame )
	textfield:setName	( parent:getName()..'_index_text' )
	textfield:setText	( tostring(gameIndex) )
	textfield:setFont	( (self.custom_font or 'Data/Fonts/Options.fnt') )
	textfield:setHeight	( 25 )
	textfield:setOrder	( 1 )
	textfield:setXY		( 0, 0 )

	return frame
end
-------------------------------------------------------------------------------

function MoreGamesPageBase:createSelector(parent)
	local entity = CreateSpriteEntityInstant( self.selectorTexture ) 
	entity:attachTo			( parent )
	entity:setName			( parent:getName().."_selector" )
	entity:setScaleX		( parent:getWidth()  / self.defaultIconSize.x )
	entity:setScaleY		( parent:getHeight() / self.defaultIconSize.y )
	entity:setOrder			( 3 )
	entity:setXY			( 0, 0 )
	entity:setRespondable	( false )
	entity:setOpacity		( 0 )

	return entity
end
-------------------------------------------------------------------------------

function MoreGamesPageBase:getFreeHolder()
	for i=1, #self.holders do
		local data = self.holders[i]

		if not data.game then
			return data
		end
	end

	return nil
end
-------------------------------------------------------------------------------

function MoreGamesPageBase:getHolder(game)
	for i, data in ipairs(self.holders) do
		if data.game == game then
			return data
		end
	end

	return nil
end
-------------------------------------------------------------------------------

function MoreGamesPageBase:onIconFocusIn(target, screenFocus)
	local holder = self:getHolder(target:getName())
	assert(holder)

	holder.selector:destroyEffectors()
	holder.selector:ChangeOpacity(nil, 1, self.selectorAppearTime)
end
-------------------------------------------------------------------------------

function MoreGamesPageBase:onIconFocusOut(target, screenFocus)
	local holder = self:getHolder(target:getName())
	assert(holder)

	holder.selector:destroyEffectors()
	holder.selector:ChangeOpacity(nil, 0, self.selectorAppearTime)
end
-------------------------------------------------------------------------------

function MoreGamesPageBase:show(time)
	local eff = self:getMainEntity():ChangeOpacity(nil, 1, time)

	eff.onFinish:add(	function()
							self:getMainEntity():setRespondable(true)
						end)
end
-------------------------------------------------------------------------------

function MoreGamesPageBase:hide(time)
	self:getMainEntity():setRespondable(false)
	self:getMainEntity():destroyEffectors()
	local eff = self:getMainEntity():ChangeOpacity(nil, 0, time)

	eff.onFinish:add(	function()
							self:getMainEntity():destroy()
						end)
end
-------------------------------------------------------------------------------

function MoreGamesPageBase:openURL(linkFile)
    local externalLinksEnabled = Application:getApplicationDescriptor().externalLinksEnabled
	if externalLinksEnabled == nil or (externalLinksEnabled and externalLinksEnabled == true) then
        self.screen.timer:add(	function()
                                    openURL(linkFile)
                                end, 0)
    end
end
-------------------------------------------------------------------------------