Advertisement
dlord

/classes/button

May 26th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. buttons = {}
  2. Button={}
  3. Button.__index=Button
  4. defaultBackgroundColor=colors.black
  5.  
  6. function Button.new(params)
  7.     assert(params.monitor, "Please set the monitor param (peripheral wrap of monitor)")
  8.     assert(params.inactiveColor, "Please set inactiveColor param")
  9.     assert(params.activeColor, "Please set activeColor param")
  10.     assert(params.textColor, "Ple  ase set textColor param")
  11.     assert(params.position.x, "Please set position.x param")
  12.     assert(params.position.y, "Please set position.y param")
  13.     assert(params.dimensions.height, "Please set dimensions.height param")
  14.     assert(params.dimensions.width, "Please set dimensions.width param")
  15.     assert(params.inactiveButtonText, "Please set the inactiveButtonText param")
  16.     assert(params.activeButtonText, "Please set the activeButtonText param")
  17.     assert(params.onClick, "Please set the onClick param (anonymous function)")
  18.  
  19.     local button={}
  20.     setmetatable(button, Button)
  21.     button.params=params
  22.    
  23.     if params.active then
  24.         button.active=true
  25.     else
  26.         button.active=false
  27.     end
  28.  
  29.     buttons[#buttons+1]=button
  30.  
  31.     return button
  32. end
  33.  
  34. function Button:draw()
  35.     local buttonColor=self.params.inactiveColor
  36.     local buttonText=self.params.inactiveButtonText
  37.    
  38.     if self.active == true then
  39.         buttonColor=self.params.activeColor
  40.         buttonText=self.params.activeButtonText
  41.     end
  42.    
  43.     self.params.monitor.setCursorPos(self.params.position.x, self.params.position.y)
  44.     self.params.monitor.setBackgroundColor(buttonColor)
  45.  
  46.     for y=1,self.params.dimensions.height-1 do
  47.         for x=1,self.params.dimensions.width-1 do
  48.             self.params.monitor.setCursorPos(self.params.position.x+x, self.params.position.y+y)
  49.             self.params.monitor.write(" ")
  50.         end
  51.     end
  52.    
  53.     local offset= {
  54.         y=0
  55.     }
  56.  
  57.     if self.params.dimensions.height%2==0 then
  58.         offset.y=1
  59.     end
  60.    
  61.     local textPositionY=math.floor(self.params.dimensions.height/2)-offset.y
  62.     local textPositionX=math.floor((self.params.dimensions.width-string.len(buttonText))/2)
  63.    
  64.     self.params.monitor.setTextColor(self.params.textColor)
  65.     self.params.monitor.setCursorPos(textPositionX+self.params.position.x, textPositionY+self.params.position.y)
  66.     self.params.monitor.write(buttonText)
  67.    
  68.     self.params.monitor.setBackgroundColor(defaultBackgroundColor)
  69. end
  70.  
  71. function Button:toggle()
  72.     self.active=not self.active
  73.     self:draw()
  74. end
  75.  
  76. function Button:clicked(x, y)
  77.     local inX = x >= self.params.position.x and x < (self.params.position.x+self.params.dimensions.width)
  78.     local inY = y >= self.params.position.y and y < (self.params.position.y+self.params.dimensions.height)
  79.    
  80.     return inX and inY
  81. end
  82.  
  83. function buttonClickHandler(spawned)
  84.     local running=true
  85.     while running do
  86.         local event, side, x, y = os.pullEvent("monitor_touch")
  87.         for index,button in ipairs(buttons) do
  88.             if button:clicked(x, y) then
  89.                 local runOnClick=function()
  90.                     button.params.onClick(button)
  91.                 end
  92.                 local spawnClickHandler=function()
  93.                     buttonClickHandler(true)
  94.                 end
  95.                 parallel.waitForAny(runOnClick, spawnClickHandler)
  96.  
  97.                 if spawned then
  98.                     print("done with spawned click handler. returning to main...")
  99.                     return
  100.                 end
  101.                
  102.                 break
  103.             end
  104.         end
  105.     end
  106. end
  107.  
  108. function drawButtons()
  109.     for index,button in ipairs(buttons) do
  110.         button:draw()
  111.     end
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement