Advertisement
Guest User

InfoPanel.lua

a guest
Feb 25th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.42 KB | None | 0 0
  1. local ecs = require("ECSAPI")
  2. local xml = require("xmlParser")
  3. local image = require("image")
  4. local event = require("event")
  5. local unicode = require("unicode")
  6. local fs = require("filesystem")
  7. local gpu = require("component").gpu
  8.  
  9. ------------------------------------------------------------------------------------------------------------------
  10.  
  11. local config = {
  12.     scale = 0.63,
  13.     leftBarWidth = 20,
  14.     scrollSpeed = 6,
  15.     pathToInfoPanelFolder = "/home/infopanel/",
  16.     colors = {
  17.         leftBar = 0xEEEEEE,
  18.         leftBarText = 0x262626,
  19.         leftBarSelection = 0x00C6FF,
  20.         leftBarSelectionText = 0xFFFFFF,
  21.         scrollbarBack = 0xEEEEEE,
  22.         scrollbarPipe = 0x3366CC,
  23.         background = 0x262626,
  24.         text = 0xFFFFFF,
  25.     },
  26. }
  27.  
  28. local xOld, yOld = gpu.getResolution()
  29. ecs.setScale(config.scale)
  30. local xSize, ySize = gpu.getResolution()
  31.  
  32. fs.makeDirectory(config.pathToInfoPanelFolder)
  33. local currentFile = 1
  34. local fileList
  35. local stroki = {}
  36. local currentString = 1
  37. local stringsHeightLimit = ySize - 2
  38. local stringsWidthLimit = xSize - config.leftBarWidth - 4
  39.  
  40. ------------------------------------------------------------------------------------------------------------------
  41.  
  42. local obj = {}
  43. local function newObj(class, name, ...)
  44.     obj[class] = obj[class] or {}
  45.     obj[class][name] = {...}
  46. end
  47.  
  48. local function drawLeftBar()
  49.     --ecs.square(1, 1, config.leftBarWidth, ySize, config.colors.leftBar)
  50.     fileList = ecs.getFileList(config.pathToInfoPanelFolder)
  51.     obj["Files"] = {}
  52.     local yPos = 1, 1
  53.     for i = 1, #fileList do
  54.         if i == currentFile then
  55.             newObj("Files", i, ecs.drawButton(1, yPos, config.leftBarWidth, 3, ecs.hideFileFormat(fileList[i]), config.colors.leftBarSelection, config.colors.leftBarSelectionText))
  56.         else
  57.             if i % 2 == 0 then
  58.                 newObj("Files", i, ecs.drawButton(1, yPos, config.leftBarWidth, 3, ecs.stringLimit("end", fileList[i], config.leftBarWidth - 2), config.colors.leftBar, config.colors.leftBarText))
  59.             else
  60.                 newObj("Files", i, ecs.drawButton(1, yPos, config.leftBarWidth, 3, ecs.stringLimit("end", fileList[i], config.leftBarWidth - 2), config.colors.leftBar - 0x111111, config.colors.leftBarText))
  61.             end
  62.         end
  63.         yPos = yPos + 3
  64.     end
  65.     ecs.square(1, yPos, config.leftBarWidth, ySize - yPos + 1, config.colors.leftBar)
  66. end
  67.  
  68. local function loadFile()
  69.     currentString = 1
  70.     stroki = {}
  71.     local file = io.open(config.pathToInfoPanelFolder .. fileList[currentFile], "r")
  72.     for line in file:lines() do table.insert(stroki, xml.collect(line)) end
  73.     file:close()
  74. end
  75.  
  76. local function drawMain()
  77.     local x, y = config.leftBarWidth + 3, 2
  78.     local xPos, yPos = x, y
  79.  
  80.     ecs.square(xPos, yPos, xSize - config.leftBarWidth - 5, ySize, config.colors.background)
  81.     gpu.setForeground(config.colors.text)
  82.  
  83.     for line = currentString, (stringsHeightLimit + currentString - 1) do
  84.         if stroki[line] then
  85.             for i = 1, #stroki[line] do
  86.                 if type(stroki[line][i]) == "table" then
  87.                     if stroki[line][i].label == "color" then
  88.                         gpu.setForeground(tonumber(stroki[line][i][1]))
  89.                     elseif stroki[line][i].label == "image" then
  90.                         local bg, fg = gpu.getBackground(), gpu.getForeground()
  91.                         local picture = image.load(stroki[line][i][1])
  92.                         image.draw(xPos, yPos, picture)
  93.                         yPos = yPos + picture.height - 1
  94.                         gpu.setForeground(fg)
  95.                         gpu.setBackground(bg)
  96.                     end
  97.                 else
  98.                     gpu.set(xPos, yPos, stroki[line][i])
  99.                     xPos = xPos + unicode.len(stroki[line][i])
  100.                 end
  101.             end
  102.             yPos = yPos + 1
  103.             xPos = x
  104.         else
  105.             break
  106.         end
  107.     end
  108.  
  109. end
  110.  
  111. local function drawScrollBar()
  112.     local name
  113.     name = " "; newObj("Scroll", name, ecs.drawButton(xSize - 2, 1, 3, 3, name, config.colors.leftBarSelection, config.colors.leftBarSelectionText))
  114.     name = " "; newObj("Scroll", name, ecs.drawButton(xSize - 2, ySize - 2, 3, 3, name, config.colors.leftBarSelection, config.colors.leftBarSelectionText))
  115.  
  116.     ecs.srollBar(xSize - 2, 4, 3, ySize - 6, #stroki, currentString, config.colors.scrollbarBack, config.colors.scrollbarPipe)
  117. end
  118.  
  119. ------------------------------------------------------------------------------------------------------------------
  120.  
  121. ecs.prepareToExit()
  122. drawLeftBar()
  123. loadFile()
  124. drawMain()
  125.  
  126. while true do
  127.     local e = {event.pull()}
  128.     if e[1] == "touch" then
  129.         for key in pairs(obj["Files"]) do
  130.             if ecs.clickedAtArea(e[3], e[4], obj["Files"][key][1], obj["Files"][key][2], obj["Files"][key][3], obj["Files"][key][4]) then
  131.                 currentFile = key
  132.                 loadFile()
  133.                 drawLeftBar()
  134.                 drawMain()
  135.                 break
  136.             end
  137.         end
  138.  
  139.     elseif e[1] == "key_down" then
  140.         if e[4] == 28 then
  141.             gpu.setResolution(xOld, yOld)
  142.             ecs.prepareToExit()
  143.             return
  144.         end
  145.     end
  146. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement