Simple tweak: allow single line in lines

This commit is contained in:
백현웅 2025-01-03 20:54:41 +09:00
parent 3179442942
commit 00601ed4a9
2 changed files with 29 additions and 21 deletions

View file

@ -8,7 +8,7 @@ local edge_data = {
y = 10,
width = 80,
height = 10,
lines = {{'fill', {0, 10, 80, 10, 40, 0}}},
lines = {'fill', {0, 10, 80, 10, 40, 0}},
},
down = {
@ -16,7 +16,7 @@ local edge_data = {
y = 220,
width = 80,
height = 10,
lines = {{'fill', {0, 0, 80, 0, 40, 10}}},
lines = {'fill', {0, 0, 80, 0, 40, 10}},
},
left = {
@ -24,7 +24,7 @@ local edge_data = {
y = 80,
width = 10,
height = 80,
lines = {{'fill', {10, 0, 10, 80, 0, 40}}},
lines = {'fill', {10, 0, 10, 80, 0, 40}},
},
right = {
@ -32,7 +32,7 @@ local edge_data = {
y = 80,
width = 10,
height = 80,
lines = {{'fill', {0, 0, 0, 80, 10, 40}}},
lines = {'fill', {0, 0, 0, 80, 10, 40}},
}
}

View file

@ -27,28 +27,36 @@ function m:setpos(x, y)
self.y = y
end
local function draw_line(l)
local mode = l[1]
local segs = l[2]
if mode == 'line' then
love.graphics.line(segs)
elseif mode == 'fill' then
local ts = love.math.triangulate(segs)
love.graphics.setColor(0, 0, 0)
for i, t in ipairs(ts) do
love.graphics.polygon('fill', t)
end
love.graphics.setColor(1, 1, 1)
love.graphics.polygon('line', segs)
else
error(string.format("mode must be one of line or fill, not %s", mode))
end
end
function m:draw()
love.graphics.push('all')
love.graphics.translate(self.x, self.y)
love.graphics.setLineWidth(3)
for _, l in ipairs(self.lines) do
local mode = l[1]
local segs = l[2]
if mode == 'line' then
love.graphics.line(segs)
elseif mode == 'fill' then
local ts = love.math.triangulate(segs)
love.graphics.setColor(0, 0, 0)
for i, t in ipairs(ts) do
love.graphics.polygon('fill', t)
end
love.graphics.setColor(1, 1, 1)
love.graphics.polygon('line', segs)
else
error(string.format("mode must be one of line or fill, not %s", mode))
if type(self.lines[1]) == 'string' then
draw_line(self.lines)
else
for _, l in ipairs(self.lines) do
draw_line(l)
end
end