Advertisement
CaptainResu

GUI

Dec 27th, 2022 (edited)
1,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.16 KB | None | 0 0
  1. -- Button CLASS
  2. gui = { monitor = {}, textsize = 0.5, buttons = {} };
  3. local Button = { name = "Button", message = "none", protocol = "protocol", host = "host", x = 0, y = 0, width = 5,  };
  4.  
  5. function Button:new (o, name, message, host, protocol, x, y)
  6.     o = o or {}
  7.     setmetatable(o, {__index = self});
  8.     -- o.__index = self;
  9.     o.name = name or "button";
  10.     o.message = message or "none";
  11.     o.host = host or "host";
  12.     o.protocol = protocol or "protocol";
  13.     o.x = x or 0;
  14.     o.y = y or 0;
  15.  
  16.     o.width = string.len(name);
  17.  
  18.     print("new button");
  19.     return o;
  20. end
  21. function Button:render(col, mon)
  22.     mon.setBackgroundColor(col);
  23.     mon.setCursorPos(self.x, self.y);
  24.     mon.write(self.name, colors.white, col);
  25. end
  26. function Button:clicked (x, y)
  27.    
  28.     local isClicked = (x >= self.x and x <= (self.x + self.width) and y == self.y);
  29.     if isClicked then
  30.         local id = rednet.lookup(self.protocol, self.host);
  31.         if id then
  32.             rednet.send(id, self.message, self.protocol);
  33.         else
  34.             print("Not found: " .. self.host .. " : " .. self.protocol);
  35.         end
  36.     end
  37.     return isClicked;
  38. end
  39.  
  40. function gui:new(o, textsize, mon)
  41.     o = o or {};
  42.     setmetatable(o, {__index = self});
  43.  
  44.     o.buttons = {};
  45.     o.textsize = textsize or 0.5;
  46.     print("mon: " .. tostring(mon));
  47.  
  48.     if (not mon) then return o end;
  49.     print("clearing");
  50.     o.monitor = mon or nil;
  51.     o.monitor.setTextScale(o.textsize);
  52.     o.monitor.setBackgroundColor(colors.gray);
  53.     o.monitor.clear();
  54.  
  55.     return o;
  56. end
  57. function gui:addButton (name, message, host, protocol, x, y)
  58.     self.buttons[#(self.buttons) + 1] = self.Button:new(nil, name, message, host, protocol, x, y)
  59. end
  60. function gui:render(x, y, col)
  61.     if(not self.monitor) then return end;
  62.  
  63.     self.monitor.setTextScale(self.textsize);
  64.     self.monitor.setBackgroundColor(colors.gray);
  65.     self.monitor.clear();
  66.     for it, val in pairs(self.buttons) do
  67.         val:render(col, self.monitor);
  68.  
  69.         if val:clicked(x, y) then
  70.             val:render(colors.red, self.monitor);
  71.         end
  72.     end
  73. end
  74.  
  75. gui.Button = Button;
  76.  
  77. -- Button CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement