Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Button CLASS
- gui = { monitor = {}, textsize = 0.5, buttons = {} };
- local Button = { name = "Button", message = "none", protocol = "protocol", host = "host", x = 0, y = 0, width = 5, };
- function Button:new (o, name, message, host, protocol, x, y)
- o = o or {}
- setmetatable(o, {__index = self});
- -- o.__index = self;
- o.name = name or "button";
- o.message = message or "none";
- o.host = host or "host";
- o.protocol = protocol or "protocol";
- o.x = x or 0;
- o.y = y or 0;
- o.width = string.len(name);
- print("new button");
- return o;
- end
- function Button:render(col, mon)
- mon.setBackgroundColor(col);
- mon.setCursorPos(self.x, self.y);
- mon.write(self.name, colors.white, col);
- end
- function Button:clicked (x, y)
- local isClicked = (x >= self.x and x <= (self.x + self.width) and y == self.y);
- if isClicked then
- local id = rednet.lookup(self.protocol, self.host);
- if id then
- rednet.send(id, self.message, self.protocol);
- else
- print("Not found: " .. self.host .. " : " .. self.protocol);
- end
- end
- return isClicked;
- end
- function gui:new(o, textsize, mon)
- o = o or {};
- setmetatable(o, {__index = self});
- o.buttons = {};
- o.textsize = textsize or 0.5;
- print("mon: " .. tostring(mon));
- if (not mon) then return o end;
- print("clearing");
- o.monitor = mon or nil;
- o.monitor.setTextScale(o.textsize);
- o.monitor.setBackgroundColor(colors.gray);
- o.monitor.clear();
- return o;
- end
- function gui:addButton (name, message, host, protocol, x, y)
- self.buttons[#(self.buttons) + 1] = self.Button:new(nil, name, message, host, protocol, x, y)
- end
- function gui:render(x, y, col)
- if(not self.monitor) then return end;
- self.monitor.setTextScale(self.textsize);
- self.monitor.setBackgroundColor(colors.gray);
- self.monitor.clear();
- for it, val in pairs(self.buttons) do
- val:render(col, self.monitor);
- if val:clicked(x, y) then
- val:render(colors.red, self.monitor);
- end
- end
- end
- gui.Button = Button;
- -- Button CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement