Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local button = {
- buton_defaults = {
- __index = {
- color_bg = colors.gray;
- color_cl = colors.red;
- color_txt = colors.black
- height = 3;
- padding = 2;
- isClicked = false;
- };
- };
- mt = {
- __call = function(self)
- for index, btn in pairs(self.buttons) do
- local color = btn.isClicked and btn.color_cl or btn.color_bg
- term.setBackgroundColor(color)
- term.setTextColor(color_txt)
- for yPos = btn.y, btn.bounds.y2 do
- term.setCursorPos(btn.x, yPos)
- term.write(string.rep(" ", btn.width)
- end
- local text = btn.isClicked and btn.clickText or btn.text
- term.setCursorPos(btn.x + (btn.width/2 - #text/2), btn.y + (btn.height/2))
- term.write(text)
- end
- end;
- __newindex = function(t, key, value)
- assert(type(value)=="table", "Requires a table")
- assert(value.x, "Requires initial x")
- assert(value.y, "Requires initial y")
- assert(value.text, "Requires text value")
- setmetatable(value, t.button_defaults)
- value.width = #text + (value.padding *2)
- value.bounds = {
- x1 = value.x;
- y1 = value.y;
- x2 = value.x + value.width - 1;
- y2 = value.y + value.height - 1;
- }
- t.buttons[key]=value
- end;
- };
- checkClick = function(self, x,y)
- for index, btn in pairs(self.button) do
- if x>=btn.x and x<=btn.bounds.x2 and y>=btn.y and y<=btn.bounds.y2 then
- btn.isClicked = true
- if btn.onClick then
- btn:onClick()
- end
- return index
- end
- end
- end;
- buttons = {};
- }
- setmetatable(button, button.mt)
- button[1] = {
- x = 5;
- y = 3;
- text = "On"
- clickText = "Working...";
- onClick = function(self)
- if Power == 0 then
- self.text = "Off"
- Power = 1
- else
- self.text = "On"
- Power = 0
- end
- end;
- }
- button[2] = {
- x = 5
- y = 5
- text = "Volume +"
- onClick = function(self)
- Volume + 1
- end;
- }
- button [3] = {
- x = 5
- y = 6
- text = "Volume -"
- onClick = function(self)
- Volume - 1
- end;
- }
- local timer = {
- index = false
- timer = false
- }
- Power = 0
- Volume = 5
- while true do
- button()
- local e = {os.pullEvent()}
- if e[1] == "mouse_click" then
- local index = mouse.checkClick(e[3], e[4])
- if index then
- timer.index = index
- timer.timer = os.startTimer
- end
- elseif e[1] == "timer" and e[2] == timer.timer then
- button.buttons[timer.index].isClicked = false
- timer = {}
- end
- if Power == 1 then
- if Volume == 1 then
- redstone.setAnalogOutput("left", 1)
- elseif Volume == 2 then
- redstone.setAnalogOutput("left", 2)
- elseif Volume == 3 then
- redstone.setAnalogOutput("left", 3)
- elseif Volume == 4 then
- redstone.setAnalogOutput("left", 4)
- elseif Volume == 5 then
- redstone.setAnalogOutput("left", 5)
- elseif Volume == 6 then
- redstone.setAnalogOutput("left", 6)
- elseif Volume == 7 then
- redstone.setAnalogOutput("left", 7)
- elseif Volume == 8 then
- redstone.setAnalogOutput("left", 8)
- elseif Volume == 9 then
- redstone.setAnalogOutput("left", 9)
- elseif Volume == 10 then
- redstone.setAnalogOutput("left", 10)
- elseif Volume == 11 then
- redstone.setAnalogOutput("left", 11)
- elseif Volume == 12 then
- redstone.setAnalogOutput("left", 12)
- elseif Volume == 13 then
- redstone.setAnalogOutput("left", 13)
- elseif Volume == 14 then
- redstone.setAnalogOutput("left", 14)
- elseif Volume == 15 then
- redstone.setAnalogOutput("left", 15)
- elseif Volume == 16 then
- redstone.setAnalogOutput("left", 16)
- elseif Volume == 0 then
- Volume = 1
- elseif Volume == 17 then
- Volume = 16
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment