Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local button = {
- button_defaults = {
- __index = {
- color_bg = colors.orange;
- color_cl = colors.blue;
- 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(btn.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-#btn.text/2),btn.y + (btn.height/2))
- end;
- __newindex = function(t,k,v)
- 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 = #value.text + (value.padding * 2)
- value.bounds = {
- x1 = value.x;
- y1 = value.y;
- x2 = value.x + value.width - 1;
- y2 = value.y + value.height - 1;
- }
- rawset(t.button, key, value)
- end;
- };
- checkClick = function(self,x,y)
- 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 btn.onClick then
- btn:onClick()
- end
- return index
- end
- end
- end;
- buttons = {};
- }
- setmetatable(button,button.mt)
- button[1] = {
- x = 1;
- y = 1;
- text = "This is a button";
- clickText = "You just clicked me!"
- }
- button[2] = {
- x = 5;
- y = 5;
- height = 5;
- text = "Another button";
- clickText = "CLICLED ME!!";
- onClick = function()
- end;
- }
- local timer = {
- index = false;
- timer = false;
- }
- while true do
- button()
- local e = {os.pullEvent()}
- if e[1] == "mouse_click" then
- local index = button:checkClick(e[3],e[4])
- timer.index = index
- timer.timer = os.startTimer(1)
- elseif e[1] == "timer" and e[2] == timer.timer then
- button.buttons[timer.index].isClicked = false
- timer = {}
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement