DrFair

PadAPI

May 4th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. local buttons = {}
  2. local lastEvent = {}
  3. local buttonInfo = {}
  4. local col = { ["white"]=1, ["orange"]=2, ["magenta"]=4, ["lightblue"]=8, ["yellow"]=16, ["lime"]=32, ["pink"]=64, ["gray"]=128, ["lightgray"]=256, ["cyan"]=512, ["purple"]=1024, ["blue"]=2048, ["brown"]=4096, ["green"]=8192, ["red"]=16384, ["black"]=32768 }
  5. local defaultBackgroundColor = "black"
  6.  
  7. function clear()
  8. term.clear()
  9. return true
  10. end
  11.  
  12. function getSize()
  13. return term.getSize()
  14. end
  15.  
  16. function setDefaultBackgroundColor( color )
  17. if col[color] == nil then
  18. error("Error: Background color "..color.." does not exist.")
  19. end
  20. local defaultBackgroundColor = color
  21. return true
  22. end
  23.  
  24. function submitEvents()
  25. local event,pr1,pr2,pr3 = os.pullEvent()
  26. lastEvent = { [1]=event, [2]=pr1, [3]=pr2, [4]=pr3 }
  27. return true
  28. end
  29.  
  30. function getEvents()
  31. return lastEvent
  32. end
  33.  
  34. function resetButtons()
  35. buttons = {}
  36. end
  37.  
  38. function drawButton( text, name, x, width, y, height , txtcolor, bgcolor )
  39. if #text > width then
  40. error("Error: "..name.." button text too big.")
  41. end
  42. if col[txtcolor] == nil then error("Error: "..name.." button txtcolor wrong.") end
  43. if col[bgcolor] == nil then error("Error: "..name.." button bgcolor wrong.") end
  44. term.setCursorPos( x, y )
  45. term.setBackgroundColor( col[bgcolor] )
  46. term.setTextColor( col[txtcolor] )
  47. for i=1,height do
  48. for j=1,width do
  49. term.setCursorPos( j + x - 1, i + y - 1 )
  50. term.write( " " )
  51. end
  52. end
  53. term.setCursorPos( x + width/2 - #text/2, y + height/2 )
  54. term.write( text )
  55. term.setBackgroundColor( col[defaultBackgroundColor] )
  56. buttons[name] = textutils.serialize( { [1]=x, [2]=x+width, [3]=y, [4]=y+height } )
  57. buttonInfo[name] = { ["text"]=text, ["x"]=x, ["width"]=width, ["y"]=y, ["height"]=height, ["txtcolor"]=txtcolor, ["bgcolor"]=bgcolor }
  58. return true
  59. end
  60.  
  61. function drawAlignButton( text, align, name, x, width, y, height, txtcolor, bgcolor )
  62. if align == "left" or align == "right" or align == "mid" or align == "middle" then
  63. if align == "left" then
  64. xAlign = x
  65. elseif align == "right" then
  66. xAlign = x - width
  67. elseif align == "mid" or align == "middle" then
  68. xAlign = x - width/2
  69. end
  70. drawButton( text, name, xAlign, width, y, height, txtcolor, bgcolor )
  71. else
  72. error("Error: Align has to be left, mid or right.")
  73. end
  74. return true
  75. end
  76.  
  77. function getButtonInfo( name )
  78. return buttonInfo[name]
  79. end
  80.  
  81. function getButtonText( name )
  82. return buttonInfo[name]["text"]
  83. end
  84.  
  85. function getButtonX( name )
  86. return buttonInfo[name]["x"]
  87. end
  88.  
  89. function getButtonY( name )
  90. return buttonInfo[name]["y"]
  91. end
  92.  
  93. function getButtonWidth( name )
  94. return buttonInfo[name]["width"]
  95. end
  96.  
  97. function getButtonHeight( name )
  98. return buttonInfo[name]["height"]
  99. end
  100.  
  101. function getButtonTextColor( name )
  102. return buttonInfo[name]["txtcolor"]
  103. end
  104.  
  105. function getButtonColor( name )
  106. return buttonInfo[name]["bgcolor"]
  107. end
  108.  
  109. function drawText( text, x, y, txtcolor, bgcolor )
  110. if col[txtcolor] == nil then error("Error: "..text.." drawText txtcolor wrong.") end
  111. if bgcolor == nil then
  112. term.setBackgroundColor( col[defaultBackgroundColor] )
  113. else
  114. if col[bgcolor] == nil then error("Error: "..text.." drawText bgcolor wrong.") end
  115. term.setBackgroundColor( col[bgcolor] )
  116. end
  117. term.setCursorPos( x, y )
  118. term.setTextColor( col[txtcolor] )
  119. term.write( text )
  120. term.setBackgroundColor( col[defaultBackgroundColor] )
  121. return true
  122. end
  123.  
  124. function drawAlignText( text, align, x, y, txtcolor, bgcolor )
  125. if align == "left" or align == "right" or align == "mid" or align == "middle" then
  126. if align == "left" then
  127. xAlign = x
  128. elseif align == "right" then
  129. xAlign = x - #text
  130. elseif align == "mid" or align == "middle" then
  131. xAlign = x - #text/2
  132. end
  133. drawText( text, xAlign, y, txtcolor, bgcolor )
  134. else
  135. error("Error: Align has to be left, mid or right.")
  136. end
  137. return true
  138. end
  139.  
  140. function drawOutline( header, footer, txtcolor, linecolor )
  141. local w,h = term.getSize()
  142. local w = w + 1
  143. local h = h + 1
  144. drawButton("", "", 1, w-1, 1, h-1, txtcolor, linecolor)
  145. drawButton("", "", 2, w-3, 2, h-3, txtcolor, defaultBackgroundColor)
  146. drawAlignText( header, "mid", w/2, 1, txtcolor, linecolor)
  147. drawAlignText( footer, "mid", w/2, h-1, txtcolor, linecolor)
  148. return true
  149. end
  150.  
  151. function buttonPressed( name )
  152. if buttons[name] == nil then
  153. return false
  154. end
  155. if lastEvent[1] == "monitor_touch" then
  156. local tButton = textutils.unserialize( buttons[name] )
  157. if lastEvent[3] >= tButton[1] and lastEvent[3] < tButton[2] and lastEvent[4] >= tButton[3] and lastEvent[4] < tButton[4] then
  158. return true
  159. end
  160. end
  161. return false
  162. end
  163.  
  164. local Args = {...}
  165. if Args[1] == "update" then
  166. print("Updating PadAPI.")
  167. fs.delete("papi")
  168. shell.run("pastebin","get","BsrHWQbW","papi")
  169. print("Finished updating.")
  170. end
Advertisement
Add Comment
Please, Sign In to add comment