2016-03-07 06:41:47 +00:00
|
|
|
--[[--
|
|
|
|
Widget for displaying progress bar.
|
|
|
|
|
|
|
|
Configurable attributes:
|
|
|
|
|
|
|
|
* width
|
|
|
|
* height
|
|
|
|
* margin_v -- vertical margin for solid infill
|
|
|
|
* margin_h -- horizontal margin for solid infill
|
|
|
|
* radius
|
|
|
|
* bordersize
|
|
|
|
* bordercolor
|
|
|
|
* bgcolor
|
2020-10-31 09:40:36 +00:00
|
|
|
* altcolor -- alternate backrgound color for "alt" pages
|
|
|
|
* rectdim -- dim amount for infill
|
2016-03-07 06:41:47 +00:00
|
|
|
* ticks (list) -- default to nil, use this if you want to insert markers
|
|
|
|
* tick_width
|
2016-04-21 06:16:40 +00:00
|
|
|
* last -- maximum tick, used with ticks
|
2016-03-07 06:41:47 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
local foo_bar = ProgressWidget:new{
|
2017-09-11 08:32:39 +00:00
|
|
|
width = Screen:scaleBySize(400),
|
|
|
|
height = Screen:scaleBySize(10),
|
2016-03-07 06:41:47 +00:00
|
|
|
percentage = 50/100,
|
|
|
|
}
|
|
|
|
UIManager:show(foo_bar)
|
|
|
|
|
|
|
|
]]
|
|
|
|
|
2019-12-06 21:55:37 +00:00
|
|
|
local BD = require("ui/bidi")
|
2014-10-22 13:34:11 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2017-09-11 08:32:39 +00:00
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local Widget = require("ui/widget/widget")
|
|
|
|
local Screen = require("device").screen
|
2013-03-12 17:18:53 +00:00
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local ProgressWidget = Widget:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
width = nil,
|
|
|
|
height = nil,
|
2017-09-11 08:32:39 +00:00
|
|
|
margin_h = Screen:scaleBySize(3),
|
|
|
|
margin_v = Screen:scaleBySize(1),
|
|
|
|
radius = Screen:scaleBySize(2),
|
|
|
|
bordersize = Screen:scaleBySize(1),
|
2014-10-22 13:34:11 +00:00
|
|
|
bordercolor = Blitbuffer.COLOR_BLACK,
|
|
|
|
bgcolor = Blitbuffer.COLOR_WHITE,
|
2020-10-31 09:40:36 +00:00
|
|
|
altcolor = Blitbuffer.COLOR_LIGHT_GRAY,
|
|
|
|
rectdim = 2/3,
|
2014-03-13 13:52:43 +00:00
|
|
|
percentage = nil,
|
2016-03-07 06:41:47 +00:00
|
|
|
ticks = nil,
|
2017-09-11 08:32:39 +00:00
|
|
|
tick_width = Screen:scaleBySize(3),
|
2014-08-09 06:58:44 +00:00
|
|
|
last = nil,
|
2019-12-06 21:55:37 +00:00
|
|
|
fill_from_right = false,
|
|
|
|
allow_mirroring = true,
|
2020-10-31 09:40:36 +00:00
|
|
|
alt = nil, -- table with alternate pages to mark with different color (in the form {{ini1, len1}, {ini2, len2}, ...})
|
2019-12-06 21:55:37 +00:00
|
|
|
_mirroredUI = BD.mirroredUILayout(),
|
2020-11-18 12:06:36 +00:00
|
|
|
_orig_margin_v = nil,
|
|
|
|
_orig_bordersize = nil,
|
2013-03-12 17:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ProgressWidget:getSize()
|
2014-03-13 13:52:43 +00:00
|
|
|
return { w = self.width, h = self.height }
|
2013-03-12 17:18:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ProgressWidget:paintTo(bb, x, y)
|
2014-03-13 13:52:43 +00:00
|
|
|
local my_size = self:getSize()
|
|
|
|
self.dimen = Geom:new{
|
|
|
|
x = x, y = y,
|
|
|
|
w = my_size.w,
|
2014-08-11 03:09:45 +00:00
|
|
|
h = my_size.h
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
2017-04-11 09:44:58 +00:00
|
|
|
if self.dimen.w == 0 or self.dimen.h == 0 then return end
|
|
|
|
|
2016-03-07 06:41:47 +00:00
|
|
|
-- fill background
|
2014-03-13 13:52:43 +00:00
|
|
|
bb:paintRoundedRect(x, y, my_size.w, my_size.h, self.bgcolor, self.radius)
|
2016-03-07 06:41:47 +00:00
|
|
|
-- paint border
|
|
|
|
bb:paintBorder(x, y,
|
|
|
|
my_size.w, my_size.h,
|
|
|
|
self.bordersize, self.bordercolor, self.radius)
|
2020-10-31 09:40:36 +00:00
|
|
|
-- background for alternate pages (e.g. non-linear flows)
|
|
|
|
if self.alt and self.alt[1] ~= nil then
|
|
|
|
local bar_width = (my_size.w-2*self.margin_h)
|
|
|
|
local y_pos = y + self.margin_v + self.bordersize
|
|
|
|
local bar_height = my_size.h-2*(self.margin_v+self.bordersize)
|
|
|
|
for i=1, #self.alt do
|
|
|
|
local tick_x = bar_width*((self.alt[i][1]-1)/self.last)
|
|
|
|
local width = bar_width*(self.alt[i][2]/self.last)
|
|
|
|
width = math.ceil(tick_x + width)
|
|
|
|
tick_x = math.floor(tick_x)
|
|
|
|
width = width - tick_x
|
|
|
|
if self._mirroredUI then
|
|
|
|
tick_x = bar_width - tick_x - width
|
|
|
|
end
|
|
|
|
bb:paintRect(
|
|
|
|
x + self.margin_h + tick_x,
|
|
|
|
y_pos,
|
|
|
|
width,
|
|
|
|
bar_height,
|
|
|
|
self.altcolor)
|
|
|
|
end
|
|
|
|
end
|
2016-03-07 06:41:47 +00:00
|
|
|
-- paint percentage infill
|
2020-10-31 09:40:36 +00:00
|
|
|
-- note that "lightenRect" is misleading, it actualy darkens stuff
|
2018-04-27 20:22:33 +00:00
|
|
|
if self.percentage >= 0 and self.percentage <= 1 then
|
2019-12-06 21:55:37 +00:00
|
|
|
if self.fill_from_right or (self._mirroredUI and not self.fill_from_right) then
|
2020-10-31 09:40:36 +00:00
|
|
|
bb:lightenRect(x+self.margin_h + math.ceil((my_size.w-2*self.margin_h)*(1-self.percentage)),
|
2019-12-06 21:55:37 +00:00
|
|
|
math.ceil(y+self.margin_v+self.bordersize),
|
|
|
|
math.ceil((my_size.w-2*self.margin_h)*self.percentage),
|
|
|
|
my_size.h-2*(self.margin_v+self.bordersize),
|
2020-10-31 09:40:36 +00:00
|
|
|
self.rectdim)
|
2019-12-06 21:55:37 +00:00
|
|
|
else
|
2020-10-31 09:40:36 +00:00
|
|
|
bb:lightenRect(x+self.margin_h,
|
2019-12-06 21:55:37 +00:00
|
|
|
math.ceil(y+self.margin_v+self.bordersize),
|
|
|
|
math.ceil((my_size.w-2*self.margin_h)*self.percentage),
|
2020-10-31 09:40:36 +00:00
|
|
|
my_size.h-2*(self.margin_v+self.bordersize),
|
|
|
|
self.rectdim)
|
2019-12-06 21:55:37 +00:00
|
|
|
end
|
2018-04-27 20:22:33 +00:00
|
|
|
end
|
2020-10-31 09:40:36 +00:00
|
|
|
-- ticks
|
2018-04-27 20:22:33 +00:00
|
|
|
if self.ticks and self.last and self.last > 0 then
|
2016-10-04 17:37:41 +00:00
|
|
|
local bar_width = (my_size.w-2*self.margin_h)
|
|
|
|
local y_pos = y + self.margin_v + self.bordersize
|
|
|
|
local bar_height = my_size.h-2*(self.margin_v+self.bordersize)
|
2020-10-31 09:40:36 +00:00
|
|
|
for i, tick in ipairs(self.ticks) do
|
|
|
|
local tick_x = bar_width*(tick/self.last)
|
2019-12-06 21:55:37 +00:00
|
|
|
if self._mirroredUI then
|
|
|
|
tick_x = bar_width - tick_x
|
|
|
|
end
|
2016-03-07 06:41:47 +00:00
|
|
|
bb:paintRect(
|
2019-12-06 21:55:37 +00:00
|
|
|
x + self.margin_h + tick_x,
|
2016-10-04 17:37:41 +00:00
|
|
|
y_pos,
|
2016-03-07 06:41:47 +00:00
|
|
|
self.tick_width,
|
2016-10-04 17:37:41 +00:00
|
|
|
bar_height,
|
2016-03-07 06:41:47 +00:00
|
|
|
self.bordercolor)
|
|
|
|
end
|
2014-08-09 06:58:44 +00:00
|
|
|
end
|
2013-03-12 17:18:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ProgressWidget:setPercentage(percentage)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.percentage = percentage
|
2013-03-12 17:18:53 +00:00
|
|
|
end
|
|
|
|
|
2019-12-06 21:55:37 +00:00
|
|
|
function ProgressWidget:getPercentageFromPosition(pos)
|
|
|
|
if not pos or not pos.x then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
local width = self.dimen.w - 2*self.margin_h
|
|
|
|
local x = pos.x - self.dimen.x - self.margin_h
|
|
|
|
if x < 0 or x > width then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
if BD.mirroredUILayout() then
|
|
|
|
x = width - x
|
|
|
|
end
|
|
|
|
return x / width
|
|
|
|
end
|
|
|
|
|
2020-11-18 12:06:36 +00:00
|
|
|
function ProgressWidget:setHeight(height)
|
|
|
|
self.height = Screen:scaleBySize(height)
|
|
|
|
-- Adjust vertical margin and border size to ensure there's
|
|
|
|
-- at least 1 pixel left for the actual bar
|
|
|
|
self._orig_margin_v = self._orig_margin_v or self.margin_v
|
|
|
|
self._orig_bordersize = self._orig_bordersize or self.bordersize
|
|
|
|
local margin_v_min = self._orig_margin_v > 0 and 1 or 0
|
|
|
|
local bordersize_min = self._orig_bordersize > 0 and 1 or 0
|
|
|
|
self.margin_v = math.min(self._orig_margin_v, math.floor((self.height - 2*self._orig_bordersize - 1) / 2))
|
|
|
|
self.margin_v = math.max(self.margin_v, margin_v_min)
|
|
|
|
self.bordersize = math.min(self._orig_bordersize, math.floor((self.height - 2*self.margin_v - 1) / 2))
|
|
|
|
self.bordersize = math.max(self.bordersize, bordersize_min)
|
|
|
|
end
|
|
|
|
|
2019-10-10 15:02:28 +00:00
|
|
|
function ProgressWidget:updateStyle(thick, height)
|
|
|
|
if thick then
|
|
|
|
self.margin_h = Screen:scaleBySize(3)
|
|
|
|
self.margin_v = Screen:scaleBySize(1)
|
|
|
|
self.bordersize = Screen:scaleBySize(1)
|
|
|
|
self.radius = Screen:scaleBySize(2)
|
|
|
|
self.bgcolor = Blitbuffer.COLOR_WHITE
|
2020-11-18 12:06:36 +00:00
|
|
|
self._orig_margin_v = nil
|
|
|
|
self._orig_bordersize = nil
|
2019-10-10 15:02:28 +00:00
|
|
|
if height then
|
2020-11-18 12:06:36 +00:00
|
|
|
self:setHeight(height)
|
2019-10-10 15:02:28 +00:00
|
|
|
end
|
2020-11-18 12:06:36 +00:00
|
|
|
else
|
2019-10-10 15:02:28 +00:00
|
|
|
self.margin_h = 0
|
|
|
|
self.margin_v = 0
|
|
|
|
self.bordersize = 0
|
|
|
|
self.radius = 0
|
|
|
|
self.bgcolor = Blitbuffer.COLOR_GRAY
|
|
|
|
self.ticks = nil
|
2020-11-18 12:06:36 +00:00
|
|
|
self._orig_margin_v = nil
|
|
|
|
self._orig_bordersize = nil
|
|
|
|
if height then
|
|
|
|
self:setHeight(height)
|
|
|
|
end
|
2019-10-10 15:02:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return ProgressWidget
|