Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local defaultColor = colors.white
- local defaultBgColor = colors.black
- local textSize = 2
- local titleText = ""
- local monitor = nil
- local knobs = {}
- local function getText(k)
- if( k.isOff and k.offText ) then
- return k.offText
- else
- return k.onText
- end
- end
- local function getTextColor(k)
- return k.isOn and k.onColor or k.offColor
- end
- local function getBgColor(k)
- return k.isOn and k.onBgColor or k.offBgColor
- end
- local Knob = {
- toggleState = function(self)
- self.isOn = not self.isOn
- end,
- setState = function(self, state)
- self.isOn = state and true or false
- end,
- -- Draws the knob on the set monitor
- draw = function(self)
- local bgColor = getBgColor(self)
- local textColor = getTextColor(self)
- local buttonText = getText(self)
- monitor.setBackgroundColor(bgColor)
- -- Finds the middle in the Y-Axis
- local ypos = math.floor(( self.yMin + self.yMax) / 2);
- -- Finds the middle in the X-Axis
- local xpos = math.floor(
- (self.xMax-self.xMin-string.len(buttonText))/2)+1
- for ytraverse = self.yMin, self.yMax do
- monitor.setCursorPos(self.xMin, ytraverse)
- -- If you are in the middle of your knob Y-Axis check for the
- -- middle of the X-Axis
- -- Whitespaces(spaces) are being filled around your knob name
- if ytraverse == ypos then
- for xtraverse = 0, (self.xMax-self.xMin-string.len(buttonText)+1) do
- if( xtraverse == xpos ) then
- monitor.setTextColor(textColor)
- monitor.write(buttonText)
- monitor.setTextColor(bgColor)
- else
- monitor.write(" ")
- end
- end
- else
- for i = self.xMin, self.xMax do
- monitor.write(" ")
- end
- end
- end
- monitor.setBackgroundColor(defaultBgColor)
- end
- }
- local knobmetatable = {
- __index = Knob
- }
- function new(name, xMin, xMax, yMin, yMax, text, textColor, bgColor)
- local k = {
- name = name,
- isOn = false,
- xMin = xMin,
- xMax = xMax,
- yMin = yMin,
- yMax = yMax,
- onBgColor = bgColor,
- offBgColor = textColor,
- onText = text,
- offText = nil,
- onColor = textColor,
- offColor = bgColor,
- onClick = nil,
- onActivate = nil,
- onDeactivate = nil,
- }
- setmetatable(k, knobmetatable)
- knobs[name] = k
- return k
- end
- function remove(name)
- knobs[name] = nil
- refreshDisplay()
- end
- function setMonitor(m)
- monitor = m
- monitor.setTextScale(textSize)
- monitor.setTextColor(defaultColor)
- monitor.setBackgroundColor(defaultBgColor)
- end
- function setTextSize(size)
- textSize = size
- if( monitor ) then
- monitor.setTextScale(textSize)
- end
- end
- function setTextColor(c)
- defaultColor = c
- if( monitor ) then
- monitor.setTextColor(c)
- end
- end
- function setBackgroundColor(c)
- defaultBgColor = c
- if( monitor ) then
- monitor.setBgColor(c)
- end
- end
- function setTitle(text)
- titleText = text
- end
- function refreshDisplay()
- monitor.clear()
- x, y = monitor.getSize()
- monitor.setCursorPos( (x - string.len(titleText))/2+1, 1)
- monitor.write(titleText)
- for name, knob in pairs(knobs) do
- knob:draw()
- end
- end
- function getKnob(name)
- return knobs[name]
- end
- -- Refreshes the Monitor and waits for touch events.
- -- On a touch event, the state of the button is toggled,
- -- and it's onClick and on(De)Activate handlers are called, in that order.
- --
- -- @param forever Set to any truthy value to run this method forever.
- function run(forever)
- refreshDisplay()
- repeat
- local event, side, x, y = os.pullEvent("monitor_touch")
- for name, knob in pairs(knobs) do
- if (y>=knob.yMin and y<=knob.yMax and x>=knob.xMin and x<=knob.xMax) then
- knob.isOn = not knob.isOn
- if( knob.onClick ) then
- knob:onClick()
- end
- if( knob.isOn and knob.onActivate ) then
- knob:onActivate()
- end
- if( not knob.isOn and knob.onDeactivate ) then
- knob:onDeactivate()
- end
- knob:draw()
- end
- end
- sleep(.1)
- until not forever
- end
- -- Convenience function for initializing variables needed to display knobs.
- -- All of these can be set individually via set* methods
- --
- -- @param side String The side your monitor is on
- -- @param textSize Integer The size of button text
- -- @param textColor Color The default textColor for non-button text
- -- @param bgColor Color The default background color for the monitor
- function init(side, textSize, textColor, bgColor)
- monitor = peripheral.wrap(side)
- monitor.setTextScale(textSize)
- monitor.setTextColor(textColor)
- monitor.setBackgroundColor(bgColor)
- defaultBgColor = bgColor
- defaultColor = textColor
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement