-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLibTable.lua
288 lines (262 loc) · 10.8 KB
/
LibTable.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
--[[
LibTable
A library to make HTML-like table structure.
Part of SavedClassic
1. Load Library, Create Instance, Manipulate Shape, Options, Callbacks
local LibTable = LibStub("LibTable")
local tbl = LibTable:CreateTable(frameName, parentFrame, size?, options?, callbacks?)
-- Returns tbl object, instance of "Frame" object
tbl:Resize(size)
tbl:SetOption(options)
tbl:SetCallback(callbacks)
size = {
rows: num of rows,
cols: num of columns,
widths: table of col width in order - last num for all rest cols
heights: table of row height in order - last num for all rest rows
borders = {
top, right, bottom, left : each border size
}
}
options = {
[function] = [args], -- Do tbl:function(args) if args is table, unpack it
SetUserPlaced = true, -- tbl:SetUserPlaced(true)
SetMovable = true, -- Also makes Frame Left-Draggable
SetPoint = { "CENTER", UIParent, "CENTER", 0, 0 }, -- tbl:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
* ESCClosable = true, -- (Custom) Make Frame ESC-Closable
}
callbacks = {
[event] = [script], -- Do tbl:SetScript(event, script)
OnEnter = function(...) Do something end,
}
2. Manipulate Cell and Data
tbl:SetTable(tbl)
tbl:SetTableData(tbl)
Sets text(shown) and data(hidden) from from
if nil, text = "" and data = nil
tbl:SetRange(rows, cols, text)
tbl:SetRangeData(rows, cols, data)
tbl:SetRangeJustify(rows, cols, method)
tbl:SetRangeOption(rows, cols, optiontbl)
tbl:SetRangeCallback(rows, cols, callbacktbl)
tbl:RangeFunction(rows, cols, function)
'range' consists of rows and cols
where rows/cols contains row/col numbers (table or one number)
if rows = true, range become whole row
if cols = true, range become whole column
options and callbacks are like above
But a cell is "FontSring", so options differ from above
3. Access certain cell by row and col
tbl.tr[row].td[col] == _G[tbl:GetName().."Row"..row.."Col"..col]
]]
local library = "LibTable"
assert(LibStub, format("%s requires LibStub", library))
---@class LibTable
local lib, oldminor = LibStub:NewLibrary(library, 1)
if not lib then return end
oldminor = oldminor or 0
local DEFAULT_WIDTH = 32
local DEFAULT_HEIGHTS = 18
local DEFAULT_SIZE = {
rows = 1,
cols = 2,
widths = DEFAULT_WIDTH,
heights = DEFAULT_HEIGHTS,
}
lib.tables = lib.tables or { }
function LibTable_SetOption(tbl, options)
tbl.ESCClosable = tbl.ESCClosable or function()
table.insert(UISpecialFrames, tbl:GetName())
end
tbl.PlaceCloseButton = tbl.PlaceCloseButton or function(...)
local _, point, relativeFrame, relativePoint, offsetx, offsety = ...
if point == true then point = "TOPRIGHT" end
tbl.closeButton = CreateFrame("Button", tbl:GetName().."CloseButton", tbl, "UIPanelCloseButton")
tbl.closeButton:SetPoint(point or "TOPRIGHT", relativeFrame or tbl, relativePoint or "TOPRIGHT", offsetx, offsety)
end
if options.SetMovable then
tbl:SetScript("OnMouseDown", tbl.StartMoving)
tbl:SetScript("OnMouseUp", tbl.StopMovingOrSizing)
end
for func, arg in pairs(options) do
if type(arg) == "table" then
tbl[func](tbl, unpack(arg))
else
tbl[func](tbl, arg)
end
end
return tbl
end
function LibTable_Resize(tbl, size)
size = size or tbl.size
tbl.size = size
size.widths = size.widths or DEFAULT_WIDTH
size.heights = size.heights or DEFAULT_HEIGHTS
if type(size.widths) == "number" then size.widths = { size.widths } end
if type(size.heights) == "number" then size.heights = { size.heights } end
local name = tbl:GetName()
size.borders = size.borders or { top = 4, right = 24, bottom = 8, left = 12 }
local totalwidth = size.borders.left + size.borders.right
local totalheight = size.borders.top + size.borders.bottom
for row = 1, size.rows do
size.heights[row] = size.heights[row] or size.heights[#size.heights] -- last for rests
totalheight = totalheight + size.heights[row]
end
for col = 1, size.cols do
size.widths[col] = size.widths[col] or size.widths[#size.widths] -- last for rests
totalwidth = totalwidth + size.widths[col]
end
tbl:SetSize(totalwidth, totalheight)
-- Consists of tr, td like HTML table
tbl.tr = tbl.tr or {}
for row = 1, size.rows do
local tr = tbl.tr[row] or CreateFrame("Button", name.."Row"..row, tbl)
tr:SetSize(1, size.heights[row])
if row == 1 then -- if 1st row
tr:SetPoint("TOPLEFT", size.borders.left, - size.borders.top)
else
tr:SetPoint("TOPLEFT", tbl.tr[row-1], "BOTTOMLEFT")
end
tr.td = tr.td or {}
local xoffset = 0
for col = 1, size.cols do
local td = tr.td[col] or tr:CreateFontString(tr:GetName().."Col"..col, "ARTWORK", "GameFontNormal")
xoffset = xoffset + (size.widths[col-1] or 0)
td:SetPoint("LEFT", xoffset, 0)
td.row = row
td.col = col
tr.td[col] = td
end
tbl.tr[row] = tr
end
return tbl
end
function LibTable_SetCallback(tbl, callbacks)
tbl.callbacks = callbacks
for event, script in pairs(callbacks) do
tbl:SetScript(event, script)
end
return tbl
end
function LibTable_SetTable(tbl, mode, vtbl)
-- Data exceed range are clipped
for row = 1, #tbl.tr do
for col = 1, #tbl.tr[row].td do
if mode == "value" then
local value = vtbl[row] and vtbl[row][col]
if value then -- Nil DOESN'T change value
tbl.tr[row].td[col]:SetText(value)
end
elseif mode == "data" then
-- Nil changes data
tbl.tr[row].td[col].data = vtbl[row] and vtbl[row][col]
end
end
end
end
function LibTable_SetRange(tbl, mode, rows, cols, value)
if type(rows) == "number" then rows = { rows } end
if type(cols) == "number" then cols = { cols } end
if type(rows) == "boolean" and rows then
rows = {}
for row = 1, tbl.size.rows do table.insert(rows, row) end
end
if type(cols) == "boolean" and cols then
cols = {}
for col = 1, tbl.size.cols do table.insert(cols, col) end
end
for _, row in pairs(rows) do
for _, col in pairs(cols) do
if row <= tbl.size.rows and col <= tbl.size.cols then
local cell = tbl.tr[row].td[col]
if mode == "text" then
if value then -- nil DOESN'T change value
cell:SetText(value)
end
elseif mode =="data" then
-- nil changes data
cell.data = value
elseif mode == "callbacks" then
for event, script in pairs(value) do
cell:SetScript(event, script)
end
elseif mode == "options" then
for func, arg in pairs(value) do
if type(arg) == "table" then
cell[func](cell, unpack(arg))
else
cell[func](cell, arg)
end
end
elseif mode == "justify" then
LibTable_Cell_Justify(tbl, row, col, strlower(value))
elseif mode == "functions" and type(value) == "function" then
value(tbl, cell)
end
end
end
end
return tbl
end
function LibTable_Cell_Justify(tbl, row, col, method)
local cell = tbl.tr[row].td[col]
local xoffset = 0
for c = 1, col-1 do
xoffset = xoffset + tbl.size.widths[c]
end
if method == "left" then
cell:SetPoint("LEFT", xoffset, 0)
cell:SetJustifyH("LEFT")
elseif method == "center" then
xoffset = xoffset + math.floor(tbl.size.widths[col] / 2)
cell:SetSize(tbl.size.widths[col], tbl.size.heights[row])
cell:SetPoint("CENTER", xoffset, 0)
elseif method == "right" then
xoffset = xoffset + tbl.size.widths[col]
cell:SetPoint("RIGHT", xoffset - 1, 0)
cell:SetJustifyH("RIGHT")
end
end
---Create Table Instance
---
---@param name string
---@param parent table
---@param size table?
---@param options table<string, any>?
---@param callbacks table<string, any>?
---@return table
function lib:CreateTable(name, parent, size, options, callbacks)
name = name or (library.."CustomTable"..(#lib.tables + 1))
parent = parent or UIParent
size = size or DEFAULT_SIZE
options = options or {}
callbacks = callbacks or {}
local tbl = CreateFrame("Frame", name, parent, "TooltipBorderedFrameTemplate")
tbl.Resize = function(...) return LibTable_Resize(...) end
tbl.SetOption = function(...) return LibTable_SetOption(...) end
tbl.SetCallback = function(...) return LibTable_SetCallback(...) end
tbl.SetTable = function(stbl, vtbl) return LibTable_SetTable(stbl, "value", vtbl) end
tbl.SetTableData = function(stbl, dtbl) return LibTable_SetTable(stbl, "data", dtbl) end
tbl.SetRange = function(stbl, rows, cols, text) return LibTable_SetRange(stbl, "text", rows, cols, text) end
tbl.SetRangeData = function(stbl, rows, cols, data) return LibTable_SetRange(stbl, "data", rows, cols, data) end
tbl.SetRangeJustify = function(stbl, rows, cols, method) return LibTable_SetRange(stbl, "justify", rows, cols, method) end
tbl.SetRangeOption = function(stbl, rows, cols, optiontbl) return LibTable_SetRange(stbl, "options", rows, cols, optiontbl) end
tbl.SetRangeCallback = function(stbl, rows, cols, callbacktbl) return LibTable_SetRange(stbl, "callbacks", rows, cols, callbacktbl) end
tbl.RangeFunction = function(stbl, rows, cols, func) return LibTable_SetRange(stbl, "functions", rows, cols, func) end
tbl:Resize(size):SetOption(options):SetCallback(callbacks)
table.insert(lib.tables, tbl)
return tbl
end
function lib:Test()
local test = self:CreateTable(library.."TestTable", UIParent, { rows = 5, cols = 5, widths = {24,64,32}, heights = 18 },
{ SetPoint = "CENTER", SetMovable = true, })
test:SetTable({
{"*",library,"Test","Table",""},
{"a","|cffff3333b|r","c","d","e"},
{1,2,3,4,5},
{"가","나","다","라","마"},
{"Long-value","Long",nil, "OK"},
})
test:Show()
return test
end