#include "ui_defs.lua"

Ui.AnimationCurves =
{
	Linear = "linear",		-- Linear transition
	Cosine = "cosine",		-- Slow at beginning and end
	Easein = "easein",		-- Slow at beginning
	Easeout = "easeout",	-- Slow at end
	Bounce = "bounce",		-- Bounce and overshoot new value
}


-- EXAMPLE
-- a = Ui.CreateAnimator()
-- a.playTime = 5.0
-- a.curveType = Ui.AnimationCurves.Bounce
-- a.onAnimationUpdate = function(self, factor)
-- 	UiBlur(factor)
-- 	UiScale(factor)
-- end

-- function draw()
-- 	UiColor(0.5, 0.5, 0.5)
-- 	UiRect(1920, 1080)

-- 	UiTranslate(500, 500)
-- 	UiColor(1, 0, 0)
-- 	UiRect(200, 200)

-- 	if a.isFinished then
-- 		a:playForward()
-- 	end

-- 	a:draw(function()
-- 				UiPush()
-- 					UiTranslate(50, 50)
-- 					UiColor(1, 1, 0)
-- 					UiRect(200, 200)
-- 				UiPop()
-- 			end
-- 			)
-- end

function Ui.CreateAnimator()
	local animator = {}
	animator.playTime = 1.0
	animator.factor = 0.0
	animator.curveType = Ui.AnimationCurves.Linear
	animator.isFinished = true
	animator.targetFactor = 0.0

	animator.onAnimationUpdate = function(self, factor)
		--place properties which should change in time
	end

	animator.playForward = function(self)
		self.factor = 0
		self.targetFactor = 1
		SetValueInTable(self, "factor", self.targetFactor, self.curveType, self.playTime)
		self.isFinished = false
	end

	animator.playBackwards = function(self)
		self.factor = 1
		self.targetFactor = 0
		SetValueInTable(self, "factor", self.targetFactor, self.curveType, self.playTime)
		self.isFinished = false
	end

	animator.reset = function(self, factorValue)
		self.factor = factorValue
		self.isFinished = true
		self.targetFactor = factorValue
		SetValueInTable(self, "factor", self.targetFactor, self.curveType, self.playTime)
	end

	animator.draw = function(self, innerDraw)
		UiPush()
			self:__logicUpdate()
			innerDraw()
		UiPop()
	end

	animator.__logicUpdate = function(self)
		self:onAnimationUpdate(self.factor)
		self.isFinished = self.targetFactor == self.factor
	end

	return animator
end