Guest User

center

a guest
Oct 2nd, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.95 KB | None | 0 0
  1. --This is the classroom monitor setup routine
  2. --written by Matt Kelliher
  3. --
  4.  
  5. --First: we assume there are 4 equal-sized
  6. --monitors in the room connected to the network
  7. --we'll find them on the network,
  8. --give them names based on color, and
  9. --wrap them with a variable
  10. local Rmon, Gmon, Bmon, Pmon = peripheral.find("monitor", function(name, object) return object.isColor() end)
  11.  
  12. --We'll also get the dimensions of the monitors
  13. --and store them (we'll assume all monitors are
  14. --the same number of columns and rows)
  15. local monRows, monColumns
  16.  
  17. --A useful function for centering text
  18. --on a monitor from the ComputerCraft forums
  19. function centerPrint(text, y, mon)
  20.   local w, h = mon.getSize() or term.getSize()
  21.   if mon then
  22.     mon.setCursorPos( math.ceil( w/2 - #text/2 + 1), y )
  23.     mon.write( text)
  24.   else
  25.     mon.setCursorPos( math.ceil( w/2 - #text/2 ), y )
  26.     term.write( text )
  27.   end
  28. end
  29.  
  30. --A function that wraps text on monitor
  31. --and calls the center function
  32. function printCenterWrap( mon, text, size )
  33.   --size is an optional parameter,
  34.   --we default to 0.5
  35.   if size == nil then size = 0.5 end
  36.  
  37.   if size > 0 then mon.setTextScale(size) end
  38.  
  39.   local MaxCols, MaxRows = mon.getSize()
  40.   local stBuffer = text
  41.   local rowCount = 1
  42.  
  43.   print("MaxCols: "..MaxCols.." MaxRows: "..MaxRows)
  44.   while string.len(stBuffer) > 0 and rowCount <= MaxRows do
  45.     --print("LoopStartBuffer: "..stBuffer.." ("..string.len(stBuffer)..")")
  46.     --find if there's a space character
  47.     local space = string.find(stBuffer, " ")
  48.  
  49.     if space == nil or space >= MaxCols then
  50.       --print("found no space before EOL")
  51.       --This is the easiest case: we didn't
  52.       --find a space before the End-of-Line MaxCols
  53.       --so just print a substring up to EOL
  54.       mon.setCursorPos(1,rowCount) --center later
  55.       --print("SetCursorPos(1,"..rowCount..")")
  56.       local wl = string.sub(stBuffer,1,MaxCols)
  57.       mon.write(wl) --center this later
  58.       --print("Wrote:"..wl)
  59.       if (MaxCols + 1) > (string.len(stBuffer)) then
  60.         --we're don't processing
  61.         stBuffer = ""
  62.         --print("Set Buffer to EOL")
  63.       else
  64.         stBuffer = string.sub(stBuffer, MaxCols + 1)
  65.         --print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")")
  66.       end
  67.     else
  68.       print("found space at: "..space)
  69.       local sentence = ""
  70.       local nextspace
  71.       --loop to find the next space in the buffer
  72.       local word = string.sub(stBuffer,1,space)
  73.       print("Word: "..word)
  74.       repeat
  75.        
  76.         sentence = sentence..word
  77.         print("Sentence: "..sentence.." ("..string.len(sentence)..")")
  78.         --test that we don't go past the end
  79.         if (string.len(word)+1) > string.len(stBuffer) then
  80.           stBuffer = ""
  81.         else
  82.           stBuffer = string.sub(stBuffer,string.len(word)+1)
  83.         end
  84.        
  85.         nextspace = string.find(stBuffer, " ")
  86.         if nextspace == nil then
  87.           print("nextspacce: nil")
  88.         else
  89.           print("nextspace: "..nextspace)
  90.         end
  91.        
  92.         word = string.sub(stBuffer,1,nextspace)
  93.         print("nextword: "..word)
  94.        
  95.       until nextspace == nil or (string.len(sentence)+nextspace) >= MaxCols
  96.      
  97.       mon.setCursorPos(1, rowCount)
  98.       print("SetCursorPos(1, "..rowCount..")")
  99.       if nextspace == nil then
  100.         mon.write(sentence..word)
  101.         print("Wrote:"..sentence..word)
  102.       else
  103.         mon.write(sentence)
  104.         print("Wrote:"..sentence)
  105.       end      
  106.      
  107.     end --of finding a space
  108.    
  109.     --increment row counter
  110.     rowCount = rowCount + 1
  111.   end --while for each row
  112.  
  113. end
  114. --Have a Function to setup monitors where you
  115. --Input/Parameter the monitor object/variable
  116. --and the colors.color you want it to be
  117. function setupMonitor(mon, xcolor)
  118.   mon.clear()
  119.   mon.setTextScale(0.5)
  120.   mon.setCursorPos(1,1)
  121.   mon.setBackgroundColor(xcolor)
  122.   mon.clear()
  123.   mon.setCursorPos(1,1)
  124.   --mon.write("Welcome to Beyond Minecraft 101!")
  125.   --centerPrint("Welcome to Beyond Minecraft 101!", 1, mon)
  126.   --printCenter(mon,"Welcome to Beyond Minecraft 101!")
  127.   printCenterWrap(mon, "Welcome to Beyond Minecraft 101!")
  128. end
  129.  
  130.  
  131. --This is our main control function
  132. --to run the program
  133. function main()
  134.   --Determine which of the 4 monitors we have
  135.   --on our network and set them up accordingly
  136.   if Rmon then
  137.     setupMonitor(Rmon, colors.red)
  138.     --now assign the size of the first monitor
  139.     --to be the size of all monitors
  140.     monCols, monRows = Rmon.getSize()
  141.     --print("Connected Red Monitor, size: "..monCols.." columns "..monRows.." rows")
  142.   end
  143.   if Gmon then
  144.     --setupMonitor(Gmon, colors.green)
  145.     --print("Connected Green Monitor")
  146.   end
  147.   if Bmon then
  148.     --setupMonitor(Bmon, colors.blue)
  149.     --print("Connected Blue Monitor")
  150.   end
  151.   if Pmon then
  152.     --setupMonitor(Pmon, colors.purple)
  153.     --print("Connected Purple Monitor")
  154.   end
  155. end
  156.  
  157.  
  158. --Now kick-off our main program:
  159. main()
Advertisement
Add Comment
Please, Sign In to add comment