Hastwell

Venatus StaticTextDisplay

Nov 28th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. -- market display sign
  2. -- files used:
  3. --  monside (top/bottom/left/etc) - side monitor is on. Can also use this to specify modem-connected stuff
  4. --  lines - the lines this sign should display
  5. --  scale - the text scaling to use
  6.  
  7. -- ===============================================
  8. -- reads a config for lines
  9. os.sleep(4)
  10. shell.run("delete", "lines")
  11. shell.run("pastebin","get","hBkj3XcW","lines")
  12. f = fs.open('lines','r')
  13. linesList = {}
  14.  
  15. curLine = f.readLine()
  16. while curLine ~= nil do
  17.     table.insert(linesList, curLine)
  18.     curLine = f.readLine()
  19. end
  20. f.close()
  21. -- end floor config read
  22. -- ===============================================
  23. -- hw init
  24.  
  25. f = fs.open('monside','r')
  26. curMon = peripheral.wrap(f.readAll())
  27. f.close()
  28. curMon.clear()
  29.  
  30. f = fs.open('scale','r')
  31. curMon.setTextScale(f.readAll() + 0.0)
  32. f.close()
  33.  
  34. curMon.setTextColor(colors.white)
  35. local width, height = curMon.getSize()
  36.  
  37. -- end hw init
  38. -- ===============================================
  39. local function computeCenteringPos(strlen, padding)
  40.     return (width-strlen)/2 + 1 + padding
  41. end
  42. local function printHorizRule(mon, y)
  43.     mon.setTextColor(colors.white)
  44.     mon.setCursorPos(1,y)
  45.     mon.write(string.rep('-', width))
  46. end
  47. local function renderLine(lineNo, mon)
  48.     local floorText = linesList[lineNo]
  49.     -- half of this stuff deals with text based on the size of it.
  50.     -- strip and extract the color preprocessors
  51.     preProcStart, preProcEnd = string.find(floorText, '~')
  52.     preProcColor = nil
  53.     if preProcStart == 1 then
  54.         -- found the preproc!
  55.         preProcStart, preProcEnd = string.find(floorText, '~',2)
  56.         preProcColor = string.sub(floorText, 2,preProcEnd-1)
  57.         print("preprocessor: "..preProcColor)
  58.         floorText = floorText.sub(floorText, preProcEnd+1)
  59.     end
  60.    
  61.  
  62.     local width, height = mon.getSize()
  63.     local strlen = string.len(floorText)
  64.     local cursorPosX = 0
  65.     local cursorPosY = lineNo
  66.     -- text rendering :D
  67.    
  68.     -- set the color appropriately
  69.     if preProcColor ~= nil and colors[preProcColor] ~= nil then
  70.         mon.setTextColor(colors[preProcColor])
  71.     else
  72.         mon.setTextColor(colors.white)
  73.     end
  74.    
  75.     if strlen > width then
  76.         -- wider than screen. Left justify it.     
  77.         mon.setCursorPos(1, cursorPosY)
  78.         mon.write(floorText)
  79.     else
  80.         -- ( (width-len(txt)) /2)*' ' + txt
  81.        
  82.         -- "+1" because LUA has 1-based indexes. This piece is designed for 0-based indices -_-
  83.         -- "+2" to make it look centered against the offset that the floor numbers take up
  84.         --cursorPosX = (width-strlen)/2 + 1 + 2
  85.         cursorPosX = computeCenteringPos(strlen,2)
  86.        
  87.         mon.setCursorPos(cursorPosX, cursorPosY)
  88.         mon.write(floorText)
  89.     end
  90. end
  91.  
  92. -- print store header
  93. printHorizRule(curMon,2)
  94.  
  95. for i = 1, #linesList do
  96.     renderLine(i,curMon)
  97. end
  98.  
  99. while true do
  100.     os.sleep(4)
  101. end
Advertisement
Add Comment
Please, Sign In to add comment