Advertisement
Lanzr

listUILib

Jul 3rd, 2024
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.24 KB | None | 0 0
  1. page = {}
  2.  
  3. local g_window_size = { w = 0, h = 0 }
  4.  
  5. page.mPageIndex = 0
  6. page.mRowIndex = 0
  7. page.mPageCount = 0
  8. page.mItemCount = 0
  9. page.mNowMaxPageIndex = 0
  10. page.topBar = nil
  11. page.body = nil
  12.  
  13. local g_entry_list = {}
  14.  
  15. local winNormColor = {
  16.     font = colors.black,
  17.     bg = colors.lime
  18. }
  19.  
  20. local winFocusColor = {
  21.     font = colors.gray,
  22.     bg = colors.pink
  23. }
  24.  
  25. -- renewTopBar and Body
  26. local function flushTopBar()
  27.     local str = string.format("%3d/%3d      size:%d",page.mPageIndex,page.mPageCount,page.mItemCount)
  28.     page.topBar.clear()
  29.     page.topBar.setCursorPos(1,1)
  30.     page.topBar.write(str)
  31. end
  32.  
  33. local function flushBody()
  34.     page.body.clear()
  35.     local nPos = 1
  36.     local startPos = (page.mPageIndex - 1) * g_window_size.h + 1
  37.     local endPos = startPos + page.mNowMaxPageIndex - 1
  38.     for index = startPos, endPos, 1 do
  39.         page.body.setCursorPos(1,nPos)
  40.         if(nPos == page.mRowIndex) then -- select backGround Color
  41.             page.body.setBackgroundColor(winFocusColor.bg)
  42.             page.body.setTextColor(winFocusColor.font)
  43.             page.body.clearLine()
  44.         else
  45.             page.body.setBackgroundColor(winNormColor.bg)
  46.             page.body.setTextColor(winNormColor.font)
  47.         end
  48.         page.body.write(g_entry_list[index])
  49.         nPos = nPos + 1
  50.     end
  51.     page.body.setBackgroundColor(winNormColor.bg)
  52.     page.body.setTextColor(winNormColor.font)
  53. end
  54.  
  55. -- switch the page
  56. local function pageChange(offset)
  57.     local targetPageIndex = page.mPageIndex + offset
  58.     -- page.LDebug(page.mPageIndex,19)
  59.     targetPageIndex = targetPageIndex > 0 and targetPageIndex or 1
  60.     targetPageIndex = targetPageIndex <= page.mPageCount and targetPageIndex or page.mPageCount
  61.     page.mPageIndex = targetPageIndex
  62.     local remainder = page.mItemCount % g_window_size.h
  63.     if (targetPageIndex == page.mPageCount and remainder ~= 0) then
  64.         -- have remainder
  65.         page.mNowMaxPageIndex = remainder
  66.     else
  67.         -- common page
  68.         page.mNowMaxPageIndex = g_window_size.h
  69.     end
  70.    
  71.     flushTopBar()
  72.     flushBody()
  73. end
  74.  
  75. --[[
  76.     the key method for use
  77. ]]
  78.    
  79. -- debug in window
  80. function page.LDebug(str,col)
  81.     term.setCursorPos(1,col)
  82.     term.setTextColor(colors.orange)
  83.     term.setBackgroundColor(colors.white)
  84.     term.clearLine()
  85.     term.write(str)
  86. end
  87.  
  88. -- init
  89. function page.init(list,width,height)
  90.     g_window_size.w = width
  91.     g_window_size.h = height - 3
  92.     page.topBar = window.create(term.current(), 1, 1, g_window_size.w, 1)
  93.     page.body = window.create(term.current(), 1, 2, g_window_size.w, g_window_size.h)
  94.    
  95.     page.topBar.setBackgroundColor(colors.orange)
  96.     page.topBar.setTextColor(colors.white)
  97.     page.body.setBackgroundColor(winNormColor.bg)
  98.     page.body.setTextColor(winNormColor.font)
  99.     page.mItemCount = table.getn(list)  
  100.     page.mPageCount =  (page.mItemCount - 1 - (page.mItemCount - 1) % g_window_size.h) / g_window_size.h + 1
  101.     page.mPageIndex = 1  
  102.     page.mRowIndex = 1
  103.     g_entry_list = list
  104.  
  105.     term.clear()
  106.     term.setTextColor(colors.white)
  107.     pageChange(0)
  108. end
  109.  
  110. -- get current select
  111. function page.getSelect()
  112.     local index = (page.mPageIndex -1) * g_window_size.h + page.mRowIndex
  113.     return index
  114. end
  115.  
  116. local keyMap = {
  117.     ["up"]=(function ()
  118.         if(page.mRowIndex > 1) then
  119.             page.mRowIndex = page.mRowIndex - 1
  120.         -- LDebug("nowPos "..(pageIndex-1) * mh + page.mRowIndex,18)
  121.             flushBody()
  122.         end
  123.     end),
  124.     ["down"]=(function ()
  125.         if(page.mRowIndex < g_window_size.h and page.mRowIndex < page.mNowMaxPageIndex) then
  126.             page.mRowIndex = page.mRowIndex + 1
  127.             -- LDebug("nowPos "..(pageIndex-1) * mh + page.mRowIndex,18)
  128.             flushBody()
  129.         end
  130.     end),
  131.     ["left"]=(function ()
  132.         if(page.mPageIndex > 1) then
  133.             page.mRowIndex = 1
  134.             pageChange(-1)
  135.         end
  136.     end),
  137.     ["right"]=(function ()
  138.         if(page.mPageIndex < page.mPageCount) then
  139.             page.mRowIndex = 1
  140.             pageChange(1)
  141.         end
  142.     end),
  143. }
  144.  
  145. -- control the page
  146. function page.control(cmd)
  147.     local cb = keyMap[cmd]
  148.     if cb ~= nil then
  149.         keyMap[cmd]()
  150.     end
  151. end
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement