crosstable.love/0003-draw_data

116 lines
3.3 KiB
Plaintext

draw_data = function()
love.graphics.setLineWidth(3)
local namepx, py = 5, 5
love.graphics.setFont(love.graphics.newFont(Font_size))
local c = love.graphics.getFont():getHeight() + py*2
local rside = max_row_width(Data) + namepx*2 -- width of name column
local nt = table.size(Data)
local hrow = total_row_width(Data) + namepx*2*nt
local width = rside + hrow + App.width('Games') + App.width('Total') + namepx*4
local height = (nt+1)*c
local top, left = 30,30
local cx = column_xs(Rows, left+rside, namepx) -- right margin of each column
-- border
love.graphics.rectangle('line', left,top, width,height, 5,5)
do
-- first column for names
local hx = left+rside
love.graphics.line(hx, top, hx, top+height)
-- remaining columns
for i=1,#cx do
love.graphics.line(cx[i], top, cx[i], top+height)
end
end
-- rows
for i=1,#Rows do
love.graphics.line(left, top+i*c, left+width, top+i*c)
i = i+1
end
-- print out row headings
for i,name in ipairs(Rows) do
love.graphics.print(name, left+namepx, top+i*c+py)
end
-- print out column headings
local l = left+rside
for i=1,#Rows do
local r = cx[i]
local w = r-l
local px = (w - App.width(Rows[i])) / 2 -- x padding
love.graphics.print(Rows[i], l+px, top+py)
l = r
end
-- 'Games' counts number of games played
love.graphics.print('Games', l+namepx, top+py)
l = l+App.width('Games')+namepx*2
-- 'Total' doubles as a button that sorts the rows by teams' total score
button(Global_state, 'total', {
x=l, y=top,
w=App.width('Total')+namepx*2,
h=c,
bg = {r=0,g=0,b=0, a=0}, -- nothing
icon = function(params)
make_button_pop(params)
App.color{r=0, g=0, b=0}
love.graphics.print('Total', params.x+namepx, params.y+py)
end,
onpress1 = function()
Rows = ordered_keys(Data)
end
})
love.graphics.print('Total', l+namepx, top+py)
-- print out cells
for y,t1 in ipairs(Rows) do
l = left+rside
for x,t2 in ipairs(Rows) do
if t1 == t2 then
-- no button
local px = (cx[x] - l - App.width('X')) / 2
App.color{r=0, g=0, b=0}
love.graphics.print('X', l+px, top+y*c+py)
else
button(Global_state, t1..t2, {
x=l, y=top+y*c,
w=cx[x] - l,
h=c,
bg = {r=0,g=0,b=0, a=0}, -- nothing
icon = function(params)
make_button_pop(params)
if Data[t1][t2] then
local px = (params.w - App.width(Data[t1][t2])) / 2
App.color{r=0, g=0, b=0}
love.graphics.print(Data[t1][t2], params.x+px, params.y+py)
end
end,
onpress1 = function()
local v = Data[t1][t2]
if v == 'X' then
return
elseif v == nil then
Data[t1][t2] = 2 Data[t2][t1] = 0
add_result(t1, t2)
elseif v == 2 then
Data[t1][t2] = 0 Data[t2][t1] = 2
delete_result(t1, t2)
add_result(t2, t1)
elseif v == 0 then
Data[t1][t2] = nil Data[t2][t1] = nil
delete_result(t2, t1)
else
error('invalid value')
end
Global_state.next_save = Current_time + 3
end,
})
end
l = cx[x]
end
App.color{r=0, g=0, b=0}
local count = Data[t1].count
local px = (App.width('Games') - App.width(count)) / 2
love.graphics.print(count, l+px, top+y*c+py)
l = l + App.width('Games') + namepx*2
local total = score(Data, t1)
local px = (App.width('Total') - App.width(total)) / 2
love.graphics.print(total, l+px, top+y*c+py)
end
end