Hastwell

Venatus Floor Display Sign

Oct 26th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 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. --  floorno - the REVERSE INDEXED floor number this computer serves (used for highlighting)
  5. --  floors  - the floor listing (top to bottom), one floor/line
  6.  
  7. -- ===============================================
  8. -- reads a config for floor names
  9. f = fs.open('floors','r')
  10. floorList = {}
  11.  
  12. curLine = f.readLine()
  13. while curLine ~= nil do
  14.     table.insert(floorList, curLine)
  15.     curLine = f.readLine()
  16. end
  17. f.close()
  18. f = fs.open('floorno','r')
  19. curFloor = tonumber( f.readAll() )
  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. curMon.setTextColor(colors.white)
  30. local width, height = curMon.getSize()
  31.  
  32. -- end hw init
  33. -- ===============================================
  34. local function computeCenteringPos(strlen, padding)
  35.     return (width-strlen)/2 + 1 + padding
  36. end
  37. local function printHorizRule(mon, y)
  38.     mon.setTextColor(colors.white)
  39.     mon.setCursorPos(1,y)
  40.     mon.write(string.rep('-', width))
  41. end
  42. local function renderFloor(floorNum, mon)
  43.     local floorText = floorList[floorNum]
  44.     -- half of this stuff deals with text based on the size of it.
  45.     -- strip and extract the color preprocessors
  46.     preProcStart, preProcEnd = string.find(floorText, '~')
  47.     preProcColor = nil
  48.     if preProcStart == 1 then
  49.         -- found the preproc!
  50.         preProcStart, preProcEnd = string.find(floorText, '~',2)
  51.         preProcColor = string.sub(floorText, 2,preProcEnd-1)
  52.         print("preprocessor: "..preProcColor)
  53.         floorText = floorText.sub(floorText, preProcEnd+1)
  54.     end
  55.    
  56.  
  57.     local width, height = mon.getSize()
  58.     local strlen = string.len(floorText)
  59.     local cursorPosX = 0
  60.     local cursorPosY = height-( (#floorList-floorNum) *2)
  61.     -- text rendering :D
  62.    
  63.     -- set the color appropriately
  64.     if floorNum == curFloor then
  65.         mon.setTextColor(colors.lime)
  66.     elseif preProcColor ~= nil and colors[preProcColor] ~= nil then
  67.         mon.setTextColor(colors[preProcColor])
  68.     else
  69.         mon.setTextColor(colors.white)
  70.     end
  71.    
  72.     floorCode = nil
  73.     -- attempt to crudely center this
  74.     if floorNum == curFloor then
  75.         floorCode = '==> '
  76.     else
  77.         floorCode = 'L'..(#floorList-floorNum+1)..': '
  78.     end
  79.     if strlen + string.len(floorCode) > width then
  80.         -- wider than screen. Left justify it.     
  81.         mon.setCursorPos(1, cursorPosY)
  82.         mon.write(floorCode..floorText)
  83.     else
  84.         -- ( (width-len(txt)) /2)*' ' + txt
  85.        
  86.         -- "+1" because LUA has 1-based indexes. This piece is designed for 0-based indices -_-
  87.         -- "+2" to make it look centered against the offset that the floor numbers take up
  88.         --cursorPosX = (width-strlen)/2 + 1 + 2
  89.         cursorPosX = computeCenteringPos(strlen,2)
  90.         mon.setCursorPos(1,cursorPosY)
  91.         mon.write(floorCode)
  92.        
  93.         mon.setCursorPos(cursorPosX, cursorPosY)
  94.         mon.write(floorText)
  95.     end
  96. end
  97.  
  98. -- print store header
  99. txt = 'Hastwell Industries'
  100. curMon.setCursorPos(computeCenteringPos(string.len(txt),0),1)
  101. curMon.write(txt)
  102.  
  103. printHorizRule(curMon,2)
  104.  
  105. for i = 1, #floorList do
  106.     renderFloor(i,curMon)
  107. end
Advertisement
Add Comment
Please, Sign In to add comment