Advertisement
Guest User

autorun.lua

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