class 'StrategyGuidePageBase' (Node)

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

	self.imgAnchorsNamePrefix = 'img_anchor_'
	self.searchPath = {
						'Data/ExtraContent/Textures/Screens/StrategyGuide/Screens/'
					  }

	self.imgAnchors  = {}
	self.imgs		 = {}

	self.scaledImg	 = nil
	self.scalingTime = 0.2
	self.scalingPos	 = Vector2f(512, 340)
	self.scalingDeltaOrder = 10

	self:createContent()
	self:initImgAnchors()

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

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

function StrategyGuidePageBase:initImgAnchors()
	local childs = self:getMainEntity():getChilds()
	local prefixLength = string.len(self.imgAnchorsNamePrefix)

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

		if string.find(childName, self.imgAnchorsNamePrefix) == 1 then
			local index	= string.sub(childName, prefixLength+1)
			child:setColor( Colorf(1, 1, 1, 0) )
			child:hide()

			self.imgAnchors[index] = child
		end
	end
end
-------------------------------------------------------------------------------

function StrategyGuidePageBase:setInstructionsText(text)
	self:getEntityNamed('text_instructions'):setText(text)
end
-------------------------------------------------------------------------------

function StrategyGuidePageBase:setImgs(imgs)
	for _, imgData in pairs(imgs) do
		local path = self:getImgPath(imgData.name)
		assert(path)

		local anchor = self.imgAnchors[ imgData.index ]
		assert(anchor)

		local img = CreateSpriteEntityInstant( path )
		img:setName(imgData.name)
		img:attachTo(anchor)
		img:setOrder(1)
		img:setScaleX( anchor:getWidth()  / img:getWidth()  )
		img:setScaleY( anchor:getHeight() / img:getHeight() )
		img.onPress:add( bind(self.onImgPress, self) )

		anchor:show()
		table.insert(self.imgs, img)
	end
end
-------------------------------------------------------------------------------

function StrategyGuidePageBase:getImgPath(imgName)
	for _, path in ipairs(self.searchPath) do
		local imgPath = path..imgName

		if resourceExists(imgPath) then
			return imgPath
		end
	end
	
	return nil
end
-------------------------------------------------------------------------------

function StrategyGuidePageBase:setImgsRespondable(bRespondable)
	for i, img in ipairs(self.imgs) do
		img:setRespondable( bRespondable )
	end
end
-------------------------------------------------------------------------------

function StrategyGuidePageBase:onImgPress(target, screenFocus)
	target:destroyEffectors()

	if self.scaledImg then
		self:decreaseImg(self.scaledImg)
	end

	if self.scaledImg and self.scaledImg:getName() == target:getName() then
		self.scaledImg = nil
	else
		self.scaledImg = target
	end
	
	if self.scaledImg then
		self:increaseImg(self.scaledImg)
	end
end
-------------------------------------------------------------------------------

function StrategyGuidePageBase:increaseImg(img)
	assert(img)

	local anchor = img:getParent()
	anchor:setOrder(anchor:getOrder() + self.scalingDeltaOrder)

	local time	 = self.scalingTime
	local scrPos = self:getMainEntity():localToScreen(self.scalingPos)
	local pos	 = anchor:screenToLocal(scrPos)

	local effector = CreateAnimationEffector()
	effector:attachTo(img)
	effector:setDestroysOnEnd(true)
	effector:setKey(_TRACK_X,	0, img:getX())
	effector:setKey(_TRACK_Y,	0, img:getY())
	effector:setKey(_TRACK_A,	0, img:getRotationRad())
	effector:setKey(_TRACK_SX,	0, img:getScale().x)
	effector:setKey(_TRACK_SY,	0, img:getScale().y)
	effector:setKey(_TRACK_X,	time, pos.x)
	effector:setKey(_TRACK_Y,	time, pos.y)
	effector:setKey(_TRACK_A,	time, -anchor:getRotationRad())
	effector:setKey(_TRACK_SX,	time, 1)
	effector:setKey(_TRACK_SY,	time, 1)
	effector:play()

	return effector
end
-------------------------------------------------------------------------------

function StrategyGuidePageBase:decreaseImg(img)
	assert(img)

	local anchor = img:getParent()
	anchor:setOrder(anchor:getOrder() - self.scalingDeltaOrder)

	local time	   = self.scalingTime
	local effector = CreateAnimationEffector()
	effector:attachTo(img)
	effector:setDestroysOnEnd(true)
	effector:setKey(_TRACK_X,	0, img:getX())
	effector:setKey(_TRACK_Y,	0, img:getY())
	effector:setKey(_TRACK_A,	0, img:getRotationRad())
	effector:setKey(_TRACK_SX,	0, img:getScale().x)
	effector:setKey(_TRACK_SY,	0, img:getScale().y)
	effector:setKey(_TRACK_X,	time, 0)
	effector:setKey(_TRACK_Y,	time, 0)
	effector:setKey(_TRACK_A,	time, 0)
	effector:setKey(_TRACK_SX,	time, anchor:getWidth()  / img:getWidth())
	effector:setKey(_TRACK_SY,	time, anchor:getHeight() / img:getHeight())	
	effector:play()

	return effector
end
-------------------------------------------------------------------------------

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

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

function StrategyGuidePageBase: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
-------------------------------------------------------------------------------