Thread: Good UI lib?
View Single Post
05-12-18, 06:13 AM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by jofmayer View Post
As I understand it currently, I would need to write something which does the parsing/ displaying based on newline, tabstops, etc ...
I don't know why you'd need to do anything with newlines or tabs. Just structure your data correctly as nested Lua tables:

Code:
local data = {
   { "this is a cell", "", "5" },
   { "hello world", "row 2 cell 2", "10" },
}
Then loop over it like so:

Code:
for i, row in ipairs(data) do
   for j, cell in ipairs(row) do
      print("Row", i, "cell", j, "contains", tostring(cell))
   end
end
Originally Posted by jofmayer View Post
2. print that out into a ui element nicely: fixed column width, table headers, autoscrolling, etc
I'm not sure what you mean by autoscrolling, but none of the other stuff sounds terribly complicated. Just break it down and tackle one problem at a time.

Each row of the table will be a frame object (or a button, if you want OnClick functionality). Each cell within the row will be a font string.

Lay out the cells within the row by anchoring the first cell's TOPLEFT point to the row's TOPLEFT point, and each subsequent cell's TOPLEFT point to the previous cell's TOPRIGHT point, and setting the width of each cell based on its column position. If you want your cells to be bottom-aligned instead of top-aligned, use BOTTOMLEFT and BOTTOMRIGHT instead. If you want vertical center-alignment, use LEFT and RIGHT instead.

The table header is just another row, maybe with a different background or different font style to distinguish it from the body rows.

To make updating your rows and cells easy, store them in another table when you create them:

Code:
local rowFrames = {}

for i = 1, 10 do
   local row = CreateFrame("Button", nil, MyTableContainerFrame)
   row:SetWidth(MyTableContainerFrame:GetWidth()
   if i == 1 then
      row:SetPoint("TOPLEFT", MyTableContainerFrame)
   else
      row:SetPoint("TOPLEFT", rowFrames[i - 1], "BOTTOMLEFT")
   end

   row.cells = {}
   for j = 1, 3 do
      local cell = row:CreateFontString(nil, "OVERLAY", "GameFontNormal")
      if j == 1 then
         cell:SetPoint("TOPLEFT", row)
      else
         cell:SetPoint("TOPLEFT", row.cells[j - 1], "BOTTOMLEFT")
      end
   end
end
Then to make it show your data, just loop over your data rows and cells and fill in each corresponding GUI object:

Code:
for i, row in ipairs(data) do
   local rowFrame = rowFrames[i]
   for j, cell in ipairs(row) do
      local cellFontString = rowFrame.cells[j]
      cellFontString:SetText(cell)
   end
end
Originally Posted by jofmayer View Post
3. read where the user currently has the cursor and highlight that line
Enable the mouse on your row frames. When a row's OnEnter script runs, change the row's background color, change the color of its cell font strings, etc. to create a highlight effect. When a row's OnLeave script runs, undo the changes to remove the highlight effect.

Originally Posted by jofmayer View Post
4. on key press execute someting based in the current line
If you want line selection to be done by hovering, then in a row's OnEnter script update your "current row" reference to point to that row, and in its OnLeave script remove the reference. If you want to select a line by clicking on it, so your keypress can work whether or not the mouse has moved, then set the reference in each row's OnClick script, and remove it whenever you think makes sense (when the frame is hidden, when the rows are shuffled, after a timeout, etc.). Then in your keypress handler act on the "current row" reference.

Originally Posted by jofmayer View Post
My hope was that there already exists a lua lib which has such a functionality ...
Keep in mind that Lua is a general purpose scripting library. Most of the places where Lua is used are not World of Warcraft, and none of those places use the same GUI widget API as World of Warcraft. There probably are libraries to help you create data table GUIs in, say, wxWidgets for desktop applications, but there's zero common API between wxWidgets and World of Warcraft. If you've searched WoWInterface and CurseForge, and didn't find a WoW library relevant to your needs, then with 99% certainty, no such library exists.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote