2023-08-26 17:34:37 -07:00
|
|
|
local dotgrid = {}
|
|
|
|
|
|
|
|
function dotgrid:shuffle(tbl)
|
|
|
|
for i = #tbl, 2, -1 do
|
|
|
|
local j = math.random(i)
|
|
|
|
tbl[i], tbl[j] = tbl[j], tbl[i]
|
|
|
|
end
|
|
|
|
return tbl
|
|
|
|
end
|
|
|
|
|
|
|
|
function dotgrid:new()
|
|
|
|
local dots = wibox.widget.base.make_widget();
|
|
|
|
|
|
|
|
dots.on_color = {1, 0, 0}
|
|
|
|
dots.off_color = {0.5, 0, 0}
|
|
|
|
dots.value = 0
|
|
|
|
dots.max = 100
|
|
|
|
dots.rows = 5
|
|
|
|
dots.cols = 5
|
|
|
|
dots.spacing = 2
|
2023-08-26 19:26:25 -07:00
|
|
|
dots.last_value = 0
|
2023-08-26 17:34:37 -07:00
|
|
|
|
|
|
|
dots.map = {}
|
|
|
|
local idx
|
|
|
|
for i=1, dots.rows*dots.cols do
|
|
|
|
dots.map[i] = false;
|
|
|
|
end
|
|
|
|
--dots.map = dotgrid:shuffle(dots.map)
|
|
|
|
|
|
|
|
|
|
|
|
function dots:fit(context, width, height)
|
|
|
|
local m = math.min(width, height)
|
|
|
|
return m, m
|
|
|
|
end
|
|
|
|
|
|
|
|
function dots:draw(context, cr, width, height)
|
2023-08-26 17:55:11 -07:00
|
|
|
local col_size = math.floor((height - self.spacing * (self.rows-1)) / self.rows)
|
|
|
|
local row_size = math.floor((width - self.spacing * (self.cols-1)) / self.cols)
|
2023-08-26 17:34:37 -07:00
|
|
|
|
|
|
|
-- Compute rounding offsets for better centering
|
2023-08-26 17:55:11 -07:00
|
|
|
local xoffset = math.floor((
|
2023-08-26 17:34:37 -07:00
|
|
|
width - (
|
|
|
|
(col_size * self.cols) +
|
|
|
|
(self.spacing * (self.cols - 1))
|
|
|
|
)
|
2023-08-26 17:55:11 -07:00
|
|
|
) / 2)
|
2023-08-26 17:34:37 -07:00
|
|
|
|
2023-08-26 17:55:11 -07:00
|
|
|
local yoffset = math.floor((
|
2023-08-26 17:34:37 -07:00
|
|
|
height - (
|
|
|
|
(row_size * self.rows) +
|
|
|
|
(self.spacing * (self.rows - 1))
|
|
|
|
)
|
2023-08-26 17:55:11 -07:00
|
|
|
) / 2)
|
2023-08-26 17:34:37 -07:00
|
|
|
|
|
|
|
-- How many dots should be on
|
|
|
|
local on = math.floor((self.value / self.max) * (self.rows * self.cols))
|
|
|
|
|
2023-08-26 19:26:25 -07:00
|
|
|
local flip
|
|
|
|
while self.last_value > on do
|
|
|
|
-- Flip the nth on dot
|
|
|
|
flip = math.random(1, self.last_value)
|
|
|
|
for i=1, dots.rows*dots.cols do
|
|
|
|
if dots.map[i] then
|
|
|
|
if flip == 1 then
|
|
|
|
dots.map[i] = false
|
|
|
|
self.last_value = self.last_value - 1
|
|
|
|
break
|
|
|
|
else
|
|
|
|
flip = flip - 1
|
|
|
|
end
|
|
|
|
end
|
2023-08-26 17:34:37 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-26 19:26:25 -07:00
|
|
|
while self.last_value < on do
|
|
|
|
-- Flip the nth off dot
|
|
|
|
flip = math.random(1, dots.rows*dots.cols - self.last_value)
|
|
|
|
for i=1, dots.rows*dots.cols do
|
|
|
|
if not dots.map[i] then
|
|
|
|
if flip == 1 then
|
|
|
|
dots.map[i] = true
|
|
|
|
self.last_value = self.last_value + 1
|
|
|
|
break
|
|
|
|
else
|
|
|
|
flip = flip - 1
|
2023-08-26 17:34:37 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-26 19:26:25 -07:00
|
|
|
local idx
|
2023-08-26 17:34:37 -07:00
|
|
|
for r=1, self.rows do
|
|
|
|
for c=1, self.cols do
|
|
|
|
idx = c + ((r-1)*self.cols)
|
|
|
|
|
|
|
|
cr:new_path();
|
|
|
|
if self.map[idx] then
|
|
|
|
cr:set_source_rgb(self.on_color[1], self.on_color[2], self.on_color[3])
|
|
|
|
else
|
|
|
|
cr:set_source_rgb(self.off_color[1], self.off_color[2], self.off_color[3])
|
|
|
|
end
|
|
|
|
|
|
|
|
cr:rectangle(
|
|
|
|
(c-1) * (col_size + self.spacing) + xoffset,
|
|
|
|
(r-1) * (row_size + self.spacing) + yoffset,
|
|
|
|
col_size,
|
|
|
|
row_size
|
|
|
|
)
|
|
|
|
cr:fill()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return dots
|
|
|
|
end
|
|
|
|
|
|
|
|
return dotgrid
|