mj41374

CenterTestV2

Oct 2nd, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     t=read() --pause to debug
  47.     --find if there's a space character
  48.     local space = string.find(stBuffer, " ")
  49.  
  50.     if space == nil or space >= MaxCols then
  51.       print("found no space before EOL")
  52.       --This is the easiest case: we didn't
  53.       --find a space before the End-of-Line MaxCols
  54.       --so just print a substring up to EOL
  55.       mon.setCursorPos(1,rowCount) --center later
  56.       --print("SetCursorPos(1,"..rowCount..")")
  57.       local wl = string.sub(stBuffer,1,MaxCols)
  58.       mon.write(wl) --center this later
  59.       --print("Wrote:"..wl)
  60.       if (MaxCols + 1) > (string.len(stBuffer)) then
  61.         --we're don't processing
  62.         stBuffer = ""
  63.         --print("Set Buffer to EOL")
  64.       else
  65.         stBuffer = string.sub(stBuffer, MaxCols + 1)
  66.         --print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")")
  67.       end
  68.     else
  69.       print("found space at: "..space)
  70.       local sentence = ""
  71.       local nextspace
  72.       --loop to find the next space in the buffer
  73.       local word = string.sub(stBuffer,1,space)
  74.       print("Word: "..word)
  75.       repeat
  76.         s=read() --pause to debug
  77.         sentence = sentence..word
  78.         print("Sentence: "..sentence.." ("..string.len(sentence)..")")
  79.         --test that we don't go past the end
  80.         if (string.len(word)+1) > string.len(stBuffer) then
  81.           stBuffer = ""
  82.         else
  83.           stBuffer = string.sub(stBuffer,string.len(word)+1)
  84.           print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")")
  85.         end
  86.        
  87.         nextspace = string.find(stBuffer, " ")
  88.         if nextspace == nil then
  89.           print("nextspace: nil")
  90.         else
  91.           print("nextspace: "..nextspace)
  92.         end
  93.        
  94.         word = string.sub(stBuffer,1,nextspace) --this should be checking for nil!
  95.         print("nextword: "..word.." ("..string.len(word)..")")
  96.        
  97.       until nextspace == nil or (string.len(sentence)+nextspace) > MaxCols
  98.      
  99.       mon.setCursorPos(1, rowCount)
  100.       print("SetCursorPos(1, "..rowCount..")")
  101.       if nextspace == nil then
  102.         mon.write(sentence..word)
  103.         print("Wrote sen+word:"..sentence..word.." ("..#sentence+#word..")")
  104.         --check that we don't go past the end
  105.         if (string.len(word)+1) > string.len(stBuffer) then
  106.             stBuffer = ""
  107.             print("Cleared the buffer")
  108.         else
  109.           stBuffer = string.sub(stBuffer,string.len(word)+1)
  110.           print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")")
  111.         end
  112.       else
  113.         mon.write(sentence)
  114.         print("Wrote sen:"..sentence.." ("..#sentence..")")
  115.       end      
  116.      
  117.     end --of finding a space
  118.    
  119.     --increment row counter
  120.     rowCount = rowCount + 1
  121.   end --while for each row
  122.  
  123. end
  124. --Have a Function to setup monitors where you
  125. --Input/Parameter the monitor object/variable
  126. --and the colors.color you want it to be
  127. function setupMonitor(mon, xcolor)
  128.   mon.clear()
  129.   mon.setTextScale(0.5)
  130.   mon.setCursorPos(1,1)
  131.   mon.setBackgroundColor(xcolor)
  132.   mon.clear()
  133.   mon.setCursorPos(1,1)
  134.   --mon.write("Welcome to Beyond Minecraft 101!")
  135.   --centerPrint("Welcome to Beyond Minecraft 101!", 1, mon)
  136.   --printCenter(mon,"Welcome to Beyond Minecraft 101!")
  137.   printCenterWrap(mon, "Welcome to Beyond Minecraft 101 Class!", 3)
  138. end
  139.  
  140.  
  141. --This is our main control function
  142. --to run the program
  143. function main()
  144.   --Determine which of the 4 monitors we have
  145.   --on our network and set them up accordingly
  146.   if Rmon then
  147.     setupMonitor(Rmon, colors.red)
  148.     --now assign the size of the first monitor
  149.     --to be the size of all monitors
  150.     monCols, monRows = Rmon.getSize()
  151.     print("Connected Red Monitor")
  152.   end
  153. --[[  
  154.   if Gmon then
  155.     setupMonitor(Gmon, colors.green)
  156.     print("Connected Green Monitor")
  157.   end
  158.   if Bmon then
  159.     setupMonitor(Bmon, colors.blue)
  160.     print("Connected Blue Monitor")
  161.   end
  162.   if Pmon then
  163.     setupMonitor(Pmon, colors.purple)
  164.     print("Connected Purple Monitor")
  165.   end
  166. --]]
  167. end
  168.  
  169. --Now kick-off our main program:
  170. main()
Advertisement
Add Comment
Please, Sign In to add comment