diff --git a/druid/base/timer.lua b/druid/base/timer.lua index 7802a49..5f2a6ab 100644 --- a/druid/base/timer.lua +++ b/druid/base/timer.lua @@ -1,6 +1,32 @@ +local data = require("druid.data") +local formats = require("druid.helper.formats") +local helper = require("druid.helper.ui_helper") + local M = {} -local formats = require "druid.helper.formats" +M.interest = { + data.LAYOUT_CHANGED, + data.ON_UPDATE +} + +local empty = function() end + +function M.init(instance, seconds_from, seconds_to, callback) + 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 + + if seconds_to - seconds_from == 0 then + instance:set_state(false) + instance.callback(instance.parent.parent, instance) + end + return instance +end + --- Set text to text field -- @param set_to - set value in seconds @@ -9,42 +35,47 @@ function M.set_to(instance, set_to) gui.set_text(instance.node, formats.second_string_min(set_to)) end + --- Called when layout updated (rotate for example) function M.on_layout_updated(instance) M.set_to(instance, instance.last_value) end + --- Called when update -- @param is_on - boolean is timer on -function M.set_work_mode(instance, is_on) +function M.set_state(instance, is_on) instance.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.second_from = from - instance.seconds_counter = from - instance.seconds_temp = 0 - instance.seconds_to = to - instance.second_step = from < to and 1 or - 1 - M.set_work_mode(instance, true) + instance.from = from + instance.value = from + instance.temp = 0 + instance.target = to + M.set_state(instance, true) M.set_to(instance, from) end + --- Called when update -- @param dt - delta time -function M.on_updated(instance, dt) +function M.update(instance, dt) if instance.is_on then - instance.seconds_temp = instance.seconds_temp + dt - if instance.seconds_temp > 1 then - instance.seconds_temp = instance.seconds_temp - 1 - instance.seconds_counter = instance.seconds_counter + instance.second_step - M.set_to(instance, instance.seconds_counter) - if instance.seconds_counter == instance.seconds_to then - instance.is_on = false - instance.callback(instance) + instance.temp = instance.temp + dt + local dist = math.min(1, math.abs(instance.value - instance.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) end end end diff --git a/druid/components/button.lua b/druid/components/button.lua deleted file mode 100644 index 9160de6..0000000 --- a/druid/components/button.lua +++ /dev/null @@ -1,119 +0,0 @@ -local M = {} - -local ui_animate = require "druid.helper.druid_animate" - -M.DEFAULT_SCALE_CHANGE = vmath.vector3(-0.05, - 0.1, 1) -M.DEFAULT_POS_CHANGE = vmath.vector3(0, - 10, 0) -M.DEFAULT_MOVE_SPEED = 5 -M.DEFAULT_ALPHA_DOWN = 0.8 -M.DEFAULT_TIME_ANIM = 0.1 -M.DEFAULT_DEACTIVATE_COLOR = vmath.vector4(0, 0, 0, 0) -M.DEFAULT_DEACTIVATE_SCALE = vmath.vector3(0.8, 0.9, 1) -M.DEFAULT_ACTIVATE_SCALE = vmath.vector3(1, 1, 1) -M.DEFAUL_ACTIVATION_TIME = 0.2 - ---- Set text to text field --- @param action_id - input action id --- @param action - input action -function M.on_input(instance, action_id, action) - if gui.is_enabled(instance.node) and gui.pick_node(instance.node, action.x, action.y) then - if not instance.disabled then - instance.tap_anim(instance) - return true - else - instance.sound_disable() - return false - end - end - return false -end - -function M.tap_scale_animation(instance) - ui_animate.scale_to(instance, instance.anim_node, instance.scale_to, - function() - if instance.back_anim then - instance.back_anim(instance) - end - instance.sound() - instance.callback(instance.parent.parent, instance.params, instance) - end - ) -end - -function M.back_scale_animation(instance) - ui_animate.scale_to(instance, instance.anim_node, instance.scale_from) -end - -function M.tap_tab_animation(instance, force) - ui_animate.alpha(instance, instance.anim_node, M.DEFAULT_ALPHA_DOWN, nil, M.DEFAULT_TIME_ANIM) - ui_animate.fly_to(instance, instance.anim_node, instance.pos + M.DEFAULT_POS_CHANGE, M.DEFAULT_MOVE_SPEED) - ui_animate.scale_to(instance, instance.anim_node, instance.scale_to, - function() - if instance.back_anim then - instance.back_anim(instance) - end - instance.callback(instance.parent.parent, instance.params, force) - end - ) -end - -function M.back_tab_animation(instance) - ui_animate.alpha(instance, instance.anim_node, 1, nil, M.DEFAULT_TIME_ANIM) - ui_animate.fly_to(instance, instance.anim_node, instance.pos, M.DEFAULT_MOVE_SPEED) - ui_animate.scale_to(instance, instance.anim_node, instance.scale_from) -end - -function M.deactivate(instance, is_animate, callback) - instance.disabled = true - if is_animate then - local counter = 0 - local clbk = function() - counter = counter + 1 - if counter == 3 and callback then - callback(instance.parent.parent) - end - end - ui_animate.color(instance, instance.node, M.DEFAULT_DEACTIVATE_COLOR, clbk, M.DEFAUL_ACTIVATION_TIME, 0, - gui.EASING_OUTBOUNCE) - ui_animate.scale_y_from_to(instance, instance.node, M.DEFAULT_ACTIVATE_SCALE.x, 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, M.DEFAULT_DEACTIVATE_SCALE.y, clbk, - M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) - else - gui.set_color(instance.node, M.DEFAULT_DEACTIVATE_COLOR) - gui.set_scale(instance.node, M.DEFAULT_DEACTIVATE_SCALE) - if callback then - callback(instance.parent.parent) - end - end -end - -function M.activate(instance, is_animate, callback) - if is_animate then - local counter = 0 - local clbk = function() - counter = counter + 1 - if counter == 3 then - instance.disabled = false - if callback then - callback(instance.parent.parent) - end - end - end - ui_animate.color(instance, instance.node, ui_animate.TINT_SHOW, clbk, M.DEFAUL_ACTIVATION_TIME, 0, - gui.EASING_OUTBOUNCE) - ui_animate.scale_y_from_to(instance, instance.node, M.DEFAULT_DEACTIVATE_SCALE.x, 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, M.DEFAULT_ACTIVATE_SCALE.y, clbk, - M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) - else - gui.set_color(instance.node, ui_animate.TINT_SHOW) - gui.set_scale(instance.node, M.DEFAULT_ACTIVATE_SCALE) - instance.disabled = false - if callback then - callback(instance.parent.parent) - end - end -end - -return M diff --git a/druid/components/counter.lua b/druid/components/counter.lua deleted file mode 100644 index 35b1389..0000000 --- a/druid/components/counter.lua +++ /dev/null @@ -1,67 +0,0 @@ -local M = {} - -local text_field = require "druid.components.text_field" - -local FRAMES = 60 - ---- Bounce text field -M.bounce = text_field.bounce - ---- Set text to text field --- @param set_to - set value to text field -M.set_to = text_field.set_to - ---- Set color --- @param color -M.set_color = text_field.set_color - ---- Set scale --- @param scale -M.set_scale = text_field.set_scale - ---- Called when layout updated (rotate for example) -function M.on_layout_updated(instance) - text_field.on_layout_updated(instance) - if instance.last_value then - text_field.set_to(instance, instance.last_value) - end -end - ---- Animate counter --- @param set_to - set value to text field -function M.animate_to(instance, set_to, frames) - if set_to == instance.last_value then - text_field.set_to(instance, set_to) - elseif not instance.is_animate then - frames = frames or FRAMES - instance.end_anim_value = set_to - local diff = set_to - instance.last_value - instance.anim_step = math.floor((set_to - instance.last_value) / frames) - if diff ~= 0 and instance.anim_step == 0 then - instance.anim_step = diff > 0 and 1 or - 1 - end - instance.is_animate = true - else - instance.end_anim_value = set_to - end -end - ---- Called when update --- @param dt - delta time -function M.on_updated(instance, dt) - if instance.is_animate then - instance.last_value = instance.last_value + instance.anim_step - text_field.set_to(instance, instance.last_value) - if not instance.is_in_bounce then - instance.is_in_bounce = true - text_field.bounce(instance, function() instance.is_in_bounce = false end) - end - if instance.anim_step > 0 and instance.last_value >= instance.end_anim_value or - instance.anim_step < 0 and instance.last_value <= instance.end_anim_value then - instance.is_animate = false - text_field.set_to(instance, instance.end_anim_value) - end - end -end - -return M diff --git a/druid/components/flying_particles.lua b/druid/components/flying_particles.lua deleted file mode 100644 index 5a63cb3..0000000 --- a/druid/components/flying_particles.lua +++ /dev/null @@ -1,52 +0,0 @@ -local M = {} - -local ui_animate = require "druid.helper.druid_animate" - -local function fly_to(instance, pos_from, speed, callback) - local pos_to = instance.get_pos_func() - instance.last_speed = speed - instance.last_callback = callback - - instance.last_particle = instance.last_particle + 1 - if instance.last_particle > #instance.fly_particles then - instance.last_particle = 1 - end - local fly_particle = instance.fly_particles[instance.last_particle] - if pos_from then - gui.set_position(fly_particle, pos_from) - end - gui.play_particlefx(fly_particle) - instance.is_anim = true - ui_animate.fly_to(nil, fly_particle, pos_to, speed, - function() - instance.is_anim = false - gui.stop_particlefx(fly_particle) - if callback then - callback(instance.parent.parent) - instance.last_callback = nil - end - end, - 0, gui.EASING_INSINE) -end - ---- Start animation of a flying particles --- @param pos_from - fly from this position --- @param speed - speed of flying --- @param callback - callback when progress ended if need -function M.fly_to(instance, pos_from, speed, callback) - fly_to(instance, pos_from, speed, callback) -end - ---- Called when layout updated (rotate for example) -function M.on_layout_updated(instance) - if instance.is_anim then - instance.last_particle = instance.last_particle - 1 - if instance.last_particle < 1 then - instance.last_particle = #instance.fly_particles - end - fly_to(instance, nil, instance.last_speed, instance.last_callback) - end -end - - -return M diff --git a/druid/components/image.lua b/druid/components/image.lua deleted file mode 100644 index cfdf6db..0000000 --- a/druid/components/image.lua +++ /dev/null @@ -1,46 +0,0 @@ -local M = {} - -local ui_animate = require "druid.helper.druid_animate" - ---- Bounce image -function M.bounce(instance) - gui.set_scale(instance.node, instance.scale_from) - ui_animate.bounce(nil, instance.node, instance.scale_to) -end - ---- Set image anim --- @param set_to - index of animation or animation name -function M.set_to(instance, set_to) - instance.last_value = set_to - gui.play_flipbook(instance.node, instance.anim_table and instance.anim_table[set_to] or set_to) -end - ---- Set position to the image --- @param pos - set position of the image -function M.set_pos(instance, pos) - instance.last_pos = pos - gui.set_position(instance.node, pos) -end - ---- Set tint to the image --- @param color - set color of the image -function M.set_color(instance, color) - instance.last_color = color - gui.set_color(instance.node, color) -end - ---- Called when layout updated (rotate for example) -function M.on_layout_updated(instance) - if instance.last_value then - M.set_to(instance, instance.last_value) - end - if instance.last_pos then - M.set_pos(instance, instance.last_pos) - end - if instance.last_color then - M.set_color(instance, instance.last_color) - end -end - - -return M diff --git a/druid/components/pie_progress_bar.lua b/druid/components/pie_progress_bar.lua deleted file mode 100644 index ec8533b..0000000 --- a/druid/components/pie_progress_bar.lua +++ /dev/null @@ -1,51 +0,0 @@ -local M = {} - -local FULL_FILL = 360 - -local function set_bar_to(instance, set_to) - instance.last_value = set_to - gui.cancel_animation(instance.node, gui.PROP_FILL_ANGLE) - gui.set_fill_angle(instance.node, FULL_FILL * set_to) -end - ---- Fill a pie progress bar and stop progress animation -function M.fill_bar(instance) - set_bar_to(instance, 1) -end - ---- To empty a pie progress bar -function M.empty_bar(instance) - set_bar_to(instance, 0) -end - ---- Set fill a pie progress bar to value --- @param to - value between 0..1 -function M.set_to(instance, to) - set_bar_to(instance, to) -end - ---- Start animation of a pie progress bar --- @param to - value between 0..1 --- @param duration - time of animation --- @param callback - callback when progress ended if need -function M.start_progress_to(instance, to, duration, callback) - instance.is_anim = true - instance.last_value = to - gui.animate(instance.node, gui.PROP_FILL_ANGLE, FULL_FILL * to, gui.EASING_LINEAR, duration, 0, - function() - instance.is_anim = false - if callback then - callback(instance.parent.parent) - end - end - ) -end - ---- Called when layout updated (rotate for example) -function M.on_layout_updated(instance) - if not instance.is_anim then - set_bar_to(instance, instance.last_value) - end -end - -return M diff --git a/druid/components/progress_bar.lua b/druid/components/progress_bar.lua deleted file mode 100644 index bb60693..0000000 --- a/druid/components/progress_bar.lua +++ /dev/null @@ -1,93 +0,0 @@ -local M = {} - -local function set_bar_to(instance, set_to) - instance.last_value = set_to - gui.cancel_animation(instance.node, instance.prop) - instance.scale[instance.key] = set_to - gui.set_scale(instance.node, instance.scale) -end - -local function circle_anim(instance, full, steps, num, full_duration, callback) - local duration = (math.abs(steps[num - 1] - steps[num]) / full) * full_duration - local to = steps[num] - gui.animate(instance.node, instance.prop, to, gui.EASING_LINEAR, duration, 0, - function() - callback(num, callback) - end - ) -end - ---- Fill a progress bar and stop progress animation -function M.fill_bar(instance) - set_bar_to(instance, 1) -end - ---- To empty a progress bar -function M.empty_bar(instance) - set_bar_to(instance, 0) -end - ---- Set fill a progress bar to value --- @param to - value between 0..1 -function M.set_to(instance, to) - set_bar_to(instance, to) -end - ---- Start animation of a progress bar --- @param to - value between 0..1 --- @param duration - time of animation --- @param callback - callback when progress ended if need --- @param callback_values - whitch values should callback -function M.start_progress_to(instance, to, duration, callback, callback_values) - instance.is_anim = true - local steps - if callback_values then - steps = {instance.last_value} - if instance.last_value > to then - table.sort(callback_values, function(a, b) return a > b end) - else - table.sort(callback_values, function(a, b) return a < b end) - end - for i, v in ipairs(callback_values) do - if (instance.last_value > v and to < v) or (instance.last_value < v and to > v) then - steps[#steps + 1] = v - end - end - steps[#steps + 1] = to - end - if not steps then - gui.animate(instance.node, instance.prop, to, gui.EASING_LINEAR, duration, 0, - function() - set_bar_to(instance, to) - instance.is_anim = false - if callback then - callback(instance.parent.parent, to) - end - end - ) - else - local full = math.abs(steps[1] - steps[#steps]) - local _callback = function (num, _callback) - if num == #steps then - set_bar_to(instance, steps[num]) - instance.is_anim = false - callback(instance.parent.parent, steps[num]) - else - callback(instance.parent.parent, steps[num]) - num = num + 1 - circle_anim(instance, full, steps, num, duration, _callback) - end - end - circle_anim(instance, full, steps, 2, duration, _callback) - end -end - ---- Called when layout updated (rotate for example) -function M.on_layout_updated(instance) - if not instance.is_anim then - set_bar_to(instance, instance.last_value) - end -end - - -return M diff --git a/druid/components/scrolling_box.lua b/druid/components/scrolling_box.lua deleted file mode 100644 index 4982194..0000000 --- a/druid/components/scrolling_box.lua +++ /dev/null @@ -1,241 +0,0 @@ -local M = {} - -local druid_input = require "druid.helper.druid_input" -local ui_animate = require "druid.helper.druid_animate" - -M.START = hash("START") -M.FINISH = hash("FINISH") - -M.SCROLLING = hash("SCROLLING") -M.INTEREST_MOVE = hash("INTEREST_MOVE") -M.OUT_OF_ZONE_MOVE = hash("OUT_OF_ZONE_MOVE") - -M.BACK_TIME = 0.2 -M.ANIM_TIME = 0.4 - -local function callback(instance, event, type, param) - if instance.callback then - instance.callback(instance.parent.parent, event, type, param) - end -end - -local function checkSwipeDirection(swipe, action) - swipe.xDistance = math.abs(swipe.endX - swipe.beginX) - swipe.yDistance = math.abs(swipe.endY - swipe.beginY) - if swipe.is_x and swipe.xDistance > swipe.yDistance then - if swipe.beginX > swipe.endX then - swipe.totalSwipeDistanceLeft = swipe.beginX - swipe.endX - if swipe.totalSwipeDistanceLeft > swipe.minSwipeDistance then - swipe.speed.x = action.dx * swipe.speed_up_coef.x * swipe.end_move_coef_x - return true - else - return false - end - else - swipe.totalSwipeDistanceRight = swipe.endX - swipe.beginX - if swipe.totalSwipeDistanceRight > swipe.minSwipeDistance then - swipe.speed.x = action.dx * swipe.speed_up_coef.x * swipe.end_move_coef_x - return true - else - return false - end - end - elseif swipe.is_y and swipe.xDistance < swipe.yDistance then - if swipe.beginY > swipe.endY then - swipe.totalSwipeDistanceUp = swipe.beginY - swipe.endY - if swipe.totalSwipeDistanceUp > swipe.minSwipeDistance then - swipe.speed.y = action.dy * swipe.speed_up_coef.y * swipe.end_move_coef_y - return true - else - return false - end - else - swipe.totalSwipeDistanceDown = swipe.endY - swipe.beginY - if swipe.totalSwipeDistanceDown > swipe.minSwipeDistance then - swipe.speed.y = action.dy * swipe.speed_up_coef.y * swipe.end_move_coef_y - return true - else - return false - end - end - end -end - -function lenght(x1, y1, x2, y2) - local a, b = x1 - x2, y1 - y2 - return math.sqrt(a * a + b * b) -end - -local function back_move(instance) - if not instance.swipe.end_position_x and not instance.swipe.end_position_y then - if instance.points_of_interest then - local min_index, min_lenght = 0, math.huge - local len - for k, v in pairs(instance.points_of_interest) do - len = lenght(instance.pos.x, instance.pos.y, v.x, v.y) - if len < min_lenght then - min_lenght = len - min_index = k - end - end - instance.swipe.speed.x = 0 - instance.swipe.speed.y = 0 - gui.cancel_animation(instance.node, gui.PROP_POSITION) - instance.swipe.special_move = true - callback(instance, M.START, M.INTEREST_MOVE, instance.points_of_interest[min_index]) - gui.animate(instance.node, gui.PROP_POSITION, instance.points_of_interest[min_index], - gui.EASING_LINEAR, M.ANIM_TIME, 0, - function() - instance.swipe.special_move = false - instance.pos.x = instance.points_of_interest[min_index].x - instance.pos.y = instance.points_of_interest[min_index].y - callback(instance, M.FINISH, M.SCROLLING, instance.pos) - callback(instance, M.FINISH, M.INTEREST_MOVE, instance.pos) - end - ) - else - callback(instance, M.FINISH, M.SCROLLING, instance.pos) - end - end - - if instance.swipe.end_position_x then - local swipe = instance.swipe - swipe.speed.x = 0 - instance.pos.x = swipe.end_position_x - swipe.special_move = true - callback(instance, M.START, M.OUT_OF_ZONE_MOVE, instance.pos) - gui.animate(instance.node, ui_animate.PROP_POS_X, swipe.end_position_x, gui.EASING_INSINE, M.BACK_TIME, 0, - function() - swipe.special_move = false - callback(instance, M.FINISH, M.SCROLLING, instance.pos) - callback(instance, M.FINISH, M.OUT_OF_ZONE_MOVE, instance.pos) - end - ) - swipe.end_position_x = nil - end - if instance.swipe.end_position_y then - local swipe = instance.swipe - swipe.speed.y = 0 - instance.pos.y = swipe.end_position_y - swipe.special_move = true - callback(instance, M.START, M.OUT_OF_ZONE_MOVE, instance.pos) - gui.animate(instance.node, ui_animate.PROP_POS_Y, swipe.end_position_y, gui.EASING_INSINE, M.BACK_TIME, 0, - function() - swipe.special_move = false - callback(instance, M.FINISH, M.SCROLLING, instance.pos) - callback(instance, M.FINISH, M.OUT_OF_ZONE_MOVE, instance.pos) - end - ) - swipe.end_position_y = nil - end -end - ---- Set text to text field --- @param action_id - input action id --- @param action - input action -function M.on_input(instance, action_id, action) - if action_id == druid_input.A_CLICK then - if gui.pick_node(instance.scrolling_zone, action.x, action.y) then - local swipe = instance.swipe - if action.pressed then - swipe.pressed = true - swipe.beginX = action.x - swipe.beginY = action.y - druid_input.is_swipe = false - swipe.end_move_coef_x = 1 - elseif not action.released and not action.pressed and not swipe.special_move then - swipe.endX = action.x - swipe.endY = action.y - local before = swipe.is_swipe - swipe.is_swipe = checkSwipeDirection(swipe, action) - if not before and swipe.is_swipe and not swipe.special_move and not swipe.waiting_for_back_move then - callback(instance, M.START, M.SCROLLING, instance.pos) - end - return swipe.is_swipe or swipe.special_move - elseif action.released then - swipe.beginX = 0 - swipe.beginY = 0 - swipe.endX = 0 - swipe.endY = 0 - swipe.pressed = false - if swipe.waiting_for_back_move then - back_move(instance) - swipe.waiting_for_back_move = false - end - return swipe.is_swipe or swipe.special_move - end - elseif action.released then - instance.swipe.pressed = false - if instance.swipe.waiting_for_back_move then - back_move(instance) - instance.swipe.waiting_for_back_move = false - end - end - end -end - ---- Called when update --- @param dt - delta time -function M.on_updated(instance, dt) - if instance.swipe.speed.x ~= 0 or instance.swipe.speed.y ~= 0 then - local swipe = instance.swipe - instance.pos.x = instance.pos.x + swipe.speed.x - instance.pos.y = instance.pos.y + swipe.speed.y - if instance.pos.x < instance.start_pos.x then - swipe.end_move_coef_x = swipe.back_slow_coef - swipe.end_position_x = instance.start_pos.x - elseif instance.pos.x > instance.maximum.x then - swipe.end_move_coef_x = swipe.back_slow_coef - swipe.end_position_x = instance.maximum.x - else - swipe.end_move_coef_x = 1 - swipe.end_position_x = nil - end - if instance.pos.y < instance.start_pos.y then - swipe.end_move_coef_y = swipe.back_slow_coef - swipe.end_position_y = instance.start_pos.y - elseif instance.pos.y > instance.maximum.y then - swipe.end_move_coef_y = swipe.back_slow_coef - swipe.end_position_y = instance.maximum.y - else - swipe.end_move_coef_y = 1 - swipe.end_position_y = nil - end - gui.set_position(instance.node, instance.pos) - swipe.speed.x = swipe.speed.x / swipe.speed_down_coef * swipe.end_move_coef_x - swipe.speed.y = swipe.speed.y / swipe.speed_down_coef * swipe.end_move_coef_y - if swipe.speed.x < swipe.min_speed and swipe.speed.x > - swipe.min_speed then - swipe.speed.x = 0 - if not swipe.pressed then - back_move(instance) - else - swipe.waiting_for_back_move = true - end - if swipe.speed.y < swipe.min_speed and swipe.speed.y > - swipe.min_speed then - swipe.speed.y = 0 - end - if swipe.speed.y == 0 and swipe.speed.x == 0 then - swipe.is_swipe = false - end - end - end -end - ---- Scroll position to --- @param pos - positon for set --- @param is_animate - is animated set -function M.scroll_to(instance, pos, is_animate, cb, time_scrolling) - local time = is_animate and M.ANIM_TIME or 0 - time = time_scrolling or time - instance.pos.x = pos.x - instance.pos.y = pos.y - gui.animate(instance.node, gui.PROP_POSITION, instance.pos, gui.EASING_INSINE, time, 0, - function() - if cb then - cb(instance.parent.parent) - end - end - ) -end - -return M diff --git a/druid/components/spine_anim.lua b/druid/components/spine_anim.lua deleted file mode 100644 index 40861c2..0000000 --- a/druid/components/spine_anim.lua +++ /dev/null @@ -1,62 +0,0 @@ -local M = {} - ---- Set animation scene --- @param scene - animations scene -function M.set_scene(instance, scene) - instance.last_scene = scene - gui.set_spine_scene(instance.node, scene) -end - - ---- Set idle animation --- @param anim - idle animation name or index in idle table --- @param properties - properties of the animation -function M.play_idle(instance, anim, properties) - if not anim then - return - end - anim = (instance.idle_table and instance.idle_table[anim]) and instance.idle_table[anim] or anim - instance.last_value = anim - properties = properties or {} - gui.play_spine_anim(instance.node, anim, gui.PLAYBACK_LOOP_FORWARD, properties) -end - ---- Set active animation --- @param anim - active animation name or index in active table --- @param callback - call when animation done --- @param idle_after - set idle after active anim -function M.play_active(instance, anim, callback, idle_after) - instance.is_play_now = true - anim = instance.active_table and instance.active_table[anim] or anim - instance.last_value = anim - instance.callback = callback - M.play_idle(instance, idle_after) - gui.play_spine_anim(instance.node, anim, gui.PLAYBACK_ONCE_FORWARD, {}, - function() - M.play_idle(instance, idle_after) - instance.is_play_now = false - if callback then - callback(instance.parent.parent) - end - end - ) -end - ---- Called when layout updated (rotate for example) -function M.on_layout_updated(instance) - if instance.last_scene then - M.set_scene(instance, instance.last_scene) - end - if instance.last_value then - M.play_idle(instance, instance.last_value) - end - if instance.is_play_now then - instance.is_play_now = false - if instance.callback then - instance.callback(instance.parent.parent) - end - end -end - - -return M diff --git a/druid/components/tab_page.lua b/druid/components/tab_page.lua deleted file mode 100644 index f6771ce..0000000 --- a/druid/components/tab_page.lua +++ /dev/null @@ -1,92 +0,0 @@ ---[[ Single tab screen module. Assumed to be used with Tab Bar module. - --- Create tab screen with parental gui node: -self.tab = tab.create "tab_bkg" - --- Show and hide tab manually: -self.tab.slide_in(self, vmath.vector3(0, -540, 0)) -self.tab.slide_out(self, vmath.vector3(0, -540, 0)) - --- Or receive show and hide messages: -function on_message(self, message_id, message, sender) - self.tab.on_message(self, message_id, message) -end - -]] - -local M = {} -M.T_SLIDE_IN = hash("t_slide_in") -M.T_SLIDE_OUT = hash("t_slide_out") - -M.STATE_START = hash("state_start") -M.STATE_FINISH = hash("state_finish") - -local ENABLE = hash("enable") -local DISABLE = hash("disable") -local PATH_COMP = "#" - -M.DEFAULT_EASING = gui.EASING_INOUTQUAD -M.DEFAULT_DURATION = 0.3 - -function M.slide_in(instance, out_pos, is_force) - msg.post(PATH_COMP, ENABLE) - if instance.callback then - instance.callback(instance.parent.parent, instance, M.T_SLIDE_IN, M.STATE_START) - end - if is_force then - gui.set_position(instance.node, instance.in_pos) - if instance.callback then - instance.callback(instance.parent.parent, instance, M.T_SLIDE_IN, M.STATE_FINISH) - end - else - instance.in_action = true - out_pos = out_pos or instance.put_pos - gui.set_position(instance.node, out_pos or instance.out_pos) - gui.animate(instance.node, gui.PROP_POSITION, instance.in_pos, instance.easing, instance.duration, 0, - function() - instance.in_action = false - if instance.callback then - instance.callback(instance.parent.parent, instance, M.T_SLIDE_IN, M.STATE_FINISH) - end - end - ) - end -end - -function M.slide_out(instance, out_pos, is_force) - out_pos = out_pos or instance.put_pos - if instance.callback then - instance.callback(instance.parent.parent, instance, M.T_SLIDE_OUT, M.STATE_START) - end - if is_force then - gui.set_position(instance.node, out_pos) - if instance.callback then - instance.callback(instance.parent.parent, instance, M.T_SLIDE_OUT, M.STATE_FINISH) - end - msg.post(PATH_COMP, DISABLE) - else - instance.in_action = true - gui.set_position(instance.node, instance.in_pos) - gui.animate(instance.node, gui.PROP_POSITION, out_pos, instance.easing, instance.duration, 0, - function() - instance.in_action = false - if instance.callback then - instance.callback(instance.parent.parent, instance, M.T_SLIDE_OUT, M.STATE_FINISH) - end - msg.post(PATH_COMP, DISABLE) - end - ) - end -end - -function M.on_message(instance, message_id, message, sender) - if message_id == M.T_SLIDE_IN then - M.slide_in(instance, message.out_pos, message.is_force) - return true - elseif message_id == M.T_SLIDE_OUT then - M.slide_out(instance, message.out_pos, message.is_force) - return true - end -end - -return M diff --git a/druid/components/tabs_container.lua b/druid/components/tabs_container.lua deleted file mode 100644 index 4a45820..0000000 --- a/druid/components/tabs_container.lua +++ /dev/null @@ -1,50 +0,0 @@ -local M = {} - ---local helper = require "modules.render.helper" -local tab_page = require "druid.components.tab_page" - -local DISABLE = hash("disable") - -function M.update_sizes(instance, width) - -- width = width or helper.config_x - instance.left = vmath.vector3(width * - 1, 0, 0) - instance.right = vmath.vector3(width * 1, 0, 0) -end - ---- Called when layout updated (rotate for example) -function M.on_layout_updated(instance, message) - -- local width = helper.settings_x - M.update_sizes(instance, width) -end - -function M.switch_tab(instance, params, force) - if instance.current == params then - return - end - if instance.current then - instance.btns[instance.current.index]:manual_back() - end - local out_pos - if instance.current and instance.current.url then - out_pos = (instance.current and instance.current.index < params.index) and instance.left or instance.right - msg.post(instance.current.url, tab_page.T_SLIDE_OUT, { out_pos = out_pos, is_force = force }) - end - if params and params.url then - out_pos = (instance.current and instance.current.index > params.index) and instance.left or instance.right - msg.post(params.url, tab_page.T_SLIDE_IN, { out_pos = out_pos, is_force = force }) - instance.current = params - end -end - ---- Select current tab manually -function M.select(instance, node_name) - for k, v in pairs(instance.btns) do - if k == instance[node_name].index then - v:tap_anim(true) - else - msg.post(instance[v.name].url, DISABLE) - end - end -end - -return M diff --git a/druid/components/text_field.lua b/druid/components/text_field.lua deleted file mode 100644 index 9d800f6..0000000 --- a/druid/components/text_field.lua +++ /dev/null @@ -1,42 +0,0 @@ -local M = {} - -local ui_animate = require "druid.helper.druid_animate" - ---- Bounce text field -function M.bounce(instance, callback) - gui.set_scale(instance.node, instance.scale_from) - ui_animate.bounce(nil, instance.node, instance.scale_to, callback) -end - ---- Set text to text field --- @param set_to - set value to text field -function M.set_to(instance, set_to) - instance.last_value = set_to - gui.set_text(instance.node, set_to) -end - ---- Set color --- @param color -function M.set_color(instance, color) - instance.last_color = color - gui.set_color(instance.node, color) -end - ---- Set scale --- @param scale -function M.set_scale(instance, scale) - instance.last_scale = scale - gui.set_scale(instance.node, scale) -end - ---- Called when layout updated (rotate for example) -function M.on_layout_updated(instance) - if instance.last_color then - M.set_color(instance, instance.last_color) - end - if instance.last_scale then - M.set_scale(instance, instance.last_scale) - end -end - -return M diff --git a/druid/components/timer.lua b/druid/components/timer.lua deleted file mode 100644 index 7802a49..0000000 --- a/druid/components/timer.lua +++ /dev/null @@ -1,53 +0,0 @@ -local M = {} - -local formats = require "druid.helper.formats" - ---- 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)) -end - ---- Called when layout updated (rotate for example) -function M.on_layout_updated(instance) - M.set_to(instance, instance.last_value) -end - ---- Called when update --- @param is_on - boolean is timer on -function M.set_work_mode(instance, is_on) - instance.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.second_from = from - instance.seconds_counter = from - instance.seconds_temp = 0 - instance.seconds_to = to - instance.second_step = from < to and 1 or - 1 - M.set_work_mode(instance, true) - M.set_to(instance, from) -end - ---- Called when update --- @param dt - delta time -function M.on_updated(instance, dt) - if instance.is_on then - instance.seconds_temp = instance.seconds_temp + dt - if instance.seconds_temp > 1 then - instance.seconds_temp = instance.seconds_temp - 1 - instance.seconds_counter = instance.seconds_counter + instance.second_step - M.set_to(instance, instance.seconds_counter) - if instance.seconds_counter == instance.seconds_to then - instance.is_on = false - instance.callback(instance) - end - end - end -end - -return M diff --git a/druid/druid.lua b/druid/druid.lua index 62c2b6a..057eff1 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -9,9 +9,9 @@ local STRING = "string" local components = { -- basic button = require("druid.base.button"), + timer = require("druid.base.timer"), -- text = require("druid.base.text"), -- android_back = require("druid.base.android_back"), - -- timer = require("druid.base.timer"), } diff --git a/druid/helper/ui_helper.lua b/druid/helper/ui_helper.lua index 89e5aa5..e592a37 100644 --- a/druid/helper/ui_helper.lua +++ b/druid/helper/ui_helper.lua @@ -13,4 +13,12 @@ function M.centrate_text_with_icon(text_node, icon_node) gui.set_position(icon_node, pos_i) end +function M.step(current, target, step) + if current < target then + return math.min(current + step, target) + else + return math.max(target, current - step) + end +end + return M diff --git a/example/example.gui.gui_script b/example/example.gui.gui_script index e7bc075..fa028f4 100644 --- a/example/example.gui.gui_script +++ b/example/example.gui.gui_script @@ -21,6 +21,10 @@ function init(self) self.druid:new_button("button_3", function() print("On button 3") end) + + self.druid:new_timer("text_1", 0.5, 0, function() + print("On timer end") + end) end function update(self, dt)