temposabel

buttonapimake

Dec 18th, 2021 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.66 KB | None | 0 0
  1. function getmodems(mode)
  2.   mode = mode or "default"
  3.  
  4.   if mode == "default" then
  5.     allp = peripheral.getNames()
  6.     d = {}
  7.     x = 0
  8.     for i,value in ipairs(allp) do
  9.       c = peripheral.getType(value)
  10.       if c == "modem" then
  11.         c = peripheral.wrap(value)
  12.         x = x + 1
  13.         d[x] = c
  14.       end
  15.     end
  16.     d["a"] = x
  17.     d["amount"] = x
  18.     return d
  19.   elseif mode == "sides" or mode =="s" then
  20.     allp = peripheral.getNames()
  21.     d = {}
  22.     x = 0
  23.     for i,value in ipairs(allp) do
  24.       c = peripheral.getType(value)
  25.       if c == "modem" then
  26.         x = x + 1
  27.         d[x] = value
  28.       end
  29.     end
  30.     d["a"] = x
  31.     d["amount"] = x
  32.     return d
  33.   end
  34. end
  35.  
  36. function getmsg()
  37.   while true do
  38.     d = {}
  39.     event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
  40.     d["e"] = event
  41.     d["s"] = side
  42.     d["f"] = frequency
  43.     d["rf"] = replyFrequency
  44.     d["m"] = message
  45.     d["d"] = distance
  46.     return d
  47.   end
  48. end
  49.  
  50. function transmitall(p,rp,message)
  51.   modems = getmodems()  
  52.   print(modems["a"])
  53.   if modems["a"] == 0 then else
  54.       for modem = 1,modems["a"] do
  55.           cm = modems[modem]
  56.           cm.transmit(p,rp,message)        
  57.       end
  58.   end
  59. end
  60.  
  61. function relay(p,rp,message,exclude)
  62.   sides = getmodems("s")
  63.   if modems["a"] == 0 then else
  64.       for side = 1,modems["a"] do
  65.           cs = sides[side]
  66.           if cs == exclude then else
  67.             cm.transmit(p,rp,message)
  68.           end  
  69.       end
  70.   end
  71. end
  72.  
  73. function createbutton(x,y,w,h,color,message)
  74.  
  75.     d = {}
  76.  
  77.     function d:init(x,y,w,h,color,message)
  78.        self.x1 = x
  79.        self.x2 = x+w
  80.        self.y1 = y
  81.        self.y2 = y+h
  82.        self.color = color
  83.        self.message = message
  84.     end
  85.  
  86.     function d:isclickon (x,y)
  87.  
  88.         if self.x1 <= x and x <= self.x2 and self.y1 <= y and y <= self.y2 then
  89.             return true
  90.         else
  91.             return false
  92.         end
  93.  
  94.        
  95.     end
  96.  
  97.     function d:draw ()
  98.         --print(self.x1)
  99.         paintutils.drawBox(self.x1,self.y1,self.x2,self.y2,self.color)
  100.         print(self.message)
  101.     end
  102.    
  103.     d:init(x,y,w,h,color,message)
  104.     return d
  105. end
  106. term.clear()
  107. m = getmodems()[1]
  108.  
  109. but1 = createbutton(2,2,9,3,colors.red,"close")
  110. but1:draw()
  111. but2 = createbutton(2,7,9,3,colors.green,"open")
  112. but2:draw()
  113.  
  114. while true do
  115.   local event, button, mx, my = os.pullEvent( "mouse_click" )
  116.   term.setTextColor(colors.white)
  117.   if but1:isclickon(mx,my) then m.transmit(313,313,"ca1") end
  118.   if but2:isclickon(mx,my) then m.transmit(313,313,"oa1") end
  119.  
  120.   --print(but1:isclickon(mx,my))
  121.   --print(but2:isclickon(mx,my))
  122.  
  123.    
  124. end
  125.  
  126.  
  127.  
Add Comment
Please, Sign In to add comment