Advertisement
tobast

Untitled

Jun 16th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.36 KB | None | 0 0
  1. --[[
  2.   Crée des boutons.
  3. --]]
  4.  
  5. function create(_side,_x,_y,_text,_callback)
  6.     btn = {
  7.         side=_side,
  8.         x=_x,
  9.         y=_y,
  10.         text=_text,
  11.         callback=_callback,
  12.         fgColor=colors.white,
  13.         bgColor=colors.black
  14.     }
  15.     if _side == 'term' then
  16.         btn.device = term
  17.     else
  18.         btn.device = peripheral.wrap(_side)
  19.     end
  20.  
  21.     function btn:draw()
  22.         x = self.x
  23.         y = self.y
  24.         text = self.text
  25.  
  26.         self.device.setCursorPos(x,y)
  27.         for chId = 1,text:len() do
  28.             ch = text:sub(chId,chId)
  29.             if ch == '\n' then
  30.                 y = y+1
  31.                 self.device.setCursorPos(x,y)
  32.             else
  33.                 self.device.write(ch)
  34.             end
  35.         end
  36.     end
  37.    
  38.     function btn:setBgColor(color)
  39.         btn.bgColor = color
  40.         btn:draw()
  41.     end
  42.     function btn:setFgColor(color)
  43.         btn.fgColor = color
  44.         btn:draw()
  45.     end
  46.  
  47.     function btn:clicked(args)
  48.         if self.side != 'term' and self.side != args[1] then
  49.             -- Not the right monitor
  50.             return
  51.         end
  52.         if args[2] >= self.x and args[3] >= self.y and
  53.             args[2] <= self.x + self.w and args[3] <= self.x + self.h then
  54.             if self.callback != nil then
  55.                 self.callback(args)
  56.             end
  57.         end
  58.     end
  59.  
  60.     maxW = 0
  61.     w = 0
  62.     h = 1
  63.     for chId = 1,_text:len() do
  64.         ch = _text:sub(chId,chId)
  65.         if ch == '\n' then
  66.             maxW = math.max(maxW,w)
  67.             h = h + 1
  68.         else
  69.             w = w + 1
  70.         end
  71.     end
  72.     btn.h = h
  73.     btn.w = maxW
  74.  
  75.     if _side == 'term' then
  76.         slot_sig.connectSlot('monitor_touch', btn:clicked)
  77.     else
  78.         slot_sig.connectSlot('mouse_click', btn:clicked)
  79.     end
  80.    
  81.     return btn
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement