Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defaultColors = {colors.white, colors.orange,
- colors.magenta, colors.lightBlue,
- colors.yellow, colors.lime,
- colors.pink, colors.gray,
- colors.lightGray, colors.cyan,
- colors.purple, colors.blue,
- colors.brown, colors.green,
- colors.red, colors.black}
- local registeredButtons = {}
- function addButton(id, decrement, increment, y, type, color, value)
- registeredButtons[id] = {}
- registeredButtons[id]['minX'] = decrement
- registeredButtons[id]['maxX'] = increment
- registeredButtons[id]['y'] = y
- registeredButtons[id]['type'] = type
- registeredButtons[id]['btn_color'] = color
- registeredButtons[id]['value'] = value
- end
- local function drawButtons(bcColor)
- term.setBackgroundColor(bcColor)
- term.clear()
- print('Start drawing') --Debug
- for i = 1, #registeredButtons do
- local data = registeredButtons[i]
- local x1 = data.minX
- local x2 = data.maxX
- local yPos = data.y
- local bType = data.type
- local bColor = defaultColors[data.btn_color]
- if bType == 'color' then
- local sColor =defaultColors[data.value]
- else
- local sColor = defaultColors[9]
- end
- print('All variables gathered') --Debug
- term.setCursorPos(x1, yPos)
- term.setBackgroundColor(bColor)
- if not bType == 'numberical' then
- write('<')
- term.setCursorPos(x2, yPos)
- write('>')
- else
- term.write('-')
- term.setCursorPos(x2, yPos)
- term.write('+')
- end
- term.setCursorPos(x1+1, yPos)
- term.setBackgroundColor(sColor)
- if not bType == 'numberical' then
- term.write('] [')
- else
- term.write(data.value)
- end
- end
- end
- local function checkForUpdates(event, mouse, x, y)
- for index, value in pairs(registeredButtons) do
- local decrement, increment, bY = value.minX, value.maxX, value.y
- if event == 'mouse_click' and mouse == 1 and y == bY then
- if x == decrement then
- return index, 1
- elseif x == increment then
- return index, -1
- end
- elseif event == 'mouse_scroll' and y == bY then
- if x >= decrement or x <= increment then
- return index, mouse*(-1)
- end
- end
- end
- return false
- end
- local function changeValue(index, multiplier)
- local data = registeredButtons[index]
- data.value = data.value + multiplier
- end
- local function updater()
- local event, mouse, x, y = os.pullEvent()
- local index, multiplier = checkForUpdates(event, mouse, x, y)
- changeValue(index, multiplier)
- drawButtons(defaultColors[16])
- end
- local function callMain()
- addButton(1, 5, 9, 3, 'color', defaultColors[9], 2)
- addButton(2, 5, 9, 5, 'color', defaultColors[9], 3)
- addButton(3, 5, 9, 7, 'color', defaultColors[9], 10)
- addButton(4, 4, 10, 9, 'numberical', defaultColors[9], 1)
- addButton(5, 5, 9, 11, 'color', defaultColors[9], 11)
- addButton(6, 4, 10, 13, 'numberical', defaultColors[9], 0)
- print('All buttons set') --Debug
- sleep(2) --Debug
- drawButtons(defaultColors[16])
- while true do
- updater()
- end
- end
- callMain()
Advertisement
Add Comment
Please, Sign In to add comment