160 lines
4.1 KiB
Lua
160 lines
4.1 KiB
Lua
------------------------------------------------
|
|
--作者: gzg
|
|
--日期: 2020-12-19
|
|
--文件: AnimValue01.lua
|
|
--模块: AnimValue01
|
|
--描述: 数据动画,在一个时间段,让这个值从0到1变化
|
|
------------------------------------------------
|
|
|
|
local AnimValue01 = {
|
|
|
|
--开始时间点
|
|
StartTime = 0,
|
|
--延迟时间
|
|
Delay = 0,
|
|
--播放长度
|
|
Duration = 0,
|
|
--是否启动
|
|
Enabled = false,
|
|
|
|
--逝去的时间
|
|
Elapse = 0,
|
|
|
|
--当前值[0,1]
|
|
CurrValue = 0,
|
|
|
|
--数据改变的回调
|
|
OnHandler = nil,
|
|
|
|
--速度数据
|
|
SpeedData = List:New(),
|
|
}
|
|
|
|
--创建一个新的对象
|
|
function AnimValue01:New()
|
|
local _m = Utils.DeepCopy(self)
|
|
_m.SpeedData = List:New();
|
|
return _m
|
|
end
|
|
|
|
--开始播放,
|
|
--delay: 表示延迟动画的时间
|
|
--duration:表示播放动画的时间
|
|
--handler:每帧值得改变通过这个回调返回,可以不传,主动获取当前值,传参需要通过Utils.Handler进行封装
|
|
function AnimValue01:Start(delay,duration,handler)
|
|
local _typeX = type(delay)
|
|
if _typeX == "number" or _typeX ~= "nil" then
|
|
self.Delay = delay;
|
|
else
|
|
self.Delay = 0;
|
|
end
|
|
|
|
_typeX = type(duration)
|
|
if _typeX == "number" or _typeX ~= "nil" then
|
|
self.Duration = duration;
|
|
else
|
|
self.Duration = 0;
|
|
end
|
|
self.StartTime = Time.GetRealtimeSinceStartup();
|
|
self.CurrValue = 0;
|
|
self.Enabled = true;
|
|
self.OnHandler = handler;
|
|
end
|
|
|
|
--停止动画
|
|
function AnimValue01:Stop()
|
|
self.Delay = 0;
|
|
self.Duration = 0;
|
|
self.Enabled = false;
|
|
end
|
|
|
|
--获取当前值
|
|
function AnimValue01:GetCurValue()
|
|
return self.CurrValue;
|
|
end
|
|
|
|
--设置动画的速度数据,其中值是一个数组. 例如: [1,1,1,2,3,4,5,10]表示速度越来越快,[10,8,4,2,1,1,1]表示速度越来越慢
|
|
--算法描述:根据数组中值的数量,把时间分割为N等分,当值越大,表示当前等分得时间段速度越快.
|
|
function AnimValue01:SetSpeed(list)
|
|
|
|
local _sum = 0;
|
|
local _tmp = 0;
|
|
--把有效数据增加到速度数据中
|
|
self.SpeedData:Clear();
|
|
for i=1,#list do
|
|
if list[i] > 0 then
|
|
self.SpeedData:Add(list[i]);
|
|
end
|
|
end
|
|
|
|
--计算总量
|
|
for i = 2,self.SpeedData:Count() do
|
|
self.SpeedData[i] = self.SpeedData[i]+self.SpeedData[i-1];
|
|
end
|
|
|
|
--格式化数据
|
|
if self.SpeedData:Count() > 0 then
|
|
_sum = self.SpeedData[self.SpeedData:Count()];
|
|
for i = 1,#self.SpeedData do
|
|
self.SpeedData[i] = self.SpeedData[i] / _sum;
|
|
end
|
|
--最后一个设置为1
|
|
self.SpeedData[self.SpeedData:Count()] = 1;
|
|
else
|
|
self.SpeedData.Add(1);
|
|
end
|
|
end
|
|
|
|
--更新,需要有外部一个驱动进行调用.
|
|
function AnimValue01:Update()
|
|
if self.Enabled then
|
|
if self.Duration > 0 then
|
|
self.Elapse = Time.GetRealtimeSinceStartup() - self.StartTime;
|
|
self.CurrValue = (self.Elapse - self.Delay)/self.Duration
|
|
self.CurrValue = self:FixedValue(self.CurrValue)
|
|
else
|
|
self.CurrValue = 1;
|
|
end
|
|
--判断是否结束
|
|
if self.CurrValue >= 1 then
|
|
self.CurrValue = 1;
|
|
self.Enabled = false;
|
|
end
|
|
--回调处理
|
|
if self.OnHandler then
|
|
self.OnHandler(self.CurrValue,self.Enabled);
|
|
end
|
|
end
|
|
end
|
|
|
|
--根据速度,对最终的数据进行修正 --私有函数
|
|
function AnimValue01:FixedValue(val)
|
|
if val < 0 then
|
|
val = 0;
|
|
end
|
|
if val > 1 then
|
|
val = 1;
|
|
end
|
|
--速度数据的长度
|
|
local _len = self.SpeedData:Count();
|
|
if _len > 1 then
|
|
local _val= _len * val;
|
|
--获取索引
|
|
local idx = math.floor(_val);
|
|
--获取增量
|
|
local _delta = _val - idx;
|
|
--获取插值的起始数据
|
|
local from = 0;
|
|
if idx > 0 then
|
|
from = self.SpeedData[idx];
|
|
end
|
|
--获取终点数据
|
|
local to = 1
|
|
if idx < _len then
|
|
to = self.SpeedData[idx+1];
|
|
end
|
|
val = (to - from) * _delta + from;
|
|
end
|
|
return val;
|
|
end
|
|
return AnimValue01 |