Advertisement
Guest User

Untitled

a guest
May 27th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. rednet.close("bottom")
  2. rednet.open("bottom")
  3. mon = peripheral.wrap("top")
  4.  
  5. function alarm(val)
  6. if val == "On" then
  7. rednet.send(0,"alarmOn")
  8. end
  9. if val == "Off" then
  10. rednet.send(0,"alarmOff")
  11. end
  12. end
  13.  
  14. -- Initial reactor and turbine status
  15. reactor = peripheral.wrap("BigReactors-Reactor_0")
  16. turbine = peripheral.wrap("BigReactors-Turbine_0")
  17. if reactor.getActive() == true then
  18. reactorBtnText = "Deactivate reactor"
  19. else
  20. reactorBtnText = "Activate reactor"
  21. end
  22. if turbine.getActive() == true then
  23. turbineBtnText = "Deactivate turbine"
  24. else
  25. turbineBtnText = "Activate turbine"
  26. end
  27.  
  28.  
  29.  
  30. local button = { --Our main button table. Contains everything from button draw functions to the button information.
  31. button_defaults = { --This is the metatable which we give set new buttons to. It provides color/size defaults.
  32. __index = {
  33. color_bg = colors.orange;
  34. color_cl = colors.blue;
  35. color_txt = colors.black;
  36.  
  37. height = 3;
  38. padding = 2;
  39. isClicked = false;
  40. };
  41. };
  42. mt = { --This is the main metatable of the button table. It changes the behavior of the table to allow for calling and adding new indexes.
  43. __call = function(self) --This allows us to call the table as if it were a function.
  44. for index, btn in pairs(self.buttons) do
  45. local color = btn.isClicked and btn.color_cl or btn.color_bg
  46. mon.setBackgroundColor(color)
  47. mon.setTextColor(btn.color_txt)
  48. for yPos = btn.y, btn.bounds.y2 do
  49. mon.setCursorPos(btn.x, yPos)
  50. mon.write(string.rep(" ", btn.width))
  51. end
  52. local text = btn.isClicked and btn.clickText or btn.text
  53. mon.setCursorPos(btn.x + (btn.width/2 - #text/2), btn.y + (btn.height/2))
  54. mon.write(text)
  55. end
  56. end;
  57.  
  58. __newindex = function(t, key, value) --This changes the behavior of the table upon adding a new button
  59. assert(type(value)=="table", "Requires a table") --assert will check that a condition is true; if it is not, it will error with the provided text
  60. assert(value.x, "Requires initial x")
  61. assert(value.y, "Requires initial y")
  62. assert(value.text, "Requires text value")
  63. setmetatable(value, t.button_defaults) --Give our new button its defaults with the __index metamethod
  64. value.width = #value.text + (value.padding * 2)
  65. value.bounds = {
  66. x1 = value.x; --I don't use the x1 or y1 vars from the bounds table due to the fact that it's shorter to simply type btn.x than btn.bounds.x, but they are equal to the same thing (obviously)
  67. y1 = value.y;
  68. x2 = value.x + value.width - 1; --In order to draw and detect clicks correctly, you need to subtract 1 from the width and height.
  69. y2 = value.y + value.height - 1;
  70. }
  71. t.buttons[key]=value --In the video, I am aware that I used rawset. However, it is actually not necessary because we are not changing the button table directly, but rather the button.buttons table (which has no newindex metamethod)
  72. end;
  73. };
  74.  
  75. checkClick = function(self, x,y) --This checks whether you have actually clicked on a table
  76. for index, btn in pairs(self.buttons) do
  77. if x>=btn.x and x<=btn.bounds.x2 and y>=btn.y and y<=btn.bounds.y2 then
  78. btn.isClicked = true --If we have actually clicked the button then set its click value to true
  79. if btn.onClick then --And check if it has an onClick function
  80. btn:onClick() --If so, then execute it and pass the button's table/info into it by using the colon operator
  81. end
  82. return index --Return the index of the button so we can unhighlight it
  83. end
  84. end
  85. end;
  86.  
  87. buttons = {}; --This is the table that we'll keep all the buttons in
  88.  
  89. }
  90. setmetatable(button, button.mt) --Set the metatable of button to button.mt
  91.  
  92. button[1] = {
  93. x = 1;
  94. y = 1;
  95. text = reactorBtnText;
  96. clickText = "Please wait...";
  97. onClick = function(self)
  98. if reactor.getActive() == true then
  99. -- Deactivate reactor
  100. alarm("On")
  101. sleep(5.5)
  102. alarm("Off")
  103. reactor.setActive(false)
  104. self.text = "Activate reactor"
  105. else
  106. -- Deactivate reactor
  107. alarm("On")
  108. sleep(7.5)
  109. alarm("Off")
  110. reactor.setActive(true)
  111. self.text = "Deactivate reactor"
  112. end
  113. end
  114. }
  115.  
  116. button[2] = {
  117. x = 4;
  118. y = 1;
  119. text = turbineBtnText;
  120. clickText = "Clicked.";
  121. onClick = function(self) --What is performed upon clicking the button
  122. mon.setBackgroundColor(colors.black)
  123. mon.setCursorPos(1,1)
  124. mon.write("New button title: ")
  125. --local input = read()
  126. --self.text = input --Set the button[2] text to input
  127. end;
  128. }
  129.  
  130. local timer = { --This will keep track of clicked buttons/the timers associated with them
  131. index = false;
  132. timer = false;
  133. }
  134. while true do
  135. mon.clear()
  136. button() --Always draw the button first
  137. local e = {os.pullEvent()} --Then pull our events
  138. if e[1] == "monitor_touch" then
  139. local index = button:checkClick(e[3], e[4]) --Check the click: make sure to pass the button table into the checkClick function
  140. if index then
  141. timer.index = index--The index of the button that is clicked
  142. timer.timer = os.startTimer(1)
  143. end
  144. elseif e[1] == "timer" and e[2] == timer.timer then --If we get a timer event and the ID is equal to the timer.timer var then
  145. button.buttons[timer.index].isClicked = false --Deselect the button
  146. timer = {} --This is actually fairly memory inneficient, but for such a small program it doesn't really matter. Rather than do this though, you should probably just manually set the values of timer.index/timer.timer to false
  147. end
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement