aboutsummaryrefslogtreecommitdiff
path: root/libs/hump/timer.lua
blob: 54f06f047cc52705c34d0c88c52a94a08a07ed81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
--[[
Copyright (c) 2010-2013 Matthias Richter

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--

local Timer = {}
Timer.__index = Timer

local function _nothing_() end
local unpack = unpack or table.unpack

local function updateTimerHandle(handle, dt)
		-- handle: {
		--   time = <number>,
		--   after = <function>,
		--   during = <function>,
		--   limit = <number>,
		--   count = <number>,
		-- }
		handle.time = handle.time + dt
		handle.during(dt, math.max(handle.limit - handle.time, 0))

		while handle.time >= handle.limit and handle.count > 0 do
			if handle.after(handle.after) == false then
				handle.count = 0
				break
			end
			handle.time = handle.time - handle.limit
			handle.count = handle.count - 1
		end
end

function Timer:update(dt)
	-- timers may create new timers, which leads to undefined behavior
	-- in pairs() - so we need to put them in a different table first
	local to_update = {}
	for handle in pairs(self.functions) do
		to_update[handle] = handle
	end

	for handle in pairs(to_update) do
		if self.functions[handle] then
			updateTimerHandle(handle, dt)
			if handle.count == 0 then
				self.functions[handle] = nil
			end
		end
	end
end

function Timer:during(delay, during, after)
	local handle = { time = 0, during = during, after = after or _nothing_, limit = delay, count = 1 }
	self.functions[handle] = true
	return handle
end

function Timer:after(delay, func)
	return self:during(delay, _nothing_, func)
end

function Timer:every(delay, after, count)
	local count = count or math.huge -- exploit below: math.huge - 1 = math.huge
	local handle = { time = 0, during = _nothing_, after = after, limit = delay, count = count }
	self.functions[handle] = true
	return handle
end

function Timer:cancel(handle)
	self.functions[handle] = nil
end

function Timer:clear()
	 self.functions = {}
end

function Timer:script(f)
	local co = coroutine.wrap(f)
	co(function(t)
		self:after(t, co)
		coroutine.yield()
	end)
end

local function func_tween(tween, self, len, subject, target, method, after,
	   setters_and_getters, ...)
   -- recursively collects fields that are defined in both subject and target into a flat list
   -- re-use of ref is confusing
   local to_func_tween = {}
   local function set_and_get(subject, k, v)
      setters_and_getters = setters_and_getters or {}

      local setter, getter
      if setters_and_getters[k] then
	 setter, getter = unpack(setters_and_getters[k])
      else
	 setter = subject['set'..k]
	 getter = subject['get'..k]
      end
      assert(setter and getter,
	     "key's value in subject is nil with no set/getter")

      if to_func_tween[subject] == nil then
	 to_func_tween[subject] = {}
      end

      ref = {getter(subject)}
      to_func_tween[subject][k] = {ref, setter}
      if type(v) == 'number' or #ref == 1 then
	 v = {v}
      end
      return ref, v
   end

   local function tween_collect_payload(subject, target, out)
      for k,v in pairs(target) do

	 -- this might not be the smoothest way to do this
	 local ref = subject[k]
	 if ref == nil then
	    ref, v = set_and_get(subject, k, v)
	 end
	 assert(type(v) == type(ref), 'Type mismatch in field "'..k..'". '
		   ..type(v)..' vs '.. type(ref))
	 if type(v) == 'table' then
	    tween_collect_payload(ref, v, out)
	 else
	    local ok, delta = pcall(function() return (v-ref)*1 end)
	    assert(ok, 'Field "'..k..'" does not support arithmetic operations')
	    out[#out+1] = {subject, k, delta}
	 end
      end
      return out
   end

   method = tween[method or 'linear'] -- see __index
   local payload, t, args = tween_collect_payload(subject, target, {}), 0, {...}

   local last_s = 0
   return self:during(len, function(dt)
      t = t + dt
      local s = method(math.min(1, t/len), unpack(args))
      local ds = s - last_s
      last_s = s
      for _, info in ipairs(payload) do
	 local ref, key, delta = unpack(info)
	 ref[key] = ref[key] + delta * ds
      end
      for ref, t in pairs(to_func_tween) do
	 for key, value in pairs(t) do
	    local setter_args, setter = unpack(value)
	    if not pcall(function() setter(ref, unpack(setter_args)) end) then
	       setter(unpack(setter_args))
	    end
	 end
      end
   end, after)
end

local function plain_tween(tween, self, len, subject, target, method, after, ...)
   return func_tween(tween, self, len, subject, target, method, after, nil, ...)
end


local function def_tween(func)
   return setmetatable(
      {
	 -- helper functions
	 out = function(f) -- 'rotates' a function
	    return function(s, ...) return 1 - f(1-s, ...) end
	 end,
	 chain = function(f1, f2) -- concatenates two functions
	    return function(s, ...) return (s < .5 and f1(2*s, ...) or 1 + f2(2*s-1, ...)) * .5 end
	 end,

	 -- useful tweening functions
	 linear = function(s) return s end,
	 quad   = function(s) return s*s end,
	 cubic  = function(s) return s*s*s end,
	 quart  = function(s) return s*s*s*s end,
	 quint  = function(s) return s*s*s*s*s end,
	 sine   = function(s) return 1-math.cos(s*math.pi/2) end,
	 expo   = function(s) return 2^(10*(s-1)) end,
	 circ   = function(s) return 1 - math.sqrt(1-s*s) end,

	 back = function(s,bounciness)
	    bounciness = bounciness or 1.70158
	    return s*s*((bounciness+1)*s - bounciness)
	 end,

	 bounce = function(s) -- magic numbers ahead
	    local a,b = 7.5625, 1/2.75
	    return math.min(a*s^2, a*(s-1.5*b)^2 + .75, a*(s-2.25*b)^2 + .9375, a*(s-2.625*b)^2 + .984375)
	 end,

	 elastic = function(s, amp, period)
	    amp, period = amp and math.max(1, amp) or 1, period or .3
	    return (-amp * math.sin(2*math.pi/period * (s-1) - math.asin(1/amp))) * 2^(10*(s-1))
	 end,


      }, {

	 -- register new tween
	 __call = func,

	 -- fetches function and generated compositions for method `key`
	 __index = function(tweens, key)
	    if type(key) == 'function' then return key end

	    assert(type(key) == 'string', 'Method must be function or string.')
	    if rawget(tweens, key) then return rawget(tweens, key) end

	    local function construct(pattern, f)
	       local method = rawget(tweens, key:match(pattern))
	       if method then return f(method) end
	       return nil
	    end

	    local out, chain = rawget(tweens,'out'), rawget(tweens,'chain')
	    return construct('^in%-([^-]+)$', function(...) return ... end)
	       or construct('^out%-([^-]+)$', out)
	    or construct('^in%-out%-([^-]+)$', function(f) return chain(f, out(f)) end)
	    or construct('^out%-in%-([^-]+)$', function(f) return chain(out(f), f) end)
	       or error('Unknown interpolation method: ' .. key)
   end})
end


Timer.tween = def_tween(plain_tween)
Timer.func_tween = def_tween(func_tween)

-- Timer instancing
function Timer.new()
   return setmetatable({functions = {}, tween = Timer.tween}, Timer)
end

-- default instance
local default = Timer.new()

-- module forwards calls to default instance
local module = {}
for k in pairs(Timer) do
   if k ~= "__index" then
      module[k] = function(...) return default[k](default, ...) end
   end
end
module.tween = setmetatable({}, {
   __index = Timer.tween,
   __newindex = function(k,v) Timer.tween[k] = v end,
   __call = function(t, ...) return default:tween(...) end,
})

return setmetatable(module, {__call = Timer.new})