rename instance to self, update example

This commit is contained in:
Insality
2019-04-07 15:19:39 +03:00
parent 636df146c9
commit 08eae3800c
7 changed files with 249 additions and 247 deletions

View File

@@ -9,66 +9,66 @@ M.interest = {
local empty = function() end
function M.init(instance, node, seconds_from, seconds_to, callback)
instance.node = helper.get_node(node)
function M.init(self, node, seconds_from, seconds_to, callback)
self.node = helper.get_node(node)
seconds_from = math.max(seconds_from, 0)
seconds_to = math.max(seconds_to or 0, 0)
callback = callback or empty
instance:set_to(seconds_from)
instance:set_interval(seconds_from, seconds_to)
instance.callback = callback
self:set_to(seconds_from)
self:set_interval(seconds_from, seconds_to)
self.callback = callback
if seconds_to - seconds_from == 0 then
instance:set_state(false)
instance.callback(instance.parent.parent, instance)
self:set_state(false)
self.callback(self.parent.parent, self)
end
return instance
return self
end
--- Set text to text field
-- @param set_to - set value in seconds
function M.set_to(instance, set_to)
instance.last_value = set_to
gui.set_text(instance.node, formats.second_string_min(set_to))
function M.set_to(self, set_to)
self.last_value = set_to
gui.set_text(self.node, formats.second_string_min(set_to))
end
--- Called when update
-- @param is_on - boolean is timer on
function M.set_state(instance, is_on)
instance.is_on = is_on
function M.set_state(self, is_on)
self.is_on = is_on
end
--- Set time interval
-- @param from - "from" time in seconds
-- @param to - "to" time in seconds
function M.set_interval(instance, from, to)
instance.from = from
instance.value = from
instance.temp = 0
instance.target = to
M.set_state(instance, true)
M.set_to(instance, from)
function M.set_interval(self, from, to)
self.from = from
self.value = from
self.temp = 0
self.target = to
M.set_state(self, true)
M.set_to(self, from)
end
--- Called when update
-- @param dt - delta time
function M.update(instance, dt)
if instance.is_on then
instance.temp = instance.temp + dt
local dist = math.min(1, math.abs(instance.value - instance.target))
function M.update(self, dt)
if self.is_on then
self.temp = self.temp + dt
local dist = math.min(1, math.abs(self.value - self.target))
if instance.temp > dist then
instance.temp = instance.temp - dist
instance.value = helper.step(instance.value, instance.target, 1)
M.set_to(instance, instance.value)
if instance.value == instance.target then
instance:set_state(false)
instance.callback(instance.parent.parent, instance)
if self.temp > dist then
self.temp = self.temp - dist
self.value = helper.step(self.value, self.target, 1)
M.set_to(self, self.value)
if self.value == self.target then
self:set_state(false)
self.callback(self.parent.parent, self)
end
end
end