Advertisement
duaiwe

Knob OO-API

Feb 12th, 2013
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local defaultColor = colors.white
  2. local defaultBgColor = colors.black
  3. local textSize = 2
  4. local titleText = ""
  5.  
  6. local monitor = nil
  7. local knobs = {}
  8.  
  9. local function getText(k)
  10.     if( k.isOff and k.offText ) then
  11.         return k.offText
  12.     else
  13.         return k.onText
  14.     end
  15. end
  16.  
  17. local function getTextColor(k)
  18.     return k.isOn and k.onColor or k.offColor
  19. end
  20.  
  21. local function getBgColor(k)
  22.     return k.isOn and k.onBgColor or k.offBgColor
  23. end
  24.  
  25. local Knob = {
  26.     toggleState = function(self)
  27.         self.isOn = not self.isOn
  28.     end,
  29.    
  30.     setState = function(self, state)
  31.         self.isOn = state and true or false
  32.     end,
  33.    
  34.     -- Draws the knob on the set monitor
  35.     draw = function(self)
  36.         local bgColor = getBgColor(self)
  37.         local textColor = getTextColor(self)
  38.         local buttonText = getText(self)
  39.        
  40.         monitor.setBackgroundColor(bgColor)
  41.        
  42.         -- Finds the middle in the Y-Axis
  43.         local ypos = math.floor(( self.yMin + self.yMax) / 2);
  44.         -- Finds the middle in the X-Axis
  45.         local xpos = math.floor(
  46.             (self.xMax-self.xMin-string.len(buttonText))/2)+1
  47.  
  48.         for ytraverse = self.yMin, self.yMax do
  49.             monitor.setCursorPos(self.xMin, ytraverse)
  50.             -- If you are in the middle of your knob Y-Axis check for the
  51.             -- middle of the X-Axis
  52.             -- Whitespaces(spaces) are being filled around your knob name
  53.             if ytraverse == ypos then
  54.                 for xtraverse = 0, (self.xMax-self.xMin-string.len(buttonText)+1) do
  55.                     if( xtraverse == xpos ) then
  56.                         monitor.setTextColor(textColor)
  57.                         monitor.write(buttonText)
  58.                         monitor.setTextColor(bgColor)
  59.                     else
  60.                         monitor.write(" ")
  61.                     end
  62.                 end
  63.             else
  64.                 for i = self.xMin, self.xMax do
  65.                     monitor.write(" ")
  66.                 end
  67.             end
  68.         end
  69.         monitor.setBackgroundColor(defaultBgColor)
  70.     end
  71. }
  72.  
  73. local knobmetatable = {
  74.     __index = Knob
  75. }
  76.  
  77. function new(name, xMin, xMax, yMin, yMax, text, textColor, bgColor)
  78.     local k = {
  79.         name = name,
  80.         isOn = false,
  81.         xMin = xMin,
  82.         xMax = xMax,
  83.         yMin = yMin,
  84.         yMax = yMax,
  85.        
  86.         onBgColor = bgColor,
  87.         offBgColor = textColor,
  88.         onText = text,
  89.         offText = nil,
  90.         onColor = textColor,
  91.         offColor = bgColor,
  92.        
  93.         onClick = nil,
  94.         onActivate = nil,
  95.         onDeactivate = nil,
  96.     }
  97.    
  98.     setmetatable(k, knobmetatable)
  99.    
  100.     knobs[name] = k
  101.     return k
  102. end
  103.  
  104. function remove(name)
  105.     knobs[name] = nil
  106.     refreshDisplay()
  107. end
  108.  
  109. function setMonitor(m)
  110.     monitor = m
  111.     monitor.setTextScale(textSize)
  112.     monitor.setTextColor(defaultColor)
  113.     monitor.setBackgroundColor(defaultBgColor)
  114. end
  115.  
  116. function setTextSize(size)
  117.     textSize = size
  118.     if( monitor ) then
  119.         monitor.setTextScale(textSize)
  120.     end
  121. end
  122.  
  123. function setTextColor(c)
  124.     defaultColor = c
  125.     if( monitor ) then
  126.         monitor.setTextColor(c)
  127.     end
  128. end
  129.  
  130. function setBackgroundColor(c)
  131.     defaultBgColor = c
  132.     if( monitor ) then
  133.         monitor.setBgColor(c)
  134.     end
  135. end
  136.  
  137. function setTitle(text)
  138.     titleText = text
  139. end
  140.  
  141. function refreshDisplay()
  142.     monitor.clear()
  143.    
  144.     x, y = monitor.getSize()
  145.     monitor.setCursorPos( (x - string.len(titleText))/2+1, 1)
  146.     monitor.write(titleText)
  147.    
  148.     for name, knob in pairs(knobs) do
  149.         knob:draw()
  150.     end
  151. end
  152.  
  153. function getKnob(name)
  154.     return knobs[name]
  155. end
  156.  
  157. -- Refreshes the Monitor and waits for touch events.
  158. -- On a touch event, the state of the button is toggled,
  159. -- and it's onClick and on(De)Activate handlers are called, in that order.
  160. --
  161. -- @param forever Set to any truthy value to run this method forever.
  162. function run(forever)
  163.     refreshDisplay()
  164.    
  165.     repeat
  166.         local event, side, x, y = os.pullEvent("monitor_touch")
  167.         for name, knob in pairs(knobs) do
  168.             if (y>=knob.yMin and y<=knob.yMax and x>=knob.xMin and x<=knob.xMax) then
  169.                 knob.isOn = not knob.isOn
  170.                 if( knob.onClick ) then
  171.                     knob:onClick()
  172.                 end
  173.                 if( knob.isOn and knob.onActivate ) then
  174.                     knob:onActivate()
  175.                 end
  176.                 if( not knob.isOn and knob.onDeactivate ) then
  177.                     knob:onDeactivate()
  178.                 end
  179.                
  180.                 knob:draw()
  181.             end
  182.         end
  183.         sleep(.1)
  184.     until not forever
  185. end
  186.  
  187. -- Convenience function for initializing variables needed to display knobs.
  188. -- All of these can be set individually via set* methods
  189. --
  190. -- @param side String The side your monitor is on
  191. -- @param textSize Integer The size of button text
  192. -- @param textColor Color The default textColor for non-button text
  193. -- @param bgColor Color The default background color for the monitor
  194. function init(side, textSize, textColor, bgColor)
  195.     monitor = peripheral.wrap(side)
  196.     monitor.setTextScale(textSize)
  197.     monitor.setTextColor(textColor)
  198.     monitor.setBackgroundColor(bgColor)
  199.     defaultBgColor = bgColor
  200.     defaultColor = textColor   
  201. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement