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:
Maxim Tuprikov 2019-04-03 23:23:06 +03:00 committed by GitHub
parent 6f41f70803
commit d49cd2777e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 1382 additions and 63 deletions

View File

@ -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
View 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

View File

@ -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

View File

@ -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)

View File

@ -12,6 +12,9 @@ M.comps = {
android_back = require("druid.base.android_back"),
text = require("druid.base.text"),
timer = require("druid.base.timer"),
progress = require("druid.base.progress"),
progress_rich = require("druid.rich.progress_rich"),
}

View File

@ -36,6 +36,27 @@ function M.step(current, target, step)
end
function M.clamp(a, min, max)
if min > max then
min, max = max, min
end
if a >= min and a <= max then
return a
elseif a < min then
return min
else
return max
end
end
function M.round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
function M.is_enabled(node)
local is_enabled = gui.is_enabled(node)
local parent = gui.get_parent(node)

View File

@ -0,0 +1,55 @@
local settings = require("druid.settings")
local pr_settings = settings.progress_rich
local M = {}
function M.init(instance, name, red, green, key)
instance.red = instance.parent:new_progress(red, key)
instance.green = instance.parent:new_progress(green, key)
instance.fill = instance.parent:new_progress(name, key)
end
function M.set_to(instance, value)
instance.red:set_to(value)
instance.green:set_to(value)
instance.fill:set_to(value)
end
function M.empty(instance)
instance.red:empty()
instance.green:empty()
instance.fill:empty()
end
function M.to(instance, to, callback)
if instance.timer then
timer.cancel(instance.timer)
instance.timer = nil
end
if instance.fill.last_value < to then
instance.red:to(instance.fill.last_value)
instance.green:to(to, function()
instance.timer = timer.delay(pr_settings.DELAY, false, function()
instance.red:to(to)
instance.fill:to(to, callback)
end)
end)
end
if instance.fill.last_value > to then
instance.green:to(instance.red.last_value)
instance.fill:to(to, function()
instance.timer = timer.delay(pr_settings.DELAY, false, function()
instance.green:to(to)
instance.red:to(to, callback)
end)
end)
end
end
return M

View File

@ -12,6 +12,14 @@ M.button = {
SCALE_CHANGE = vmath.vector3(-0.05, - 0.05, 1),
}
M.progress = {
SPEED = 5, -- progress bar fill rate, more faster
MIN_DELTA = 0.005
}
M.progress_rich = {
DELAY = 1, -- delay in seconds before main fill
}
function M.get_text(name)

File diff suppressed because it is too large Load Diff

View File

@ -4,52 +4,101 @@ local druid_settings = require("druid.settings")
local lang = {
locale_text = "Localized"
}
local start_x = 300
local page_width = 600
local cur_page = 1
local max_page = 2
local function log(self, params)
print(params)
end
local function setup_druid(self)
-- two different way of exernal component regesstration
druid.comps["my_mega_test_comp"] = require "druid.base.text"
druid.register("my_custom_component", {})
druid_settings.is_debug = true
druid_settings.play_sound = function(name)
sound.play("sounds#" .. name)
end
druid_settings.get_text = function(text_id)
return lang[text_id]
end
end
local function change_page(self, delta)
cur_page = cur_page + delta
cur_page = math.max(1, cur_page)
cur_page = math.min(cur_page, max_page)
gui.animate(gui.get_node("center"), "position.x",
start_x - (page_width * (cur_page - 1)), gui.EASING_OUTSINE, 0.1)
end
local function init_pages(self)
self.druid:new_button("prev", change_page, -1)
self.druid:new_button("next", change_page, 1)
end
local function init_buttons(self)
self.druid:new_button("button_1", log, "button 1")
self.druid:new(druid.comps.button, "button_2", log, "button 2")
end
local function init_progress(self)
local val = 0.4
local simple = self.druid:new_progress("simple_fill", "x", val)
local vert = self.druid:new_progress("simple_vert_fill", "y", val)
simple:set_steps({0, 0.3, 0.6, 1}, function(self, step)
print("STEP:", step)
end)
timer.delay(0.4, true, function()
val = val + 0.1
if val > 1 then
simple:empty()
vert:empty()
val = 0
end
simple:to(val)
vert:to(val)
end)
local rich = self.druid:new_progress_rich("rich_fill", "rich_red", "rich_green", "x")
rich:set_to(0.3)
local rich_val = 0.3
self.druid:new_button("rich_add", function()
rich_val = rich_val + 0.1
rich:to(rich_val)
end)
self.druid:new_button("rich_dec", function()
rich_val = rich_val - 0.1
rich:to(rich_val)
end)
end
function init(self)
setup_druid(self)
self.druid = druid.new(self)
self.druid:new_button("button_1", function()
print("On button 1")
end)
--alternative way of component creation
self.druid:new(druid.comps.button, "button_2", function()
print("On button 2")
end)
self.druid:new_button("button_3", function()
print("On button 3")
end)
self.druid:new_android_back(function(self, params)
print("On android back", params)
end, 2)
self.druid:new_text("text_2", "Simple text", false, 400)
self.druid:new_text("text_3", "locale_text", true)
self.druid:new_timer("text_1", 5, 0, function()
print("On timer end")
end)
init_pages(self)
init_buttons(self)
init_progress(self)
self.druid:new_android_back(log, "some")
end
function update(self, dt)

View File

@ -4,6 +4,21 @@ images {
images {
image: "/example/res/green_long.png"
}
margin: 0
extrude_borders: 0
images {
image: "/example/res/progress_back.png"
}
images {
image: "/example/res/progress_green.png"
}
images {
image: "/example/res/progress_red.png"
}
images {
image: "/example/res/progress_yellow.png"
}
images {
image: "/example/res/empty.png"
}
margin: 2
extrude_borders: 2
inner_padding: 0

View File

@ -0,0 +1,18 @@
path_settings {
path: "**"
profile: "Default"
}
profiles {
name: "Default"
platforms {
os: OS_ID_GENERIC
formats {
format: TEXTURE_FORMAT_RGBA
compression_level: BEST
compression_type: COMPRESSION_TYPE_DEFAULT
}
mipmaps: false
max_texture_size: 0
premultiply_alpha: true
}
}

BIN
example/res/empty.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
example/res/progress_back.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
example/res/progress_green.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
example/res/progress_red.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
example/res/progress_yellow.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -5,8 +5,8 @@ main_collection = /example/example.collectionc
shared_state = 1
[display]
width = 400
height = 400
width = 600
height = 900
[project]
title = druid
@ -14,3 +14,6 @@ title = druid
[library]
include_dirs = druid
[graphics]
texture_profiles = /example/res/custom.texture_profiles