Advertisement
VADemon

Button / Text API

Nov 30th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.49 KB | None | 0 0
  1. monitorSide = "right"
  2.  
  3. mon = peripheral.wrap(monitorSide)
  4.  
  5. buttonInfo = {}
  6. textInfo = {}
  7.  
  8. if mon.isColor() then
  9.     print("Advanced monitor attached, continuing!")
  10. else
  11.     print("You must attach an advanced monitor!\n\nAborting!")
  12.     error()
  13. end
  14.  
  15. function fillBackground(peripheral, color, xStart, yStart, xEnd, yEnd)
  16.     peripheral.setBackgroundColor(color)
  17.     for y = yStart, yEnd do
  18.         peripheral.setCursorPos(xStart, y)
  19.         peripheral.write(string.rep(" ", xEnd - xStart))
  20.     end
  21. end
  22.  
  23. -- Parameters:
  24. -- per = attached peripheral function
  25. -- func = function to execute onClick
  26. -- colorON/OFF = (in-)active color
  27. -- enabled = enabled when drawing?
  28. -- mode = 0 toggle | >0 - change color for * seconds | <0 - blink for * seconds
  29. function createButton(per, func, text, textColorOFF, textColorON, bgColorOFF, bgColorON,  enabled, mode,  xStart, yStart, xSize, ySize)
  30.     if textColorON == -1 then
  31.         textColorON = textColorOFF
  32.     end
  33.     if bgColorON == -1 then
  34.         bgColorON = bgColorOFF
  35.     end
  36.    
  37.     if enabled then
  38.         drawButton(per, text, textColorON, bgColorON, xStart, yStart, xSize, ySize)
  39.     else
  40.         drawButton(per, text, textColorOFF, bgColorOFF, xStart, yStart, xSize, ySize)
  41.     end
  42.    
  43.     buttonInfo[ #buttonInfo + 1 ] = {
  44.         [1] = xStart,
  45.         [2] = yStart,
  46.         [3] = (xSize == 0 and xStart + string.len(text) or xSize),  -- xEnd
  47.         [4] = (ySize == 0 and ySize + 1 or ySize),  -- yEnd
  48.         [5] = per,
  49.         [6] = func,
  50.         [7] = text,
  51.         [8] = textColorOFF,
  52.         [9] = textColorON,
  53.         [10] = bgColorOFF,
  54.         [11] = bgColorON,
  55.         [12] = enabled,
  56.         [13] = mode,
  57.         [14] = xSize,
  58.         [15] = ySize
  59.     }
  60.     return #buttonInfo - 1
  61. end
  62.  
  63. function createText(per, text, textColor, bgColor, xStart, yStart, xSize, ySize)
  64. -- multiline draw functionality
  65.     --drawButton(per, text, textColor, bgColor, xStart, yStart, xSize, ySize)
  66.    
  67.     textInfo[ #buttonInfo + 1 ] = {
  68.         [1] = xStart,
  69.         [2] = yStart,
  70.         [3] = (xSize == 0 and xStart + string.len(text) or xSize),  -- xEnd
  71.         [4] = (ySize == 0 and ySize + 1 or ySize),  -- yEnd
  72.         [5] = per,
  73.         [6] = text,
  74.         [7] = textColor,
  75.         [8] = bgColor,
  76.         [9] = xSize,
  77.         [10] = ySize
  78.     }
  79.     return #textInfo - 1
  80. end
  81.  
  82. function drawButton(per, text, textColor, bgColor, xStart, yStart, xSize, ySize)
  83.     if xSize or ySize then -- need to fill background?
  84.         if not xSize then xSize = 0 end
  85.         if not ySize then ySize = 0 end
  86.         fillBackground(per, bgColor, xStart - xSize, yStart - ySize, xStart + string.len(text) + xSize, yStart + ySize)
  87.     end
  88.    
  89.     per.setBackgroundColor(bgColor)
  90.     per.setTextColor(textColor)
  91.     per.setCursorPos(xStart, yStart)
  92.    
  93.     per.write(text)
  94. end
  95.  
  96. function blinkButton(per, text, textColorOFF, textColorON, bgColorOFF, bgColorON, mode, enabled, xStart, yStart, xSize, ySize)
  97.    
  98.    
  99. end
  100.  
  101. function executeMode(per, text, textColorOFF, textColorON, bgColorOFF, bgColorON, mode, enabled, xStart, yStart, xSize, ySize)
  102.     local textColor1, bgColor1, textColor2, bgColor2
  103.     if enabled then -- swap colors
  104.         textColor1, bgColor1, textColor2, bgColor2 = textColorOFF, bgColorOFF, textColorON, bgColorON
  105.     else
  106.         textColor1, bgColor1, textColor2, bgColor2 = textColorON, bgColorON, textColorOFF, bgColorOFF
  107.     end
  108.  
  109.    
  110.     if mode == 0 then -- TOGGLE
  111.         drawButton(per, text, textColor1, bgColor1, xStart, yStart, xSize, ySize)
  112.  
  113.     elseif mode > 0 then -- CHANGE COLOR FOR <MODE> SECONDS
  114.         drawButton(per, text, textColorOFF, bgColorOFF, xStart, yStart, xSize, ySize)
  115.         sleep(mode)
  116.         drawButton(per, text, textColorON, bgColorON, xStart, yStart, xSize, ySize)
  117.        
  118.     else -- mode < 0 && BLINK
  119.         local time = math.abs(mode)
  120.         print("Blink time: ".. time)
  121.         while time > 0 do
  122.             print("tick ", time)
  123.             drawButton(per, text, textColorOFF, bgColorOFF, xStart, yStart, xSize, ySize)
  124.             sleep( .4 )
  125.             drawButton(per, text, textColorON, bgColorON, xStart, yStart, xSize, ySize)
  126.             sleep( .4 )
  127.             print("tok")
  128.             time = time - .4
  129.         end
  130.     end
  131. end
  132.  
  133. function onButtonClick(id, x, y)
  134.     local xStart, yStart, xEnd, yEnd, per, func, text, textColorOFF, textColorON, bgColorOFF, bgColorON, enabled, mode, xSize, ySize = unpack(buttonInfo[ id ])
  135.     -- ^ should work defining all vars
  136.     print(xStart == buttonInfo[id][1], mode == buttonInfo[id][13], ySize == buttonInfo[ id ][15])
  137.  
  138.     func(id, x, y)
  139.     executeMode(per, text, textColorOFF, textColorON, bgColorOFF, bgColorON, mode, enabled, xStart, yStart, xSize, ySize)
  140.    
  141.     buttonInfo[ id ][12] = not enabled --toggle state
  142. end
  143.  
  144. function buttonBlink(id)
  145.    
  146. end
  147.  
  148. fillBackground(mon, 4096, 0, 0, mon.getSize())
  149. --fillBackground(mon, 2048, 5, 8, 4, 5)
  150. createButton(mon, print, "TOGGLE", 1, -1, 8192, 2048, true, 0, 7, 8, 4, 3)
  151. createButton(mon, print, "BLINK", 1, 16, 64, 256, true, -6, 7, 1, 10, 1)
  152. createButton(mon, print, "CHANGE COLOR", 1, 16, 64, 256, true, 2, 15, 16, 2.5, 1)
  153.  
  154. while true do
  155.     local event, side, x, y = os.pullEvent("monitor_touch")
  156.     print("\n"..x .. ":" .. y)
  157.    
  158.     for key, button in pairs(buttonInfo) do
  159.         local xStart, yStart, xEnd, yEnd = button[1] - button[14], button[2] - button[15], button[1] + string.len(button[7]) + button[14], button[2] + button[15]
  160.         print("\n"..xStart .. ":" .. yStart .. " -- " .. xEnd .. ":" .. yEnd)
  161.         print("cYs: " .. yStart .." <= "..y..", "..tostring(yStart<=y) )
  162.         print("cYe: " .. y .." <= "..yEnd..", "..tostring(y<=yEnd) )
  163.         if yStart <= y and y <= yEnd then
  164.             print("cXs: " .. xStart .." <= "..x..", "..tostring(xStart<=x) )
  165.             print("cXe: " .. x .." <= "..xEnd..", "..tostring(x<=xEnd) )
  166.             if xStart <= x and x <= xEnd then
  167.                 print("Key is: " .. key)
  168.                 onButtonClick(key, x, y)
  169.                 break
  170.             end
  171.         end
  172.     end
  173. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement