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

@ -6,19 +6,19 @@ M.interest = {
} }
function M.init(instance, callback, params) function M.init(self, callback, params)
instance.event = data.A_ANDR_BACK self.event = data.A_ANDR_BACK
instance.callback = callback self.callback = callback
instance.params = params self.params = params
end end
--- input handler --- input handler
-- @param action_id - input action id -- @param action_id - input action id
-- @param action - input action -- @param action - input action
function M.on_input(instance, action_id, action) function M.on_input(self, action_id, action)
if action[data.RELEASED] then if action[data.RELEASED] then
instance.callback(instance.parent.parent, instance.params) self.callback(self.parent.parent, self.params)
end end
return true return true
end end

View File

@ -15,47 +15,47 @@ M.DEFAULT_ACTIVATE_SCALE = vmath.vector3(1, 1, 1)
M.DEFAUL_ACTIVATION_TIME = 0.2 M.DEFAUL_ACTIVATION_TIME = 0.2
function M.init(instance, node, callback, params, anim_node, event) function M.init(self, node, callback, params, anim_node, event)
instance.node = helper.get_node(node) self.node = helper.get_node(node)
instance.event = data.A_TOUCH self.event = data.A_TOUCH
instance.anim_node = anim_node and helper.get_node(anim_node) or instance.node self.anim_node = anim_node and helper.get_node(anim_node) or self.node
instance.scale_from = gui.get_scale(instance.anim_node) self.scale_from = gui.get_scale(self.anim_node)
instance.scale_to = instance.scale_from + b_settings.SCALE_CHANGE self.scale_to = self.scale_from + b_settings.SCALE_CHANGE
instance.scale_hover_to = instance.scale_from + b_settings.HOVER_SCALE self.scale_hover_to = self.scale_from + b_settings.HOVER_SCALE
instance.pos = gui.get_position(instance.anim_node) self.pos = gui.get_position(self.anim_node)
instance.callback = callback self.callback = callback
instance.params = params self.params = params
instance.tap_anim = M.tap_scale_animation self.tap_anim = M.tap_scale_animation
instance.back_anim = M.back_scale_animation self.back_anim = M.back_scale_animation
instance.hover_anim = b_settings.IS_HOVER self.hover_anim = b_settings.IS_HOVER
instance.sound = b_settings.BTN_SOUND self.sound = b_settings.BTN_SOUND
instance.sound_disable = b_settings.BTN_SOUND_DISABLED self.sound_disable = b_settings.BTN_SOUND_DISABLED
instance.ext_zone = nil self.ext_zone = nil
end end
local function set_hover(instance, state) local function set_hover(self, state)
if instance.hover_anim and instance._is_hovered ~= state then if self.hover_anim and self._is_hovered ~= state then
local target_scale = state and instance.scale_hover_to or instance.scale_from local target_scale = state and self.scale_hover_to or self.scale_from
ui_animate.scale(instance, instance.node, target_scale, b_settings.HOVER_TIME) ui_animate.scale(self, self.node, target_scale, b_settings.HOVER_TIME)
instance._is_hovered = state self._is_hovered = state
end end
end end
local function on_button_release(instance) local function on_button_release(self)
if not instance.disabled then if not self.disabled then
if not instance.stub and instance.can_action then if not self.stub and self.can_action then
instance.can_action = false self.can_action = false
instance.tap_anim(instance) self.tap_anim(self)
settings.play_sound(instance.sound) settings.play_sound(self.sound)
instance.callback(instance.parent.parent, instance.params, instance) self.callback(self.parent.parent, self.params, self)
else else
set_hover(instance, false) set_hover(self, false)
end end
return true return true
else else
instance.sound_disable() self.sound_disable()
return false return false
end end
end end
@ -64,124 +64,124 @@ end
--- Set text to text field --- Set text to text field
-- @param action_id - input action id -- @param action_id - input action id
-- @param action - input action -- @param action - input action
function M.on_input(instance, action_id, action) function M.on_input(self, action_id, action)
if not helper.is_enabled(instance.node) then if not helper.is_enabled(self.node) then
return false return false
end end
local is_pick = gui.pick_node(instance.node, action.x, action.y) local is_pick = gui.pick_node(self.node, action.x, action.y)
if instance.ext_zone then if self.ext_zone then
is_pick = is_pick and gui.pick_node(instance.ext_zone, action.x, action.y) is_pick = is_pick and gui.pick_node(self.ext_zone, action.x, action.y)
end end
if is_pick then if is_pick then
if action.pressed then if action.pressed then
-- Can interact if start touch on the button -- Can interact if start touch on the button
instance.can_action = true self.can_action = true
return true return true
end end
if action.released then if action.released then
set_hover(instance, false) set_hover(self, false)
return on_button_release(instance) return on_button_release(self)
else else
set_hover(instance, true) set_hover(self, true)
end end
return not instance.disabled return not self.disabled
else else
-- Can't interact, if touch outside of button -- Can't interact, if touch outside of button
instance.can_action = false self.can_action = false
set_hover(instance, false) set_hover(self, false)
return false return false
end end
end end
function M.on_swipe(instance) function M.on_swipe(self)
-- unhover button if start swipe -- unhover button if start swipe
instance.can_action = false self.can_action = false
set_hover(instance, false) set_hover(self, false)
end end
function M.tap_scale_animation(instance) function M.tap_scale_animation(self)
ui_animate.scale_to(instance, instance.anim_node, instance.scale_to, ui_animate.scale_to(self, self.anim_node, self.scale_to,
function() function()
if instance.back_anim then if self.back_anim then
instance.back_anim(instance) self.back_anim(self)
end end
end end
) )
end end
function M.back_scale_animation(instance) function M.back_scale_animation(self)
ui_animate.scale_to(instance, instance.anim_node, instance.scale_from) ui_animate.scale_to(self, self.anim_node, self.scale_from)
end end
function M.deactivate(instance, is_animate, callback) function M.deactivate(self, is_animate, callback)
instance.disabled = true self.disabled = true
if is_animate then if is_animate then
local counter = 0 local counter = 0
local clbk = function() local clbk = function()
counter = counter + 1 counter = counter + 1
if counter == 3 and callback then if counter == 3 and callback then
callback(instance.parent.parent) callback(self.parent.parent)
end end
end end
ui_animate.color(instance, instance.node, M.DEFAULT_DEACTIVATE_COLOR, ui_animate.color(self, self.node, M.DEFAULT_DEACTIVATE_COLOR,
clbk, M.DEFAUL_ACTIVATION_TIME, 0, gui.EASING_OUTBOUNCE) clbk, M.DEFAUL_ACTIVATION_TIME, 0, gui.EASING_OUTBOUNCE)
ui_animate.scale_y_from_to(instance, instance.node, M.DEFAULT_ACTIVATE_SCALE.x, ui_animate.scale_y_from_to(self, self.node, M.DEFAULT_ACTIVATE_SCALE.x,
M.DEFAULT_DEACTIVATE_SCALE.x, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) M.DEFAULT_DEACTIVATE_SCALE.x, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
ui_animate.scale_x_from_to(instance, instance.node, M.DEFAULT_ACTIVATE_SCALE.y, ui_animate.scale_x_from_to(self, self.node, M.DEFAULT_ACTIVATE_SCALE.y,
M.DEFAULT_DEACTIVATE_SCALE.y, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) M.DEFAULT_DEACTIVATE_SCALE.y, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
else else
gui.set_color(instance.node, M.DEFAULT_DEACTIVATE_COLOR) gui.set_color(self.node, M.DEFAULT_DEACTIVATE_COLOR)
gui.set_scale(instance.node, M.DEFAULT_DEACTIVATE_SCALE) gui.set_scale(self.node, M.DEFAULT_DEACTIVATE_SCALE)
if callback then if callback then
callback(instance.parent.parent) callback(self.parent.parent)
end end
end end
end end
function M.activate(instance, is_animate, callback) function M.activate(self, is_animate, callback)
if is_animate then if is_animate then
local counter = 0 local counter = 0
local clbk = function() local clbk = function()
counter = counter + 1 counter = counter + 1
if counter == 3 then if counter == 3 then
instance.disabled = false self.disabled = false
if callback then if callback then
callback(instance.parent.parent) callback(self.parent.parent)
end end
end end
end end
ui_animate.color(instance, instance.node, ui_animate.TINT_SHOW, ui_animate.color(self, self.node, ui_animate.TINT_SHOW,
clbk, M.DEFAUL_ACTIVATION_TIME, 0, gui.EASING_OUTBOUNCE) clbk, M.DEFAUL_ACTIVATION_TIME, 0, gui.EASING_OUTBOUNCE)
ui_animate.scale_y_from_to(instance, instance.node, M.DEFAULT_DEACTIVATE_SCALE.x, ui_animate.scale_y_from_to(self, self.node, M.DEFAULT_DEACTIVATE_SCALE.x,
M.DEFAULT_ACTIVATE_SCALE.x, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) M.DEFAULT_ACTIVATE_SCALE.x, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
ui_animate.scale_x_from_to(instance, instance.node, M.DEFAULT_DEACTIVATE_SCALE.y, ui_animate.scale_x_from_to(self, self.node, M.DEFAULT_DEACTIVATE_SCALE.y,
M.DEFAULT_ACTIVATE_SCALE.y, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) M.DEFAULT_ACTIVATE_SCALE.y, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
else else
gui.set_color(instance.node, ui_animate.TINT_SHOW) gui.set_color(self.node, ui_animate.TINT_SHOW)
gui.set_scale(instance.node, M.DEFAULT_ACTIVATE_SCALE) gui.set_scale(self.node, M.DEFAULT_ACTIVATE_SCALE)
instance.disabled = false self.disabled = false
if callback then if callback then
callback(instance.parent.parent) callback(self.parent.parent)
end end
end end
end end
--- Set additional node, what need to be clicked on button click --- Set additional node, what need to be clicked on button click
-- Used, if need setup, what button can be clicked only in special zone -- Used, if need setup, what button can be clicked only in special zone
function M.set_ext_zone(instance, zone) function M.set_ext_zone(self, zone)
instance.ext_zone = helper.get_node(zone) self.ext_zone = helper.get_node(zone)
end end

View File

@ -7,107 +7,107 @@ local M = {}
-- Allow different node sizes, allow animation with node insert -- Allow different node sizes, allow animation with node insert
function M.init(instance, parent, element, in_row) function M.init(self, parent, element, in_row)
instance.parent = helper.get_node(parent) self.parent = helper.get_node(parent)
instance.nodes = {} self.nodes = {}
instance.offset = vmath.vector3(0) self.offset = vmath.vector3(0)
instance.anchor = vmath.vector3(0.5, 0, 0) self.anchor = vmath.vector3(0.5, 0, 0)
instance.in_row = in_row or 1 self.in_row = in_row or 1
instance.node_size = gui.get_size(helper.get_node(element)) self.node_size = gui.get_size(helper.get_node(element))
instance.border = vmath.vector4(0) self.border = vmath.vector4(0)
instance.border_offset = vmath.vector3(0) self.border_offset = vmath.vector3(0)
end end
local function check_border(instance, pos) local function check_border(self, pos)
local border = instance.border local border = self.border
local size = instance.node_size local size = self.node_size
local W = pos.x - size.x/2 + instance.border_offset.x local W = pos.x - size.x/2 + self.border_offset.x
local E = pos.x + size.x/2 + instance.border_offset.x local E = pos.x + size.x/2 + self.border_offset.x
local N = pos.y + size.y/2 + instance.border_offset.y local N = pos.y + size.y/2 + self.border_offset.y
local S = pos.y - size.y/2 + instance.border_offset.y local S = pos.y - size.y/2 + self.border_offset.y
border.x = math.min(border.x, W) border.x = math.min(border.x, W)
border.y = math.max(border.y, N) border.y = math.max(border.y, N)
border.z = math.max(border.z, E) border.z = math.max(border.z, E)
border.w = math.min(border.w, S) border.w = math.min(border.w, S)
instance.border_offset = vmath.vector3( self.border_offset = vmath.vector3(
(border.x + (border.z - border.x) * instance.anchor.x), (border.x + (border.z - border.x) * self.anchor.x),
(border.y + (border.w - border.y) * instance.anchor.y), (border.y + (border.w - border.y) * self.anchor.y),
0 0
) )
end end
local temp_pos = vmath.vector3(0) local temp_pos = vmath.vector3(0)
local function get_pos(instance, index) local function get_pos(self, index)
local row = math.ceil(index / instance.in_row) - 1 local row = math.ceil(index / self.in_row) - 1
local col = (index - row * instance.in_row) - 1 local col = (index - row * self.in_row) - 1
temp_pos.x = col * (instance.node_size.x + instance.offset.x) - instance.border_offset.x temp_pos.x = col * (self.node_size.x + self.offset.x) - self.border_offset.x
temp_pos.y = -row * (instance.node_size.y + instance.offset.y) - instance.border_offset.y temp_pos.y = -row * (self.node_size.y + self.offset.y) - self.border_offset.y
return temp_pos return temp_pos
end end
local function update_pos(instance) local function update_pos(self)
for i = 1, #instance.nodes do for i = 1, #self.nodes do
local node = instance.nodes[i] local node = self.nodes[i]
gui.set_position(node, get_pos(instance, i)) gui.set_position(node, get_pos(self, i))
end end
end end
function M.set_offset(instance, offset) function M.set_offset(self, offset)
instance.offset = offset self.offset = offset
update_pos(instance) update_pos(self)
end end
function M.set_anchor(instance, anchor) function M.set_anchor(self, anchor)
instance.anchor = anchor self.anchor = anchor
update_pos(instance) update_pos(self)
end end
function M.add(instance, item, index) function M.add(self, item, index)
index = index or (#instance.nodes + 1) index = index or (#self.nodes + 1)
table.insert(instance.nodes, index, item) table.insert(self.nodes, index, item)
gui.set_parent(item, instance.parent) gui.set_parent(item, self.parent)
local pos = get_pos(instance, index) local pos = get_pos(self, index)
check_border(instance, pos) check_border(self, pos)
update_pos(instance) update_pos(self)
end end
function M.get_size(instance) function M.get_size(self)
return vmath.vector3( return vmath.vector3(
instance.border.z - instance.border.x, self.border.z - self.border.x,
instance.border.w - instance.border.y, self.border.w - self.border.y,
0) 0)
end end
function M.get_all_pos(instance) function M.get_all_pos(self)
local result = {} local result = {}
for i = 1, #instance.nodes do for i = 1, #self.nodes do
table.insert(result, gui.get_position(instance.nodes[i])) table.insert(result, gui.get_position(self.nodes[i]))
end end
return result return result
end end
function M.clear(instance) function M.clear(self)
for i = 1, #instance.nodes do for i = 1, #self.nodes do
gui.delete_node(instance.nodes[i]) gui.delete_node(self.nodes[i])
end end
instance.nodes = {} self.nodes = {}
end end

View File

@ -13,135 +13,135 @@ local PROP_Y = "y"
local PROP_X = "x" local PROP_X = "x"
function M.init(instance, name, key, init_value) function M.init(self, name, key, init_value)
if key ~= PROP_X and key ~= PROP_Y then if key ~= PROP_X and key ~= PROP_Y then
settings.log("progress component: key must be 'x' or 'y'. Passed:", key) settings.log("progress component: key must be 'x' or 'y'. Passed:", key)
key = PROP_X key = PROP_X
end end
instance.prop = hash("scale."..key) self.prop = hash("scale."..key)
instance.key = key self.key = key
instance.node = helper.get_node(name) self.node = helper.get_node(name)
instance.scale = gui.get_scale(instance.node) self.scale = gui.get_scale(self.node)
instance.size = gui.get_size(instance.node) self.size = gui.get_size(self.node)
instance.max_size = instance.size[instance.key] self.max_size = self.size[self.key]
instance.slice = gui.get_slice9(instance.node) self.slice = gui.get_slice9(self.node)
if key == PROP_X then if key == PROP_X then
instance.slice_size = instance.slice.x + instance.slice.z self.slice_size = self.slice.x + self.slice.z
else else
instance.slice_size = instance.slice.y + instance.slice.w self.slice_size = self.slice.y + self.slice.w
end end
instance:set_to(init_value or 1) self:set_to(init_value or 1)
end end
local function check_steps(instance, from, to, exactly) local function check_steps(self, from, to, exactly)
if not instance.steps then if not self.steps then
return return
end end
for i = 1, #instance.steps do for i = 1, #self.steps do
local step = instance.steps[i] local step = self.steps[i]
local v1, v2 = from, to local v1, v2 = from, to
if v1 > v2 then if v1 > v2 then
v1, v2 = v2, v1 v1, v2 = v2, v1
end end
if v1 < step and step < v2 then if v1 < step and step < v2 then
instance.step_callback(instance.parent.parent, step) self.step_callback(self.parent.parent, step)
end end
if exactly and exactly == step then if exactly and exactly == step then
instance.step_callback(instance.parent.parent, step) self.step_callback(self.parent.parent, step)
end end
end end
end end
local function set_bar_to(instance, set_to, is_silence) local function set_bar_to(self, set_to, is_silence)
local prev_value = instance.last_value local prev_value = self.last_value
instance.last_value = set_to self.last_value = set_to
local total_width = set_to * instance.max_size local total_width = set_to * self.max_size
local scale = math.min(total_width / instance.slice_size, 1) local scale = math.min(total_width / self.slice_size, 1)
local size = math.max(total_width, instance.slice_size) local size = math.max(total_width, self.slice_size)
instance.scale[instance.key] = scale self.scale[self.key] = scale
gui.set_scale(instance.node, instance.scale) gui.set_scale(self.node, self.scale)
instance.size[instance.key] = size self.size[self.key] = size
gui.set_size(instance.node, instance.size) gui.set_size(self.node, self.size)
if not is_silence then if not is_silence then
check_steps(instance, prev_value, set_to) check_steps(self, prev_value, set_to)
end end
end end
--- Fill a progress bar and stop progress animation --- Fill a progress bar and stop progress animation
function M.fill(instance) function M.fill(self)
set_bar_to(instance, 1, true) set_bar_to(self, 1, true)
end end
--- To empty a progress bar --- To empty a progress bar
function M.empty(instance) function M.empty(self)
set_bar_to(instance, 0, true) set_bar_to(self, 0, true)
end end
--- Set fill a progress bar to value --- Set fill a progress bar to value
-- @param to - value between 0..1 -- @param to - value between 0..1
function M.set_to(instance, to) function M.set_to(self, to)
set_bar_to(instance, to) set_bar_to(self, to)
end end
function M.get(instance) function M.get(self)
return instance.last_value return self.last_value
end end
function M.set_steps(instance, steps, step_callback) function M.set_steps(self, steps, step_callback)
instance.steps = steps self.steps = steps
instance.step_callback = step_callback self.step_callback = step_callback
end end
--- Start animation of a progress bar --- Start animation of a progress bar
-- @param to - value between 0..1 -- @param to - value between 0..1
-- @param callback - callback when progress ended if need -- @param callback - callback when progress ended if need
function M.to(instance, to, callback) function M.to(self, to, callback)
to = helper.clamp(to, 0, 1) to = helper.clamp(to, 0, 1)
-- cause of float error -- cause of float error
local value = helper.round(to, 5) local value = helper.round(to, 5)
if value ~= instance.last_value then if value ~= self.last_value then
instance.target = value self.target = value
instance.target_callback = callback self.target_callback = callback
else else
if callback then if callback then
callback(instance.parent.parent, to) callback(self.parent.parent, to)
end end
end end
end end
function M.update(instance, dt) function M.update(self, dt)
if instance.target then if self.target then
local prev_value = instance.last_value local prev_value = self.last_value
local step = math.abs(instance.last_value - instance.target) * (p_settings.SPEED*dt) local step = math.abs(self.last_value - self.target) * (p_settings.SPEED*dt)
step = math.max(step, p_settings.MIN_DELTA) step = math.max(step, p_settings.MIN_DELTA)
instance:set_to(helper.step(instance.last_value, instance.target, step)) self:set_to(helper.step(self.last_value, self.target, step))
if instance.last_value == instance.target then if self.last_value == self.target then
check_steps(instance, prev_value, instance.target, instance.target) check_steps(self, prev_value, self.target, self.target)
if instance.target_callback then if self.target_callback then
instance.target_callback(instance.parent.parent, instance.target) self.target_callback(self.parent.parent, self.target)
end end
instance.target = nil self.target = nil
end end
end end
end end

View File

@ -8,76 +8,76 @@ M.interest = {
} }
function M.init(instance, node, value, is_locale, max_width) function M.init(self, node, value, is_locale, max_width)
instance.max_width = max_width self.max_width = max_width
instance.node = helper.get_node(node) self.node = helper.get_node(node)
instance.start_scale = gui.get_scale(instance.node) self.start_scale = gui.get_scale(self.node)
instance.last_color = gui.get_color(instance.node) self.last_color = gui.get_color(self.node)
if is_locale then if is_locale then
instance.text_id = value self.text_id = value
instance:translate() self:translate()
else else
instance:set_to(value or 0) self:set_to(value or 0)
end end
return instance return self
end end
function M.translate(instance) function M.translate(self)
if instance.text_id then if self.text_id then
instance:set_to(settings.get_text(instance.text_id)) self:set_to(settings.get_text(self.text_id))
end end
end end
--- Setup scale x, but can only be smaller, than start text scale --- Setup scale x, but can only be smaller, than start text scale
local function setup_max_width(instance) local function setup_max_width(self)
local metrics = gui.get_text_metrics_from_node(instance.node) local metrics = gui.get_text_metrics_from_node(self.node)
local cur_scale = gui.get_scale(instance.node) local cur_scale = gui.get_scale(self.node)
if metrics.width * cur_scale.x > instance.max_width then if metrics.width * cur_scale.x > self.max_width then
local scale_modifier = instance.max_width / metrics.width local scale_modifier = self.max_width / metrics.width
scale_modifier = math.min(scale_modifier, instance.start_scale.x) scale_modifier = math.min(scale_modifier, self.start_scale.x)
local new_scale = vmath.vector3(scale_modifier, scale_modifier, cur_scale.z) local new_scale = vmath.vector3(scale_modifier, scale_modifier, cur_scale.z)
gui.set_scale(instance.node, new_scale) gui.set_scale(self.node, new_scale)
end end
end end
--- Set text to text field --- Set text to text field
-- @param set_to - set value to text field -- @param set_to - set value to text field
function M.set_to(instance, set_to) function M.set_to(self, set_to)
instance.last_value = set_to self.last_value = set_to
gui.set_text(instance.node, set_to) gui.set_text(self.node, set_to)
if instance.max_width then if self.max_width then
setup_max_width(instance) setup_max_width(self)
end end
end end
--- Set color --- Set color
-- @param color -- @param color
function M.set_color(instance, color) function M.set_color(self, color)
instance.last_color = color self.last_color = color
gui.set_color(instance.node, color) gui.set_color(self.node, color)
end end
--- Set alpha --- Set alpha
-- @param alpha, number [0-1] -- @param alpha, number [0-1]
function M.set_alpha(instance, alpha) function M.set_alpha(self, alpha)
instance.last_color.w = alpha self.last_color.w = alpha
gui.set_color(instance.node, instance.last_color) gui.set_color(self.node, self.last_color)
end end
--- Set scale --- Set scale
-- @param scale -- @param scale
function M.set_scale(instance, scale) function M.set_scale(self, scale)
instance.last_scale = scale self.last_scale = scale
gui.set_scale(instance.node, scale) gui.set_scale(self.node, scale)
end end

View File

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

View File

@ -94,6 +94,7 @@ local function init_grid(self)
-- 4 items per row -- 4 items per row
local grid = self.druid:new_grid("grid_1", prefab, 4) local grid = self.druid:new_grid("grid_1", prefab, 4)
grid:set_anchor(vmath.vector3(0))
grid:set_offset(vmath.vector3(2, 2, 0)) grid:set_offset(vmath.vector3(2, 2, 0))
for i = 1, 16 do for i = 1, 16 do
local node = gui.clone(prefab) local node = gui.clone(prefab)
@ -120,6 +121,7 @@ local function init_grid(self)
local grid3 = self.druid:new_grid("grid_3", prefab, 10) local grid3 = self.druid:new_grid("grid_3", prefab, 10)
grid3:set_anchor(vmath.vector3(0))
grid3:set_offset(vmath.vector3(5, 0, 0)) grid3:set_offset(vmath.vector3(5, 0, 0))
for i = 1, 4 do for i = 1, 4 do
local node = gui.clone(prefab) local node = gui.clone(prefab)