local Button = {} function Button.new( width, height, label, bgClrNorm, bgClrPressed, txtClrNorm, txtClrPressed, hasBorder, borderClrNorm, borderClrPressed, startCol, startRow, isPressed, dfltBGClr, dfltTxtClr ) local button = {} button.height = height or 1 button.width = width or 1 button.label = label or "Button" button.bgClrNorm = bgClrNorm or colors.lightGray button.bgClrPressed = bgClrPressed or colors.gray button.txtClrNorm = txtClrNorm or colors.black button.txtClrPressed = txtClrPressed or colors.white button.hasBorder = hasBorder or true button.borderClrNorm = borderClrNorm or colors.black button.borderClrPressed = borderClrPressed or colors.white button.dfltBGClr = dfltBGClr or colors.lightGray button.dfltTxtClr = dfltTxtClr or colors.black button.startCol = startCol or 1 button.startRow = startRow or 1 button.isPressed = isPressed or false function button.press() button.isPressed = not button.isPressed end function button.draw(display, isPressed, startCol, startRow) button.startCol = startCol or button.startCol button.startRow = startRow or button.startRow display = display or term if isPressed == false or isPressed then button.isPressed = isPressed else isPressed = button.isPressed end local width = button.width local height = button.height startCol = button.startCol startRow = button.startRow local label = button.label local labelPad = 2 --creating and drawing border if button.hasBorder == true then if width < 3 then width = 3 end if height < 3 then height = 3 end if not isPressed then if not display.isColor() then display.setBackgroundColor(colors.white) else display.setBackgroundColor(button.borderClrNorm) end else if not display.isColor() then display.setBackgroundColor(colors.white) else display.setBackgroundColor(button.borderClrPressed) end end display.setCursorPos(startCol, startRow) display.write(string.rep(" ", width)) for row = 2, height-1 do display.setCursorPos(startCol, button.startRow + row -1) display.write(" ") display.setCursorPos(startCol + width - 1, startRow + row - 1) display.write(" ") end display.setCursorPos(startCol, startRow + height - 1) display.write(string.rep(" ", width)) --resetting values to inset button and label startCol = startCol + 1 startRow = startRow + 1 width = width - 2 height = height - 2 end --set button colors if not isPressed then if not display.isColor() then display.setBackgroundColor(colors.black) display.setTextColor(colors.white) else display.setBackgroundColor(button.bgClrNorm) display.setTextColor(button.txtClrNorm) end else if not display.isColor() then display.setBackgroundColor(colors.white) display.setTextColor(colors.black) else display.setBackgroundColor(button.bgClrPressed) display.setTextColor(button.txtClrPressed) end end --draw button background for row = 1, height do display.setCursorPos(startCol, startRow + row - 1) display.write(string.rep(" ", width)) end if width < 3 then labelPad = 0 end if #label > width - labelPad then label = label:sub(1, width - labelPad) end display.setCursorPos(startCol + math.floor((width - #label) / 2), startRow + math.floor((height - 1) / 2)) display.write(label) display.setBackgroundColor(button.dfltBGClr) display.setTextColor(button.dfltTxtClr) end button.toggle = function () button.isPressed = not button.isPressed return button.isPressed end return button end return Button