Advertisement
feedmecookies

Button api

Nov 21st, 2020 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. --TouchPc API--
  2. button = {}
  3. Text = {}
  4. CurrentPage = 1
  5. side = "left"
  6. pp = peripheral.isPresent(side)
  7. if pp then
  8. m = peripheral.wrap(side)
  9. end
  10.  
  11.  
  12. floppy = false --Currently does nothing, but is going to be used in a later release to be able to save and retrieve the status on each button on load/exit--
  13.  
  14.  
  15. function setPage(page)
  16. CurrentPage = page
  17. ClearScreen()
  18. end
  19.  
  20.  
  21.  
  22. function ChangePeripheral()
  23. end
  24.  
  25. function CreateButton(Id,MinX,MaxX,MinY,MaxY,Page,Function,Color,State,Text,...) -- the ... is for the optional function to be triggered when a button changes to the "off" state
  26. -- Create a list of buttons and their variables --
  27. button[Id] = {}
  28. button[Id][1] = MinX
  29. button[Id][2] = MaxX
  30. button[Id][3] = MinY
  31. button[Id][4] = MaxY
  32. button[Id][5] = Text
  33. button[Id][6] = Function
  34. button[Id][7] = Color
  35. button[Id][8] = State
  36. button[Id][9] = Page
  37. button[Id][10] = ...
  38. end
  39.  
  40. function AddText(Id,text,x,y,page,colorT,colorB)
  41. Text[Id] = {}
  42. Text[Id][1] = text
  43. Text[Id][2] = x
  44. Text[Id][3] = y
  45. Text[Id][4] = page
  46. Text[Id][5] = colorT
  47. Text[Id][6] = colorB
  48. end
  49.  
  50.  
  51.  
  52.  
  53.  
  54. function CheckButtons(event,x,y) --eventc is used to define whether it is a monitor event or a terminal event.--
  55. -- Checks if the the player clicked a button--
  56.  
  57.  
  58.  
  59. --Change it to be able to distinguish what page the button is on
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. if event == "monitor_touch" or event == "mouse_click" then
  71. for i = 1, table.getn(button) do
  72. if x >= button[i][1] and x <= button[i][2] and y >= button[i][3] and y <= button[i][4] and button[i][9] == CurrentPage then
  73. if button[i][8] == "on" then
  74. button[i][8] = "off"
  75. button[i][10]()
  76. elseif button[i][8] == "off" then
  77. button[i][8] = "on"
  78. button[i][6]()
  79. elseif button[i][8] == "none" then
  80. button[i][6]()
  81. end
  82. sleep(0.1)
  83. end
  84. end
  85. end
  86. end
  87.  
  88.  
  89.  
  90. function ClearScreen()
  91. if pp == true then
  92. m.setBackgroundColor(colors.black)
  93. m.clear()
  94. else
  95. term.setBackgroundColor(colors.black)
  96. term.clear()
  97. end
  98. end
  99.  
  100.  
  101.  
  102. function DrawButton()
  103. ClearScreen()
  104. --draws the button on the screen--
  105. for i = 1, table.getn(button) do
  106. if button[i][9] == CurrentPage then
  107. if pp == true then
  108. m.setBackgroundColor(button[i][7])
  109. else
  110. term.setBackgroundColor(button[i][7])
  111. end
  112. for y = button[i][3], button[i][4] do
  113. for x = button[i][1], button[i][2] do
  114. if pp == true then
  115. m.setCursorPos(x,y)
  116. m.write(" ")
  117. else
  118. term.setCursorPos(x,y)
  119. term.write(" ")
  120. end
  121. end
  122. if pp == true then
  123. m.setCursorPos(button[i][1],button[i][3])
  124. m.write(button[i][5])
  125. else
  126. term.setCursorPos(button[i][1],button[i][3])
  127. term.write(button[i][5])
  128. end
  129. end
  130. if pp == true then
  131. m.setBackgroundColor(colors.black)
  132. else
  133. term.setBackgroundColor(colors.black)
  134. end
  135. end
  136. end
  137. for i = 1, table.getn(Text) do
  138. if Text[i][4] == CurrentPage then
  139. if pp == true then
  140. m.setBackgroundColor(Text[i][6])
  141. m.setTextColor(Text[i][5])
  142. m.setCursorPos(Text[i][2],Text[i][3])
  143. m.write(Text[i][1])
  144. m.setBackgroundColor(colors.black)
  145. else
  146. term.setBackgroundColor(Text[i][6])
  147. term.setTextColor(Text[i][5])
  148. term.setCursorPos(Text[i][2],Text[i][3])
  149. term.write(Text[i][1])
  150. term.setBackgroundColor(colors.black)
  151. end
  152. end
  153. end
  154. return event,z,x,y
  155. end
  156.  
  157.  
  158.  
  159. function DialogBox(message,x,y)
  160. --enables the use of dialog boxes--
  161. if pp == true then
  162. m.setBackgroundColor(colors.black)
  163. m.clear()
  164. m.setCursorPos(x,y)
  165. m.write(message)
  166. m.setCursorPos(x,y+1)
  167. msg = read()
  168. m.clear()
  169. return msg
  170. end
  171.  
  172.  
  173. end
  174.  
  175. function SaveTo(info,file)
  176. -- in the case you would like to save a information to a file(especially useful to save dialog box messages) --
  177. end
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement