Advertisement
Guest User

test

a guest
May 3rd, 2014
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.01 KB | None | 0 0
  1. --//MultiSign
  2.  
  3. --//Returns an empty table. Obviously you can make an empty table of your own directly I added this for sake of completeness. The table is needed to add new signInstructions that can be drawn
  4.  
  5. function createSignHolder()
  6.     return {}
  7. end
  8.  
  9. --//Adds a new sign to a sign holder (table created by above method). You need to specify which table you are adding to (signHolder), and the side that the monitor is on. You should need to provide a function to draw the sign as well as any arguments needed (store an arbitrary amount of arguments inside a table and pass it to functionArgs)
  10.  
  11. function addNewSign(signHolder, monitorSide, drawFunction, drawFunctionArgs)
  12.     local t = {
  13.         ["monitor"] = monitorSide,
  14.         ["drawFunction"] = drawFunction,
  15.         ["functionArgs"] = drawFunctionArgs,
  16.         }
  17.     table.insert(signHolder, 0, t)
  18.     return t
  19. end
  20.  
  21. --//Wrapper function that can be called to organise all the right parameters to in turn call properly another function (called basicLabel). If you want to call basicLabel to draw your signs, pass this function as the drawFunction parameter in addNewSign
  22.  
  23. function basicLabelWrapper(mon, args)
  24.     basicLabel(mon, args["text"], args["background"], args["border"], args["title"], args["subTitle"], args["textSize"])
  25. end
  26.  
  27. --//An automatic way of adding arguments to basicLabelWrapper. title is required. Everything else has defaults or safely defaults to nil. This method returns a table which arranges all the parameters using the correct keys, ensuring fewer errors. It is not strictly necessary as you can make the tables manually. Pass the returned table into drawFunctionArgs in addNewSign()
  28.  
  29. function createSignArgsBasicLabel(title, subTitle, textSize, textColor, backGroundColor, borderColor)
  30.     if title == nil then
  31.         error("No title provided")
  32.     end
  33.     local t = {
  34.         ["text"] = textColor or colors.white,
  35.         ["background"] = backGroundColor or colors.black,
  36.         ["border"] = borderColor,
  37.         ["title"] = title,
  38.         ["subTitle"] = subTitle,
  39.         ["textSize"] = textSize or 1,
  40.         }
  41.     return t
  42. end
  43.  
  44. --//Pass a signHolder (containing an arbitrary number of sign tables made with addNewSign()) and drawSigns will loop through it and attempt to draw all your signs to your monitors. Make sure your monitors are connected.
  45.  
  46. function drawSigns(signHolder)
  47.     for i,v in pairs(signHolder) do
  48.         local monSide = v["monitor"]
  49.         local func = v["drawFunction"]
  50.         local args = v["functionArgs"]
  51.         local mon = nil
  52.         print(monSide)
  53.         if type(monSide) ~= "string" then
  54.             local str =  "Setting Sign at: "
  55.             local isAdjacent = false
  56.             for i,v in pairs(redstone.getSides()) do
  57.                 if monSide == v then
  58.                     isAdjacent = true
  59.                     break
  60.                 end
  61.             end
  62.             if isAdjacent == true then
  63.                 str = str.."monitor_"..monSide
  64.             else
  65.                 str = str..monSide
  66.             end
  67.             print(str)
  68.         end
  69.         mon = peripheral.wrap(monSide)
  70.         print(type(mon))
  71.         if type(mon) == "table" then
  72.             func(mon, args)
  73.         else
  74.             print("No valid monitor provided, skipping this sign. Key = "..i)
  75.         end    
  76.     end
  77. end
  78.  
  79. --//This function actually draws the signs. This can't be called directly inside the drawSigns function so don't run it through addNewSign(). Use the basicLabelWrapper instead. Otherwise, feel free to use it.
  80.  
  81. function basicLabel(mon, text, background, border, title, subTitle, textSize)
  82.     if textSize == nil then
  83.         textSize = 1
  84.     end
  85.     mon.setTextScale(textSize)
  86.     local x,y = mon.getSize()
  87.     mon.setBackgroundColor(background)
  88.     mon.clear()
  89.     if border ~= nil then
  90.         mon.setBackgroundColor(border)
  91.         for i = 1,x do
  92.             for j = 1,y do
  93.                 if (i == 1 or i == x) or (j == 1 or j == y) then
  94.                     mon.setCursorPos(i,j)
  95.                     mon.write(" ")
  96.                 end
  97.             end
  98.         end
  99.         mon.setBackgroundColor(background)
  100.     end
  101.     mon.setTextColor(text)
  102.     local tLength = string.len(title)
  103.     local height = nil
  104.     if subTitle ~= nil then
  105.         if y % 2 == 0 then
  106.             height = 2
  107.         else
  108.             height = 3
  109.         end
  110.     else
  111.         height = 1
  112.     end
  113.     local nX = (x/2)-(tLength/2) + 1
  114.     local nY = (y/2)-(height/2) + 1
  115.     mon.setCursorPos(nX,nY)
  116.     mon.write(title)
  117.     if subTitle ~= nil then
  118.         local sTlength = string.len(subTitle)
  119.         nX = (x/2)-(sTlength/2) + 1
  120.         nY = (y/2)+(height/2)
  121.         mon.setCursorPos(nX,nY)
  122.         mon.write(subTitle)
  123.     end
  124. end
  125.  
  126. local newSignHolder = createSignHolder()
  127. local temp = nil
  128.  
  129. --If you wish to test this program yourself, make sure to edit the monitor side names accordingly
  130.  
  131. temp = createSignArgsBasicLabel("Hello", "World", 2, colors.cyan, colors.black, colors.orange)
  132. addNewSign(newSignHolder, "monitor_0", basicLabelWrapper, temp)
  133.  
  134. temp = createSignArgsBasicLabel("I'm a sign", nil, 1.5, colors.lightBlue, colors.black, colors.purple)
  135. addNewSign(newSignHolder, "monitor_1", basicLabelWrapper, temp)
  136.  
  137. temp = createSignArgsBasicLabel("Borders are", "Optional", 1, colors.red, colors.black, nil)
  138. addNewSign(newSignHolder, "monitor_2", basicLabelWrapper, temp)
  139.  
  140. temp = createSignArgsBasicLabel("Optional", "Subtitles", 2, colors.yellow, colors.black, colors.purple)
  141. addNewSign(newSignHolder, "monitor_3", basicLabelWrapper, temp)
  142.  
  143. drawSigns(newSignHolder)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement