Advertisement
Guest User

text_viewer

a guest
May 1st, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.90 KB | None | 0 0
  1.  
  2. mon = peripheral.find("monitor")
  3. areas = {}
  4. defaultTextColor = colors.black
  5. defaultBgColor = colors.white
  6. titleTextColor = colors.white
  7. titleBgColor = colors.black
  8. headerTextColor = colors.black
  9. headerBgColor = colors.gray
  10. buttonTextColor = colors.black
  11. buttonBgColor = colors.green
  12.  
  13. function createAreas()
  14.   local monWidth, monHeight = mon.getSize()
  15.   areas["title"] = {width=monWidth, height=1, x=1, y=1, textColor=titleTextColor, bgColor=titleBgColor}
  16.   areas["itemHeader"] = {width=monWidth-11, height=1, x=1, y=2, textColor=headerTextColor, bgColor=headerBgColor}
  17.   areas["ipmHeader"] = {width=5, height=1, x=areas.itemHeader.width+1, y=2, textColor=headerTextColor, bgColor=headerBgColor}
  18.   areas["tot15mHeader"] = {width=6, height=1, x=areas.itemHeader.width+areas.ipmHeader.width+1, y=2, textColor=headerTextColor, bgColor=headerBgColor}
  19.   local colHeight = monHeight - 4
  20.   areas["itemCol"] = {width=areas.itemHeader.width, height=colHeight, x=1, y=3, textColor=defaultTextColor, bgColor=defaultBgColor}
  21.   areas["ipmCol"] = {width=areas.ipmHeader.width, height=colHeight, x=areas.itemCol.width+1, y=3, textColor=defaultTextColor, bgColor=defaultBgColor}
  22.   areas["tot15mCol"] = {width=areas.tot15mHeader.width, height=colHeight, x=areas.itemCol.width+areas.ipmCol.width+1, y=3, textColor=defaultTextColor, bgColor=defaultBgColor}
  23.   local controlSpace1 = (monWidth - (8+8+3)) / 2
  24.   local controlSpace2 = (monWidth - (8+8+3)) / 2
  25.   if (controlSpace1 % 1 ~= 0) then
  26.     controlSpace1 = controlSpace1 - 0.5
  27.     controlSpace2 = controlSpace2 + 0.5
  28.   end
  29.   areas["sortAzControl"] = {width=8, height=1, x=1, y=areas.itemCol.height+2+1, textColor=buttonTextColor, bgColor=buttonBgColor}
  30.   areas["sortAmtControl"] = {width=8, height=1, x=8+controlSpace1+1, y=areas.itemCol.height+2+1, textColor=buttonTextColor, bgColor=buttonBgColor}
  31.   local pageControlSpace = 1
  32.   areas["leftPageControl"] = {width=1, height=1, x=8+controlSpace1+8+controlSpace2+1, y=areas.itemCol.height+2+1, textColor=buttonTextColor, bgColor=buttonBgColor}
  33.   areas["rightPageControl"] = {width=1, height=1, x=areas.leftPageControl.x+areas.leftPageControl.width+pageControlSpace+1, y=areas.itemCol.height+2+1, textColor=buttonTextColor, bgColor=buttonBgColor}
  34.   local statusSpace = monWidth - (7+8+6+6)
  35.   areas["statusTotalLabel"] = {width=7, height=1, x=1, y=areas.itemCol.height+3+1, textColor=defaultTextColor, bgColor=defaultBgColor}
  36.   areas["statusTotal"] = {width=8, height=1, x=7+1, y=areas.itemCol.height+3+1, textColor=defaultTextColor, bgColor=defaultBgColor}
  37.   areas["statusDataLabel"] = {width=6, height=1, x=7+statusSpace+1, y=areas.itemCol.height+3+1, textColor=defaultTextColor, bgColor=defaultBgColor}
  38.   areas["statusData"] = {width=6, height=1, x=7+statusSpace+6+1, y=areas.itemCol.height+3+1, textColor=defaultTextColor, bgColor=defaultBgColor}
  39. end
  40.  
  41. function init()
  42.   mon.setTextScale(1)
  43.   mon.setTextColor(colors.black)
  44.   mon.setBackgroundColor(colors.white)
  45.   mon.clear()
  46.   createAreas()
  47.   writeLine("Item Input Statistics", areas.title)
  48.   writeLine("item", areas.itemHeader)
  49.   writeLine("ipm", areas.ipmHeader)
  50.   writeLine("15mTot", areas.tot15mHeader)
  51.   writeLine("Sort A-Z", areas.sortAzControl)
  52.   writeLine("Sort Amt", areas.sortAmtControl)
  53.   writeLine("<", areas.leftPageControl)
  54.   writeLine(">", areas.rightPageControl)
  55.   writeLine("Total: ", areas.statusTotalLabel)
  56.   writeLine("Data: ", areas.statusDataLabel)
  57. end
  58.  
  59. function clearArea(area)
  60.   writeLine("", area)
  61. end
  62.  
  63. function writeLine(line, area)
  64.   writeLines({line}, area)
  65. end
  66.  
  67. function writeLines(lines, area)
  68.   mon.setTextColor(area.textColor)
  69.   mon.setBackgroundColor(area.bgColor)
  70.   local y = area.y
  71.   local n = 1
  72.   while y <= area.y + area.height - 1 do
  73.     mon.setCursorPos(area.x, y)
  74.     local buffer = ""
  75.     if (lines[n] ~= nil) then
  76.       buffer = lines[n]
  77.     else
  78.       buffer = spaces(area.width)
  79.     end
  80.    
  81.     if (string.len(buffer) > area.width) then
  82.       buffer = string.sub(buffer, 1, area.width)
  83.     elseif (string.len(buffer) < area.width) then
  84.       buffer = buffer..spaces(area.width - string.len(buffer))
  85.     end
  86.  
  87.     print("writing @"..y..": '"..buffer.."'")    
  88.     mon.write(buffer)
  89.     y = y + 1
  90.     n = n + 1
  91.   end
  92. end
  93.  
  94. function spaces(length)
  95.   local result = ""
  96.   for i=1, length do
  97.     result = result.." "
  98.   end
  99.   return result
  100. end
  101.  
  102. function printEnv()
  103.   local xPos, yPos = mon.getCursorPos()
  104.   print("x="..xPos..",y="..yPos)
  105.   local monWidth, monHeight = mon.getSize()
  106.   print("width="..monWidth..",height="..monHeight)
  107.   for areaName, obj in pairs(areas) do
  108.     print(areaName)
  109.     for key, val in pairs(obj) do
  110.       print("  "..key.." = "..val)
  111.     end
  112.   end
  113. end
  114.  
  115. function main()
  116.   while true do
  117.     local event, side, x, y = os.pullEvent("monitor_touch")
  118.     mon.setCursorPos(x, y)
  119.     mon.write("click")
  120.   end
  121. end
  122.  
  123. init()
  124. --printEnv()
  125. --print("|"..spaces(5).."|")
  126. --writeLine("Title", areas.title)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement