mirror of
https://github.com/Insality/druid
synced 2025-09-27 18:12:21 +02:00
Feature/progress (#17)
* improve example gui. Add simple pages * return old progress bar with example * base progress with steps, there is bug with step? 1 ~= 1, small delta? * polish progress, check float error case * add callback on end of "to" function, value check * start of green/red in progress * add first version of rich progress bar * make green bar darker * rich bar fixes * add delay, before filling in rich progress * PR fixes * remove dublicate of 'progress_rich'
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
local data = require("druid.data")
|
||||
local ui_animate = require("druid.helper.druid_animate")
|
||||
local settings = require("druid.settings")
|
||||
local helper = require("druid.helper.helper")
|
||||
local helper = require("druid.helper")
|
||||
local b_settings = settings.button
|
||||
|
||||
local M = {}
|
||||
@@ -18,7 +18,7 @@ M.DEFAUL_ACTIVATION_TIME = 0.2
|
||||
function M.init(instance, node, callback, params, anim_node, event)
|
||||
instance.node = helper.get_node(node)
|
||||
instance.event = data.A_TOUCH
|
||||
instance.anim_node = anim_node and gui.get_node(anim_node) or instance.node
|
||||
instance.anim_node = anim_node and helper.get_node(anim_node) or instance.node
|
||||
instance.scale_from = gui.get_scale(instance.anim_node)
|
||||
instance.scale_to = instance.scale_from + b_settings.SCALE_CHANGE
|
||||
instance.scale_hover_to = instance.scale_from + b_settings.HOVER_SCALE
|
||||
|
150
druid/base/progress.lua
Normal file
150
druid/base/progress.lua
Normal file
@@ -0,0 +1,150 @@
|
||||
local data = require("druid.data")
|
||||
local helper = require("druid.helper")
|
||||
local settings = require("druid.settings")
|
||||
local p_settings = settings.progress
|
||||
|
||||
local M = {}
|
||||
|
||||
M.interest = {
|
||||
data.ON_UPDATE,
|
||||
}
|
||||
|
||||
local PROP_Y = "y"
|
||||
local PROP_X = "x"
|
||||
|
||||
|
||||
function M.init(instance, name, key, init_value)
|
||||
if key ~= PROP_X and key ~= PROP_Y then
|
||||
settings.log("progress component: key must be 'x' or 'y'. Passed:", key)
|
||||
key = PROP_X
|
||||
end
|
||||
|
||||
instance.prop = hash("scale."..key)
|
||||
instance.key = key
|
||||
|
||||
instance.node = helper.get_node(name)
|
||||
instance.scale = gui.get_scale(instance.node)
|
||||
instance.size = gui.get_size(instance.node)
|
||||
instance.max_size = instance.size[instance.key]
|
||||
instance.slice = gui.get_slice9(instance.node)
|
||||
if key == PROP_X then
|
||||
instance.slice_size = instance.slice.x + instance.slice.z
|
||||
else
|
||||
instance.slice_size = instance.slice.y + instance.slice.w
|
||||
end
|
||||
|
||||
instance:set_to(init_value or 1)
|
||||
end
|
||||
|
||||
|
||||
local function check_steps(instance, from, to, exactly)
|
||||
if not instance.steps then
|
||||
return
|
||||
end
|
||||
|
||||
for i = 1, #instance.steps do
|
||||
local step = instance.steps[i]
|
||||
local v1, v2 = from, to
|
||||
if v1 > v2 then
|
||||
v1, v2 = v2, v1
|
||||
end
|
||||
|
||||
if v1 < step and step < v2 then
|
||||
instance.step_callback(instance.parent.parent, step)
|
||||
end
|
||||
if exactly and exactly == step then
|
||||
instance.step_callback(instance.parent.parent, step)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local function set_bar_to(instance, set_to, is_silence)
|
||||
local prev_value = instance.last_value
|
||||
instance.last_value = set_to
|
||||
|
||||
local total_width = set_to * instance.max_size
|
||||
|
||||
local scale = math.min(total_width / instance.slice_size, 1)
|
||||
local size = math.max(total_width, instance.slice_size)
|
||||
|
||||
instance.scale[instance.key] = scale
|
||||
gui.set_scale(instance.node, instance.scale)
|
||||
instance.size[instance.key] = size
|
||||
gui.set_size(instance.node, instance.size)
|
||||
|
||||
if not is_silence then
|
||||
check_steps(instance, prev_value, set_to)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--- Fill a progress bar and stop progress animation
|
||||
function M.fill(instance)
|
||||
set_bar_to(instance, 1, true)
|
||||
end
|
||||
|
||||
|
||||
--- To empty a progress bar
|
||||
function M.empty(instance)
|
||||
set_bar_to(instance, 0, true)
|
||||
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
|
||||
|
||||
|
||||
function M.get(instance)
|
||||
return instance.last_value
|
||||
end
|
||||
|
||||
|
||||
function M.set_steps(instance, steps, step_callback)
|
||||
instance.steps = steps
|
||||
instance.step_callback = step_callback
|
||||
end
|
||||
|
||||
|
||||
--- Start animation of a progress bar
|
||||
-- @param to - value between 0..1
|
||||
-- @param callback - callback when progress ended if need
|
||||
function M.to(instance, to, callback)
|
||||
to = helper.clamp(to, 0, 1)
|
||||
-- cause of float error
|
||||
local value = helper.round(to, 5)
|
||||
if value ~= instance.last_value then
|
||||
instance.target = value
|
||||
instance.target_callback = callback
|
||||
else
|
||||
if callback then
|
||||
callback(instance.parent.parent, to)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function M.update(instance, dt)
|
||||
if instance.target then
|
||||
local prev_value = instance.last_value
|
||||
local step = math.abs(instance.last_value - instance.target) * (p_settings.SPEED*dt)
|
||||
step = math.max(step, p_settings.MIN_DELTA)
|
||||
instance:set_to(helper.step(instance.last_value, instance.target, step))
|
||||
|
||||
if instance.last_value == instance.target then
|
||||
check_steps(instance, prev_value, instance.target, instance.target)
|
||||
|
||||
if instance.target_callback then
|
||||
instance.target_callback(instance.parent.parent, instance.target)
|
||||
end
|
||||
|
||||
instance.target = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return M
|
@@ -1,11 +1,10 @@
|
||||
local data = require("druid.data")
|
||||
local settings = require("druid.settings")
|
||||
local helper = require("druid.helper.helper")
|
||||
local helper = require("druid.helper")
|
||||
|
||||
local M = {}
|
||||
M.interest = {
|
||||
data.TRANSLATABLE,
|
||||
data.LAYOUT_CHANGED
|
||||
}
|
||||
|
||||
|
||||
@@ -82,15 +81,4 @@ function M.set_scale(instance, 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
|
@@ -1,10 +1,9 @@
|
||||
local data = require("druid.data")
|
||||
local formats = require("druid.helper.formats")
|
||||
local helper = require("druid.helper.helper")
|
||||
local helper = require("druid.helper")
|
||||
|
||||
local M = {}
|
||||
M.interest = {
|
||||
data.LAYOUT_CHANGED,
|
||||
data.ON_UPDATE
|
||||
}
|
||||
|
||||
@@ -36,12 +35,6 @@ function M.set_to(instance, 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_state(instance, is_on)
|
||||
|
Reference in New Issue
Block a user