Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.close("bottom")
- rednet.open("bottom")
- mon = peripheral.wrap("top")
- function alarm(val)
- if val == "On" then
- rednet.send(0,"alarmOn")
- end
- if val == "Off" then
- rednet.send(0,"alarmOff")
- end
- end
- -- Initial reactor and turbine status
- reactor = peripheral.wrap("BigReactors-Reactor_0")
- turbine = peripheral.wrap("BigReactors-Turbine_0")
- if reactor.getActive() == true then
- reactorBtnText = "Deactivate reactor"
- else
- reactorBtnText = "Activate reactor"
- end
- if turbine.getActive() == true then
- turbineBtnText = "Deactivate turbine"
- else
- turbineBtnText = "Activate turbine"
- end
- local button = { --Our main button table. Contains everything from button draw functions to the button information.
- button_defaults = { --This is the metatable which we give set new buttons to. It provides color/size defaults.
- __index = {
- color_bg = colors.orange;
- color_cl = colors.blue;
- color_txt = colors.black;
- height = 3;
- padding = 2;
- isClicked = false;
- };
- };
- 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.
- __call = function(self) --This allows us to call the table as if it were a function.
- for index, btn in pairs(self.buttons) do
- local color = btn.isClicked and btn.color_cl or btn.color_bg
- mon.setBackgroundColor(color)
- mon.setTextColor(btn.color_txt)
- for yPos = btn.y, btn.bounds.y2 do
- mon.setCursorPos(btn.x, yPos)
- mon.write(string.rep(" ", btn.width))
- end
- local text = btn.isClicked and btn.clickText or btn.text
- mon.setCursorPos(btn.x + (btn.width/2 - #text/2), btn.y + (btn.height/2))
- mon.write(text)
- end
- end;
- __newindex = function(t, key, value) --This changes the behavior of the table upon adding a new button
- 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
- assert(value.x, "Requires initial x")
- assert(value.y, "Requires initial y")
- assert(value.text, "Requires text value")
- setmetatable(value, t.button_defaults) --Give our new button its defaults with the __index metamethod
- value.width = #value.text + (value.padding * 2)
- value.bounds = {
- 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)
- y1 = value.y;
- x2 = value.x + value.width - 1; --In order to draw and detect clicks correctly, you need to subtract 1 from the width and height.
- y2 = value.y + value.height - 1;
- }
- 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)
- end;
- };
- checkClick = function(self, x,y) --This checks whether you have actually clicked on a table
- for index, btn in pairs(self.buttons) do
- if x>=btn.x and x<=btn.bounds.x2 and y>=btn.y and y<=btn.bounds.y2 then
- btn.isClicked = true --If we have actually clicked the button then set its click value to true
- if btn.onClick then --And check if it has an onClick function
- btn:onClick() --If so, then execute it and pass the button's table/info into it by using the colon operator
- end
- return index --Return the index of the button so we can unhighlight it
- end
- end
- end;
- buttons = {}; --This is the table that we'll keep all the buttons in
- }
- setmetatable(button, button.mt) --Set the metatable of button to button.mt
- button[1] = {
- x = 1;
- y = 1;
- text = reactorBtnText;
- clickText = "Please wait...";
- onClick = function(self)
- if reactor.getActive() == true then
- -- Deactivate reactor
- alarm("On")
- sleep(5.5)
- alarm("Off")
- reactor.setActive(false)
- self.text = "Activate reactor"
- else
- -- Deactivate reactor
- alarm("On")
- sleep(7.5)
- alarm("Off")
- reactor.setActive(true)
- self.text = "Deactivate reactor"
- end
- end
- }
- button[2] = {
- x = 4;
- y = 1;
- text = turbineBtnText;
- clickText = "Clicked.";
- onClick = function(self) --What is performed upon clicking the button
- mon.setBackgroundColor(colors.black)
- mon.setCursorPos(1,1)
- mon.write("New button title: ")
- --local input = read()
- --self.text = input --Set the button[2] text to input
- end;
- }
- local timer = { --This will keep track of clicked buttons/the timers associated with them
- index = false;
- timer = false;
- }
- while true do
- mon.clear()
- button() --Always draw the button first
- local e = {os.pullEvent()} --Then pull our events
- if e[1] == "monitor_touch" then
- local index = button:checkClick(e[3], e[4]) --Check the click: make sure to pass the button table into the checkClick function
- if index then
- timer.index = index--The index of the button that is clicked
- timer.timer = os.startTimer(1)
- end
- 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
- button.buttons[timer.index].isClicked = false --Deselect the button
- 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
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement