-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrid.lua
113 lines (90 loc) · 3.08 KB
/
grid.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
local function createGrid( design, data )
-- shortcut for creating text - x position calculated outside
function genTxt(txt, yp)
return display.newText( txt, 0, yp, native.systemFont, 13 )
end
local bg
local grid = display.newGroup()
local columns = {}
local xp = 0
local yp = 0
for c, wid in ipairs( design.cols ) do
-- print("formatting", s, set)
-- 1's and 2's below are padding options. could be passed in via design var
bg = display.newRect( xp + 2, yp + 2, wid - 4, design.header.height - 4)
bg:setFillColor( design.header.bg.r, design.header.bg.g, design.header.bg.b, design.header.bg.a )
bg:setStrokeColor( design.header.border.r, design.header.border.g, design.header.border.b, design.header.border.a )
bg.strokeWidth = 2
grid:insert( bg, false )
txt = genTxt( data[1][c], design.header.height / 2 + design.toprowfontfix )
if c == 1 then
txt.x = xp + txt.width / 2 + design.leftcolfontfix + design.headerfontfix
else
txt.x = xp + wid / 2 + design.headerfontfix
end
grid:insert( txt, false )
-- create a reference table of calculated column positions and widths
columns[ c ] = { xp, wid }
xp = xp + wid
end
yp = yp + design.header.height
-- start from row 2 since 1st row is headers
for r = 2, #data do
row = data[r]
--print("Row", r, row, row[1])
for c,col in ipairs( row ) do
--print("Col", c, col, columns[ c ], columns[ c ][ 1 ], columns[ c ][ 2 ] )
xp,wid = columns[ c ][ 1 ], columns[ c ][ 2 ]
-- 2's and 4's are padding based on borders. could be passed in via design var
bg = display.newRect( xp + 2, yp + 2, wid - 4, design.row.height - 4)
bg:setFillColor( design.row.bg.r, design.row.bg.g, design.row.bg.b, design.row.bg.a )
bg:setStrokeColor( design.row.border.r, design.row.border.g, design.row.border.b, design.row.border.a )
bg.strokeWidth = 2
grid:insert( bg, false )
txt = genTxt(col, yp)
if c == 1 then -- do left most column
txt.x = xp + txt.width / 2 + design.leftcolfontfix + design.cellfontfix
else
txt.x = xp + wid / 2 + design.cellfontfix
end
grid:insert( txt, false )
end
yp = yp + design.row.height
end
return grid
end
-- below are the settings for the grid
design = {
-- column widths
cols = { 110, 70, 40, 40 },
-- vertical alignment for header
toprowfontfix = -12,
-- left most column is left aligned, so padding from left side
leftcolfontfix = 6,
-- in case your chosen font needs some alignment nudging
headerfontfix = 1, cellfontfix = 2,
-- colours
header = {
bg = { r = 150, g = 20, b = 20, a = 200 },
border = { r = 150, g = 20, b = 20, a = 200},
height = 30
},
row = {
bg = { r = 150, g = 100, b = 150, a = 80 },
border = { r = 150, g = 100, b = 150, a = 150 },
height = 25
}
}
data = {
{ "Act","Country","Rel.","Gigs"}, -- header row
{ "Prometheus","UK",5,10 },
{ "Logic Bomb","Sweden",7,20 },
{ "Psykovsky","Russia",3,6},
{ "Hux Flux","Sweden",4,8},
{ "Ticon","Sweden",8,30},
{ "Odd Harmonic","Aus",2,10},
{ "Nam Shub","Aus",8,20}
}
-- pass them in and voila.
local t = createGrid( design, data )
t.x, t.y = 30, 150