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, y = 10,
width = 80, width = 80,
height = 10, height = 10,
lines = {{'fill', {0, 10, 80, 10, 40, 0}}}, lines = {'fill', {0, 10, 80, 10, 40, 0}},
}, },
down = { down = {
@ -16,7 +16,7 @@ local edge_data = {
y = 220, y = 220,
width = 80, width = 80,
height = 10, height = 10,
lines = {{'fill', {0, 0, 80, 0, 40, 10}}}, lines = {'fill', {0, 0, 80, 0, 40, 10}},
}, },
left = { left = {
@ -24,7 +24,7 @@ local edge_data = {
y = 80, y = 80,
width = 10, width = 10,
height = 80, height = 80,
lines = {{'fill', {10, 0, 10, 80, 0, 40}}}, lines = {'fill', {10, 0, 10, 80, 0, 40}},
}, },
right = { right = {
@ -32,7 +32,7 @@ local edge_data = {
y = 80, y = 80,
width = 10, width = 10,
height = 80, 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 self.y = y
end 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() function m:draw()
love.graphics.push('all') love.graphics.push('all')
love.graphics.translate(self.x, self.y) love.graphics.translate(self.x, self.y)
love.graphics.setLineWidth(3) love.graphics.setLineWidth(3)
for _, l in ipairs(self.lines) do if type(self.lines[1]) == 'string' then
local mode = l[1] draw_line(self.lines)
local segs = l[2] else
for _, l in ipairs(self.lines) do
if mode == 'line' then draw_line(l)
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
end end