Skip to content

Commit 696d227

Browse files
committed
Make sure layers indices can never be skipped in pd.lua
1 parent 2f88492 commit 696d227

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

pd.lua

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -462,44 +462,41 @@ function pd.Class:canvas_realizedollar(s)
462462
end
463463

464464
function pd.Class:repaint(layer)
465-
if layer == nil or layer <= 1 then
466-
if type(self.paint) == "function" then
467-
local g = _gfx_internal.start_paint(self._object, 1);
468-
if type(self.paint) == "function" and g ~= nil then
469-
self:paint(g);
470-
_gfx_internal.end_paint(g, 1);
465+
local paint_funcs = {}
466+
if type(self.paint) == "function" then
467+
table.insert(paint_funcs, self.paint)
468+
-- Check for layer paint functions
469+
local i = 2
470+
while true do
471+
local paint_layer_method = "paint_layer_" .. tostring(i)
472+
if type(self[paint_layer_method]) == "function" then
473+
table.insert(paint_funcs, self[paint_layer_method])
474+
i = i + 1;
475+
else
476+
break -- Exit the loop when no more paint_layer_X methods are found
477+
end
471478
end
472-
end
473479
end
474-
if layer == nil or layer == 0 then
475-
local i = 2
476-
while true do
477-
local paint_layer_method = "paint_layer_" .. tostring(i)
478-
if type(self[paint_layer_method]) == "function" then
479-
local g = _gfx_internal.start_paint(self._object, i)
480-
if g ~= nil then
481-
self[paint_layer_method](self, g)
482-
_gfx_internal.end_paint(g, i)
483-
i = i + 1
484-
else
485-
break;
486-
end
487-
480+
-- repaint all
481+
if layer == nil or layer == 0 then
482+
for i, paint_fn in ipairs(paint_funcs) do
483+
local g = _gfx_internal.start_paint(self._object, i);
484+
if type(paint_fn) == "function" and g ~= nil then
485+
paint_fn(self, g);
486+
_gfx_internal.end_paint(g, i);
488487
else
489-
break -- Exit the loop when no more paint_layer_X methods are found
488+
break;
490489
end
491490
end
492-
end
493-
if layer ~= nil and layer >= 2 then
494-
local paint_layer_method = "paint_layer_" .. tostring(layer)
495-
if type(self[paint_layer_method]) == "function" then
496-
local g = _gfx_internal.start_paint(self._object, layer)
497-
if g ~= nil then
498-
self[paint_layer_method](self, g)
499-
_gfx_internal.end_paint(g, layer)
500-
end
491+
-- repaint only chosen index
492+
elseif layer <= #paint_funcs then
493+
local g = _gfx_internal.start_paint(self._object, layer);
494+
local paint_fn = paint_funcs[layer]
495+
if type(paint_fn) == "function" and g ~= nil then
496+
paint_fn(self, g);
497+
_gfx_internal.end_paint(g, layer);
501498
end
502-
end
499+
end
503500
end
504501

505502
function pd.Class:get_size()

0 commit comments

Comments
 (0)